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-2005, by Sasa Markovic. 9 * 10 * Developers: Sasa Markovic (saxon@jrobin.org) 11 * 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.awt.*; 29 import java.io.File; 30 import java.io.OutputStream; 31 import java.io.PrintWriter; 32 import java.util.Stack; 33 34 /** 35 * Extremely simple utility class used to create XML documents. 36 */ 37 public class XmlWriter { 38 static final String INDENT_STR = " "; 39 40 private PrintWriter writer; 41 private StringBuffer indent = new StringBuffer(""); 42 private Stack<String> openTags = new Stack<String>(); 43 44 /** 45 * Creates XmlWriter with the specified output stream to send XML code to. 46 * 47 * @param stream Output stream which receives XML code 48 */ 49 public XmlWriter(OutputStream stream) { 50 writer = new PrintWriter(stream, true); 51 } 52 53 /** 54 * Opens XML tag 55 * 56 * @param tag XML tag name 57 */ 58 public void startTag(String tag) { 59 writer.println(indent + "<" + tag + ">"); 60 openTags.push(tag); 61 indent.append(INDENT_STR); 62 } 63 64 /** 65 * Closes the corresponding XML tag 66 */ 67 public void closeTag() { 68 String tag = openTags.pop(); 69 indent.setLength(indent.length() - INDENT_STR.length()); 70 writer.println(indent + "</" + tag + ">"); 71 } 72 73 /** 74 * Writes <tag>value</tag> to output stream 75 * 76 * @param tag XML tag name 77 * @param value value to be placed between <code><tag></code> and <code></tag></code> 78 */ 79 public void writeTag(String tag, Object value) { 80 if (value != null) { 81 writer.println(indent + "<" + tag + ">" + 82 escape(value.toString()) + "</" + tag + ">"); 83 } 84 else { 85 writer.println(indent + "<" + tag + "></" + tag + ">"); 86 } 87 } 88 89 /** 90 * Writes <tag>value</tag> to output stream 91 * 92 * @param tag XML tag name 93 * @param value value to be placed between <code><tag></code> and <code></tag></code> 94 */ 95 public void writeTag(String tag, int value) { 96 writeTag(tag, "" + value); 97 } 98 99 /** 100 * Writes <tag>value</tag> to output stream 101 * 102 * @param tag XML tag name 103 * @param value value to be placed between <code><tag></code> and <code></tag></code> 104 */ 105 public void writeTag(String tag, long value) { 106 writeTag(tag, "" + value); 107 } 108 109 /** 110 * Writes <tag>value</tag> to output stream 111 * 112 * @param tag XML tag name 113 * @param value value to be placed between <code><tag></code> and <code></tag></code> 114 */ 115 public void writeTag(String tag, double value, String nanString) { 116 writeTag(tag, Util.formatDouble(value, nanString, true)); 117 } 118 119 /** 120 * Writes <tag>value</tag> to output stream 121 * 122 * @param tag XML tag name 123 * @param value value to be placed between <code><tag></code> and <code></tag></code> 124 */ 125 public void writeTag(String tag, double value) { 126 writeTag(tag, Util.formatDouble(value, true)); 127 } 128 129 /** 130 * Writes <tag>value</tag> to output stream 131 * 132 * @param tag XML tag name 133 * @param value value to be placed between <code><tag></code> and <code></tag></code> 134 */ 135 public void writeTag(String tag, boolean value) { 136 writeTag(tag, "" + value); 137 } 138 139 /** 140 * Writes <tag>value</tag> to output stream 141 * 142 * @param tag XML tag name 143 * @param value value to be placed between <code><tag></code> and <code></tag></code> 144 */ 145 public void writeTag(String tag, Color value) { 146 int rgb = value.getRGB() & 0xFFFFFF; 147 writeTag(tag, "#" + Integer.toHexString(rgb).toUpperCase()); 148 } 149 150 /** 151 * Writes <tag>value</tag> to output stream 152 * 153 * @param tag XML tag name 154 * @param value value to be placed between <code><tag></code> and <code></tag></code> 155 */ 156 public void writeTag(String tag, Font value) { 157 startTag(tag); 158 writeTag("name", value.getName()); 159 int style = value.getStyle(); 160 if ((style & Font.BOLD) != 0 && (style & Font.ITALIC) != 0) { 161 writeTag("style", "BOLDITALIC"); 162 } 163 else if ((style & Font.BOLD) != 0) { 164 writeTag("style", "BOLD"); 165 } 166 else if ((style & Font.ITALIC) != 0) { 167 writeTag("style", "ITALIC"); 168 } 169 else { 170 writeTag("style", "PLAIN"); 171 } 172 writeTag("size", value.getSize()); 173 closeTag(); 174 } 175 176 /** 177 * Writes <tag>value</tag> to output stream 178 * 179 * @param tag XML tag name 180 * @param value value to be placed between <code><tag></code> and <code></tag></code> 181 */ 182 public void writeTag(String tag, File value) { 183 writeTag(tag, value.getPath()); 184 } 185 186 /** 187 * Flushes the output stream 188 */ 189 public void flush() { 190 writer.flush(); 191 } 192 193 protected void finalize() throws Throwable { 194 super.finalize(); 195 writer.close(); 196 } 197 198 /** 199 * Writes XML comment to output stream 200 * 201 * @param comment comment string 202 */ 203 public void writeComment(Object comment) { 204 writer.println(indent + "<!-- " + escape(comment.toString()) + " -->"); 205 } 206 207 private static String escape(String s) { 208 return s.replaceAll("<", "<").replaceAll(">", ">"); 209 } 210 }