1 56 package org.objectstyle.cayenne.util; 57 58 import java.io.PrintWriter ; 59 import java.util.Collection ; 60 import java.util.Iterator ; 61 import java.util.Map ; 62 63 69 public class XMLEncoder { 70 protected String indent; 71 protected PrintWriter out; 72 73 protected boolean indentLine; 74 protected int indentTimes; 75 76 public XMLEncoder(PrintWriter out) { 77 this.out = out; 78 } 79 80 public XMLEncoder(PrintWriter out, String indent) { 81 this.indent = indent; 82 this.out = out; 83 } 84 85 public PrintWriter getPrintWriter() { 86 return out; 87 } 88 89 public void indent(int i) { 90 indentTimes += i; 91 if (indentTimes < 0) { 92 indentTimes = 0; 93 } 94 } 95 96 100 public void print(Map map) { 101 Iterator it = map.entrySet().iterator(); 102 while (it.hasNext()) { 103 Map.Entry entry = (Map.Entry ) it.next(); 104 ((XMLSerializable) entry.getValue()).encodeAsXML(this); 105 } 106 } 107 108 112 public void print(Collection c) { 113 Iterator it = c.iterator(); 114 while (it.hasNext()) { 115 ((XMLSerializable) it.next()).encodeAsXML(this); 116 } 117 } 118 119 122 public void printProperty(String name, String value) { 123 printIndent(); 124 out.print("<property name=\""); 125 out.print(name); 126 out.print("\" value=\""); 127 out.print(value); 128 out.println("\"/>"); 129 indentLine = true; 130 } 131 132 135 public void printProperty(String name, boolean b) { 136 printProperty(name, String.valueOf(b)); 137 } 138 139 142 public void printProperty(String name, int i) { 143 printProperty(name, String.valueOf(i)); 144 } 145 146 public void print(String text) { 147 printIndent(); 148 out.print(text); 149 } 150 151 public void print(char c) { 152 printIndent(); 153 out.print(c); 154 } 155 156 public void print(boolean b) { 157 printIndent(); 158 out.print(b); 159 } 160 161 public void print(int i) { 162 printIndent(); 163 out.print(i); 164 } 165 166 public void println(String text) { 167 printIndent(); 168 out.println(text); 169 indentLine = true; 170 } 171 172 public void println(char c) { 173 printIndent(); 174 out.println(c); 175 indentLine = true; 176 } 177 178 private void printIndent() { 179 if (indentLine) { 180 indentLine = false; 181 182 if (indentTimes > 0 && indent != null) { 183 for (int i = 0; i < indentTimes; i++) { 184 out.print(indent); 185 } 186 } 187 } 188 } 189 } 190 | Popular Tags |