KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jrobin > core > XmlWriter


1 /* ============================================================
2  * JRobin : Pure java implementation of RRDTool's functionality
3  * ============================================================
4  *
5  * Project Info: http://www.jrobin.org
6  * Project Lead: Sasa Markovic (saxon@jrobin.org);
7  *
8  * (C) Copyright 2003, by Sasa Markovic.
9  *
10  * Developers: Sasa Markovic (saxon@jrobin.org)
11  * Arne Vandamme (cobralord@jrobin.org)
12  *
13  * This library is free software; you can redistribute it and/or modify it under the terms
14  * of the GNU Lesser General Public License as published by the Free Software Foundation;
15  * either version 2.1 of the License, or (at your option) any later version.
16  *
17  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
18  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
19  * See the GNU Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public License along with this
22  * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
23  * Boston, MA 02111-1307, USA.
24  */

25
26 package org.jrobin.core;
27
28 import java.io.OutputStream JavaDoc;
29 import java.io.PrintWriter JavaDoc;
30 import java.io.File JavaDoc;
31 import java.util.Stack JavaDoc;
32 import java.awt.*;
33
34 /**
35  * Extremely simple utility class used to create XML documents.
36  */

37 public class XmlWriter {
38     static final String JavaDoc INDENT_STR = " ";
39
40     private PrintWriter JavaDoc writer;
41     private StringBuffer JavaDoc indent = new StringBuffer JavaDoc("");
42     private Stack JavaDoc openTags = new Stack JavaDoc();
43
44     /**
45      * Creates XmlWriter with the specified output stream to send XML code to.
46      * @param stream Output stream which receives XML code
47      */

48     public XmlWriter(OutputStream JavaDoc stream) {
49         writer = new PrintWriter JavaDoc(stream);
50     }
51
52     /**
53      * Opens XML tag
54      * @param tag XML tag name
55      */

56     public void startTag(String JavaDoc tag) {
57         writer.println(indent + "<" + tag + ">");
58         openTags.push(tag);
59         indent.append(INDENT_STR);
60     }
61
62     /**
63      * Closes the corresponding XML tag
64      */

65     public void closeTag() {
66         String JavaDoc tag = (String JavaDoc) openTags.pop();
67         indent.setLength(indent.length() - INDENT_STR.length());
68         writer.println(indent + "</" + tag + ">");
69     }
70
71     /**
72      * Writes &lt;tag&gt;value&lt;/tag&gt; to output stream
73      * @param tag XML tag name
74      * @param value value to be placed between <code>&lt;tag&gt</code> and <code>&lt;/tag&gt;</code>
75      */

76     public void writeTag(String JavaDoc tag, Object JavaDoc value) {
77         if(value != null) {
78             writer.println(indent + "<" + tag + ">" +
79                 escape(value.toString()) + "</" + tag + ">");
80         }
81         else {
82             writer.println(indent + "<" + tag + "></" + tag + ">");
83         }
84     }
85
86     /**
87      * Writes &lt;tag&gt;value&lt;/tag&gt; to output stream
88      * @param tag XML tag name
89      * @param value value to be placed between <code>&lt;tag&gt</code> and <code>&lt;/tag&gt;</code>
90      */

91     public void writeTag(String JavaDoc tag, int value) {
92         writeTag(tag, "" + value);
93     }
94
95     /**
96      * Writes &lt;tag&gt;value&lt;/tag&gt; to output stream
97      * @param tag XML tag name
98      * @param value value to be placed between <code>&lt;tag&gt</code> and <code>&lt;/tag&gt;</code>
99      */

100     public void writeTag(String JavaDoc tag, long value) {
101         writeTag(tag, "" + value);
102     }
103
104     /**
105      * Writes &lt;tag&gt;value&lt;/tag&gt; to output stream
106      * @param tag XML tag name
107      * @param value value to be placed between <code>&lt;tag&gt</code> and <code>&lt;/tag&gt;</code>
108      */

109     public void writeTag(String JavaDoc tag, double value, String JavaDoc nanString) {
110         writeTag(tag, Util.formatDouble(value, nanString, true));
111     }
112
113     /**
114      * Writes &lt;tag&gt;value&lt;/tag&gt; to output stream
115      * @param tag XML tag name
116      * @param value value to be placed between <code>&lt;tag&gt</code> and <code>&lt;/tag&gt;</code>
117      */

118     public void writeTag(String JavaDoc tag, double value) {
119         writeTag(tag, Util.formatDouble(value, true));
120     }
121
122     /**
123      * Writes &lt;tag&gt;value&lt;/tag&gt; to output stream
124      * @param tag XML tag name
125      * @param value value to be placed between <code>&lt;tag&gt</code> and <code>&lt;/tag&gt;</code>
126      */

127     public void writeTag(String JavaDoc tag, boolean value) {
128         writeTag(tag, "" + value);
129     }
130
131     /**
132      * Writes &lt;tag&gt;value&lt;/tag&gt; to output stream
133      * @param tag XML tag name
134      * @param value value to be placed between <code>&lt;tag&gt</code> and <code>&lt;/tag&gt;</code>
135      */

136     public void writeTag(String JavaDoc tag, Color value) {
137         int rgb = value.getRGB() & 0xFFFFFF;
138         writeTag(tag, "#" + Integer.toHexString(rgb).toUpperCase());
139     }
140
141     /**
142      * Writes &lt;tag&gt;value&lt;/tag&gt; to output stream
143      * @param tag XML tag name
144      * @param value value to be placed between <code>&lt;tag&gt</code> and <code>&lt;/tag&gt;</code>
145      */

146     public void writeTag(String JavaDoc tag, Font value) {
147         startTag(tag);
148         writeTag("name", value.getName());
149         int style = value.getStyle();
150         if((style & Font.BOLD) != 0 && (style & Font.ITALIC) != 0) {
151             writeTag("style", "BOLDITALIC");
152         }
153         else if((style & Font.BOLD) != 0) {
154             writeTag("style", "BOLD");
155         }
156         else if((style & Font.ITALIC) != 0) {
157             writeTag("style", "ITALIC");
158         }
159         else {
160             writeTag("style", "PLAIN");
161         }
162         writeTag("size", value.getSize());
163         closeTag();
164     }
165
166     /**
167      * Writes &lt;tag&gt;value&lt;/tag&gt; to output stream
168      * @param tag XML tag name
169      * @param value value to be placed between <code>&lt;tag&gt</code> and <code>&lt;/tag&gt;</code>
170      */

171     public void writeTag(String JavaDoc tag, File JavaDoc value) {
172         writeTag(tag, value.getPath());
173     }
174
175     /**
176      * Flushes the output stream
177      */

178     public void flush() {
179         writer.flush();
180     }
181
182     protected void finalize() {
183         writer.close();
184     }
185
186     /**
187      * Writes XML comment to output stream
188      * @param comment comment string
189      */

190     public void writeComment(Object JavaDoc comment) {
191         writer.println(indent + "<!-- " + escape(comment.toString()) + " -->");
192     }
193
194     private static String JavaDoc escape(String JavaDoc s) {
195         return s.replaceAll("<", "&lt;").replaceAll(">", "&gt;");
196     }
197 }
198
Popular Tags