~ubuntu-branches/ubuntu/precise/xom/precise

« back to all changes in this revision

Viewing changes to src/nu/xom/samples/EZSerializer.java

  • Committer: Bazaar Package Importer
  • Author(s): Varun Hiremath
  • Date: 2007-11-25 15:50:40 UTC
  • Revision ID: james.westby@ubuntu.com-20071125155040-r75ikcqf1vu0cei7
Tags: upstream-1.1
ImportĀ upstreamĀ versionĀ 1.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright 2003, 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;
 
25
import java.io.OutputStream;
 
26
 
 
27
import nu.xom.Document;
 
28
import nu.xom.Serializer;
 
29
 
 
30
/**
 
31
 * <p>
 
32
 *  A collection of static methods for serializing documents
 
33
 *  in one method call. 
 
34
 * </p>
 
35
 * 
 
36
 * @author Elliotte Rusty Harold
 
37
 * @version 1.0
 
38
 * 
 
39
 */
 
40
public class EZSerializer {
 
41
 
 
42
    
 
43
    /**
 
44
     * <p>
 
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 
 
47
     * method completes.
 
48
     * </p>
 
49
     * 
 
50
     * @param doc the <code>Document</code> to serialize
 
51
     * @param out the <code>OutputStream</code> on which the document
 
52
     *     is written
 
53
     * 
 
54
     * @throws IOException if the underlying <code>OutputStream</code>
 
55
     *     encounters an I/O error
 
56
     * @throws NullPointerException if <code>doc</code> is null
 
57
     */
 
58
    public static void write(Document doc, OutputStream out) 
 
59
      throws IOException {
 
60
        Serializer serializer = new Serializer(out);
 
61
        serializer.write(doc);
 
62
        serializer.flush();
 
63
    }
 
64
    
 
65
    /**
 
66
     * <p>
 
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 
 
72
     * method completes.
 
73
     * </p>
 
74
     * 
 
75
     * <p>
 
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.
 
82
     * </p>
 
83
     * 
 
84
     * @param doc the <code>Document</code> to serialize
 
85
     * @param out the <code>OutputStream</code> on which the document
 
86
     *     is written
 
87
     * @param encoding the character encoding in which to write 
 
88
     *     the document
 
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
 
92
     *    line
 
93
     * 
 
94
     * @throws IOException if the underlying <code>OutputStream</code>
 
95
     *     encounters an I/O error
 
96
     * @throws NullPointerException if <code>doc</code> is null
 
97
     */
 
98
    public static void write(Document doc, OutputStream out, String encoding,
 
99
      int indent, int maxLength) 
 
100
      throws IOException {
 
101
        Serializer serializer = new Serializer(out, encoding);
 
102
        serializer.setIndent(indent);
 
103
        serializer.setMaxLength(maxLength);
 
104
        serializer.write(doc);
 
105
        serializer.flush();
 
106
    }
 
107
    
 
108
}