1
/* Copyright 2003, 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;
27
import nu.xom.Document;
28
import nu.xom.Serializer;
32
* A collection of static methods for serializing documents
36
* @author Elliotte Rusty Harold
40
public class EZSerializer {
45
* Serializes a document onto the output stream in UTF-8 with no
46
* pretty printing. The stream is flushed but not closed when this
50
* @param doc the <code>Document</code> to serialize
51
* @param out the <code>OutputStream</code> on which the document
54
* @throws IOException if the underlying <code>OutputStream</code>
55
* encounters an I/O error
56
* @throws NullPointerException if <code>doc</code> is null
58
public static void write(Document doc, OutputStream out)
60
Serializer serializer = new Serializer(out);
61
serializer.write(doc);
67
* Serializes a document onto the output stream in the specified
68
* encoding. White space is added to attempt to pretty print the
69
* document, potentially changing the document's content.
70
* Existing white space in the document may be trimmed or changed
71
* in the process. The stream is flushed but not closed when this
76
* The <code>indent</code> and <code>maxLength</code> arguments
77
* are suggestive, not prescriptive. XOM will attempt to honor
78
* them but cannot guarantee to do so. For instance, if an
79
* element name is longer than the maximum line length, XOM
80
* cannot break the element name. It has to emit an
81
* excessively long line.
84
* @param doc the <code>Document</code> to serialize
85
* @param out the <code>OutputStream</code> on which the document
87
* @param encoding the character encoding in which to write
89
* @param indent the number of spaces to indent each successive
90
* level of the hierarchy
91
* @param maxLength the maximum preferred number of characters per
94
* @throws IOException if the underlying <code>OutputStream</code>
95
* encounters an I/O error
96
* @throws NullPointerException if <code>doc</code> is null
98
public static void write(Document doc, OutputStream out, String encoding,
99
int indent, int maxLength)
101
Serializer serializer = new Serializer(out, encoding);
102
serializer.setIndent(indent);
103
serializer.setMaxLength(maxLength);
104
serializer.write(doc);