KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > nu > xom > samples > TextSerializer


1 /* Copyright 2003 Elliotte Rusty Harold
2    
3    This library is free software; you can redistribute it and/or modify
4    it under the terms of version 2.1 of the GNU Lesser General Public
5    License as published by the Free Software Foundation.
6    
7    This library is distributed in the hope that it will be useful,
8    but WITHOUT ANY WARRANTY; without even the implied warranty of
9    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10    GNU Lesser General Public License for more details.
11    
12    You should have received a copy of the GNU Lesser General Public
13    License along with this library; if not, write to the
14    Free Software Foundation, Inc., 59 Temple Place, Suite 330,
15    Boston, MA 02111-1307 USA
16    
17    You can contact Elliotte Rusty Harold by sending e-mail to
18    elharo@metalab.unc.edu. Please include the word "XOM" in the
19    subject line. The XOM home page is located at http://www.xom.nu/
20 */

21
22 package nu.xom.samples;
23
24 import java.io.IOException JavaDoc;
25 import java.io.OutputStream JavaDoc;
26 import java.io.UnsupportedEncodingException JavaDoc;
27
28 import nu.xom.Builder;
29 import nu.xom.Comment;
30 import nu.xom.DocType;
31 import nu.xom.Document;
32 import nu.xom.Element;
33 import nu.xom.ParsingException;
34 import nu.xom.ProcessingInstruction;
35 import nu.xom.Serializer;
36 import nu.xom.Text;
37
38 /**
39  * <p>
40  * This <code>Serializer</code> subclass outputs raw, unescaped text
41  * from the text nodes, but no markup of any kind. In essence, it
42  * converts XML into plain text.
43  * </p>
44  *
45  * @author Elliotte Rusty Harold
46  * @version 1.0
47  *
48  */

49 public class TextSerializer extends Serializer {
50
51     /**
52      * <p>
53      * Create a new text serializer that uses the UTF-8 encoding.
54      * </p>
55      *
56      * @param out the output stream to write the document on
57      *
58      * @throws NullPointerException if out is null
59      */

60     public TextSerializer(OutputStream JavaDoc out) {
61         super(out);
62     }
63     
64     /**
65      * <p>
66      * Create a new serializer that uses a specified encoding.
67      * The encoding must be recognized by the Java virtual machine.
68      * </p>
69      *
70      * @param out the output stream to write the document on
71      * @param encoding the character encoding for the serialization
72      *
73      * @throws NullPointerException if <code>out</code>
74      * or <code>encoding</code> is null
75      * @throws UnsupportedEncodingException if the VM does not
76      * support the requested encoding
77      *
78      */

79     public TextSerializer(OutputStream JavaDoc out, String JavaDoc encoding)
80       throws UnsupportedEncodingException JavaDoc {
81         super(out, encoding);
82     }
83     
84     protected void writeStartTag(Element element) {}
85     protected void writeEmptyElementTag(Element element) {}
86     protected void writeEndTag(Element element) {}
87     protected void writeXMLDeclaration() {}
88     protected void write(Comment comment) {}
89     protected void write(ProcessingInstruction instruction) {}
90     protected void write(DocType doctype) {}
91
92     // Here we use writeRaw because we don't want characters like &
93
// and < to be escaped. If they can't be written in the specified
94
// encoding, an exception is thrown.
95
protected void writeText(Text text) throws IOException JavaDoc {
96         writeRaw(text.getValue());
97     }
98
99     public static void main(String JavaDoc[] args) {
100       
101         if (args.length <= 0) {
102           System.out.println(
103             "Usage: java nu.xom.samples.TextSerializer URL");
104           return;
105         }
106         
107         try {
108             Builder parser = new Builder();
109             Document doc = parser.build(args[0]);
110             Serializer serializer = new TextSerializer(System.out);
111             serializer.write(doc);
112         }
113         catch (ParsingException ex) {
114             System.out.println(args[0] + " is not well-formed.");
115             System.out.println(" at line " + ex.getLineNumber()
116               + ", column " + ex.getColumnNumber());
117             System.out.println(ex.getMessage());
118         }
119         catch (IOException JavaDoc ex) {
120           System.out.println(
121            "Due to an IOException, the serialization of "
122            + args[0] + " could not be completed."
123           );
124         }
125       
126     }
127
128 }
129
Popular Tags