1
/* ========================================================================
2
* JCommon : a free general purpose class library for the Java(tm) platform
3
* ========================================================================
5
* (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
7
* Project Info: http://www.jfree.org/jcommon/index.html
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.
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.
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,
24
* [Java is a trademark or registered trademark of Sun Microsystems, Inc.
25
* in the United States and other countries.]
27
* ---------------------
28
* XMLWriterSupport.java
29
* ---------------------
30
* (C)opyright 2003-2005, by Thomas Morgner and Contributors.
32
* Original Author: Thomas Morgner;
33
* Contributor(s): David Gilbert (for Object Refinery Limited);
35
* $Id: XMLWriterSupport.java,v 1.6 2005/11/08 14:35:52 mungady Exp $
39
* 21-Jun-2003 : Initial version (TM);
40
* 26-Nov-2003 : Updated Javadocs (DG);
44
package org.jfree.xml.writer;
46
import java.io.IOException;
47
import java.io.Writer;
48
import java.util.Enumeration;
49
import java.util.Iterator;
50
import java.util.Properties;
53
* A support class for writing XML files.
55
* @author Thomas Morgner
57
public class XMLWriterSupport {
59
/** A constant for controlling the indent function. */
60
public static final int OPEN_TAG_INCREASE = 1;
62
/** A constant for controlling the indent function. */
63
public static final int CLOSE_TAG_DECREASE = 2;
65
/** A constant for controlling the indent function. */
66
public static final int INDENT_ONLY = 3;
68
/** A constant for close. */
69
public static final boolean CLOSE = true;
71
/** A constant for open. */
72
public static final boolean OPEN = false;
74
/** The line separator. */
75
private static String lineSeparator;
77
/** A list of safe tags. */
78
private SafeTagList safeTags;
80
/** The indent level for that writer. */
81
private int indentLevel;
83
/** The indent string. */
84
private String indentString;
87
* A flag indicating whether to force a linebreak before printing the next
90
private boolean newLineOk;
93
* Default Constructor. The created XMLWriterSupport will not have no safe
94
* tags and starts with an indention level of 0.
96
public XMLWriterSupport() {
97
this(new SafeTagList(), 0);
101
* Creates a new support instance.
103
* @param safeTags tags that are safe for line breaks.
104
* @param indentLevel the index level.
106
public XMLWriterSupport(final SafeTagList safeTags, final int indentLevel) {
107
this(safeTags, indentLevel, " ");
111
* Creates a new support instance.
113
* @param safeTags the tags that are safe for line breaks.
114
* @param indentLevel the indent level.
115
* @param indentString the indent string.
117
public XMLWriterSupport(final SafeTagList safeTags, final int indentLevel,
118
final String indentString) {
119
if (indentString == null) {
120
throw new NullPointerException("IndentString must not be null");
123
this.safeTags = safeTags;
124
this.indentLevel = indentLevel;
125
this.indentString = indentString;
129
* Starts a new block by increasing the indent level.
131
* @throws IOException if an IO error occurs.
133
public void startBlock() throws IOException {
139
* Ends the current block by decreasing the indent level.
141
* @throws IOException if an IO error occurs.
143
public void endBlock() throws IOException {
149
* Forces a linebreak on the next call to writeTag or writeCloseTag.
151
* @throws IOException if an IO error occurs.
153
public void allowLineBreak() throws IOException {
154
this.newLineOk = true;
158
* Returns the line separator.
160
* @return the line separator.
162
public static String getLineSeparator() {
163
if (lineSeparator == null) {
165
lineSeparator = System.getProperty("line.separator", "\n");
167
catch (SecurityException se) {
168
lineSeparator = "\n";
171
return lineSeparator;
175
* Writes an opening XML tag that has no attributes.
177
* @param w the writer.
178
* @param name the tag name.
180
* @throws java.io.IOException if there is an I/O problem.
182
public void writeTag(final Writer w, final String name) throws IOException {
183
if (this.newLineOk) {
184
w.write(getLineSeparator());
186
indent(w, OPEN_TAG_INCREASE);
191
if (getSafeTags().isSafeForOpen(name)) {
192
w.write(getLineSeparator());
197
* Writes a closing XML tag.
199
* @param w the writer.
200
* @param tag the tag name.
202
* @throws java.io.IOException if there is an I/O problem.
204
public void writeCloseTag(final Writer w, final String tag)
206
// check whether the tag contains CData - we ma not indent such tags
207
if (this.newLineOk || getSafeTags().isSafeForOpen(tag)) {
208
if (this.newLineOk) {
209
w.write(getLineSeparator());
211
indent(w, CLOSE_TAG_DECREASE);
219
if (getSafeTags().isSafeForClose(tag)) {
220
w.write(getLineSeparator());
222
this.newLineOk = false;
226
* Writes an opening XML tag with an attribute/value pair.
228
* @param w the writer.
229
* @param name the tag name.
230
* @param attributeName the attribute name.
231
* @param attributeValue the attribute value.
232
* @param close controls whether the tag is closed.
234
* @throws java.io.IOException if there is an I/O problem.
236
public void writeTag(final Writer w, final String name,
237
final String attributeName, final String attributeValue,
238
final boolean close) throws IOException {
239
final AttributeList attr = new AttributeList();
240
if (attributeName != null) {
241
attr.setAttribute(attributeName, attributeValue);
243
writeTag(w, name, attr, close);
247
* Writes an opening XML tag along with a list of attribute/value pairs.
249
* @param w the writer.
250
* @param name the tag name.
251
* @param attributes the attributes.
252
* @param close controls whether the tag is closed.
254
* @throws java.io.IOException if there is an I/O problem.
255
* @deprecated use the attribute list instead of the properties.
257
public void writeTag(final Writer w, final String name,
258
final Properties attributes, final boolean close)
260
final AttributeList attList = new AttributeList();
261
final Enumeration keys = attributes.keys();
262
while (keys.hasMoreElements()) {
263
final String key = (String) keys.nextElement();
264
attList.setAttribute(key, attributes.getProperty(key));
266
writeTag(w, name, attList, close);
270
* Writes an opening XML tag along with a list of attribute/value pairs.
272
* @param w the writer.
273
* @param name the tag name.
274
* @param attributes the attributes.
275
* @param close controls whether the tag is closed.
277
* @throws java.io.IOException if there is an I/O problem.
279
public void writeTag(final Writer w, final String name,
280
final AttributeList attributes, final boolean close)
283
if (this.newLineOk) {
284
w.write(getLineSeparator());
285
this.newLineOk = false;
287
indent(w, OPEN_TAG_INCREASE);
291
final Iterator keys = attributes.keys();
292
while (keys.hasNext()) {
293
final String key = (String) keys.next();
294
final String value = attributes.getAttribute(key);
298
w.write(normalize(value));
303
if (getSafeTags().isSafeForClose(name)) {
304
w.write(getLineSeparator());
310
if (getSafeTags().isSafeForOpen(name)) {
311
w.write(getLineSeparator());
317
* Normalises a string, replacing certain characters with their escape
318
* sequences so that the XML text is not corrupted.
320
* @param s the string.
322
* @return the normalised string.
324
public static String normalize(final String s) {
328
final StringBuffer str = new StringBuffer();
329
final int len = s.length();
331
for (int i = 0; i < len; i++) {
332
final char ch = s.charAt(i);
352
str.append(""");
358
final char lastChar = str.charAt(str.length() - 1);
360
if (lastChar != '\r') {
361
str.append(getLineSeparator());
368
str.append(getLineSeparator());
379
return (str.toString());
383
* Indent the line. Called for proper indenting in various places.
385
* @param writer the writer which should receive the indentention.
386
* @param increase the current indent level.
387
* @throws java.io.IOException if writing the stream failed.
389
public void indent(final Writer writer, final int increase)
391
if (increase == CLOSE_TAG_DECREASE) {
394
for (int i = 0; i < this.indentLevel; i++) {
395
writer.write(this.indentString); // 4 spaces, we could also try tab,
396
// but I do not know whether this works
397
// with our XML edit pane
399
if (increase == OPEN_TAG_INCREASE) {
405
* Returns the current indent level.
407
* @return the current indent level.
409
public int getIndentLevel() {
410
return this.indentLevel;
414
* Increases the indention by one level.
416
protected void increaseIndent() {
421
* Decreates the indention by one level.
423
protected void decreaseIndent() {
428
* Returns the list of safe tags.
432
public SafeTagList getSafeTags() {
433
return this.safeTags;