1
/* Copyright 2002-2004 Elliotte Rusty Harold
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.
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.
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
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/
22
package nu.xom.samples;
24
import java.io.IOException;
25
import java.io.OutputStream;
26
import java.io.UnsupportedEncodingException;
28
import nu.xom.Builder;
29
import nu.xom.Document;
30
import nu.xom.Element;
32
import nu.xom.ParentNode;
33
import nu.xom.ParsingException;
34
import nu.xom.Serializer;
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.
44
* @author Elliotte Rusty Harold
48
public final class NonRecursiveSerializer extends Serializer {
51
public NonRecursiveSerializer(OutputStream out) {
56
public NonRecursiveSerializer(OutputStream out, String encoding)
57
throws UnsupportedEncodingException {
62
protected final void write(Element element)
65
// treat empty elements specially to avoid an instance of test
66
if (element.getChildCount() == 0) {
67
super.writeEmptyElementTag(element);
70
Node current = element;
74
if (!end && current.getChildCount() > 0) {
75
writeStartTag((Element) current);
76
current = current.getChild(0);
81
writeEndTag((Element) current);
82
if (current == element) break;
88
ParentNode parent = current.getParent();
89
if (parent.getChildCount() - 1 == index) {
91
if (current != element) {
92
parent = current.getParent();
93
index = parent.indexOf(current);
99
current = parent.getChild(index);
108
public static void main(String[] args) {
110
if (args.length <= 0) {
111
System.out.println("Usage: java nu.xom.samples.PrettyPrinter URL");
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);
125
catch (ParsingException ex) {
126
System.out.println(args[0] + " is not well-formed.");
127
System.out.println(ex.getMessage());
129
catch (IOException ex) {
131
"Due to an IOException, the parser could not check "