01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: package org.apache.cocoon.generation;
18:
19: import java.io.IOException;
20: import java.util.Enumeration;
21: import java.util.Map;
22:
23: import org.apache.avalon.framework.parameters.Parameters;
24: import org.apache.cocoon.ProcessingException;
25: import org.apache.cocoon.environment.Context;
26: import org.apache.cocoon.environment.ObjectModelHelper;
27: import org.apache.cocoon.environment.SourceResolver;
28: import org.apache.cocoon.xml.XMLConsumer;
29: import org.xml.sax.SAXException;
30: import org.xml.sax.helpers.AttributesImpl;
31:
32: /**
33: * Outputs an XML representation of the set of attributes contained in the
34: * Cocoon environment's context for testing purposes.
35: *
36: * @version $Id: ContextGenerator.java 433543 2006-08-22 06:22:54Z crossley $
37: */
38: public class ContextGenerator implements Generator {
39: XMLConsumer consumer;
40: Map objectModel;
41:
42: public void setConsumer(XMLConsumer consumer) {
43: this .consumer = consumer;
44: }
45:
46: public void generate() throws IOException, SAXException,
47: ProcessingException {
48: consumer.startDocument();
49: consumer.startElement("", "context", "context",
50: new AttributesImpl());
51: Context context = ObjectModelHelper.getContext(objectModel);
52: Enumeration keys = context.getAttributeNames();
53: while (keys.hasMoreElements()) {
54: String key = (String) keys.nextElement();
55: Object value = context.getAttribute(key);
56: AttributesImpl attrs = new AttributesImpl();
57: attrs.addAttribute("", "name", "name", "CDATA", key);
58: consumer.startElement("", "key", "key", attrs);
59: String str = value.toString();
60: consumer.characters(str.toCharArray(), 0, str.length());
61: consumer.endElement("", "key", "key");
62: }
63: consumer.endElement("", "context", "context");
64: consumer.endDocument();
65: }
66:
67: public void setup(SourceResolver resolver, Map objectModel,
68: String src, Parameters par) throws ProcessingException,
69: SAXException, IOException {
70: this.objectModel = objectModel;
71: }
72: }
|