1 19 20 package org.apache.cayenne.util; 21 22 import java.io.PrintWriter ; 23 import java.util.Collection ; 24 import java.util.Iterator ; 25 import java.util.Map ; 26 27 33 public class XMLEncoder { 34 protected String indent; 35 protected PrintWriter out; 36 37 protected boolean indentLine; 38 protected int indentTimes; 39 40 public XMLEncoder(PrintWriter out) { 41 this.out = out; 42 } 43 44 public XMLEncoder(PrintWriter out, String indent) { 45 this.indent = indent; 46 this.out = out; 47 } 48 49 public PrintWriter getPrintWriter() { 50 return out; 51 } 52 53 public void indent(int i) { 54 indentTimes += i; 55 if (indentTimes < 0) { 56 indentTimes = 0; 57 } 58 } 59 60 64 public void print(Map map) { 65 Iterator it = map.entrySet().iterator(); 66 while (it.hasNext()) { 67 Map.Entry entry = (Map.Entry ) it.next(); 68 ((XMLSerializable) entry.getValue()).encodeAsXML(this); 69 } 70 } 71 72 76 public void print(Collection c) { 77 Iterator it = c.iterator(); 78 while (it.hasNext()) { 79 ((XMLSerializable) it.next()).encodeAsXML(this); 80 } 81 } 82 83 86 public void printProperty(String name, String value) { 87 printIndent(); 88 out.print("<property name=\""); 89 out.print(name); 90 out.print("\" value=\""); 91 out.print(value); 92 out.println("\"/>"); 93 indentLine = true; 94 } 95 96 99 public void printProperty(String name, boolean b) { 100 printProperty(name, String.valueOf(b)); 101 } 102 103 106 public void printProperty(String name, int i) { 107 printProperty(name, String.valueOf(i)); 108 } 109 110 public void print(String text) { 111 printIndent(); 112 out.print(text); 113 } 114 115 public void print(char c) { 116 printIndent(); 117 out.print(c); 118 } 119 120 public void print(boolean b) { 121 printIndent(); 122 out.print(b); 123 } 124 125 public void print(int i) { 126 printIndent(); 127 out.print(i); 128 } 129 130 public void println(String text) { 131 printIndent(); 132 out.println(text); 133 indentLine = true; 134 } 135 136 public void println(char c) { 137 printIndent(); 138 out.println(c); 139 indentLine = true; 140 } 141 142 private void printIndent() { 143 if (indentLine) { 144 indentLine = false; 145 146 if (indentTimes > 0 && indent != null) { 147 for (int i = 0; i < indentTimes; i++) { 148 out.print(indent); 149 } 150 } 151 } 152 } 153 } 154 | Popular Tags |