001: /* ========================================================================
002: * JCommon : a free general purpose class library for the Java(tm) platform
003: * ========================================================================
004: *
005: * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
006: *
007: * Project Info: http://www.jfree.org/jcommon/index.html
008: *
009: * This library is free software; you can redistribute it and/or modify it
010: * under the terms of the GNU Lesser General Public License as published by
011: * the Free Software Foundation; either version 2.1 of the License, or
012: * (at your option) any later version.
013: *
014: * This library is distributed in the hope that it will be useful, but
015: * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
016: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
017: * License for more details.
018: *
019: * You should have received a copy of the GNU Lesser General Public
020: * License along with this library; if not, write to the Free Software
021: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
022: * USA.
023: *
024: * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
025: * in the United States and other countries.]
026: *
027: * --------------
028: * XMLWriter.java
029: * --------------
030: * (C) Copyright 2003-2005, by Thomas Morgner and Contributors.
031: *
032: * Original Author: Thomas Morgner;
033: * Contributor(s): David Gilbert (for Object Refinery Limited);
034: *
035: * $Id: XMLWriter.java,v 1.5 2005/11/08 14:42:42 mungady Exp $
036: *
037: * Changes (from 26-Nov-2003)
038: * --------------------------
039: * 26-Nov-2003 : Added standard header and Javadocs (DG);
040: *
041: */
042:
043: package org.jfree.xml.writer;
044:
045: import java.io.IOException;
046: import java.io.Writer;
047: import java.util.Properties;
048:
049: /**
050: * A class for writing XML to a character stream.
051: */
052: public class XMLWriter extends XMLWriterSupport {
053:
054: /**
055: * The character stream.
056: */
057: private Writer writer;
058:
059: /**
060: * Creates a new XML writer for the specified character stream. By
061: * default, four spaces are used for indentation.
062: *
063: * @param writer the character stream.
064: */
065: public XMLWriter(final Writer writer) {
066: this (writer, " ");
067: }
068:
069: /**
070: * Creates a new XML writer for the specified character stream.
071: *
072: * @param writer the character stream.
073: * @param indentString the string used for indentation (should contain
074: * white space, for example four spaces).
075: */
076: public XMLWriter(final Writer writer, final String indentString) {
077: super (new SafeTagList(), 0, indentString);
078: if (writer == null) {
079: throw new NullPointerException("Writer must not be null.");
080: }
081:
082: this .writer = writer;
083: }
084:
085: /**
086: * Writes the XML declaration that usually appears at the top of every XML
087: * file.
088: *
089: * @throws IOException if there is a problem writing to the character
090: * stream.
091: */
092: public void writeXmlDeclaration() throws IOException {
093: this .writer.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
094: this .writer.write(getLineSeparator());
095: }
096:
097: /**
098: * Writes an opening XML tag that has no attributes.
099: *
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 name, final boolean close)
106: throws IOException {
107: if (close) {
108: writeTag(this .writer, name, new AttributeList(), close);
109: } else {
110: writeTag(this .writer, name);
111: }
112: }
113:
114: /**
115: * Writes a closing XML tag.
116: *
117: * @param tag the tag name.
118: * @throws java.io.IOException if there is an I/O problem.
119: */
120: public void writeCloseTag(final String tag) throws IOException {
121: writeCloseTag(this .writer, tag);
122: }
123:
124: /**
125: * Writes an opening XML tag with an attribute/value pair.
126: *
127: * @param name the tag name.
128: * @param attributeName the attribute name.
129: * @param attributeValue the attribute value.
130: * @param close controls whether the tag is closed.
131: * @throws java.io.IOException if there is an I/O problem.
132: */
133: public void writeTag(final String name, final String attributeName,
134: final String attributeValue, final boolean close)
135: throws IOException {
136: writeTag(this .writer, name, attributeName, attributeValue,
137: 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 name,
149: final AttributeList attributes, final boolean close)
150: throws IOException {
151: writeTag(this .writer, name, attributes, close);
152: }
153:
154: /**
155: * Writes an opening XML tag along with a list of attribute/value pairs.
156: *
157: * @param name the tag name.
158: * @param attributes the attributes.
159: * @param close controls whether the tag is closed.
160: * @throws java.io.IOException if there is an I/O problem.
161: * @deprecated use the attribute list instead ...
162: */
163: public void writeTag(final String name,
164: final Properties attributes, final boolean close)
165: throws IOException {
166: writeTag(this .writer, name, attributes, close);
167: }
168:
169: /**
170: * Writes some text to the character stream.
171: *
172: * @param text the text.
173: * @throws IOException if there is a problem writing to the character
174: * stream.
175: */
176: public void writeText(final String text) throws IOException {
177: this .writer.write(text);
178: }
179:
180: /**
181: * Closes the underlying character stream.
182: *
183: * @throws IOException if there is a problem closing the character stream.
184: */
185: public void close() throws IOException {
186: this.writer.close();
187: }
188:
189: }
|