KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > xml > writer > XMLWriter


1 /* ========================================================================
2  * JCommon : a free general purpose class library for the Java(tm) platform
3  * ========================================================================
4  *
5  * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
6  *
7  * Project Info: http://www.jfree.org/jcommon/index.html
8  *
9  * This library is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2.1 of the License, or
12  * (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
17  * License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
22  * USA.
23  *
24  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
25  * in the United States and other countries.]
26  *
27  * --------------
28  * XMLWriter.java
29  * --------------
30  * (C) Copyright 2003-2005, by Thomas Morgner and Contributors.
31  *
32  * Original Author: Thomas Morgner;
33  * Contributor(s): David Gilbert (for Object Refinery Limited);
34  *
35  * $Id: XMLWriter.java,v 1.5 2005/11/08 14:42:42 mungady Exp $
36  *
37  * Changes (from 26-Nov-2003)
38  * --------------------------
39  * 26-Nov-2003 : Added standard header and Javadocs (DG);
40  *
41  */

42
43 package org.jfree.xml.writer;
44
45 import java.io.IOException JavaDoc;
46 import java.io.Writer JavaDoc;
47 import java.util.Properties JavaDoc;
48
49 /**
50  * A class for writing XML to a character stream.
51  */

52 public class XMLWriter extends XMLWriterSupport {
53
54     /**
55      * The character stream.
56      */

57     private Writer JavaDoc writer;
58
59     /**
60      * Creates a new XML writer for the specified character stream. By
61      * default, four spaces are used for indentation.
62      *
63      * @param writer the character stream.
64      */

65     public XMLWriter(final Writer JavaDoc writer) {
66         this(writer, " ");
67     }
68
69     /**
70      * Creates a new XML writer for the specified character stream.
71      *
72      * @param writer the character stream.
73      * @param indentString the string used for indentation (should contain
74      * white space, for example four spaces).
75      */

76     public XMLWriter(final Writer JavaDoc writer, final String JavaDoc indentString) {
77         super(new SafeTagList(), 0, indentString);
78         if (writer == null) {
79             throw new NullPointerException JavaDoc("Writer must not be null.");
80         }
81
82         this.writer = writer;
83     }
84
85     /**
86      * Writes the XML declaration that usually appears at the top of every XML
87      * file.
88      *
89      * @throws IOException if there is a problem writing to the character
90      * stream.
91      */

92     public void writeXmlDeclaration() throws IOException JavaDoc {
93         this.writer.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
94         this.writer.write(getLineSeparator());
95     }
96
97     /**
98      * Writes an opening XML tag that has no attributes.
99      *
100      * @param name the tag name.
101      * @param close a flag that controls whether or not the tag is closed
102      * immediately.
103      * @throws java.io.IOException if there is an I/O problem.
104      */

105     public void writeTag(final String JavaDoc name, final boolean close)
106             throws IOException JavaDoc {
107         if (close) {
108             writeTag(this.writer, name, new AttributeList(), close);
109         }
110         else {
111             writeTag(this.writer, name);
112         }
113     }
114
115     /**
116      * Writes a closing XML tag.
117      *
118      * @param tag the tag name.
119      * @throws java.io.IOException if there is an I/O problem.
120      */

121     public void writeCloseTag(final String JavaDoc tag) throws IOException JavaDoc {
122         writeCloseTag(this.writer, tag);
123     }
124
125     /**
126      * Writes an opening XML tag with an attribute/value pair.
127      *
128      * @param name the tag name.
129      * @param attributeName the attribute name.
130      * @param attributeValue the attribute value.
131      * @param close controls whether the tag is closed.
132      * @throws java.io.IOException if there is an I/O problem.
133      */

134     public void writeTag(final String JavaDoc name, final String JavaDoc attributeName,
135                          final String JavaDoc attributeValue, final boolean close)
136             throws IOException JavaDoc {
137         writeTag(this.writer, name, attributeName, attributeValue, close);
138     }
139
140     /**
141      * Writes an opening XML tag along with a list of attribute/value pairs.
142      *
143      * @param name the tag name.
144      * @param attributes the attributes.
145      * @param close controls whether the tag is closed.
146      * @throws java.io.IOException if there is an I/O problem.
147      */

148     public void writeTag(final String JavaDoc name, final AttributeList attributes,
149             final boolean close) throws IOException JavaDoc {
150         writeTag(this.writer, name, attributes, close);
151     }
152
153     /**
154      * Writes an opening XML tag along with a list of attribute/value pairs.
155      *
156      * @param name the tag name.
157      * @param attributes the attributes.
158      * @param close controls whether the tag is closed.
159      * @throws java.io.IOException if there is an I/O problem.
160      * @deprecated use the attribute list instead ...
161      */

162     public void writeTag(final String JavaDoc name, final Properties JavaDoc attributes,
163             final boolean close) throws IOException JavaDoc {
164         writeTag(this.writer, name, attributes, close);
165     }
166
167     /**
168      * Writes some text to the character stream.
169      *
170      * @param text the text.
171      * @throws IOException if there is a problem writing to the character
172      * stream.
173      */

174     public void writeText(final String JavaDoc text) throws IOException JavaDoc {
175         this.writer.write(text);
176     }
177
178     /**
179      * Closes the underlying character stream.
180      *
181      * @throws IOException if there is a problem closing the character stream.
182      */

183     public void close() throws IOException JavaDoc {
184         this.writer.close();
185     }
186
187 }
188
Popular Tags