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
043package org.jfree.xml.writer;
044
045import java.io.IOException;
046import java.io.Writer;
047import java.util.Properties;
048
049/**
050 * A class for writing XML to a character stream.
051 */
052public 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        }
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 tag) throws IOException {
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 name, final String attributeName, 
135                         final String attributeValue, final boolean close) 
136            throws IOException {
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 name, final AttributeList attributes, 
149            final boolean close) throws IOException {
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 name, final Properties attributes, 
163            final boolean close) throws IOException {
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 text) throws IOException {
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 {
184        this.writer.close();
185    }
186
187}