KickJava   Java API By Example, From Geeks To Geeks.

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


1 /* Copyright 2002-2004 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.Document;
30 import nu.xom.Element;
31 import nu.xom.Node;
32 import nu.xom.ParentNode;
33 import nu.xom.ParsingException;
34 import nu.xom.Serializer;
35
36
37 /**
38  * <p>
39  * Demonstrates a serializer which, unlike
40  * <code>nu.xom.Serializer</code>, is not limited by the Java stack
41  * size and can process arbitrarily deep documents.
42  * </p>
43  *
44  * @author Elliotte Rusty Harold
45  * @version 1.0
46  *
47  */

48 public final class NonRecursiveSerializer extends Serializer {
49     
50     
51     public NonRecursiveSerializer(OutputStream JavaDoc out) {
52         super(out);
53     }
54     
55
56     public NonRecursiveSerializer(OutputStream JavaDoc out, String JavaDoc encoding)
57       throws UnsupportedEncodingException JavaDoc {
58         super(out, encoding);
59     }
60     
61     
62     protected final void write(Element element)
63       throws IOException JavaDoc {
64
65         // treat empty elements specially to avoid an instance of test
66
if (element.getChildCount() == 0) {
67             super.writeEmptyElementTag(element);
68         }
69         else {
70             Node current = element;
71             boolean end = false;
72             int index = -1;
73             while (true) {
74                 if (!end && current.getChildCount() > 0) {
75                    writeStartTag((Element) current);
76                    current = current.getChild(0);
77                    index = 0;
78                 }
79                 else {
80                     if (end) {
81                         writeEndTag((Element) current);
82                         if (current == element) break;
83                     }
84                     else {
85                         writeChild(current);
86                     }
87                     end = false;
88                     ParentNode parent = current.getParent();
89                     if (parent.getChildCount() - 1 == index) {
90                         current = parent;
91                         if (current != element) {
92                             parent = current.getParent();
93                             index = parent.indexOf(current);
94                         }
95                         end = true;
96                     }
97                     else {
98                         index++;
99                         current = parent.getChild(index);
100                     }
101                 }
102             }
103         }
104         
105     }
106
107     
108     public static void main(String JavaDoc[] args) {
109   
110         if (args.length <= 0) {
111           System.out.println("Usage: java nu.xom.samples.PrettyPrinter URL");
112           return;
113         }
114         
115         try {
116           Builder parser = new Builder();
117           Document doc = parser.build(args[0]);
118           Serializer serializer = new Serializer(System.out, "ISO-8859-1");
119           serializer.setIndent(4);
120           serializer.setMaxLength(64);
121           serializer.setPreserveBaseURI(true);
122           serializer.write(doc);
123           serializer.flush();
124         }
125         catch (ParsingException ex) {
126           System.out.println(args[0] + " is not well-formed.");
127           System.out.println(ex.getMessage());
128         }
129         catch (IOException JavaDoc ex) {
130           System.out.println(
131            "Due to an IOException, the parser could not check "
132            + args[0]
133           );
134         }
135   
136     }
137     
138 }
139
Popular Tags