1 25 26 package org.jrobin.core; 27 28 import java.io.OutputStream ; 29 import java.io.PrintWriter ; 30 import java.io.File ; 31 import java.util.Stack ; 32 import java.awt.*; 33 34 37 public class XmlWriter { 38 static final String INDENT_STR = " "; 39 40 private PrintWriter writer; 41 private StringBuffer indent = new StringBuffer (""); 42 private Stack openTags = new Stack (); 43 44 48 public XmlWriter(OutputStream stream) { 49 writer = new PrintWriter (stream); 50 } 51 52 56 public void startTag(String tag) { 57 writer.println(indent + "<" + tag + ">"); 58 openTags.push(tag); 59 indent.append(INDENT_STR); 60 } 61 62 65 public void closeTag() { 66 String tag = (String ) openTags.pop(); 67 indent.setLength(indent.length() - INDENT_STR.length()); 68 writer.println(indent + "</" + tag + ">"); 69 } 70 71 76 public void writeTag(String tag, Object 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 91 public void writeTag(String tag, int value) { 92 writeTag(tag, "" + value); 93 } 94 95 100 public void writeTag(String tag, long value) { 101 writeTag(tag, "" + value); 102 } 103 104 109 public void writeTag(String tag, double value, String nanString) { 110 writeTag(tag, Util.formatDouble(value, nanString, true)); 111 } 112 113 118 public void writeTag(String tag, double value) { 119 writeTag(tag, Util.formatDouble(value, true)); 120 } 121 122 127 public void writeTag(String tag, boolean value) { 128 writeTag(tag, "" + value); 129 } 130 131 136 public void writeTag(String tag, Color value) { 137 int rgb = value.getRGB() & 0xFFFFFF; 138 writeTag(tag, "#" + Integer.toHexString(rgb).toUpperCase()); 139 } 140 141 146 public void writeTag(String 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 171 public void writeTag(String tag, File value) { 172 writeTag(tag, value.getPath()); 173 } 174 175 178 public void flush() { 179 writer.flush(); 180 } 181 182 protected void finalize() { 183 writer.close(); 184 } 185 186 190 public void writeComment(Object comment) { 191 writer.println(indent + "<!-- " + escape(comment.toString()) + " -->"); 192 } 193 194 private static String escape(String s) { 195 return s.replaceAll("<", "<").replaceAll(">", ">"); 196 } 197 } 198 | Popular Tags |