KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > serialization > TextSerializer


1 /*
2  * Copyright 1999-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.serialization;
17
18 import org.apache.avalon.framework.configuration.Configuration;
19 import org.apache.avalon.framework.configuration.ConfigurationException;
20 import org.apache.cocoon.CascadingIOException;
21 import org.apache.cocoon.xml.XMLUtils;
22
23 import org.xml.sax.SAXException JavaDoc;
24 import org.xml.sax.Attributes JavaDoc;
25
26 import javax.xml.transform.OutputKeys JavaDoc;
27 import javax.xml.transform.sax.TransformerHandler JavaDoc;
28 import javax.xml.transform.stream.StreamResult JavaDoc;
29 import java.io.IOException JavaDoc;
30 import java.io.OutputStream JavaDoc;
31
32 /**
33  * Text serializer converts XML into plain text.
34  * It omits all XML tags and writes only character events to the output.
35  * Internally, text serializer uses XML serializer with {@link OutputKeys#METHOD}
36  * set to <code>text</code>.
37  *
38  * <p>Input document must have at least one element - root element - which
39  * should wrap all the text inside it.
40  *
41  * @author <a HREF="mailto:stefano@apache.org">Stefano Mazzocchi</a>
42  * @version $Id: TextSerializer.java 265735 2005-09-01 14:04:30Z cziegeler $
43  */

44 public class TextSerializer extends AbstractTextSerializer {
45
46     /**
47      * Set to true after first XML element
48      */

49     private boolean hasRootElement;
50
51     /**
52      * Set to true after first XML element
53      */

54     private boolean hadNoRootElement;
55
56     /**
57      * Set the configurations for this serializer.
58      */

59     public void configure(Configuration conf) throws ConfigurationException {
60         super.configure(conf);
61         this.format.put(OutputKeys.METHOD, "text");
62     }
63
64     /**
65      * Set the {@link OutputStream} where the requested resource should
66      * be serialized.
67      */

68     public void setOutputStream(OutputStream JavaDoc out) throws IOException {
69         super.setOutputStream(out);
70         try {
71             TransformerHandler JavaDoc handler = this.getTransformerHandler();
72             handler.getTransformer().setOutputProperties(format);
73             handler.setResult(new StreamResult JavaDoc(this.output));
74             this.setContentHandler(handler);
75             this.setLexicalHandler(handler);
76        } catch (Exception JavaDoc e) {
77             final String JavaDoc message = "Cannot set TextSerializer outputstream";
78             throw new CascadingIOException(message, e);
79         }
80     }
81
82     public void startElement(String JavaDoc uri, String JavaDoc loc, String JavaDoc raw, Attributes JavaDoc a)
83     throws SAXException JavaDoc {
84         this.hasRootElement = true;
85         super.startElement(uri, loc, raw, a);
86     }
87
88     public void characters(char c[], int start, int len)
89     throws SAXException JavaDoc {
90         if (!this.hasRootElement) {
91             this.hasRootElement = this.hadNoRootElement = true;
92             getLogger().warn("Encountered text before root element. Creating <text> wrapper element.");
93             super.startElement("", "text", "text", XMLUtils.EMPTY_ATTRIBUTES);
94         }
95         super.characters(c, start, len);
96     }
97
98     public void endDocument() throws SAXException JavaDoc {
99         if (this.hadNoRootElement) {
100             super.endElement("", "text", "text");
101         }
102         super.endDocument();
103     }
104
105     public void recycle() {
106         super.recycle();
107         this.hasRootElement = false;
108         this.hadNoRootElement = false;
109     }
110 }
111
Popular Tags