| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
package org.kuali.student.contract.writer; |
| 17 | |
|
| 18 | |
import java.io.PrintStream; |
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
|
| 23 | |
|
| 24 | |
public class XmlWriter { |
| 25 | |
|
| 26 | 0 | private char indentChar = '\t'; |
| 27 | |
private int indent; |
| 28 | |
private PrintStream out; |
| 29 | |
|
| 30 | 0 | public XmlWriter() { |
| 31 | 0 | } |
| 32 | |
|
| 33 | 0 | public XmlWriter(PrintStream out, int indent) { |
| 34 | 0 | this.indent = indent; |
| 35 | 0 | this.out = out; |
| 36 | 0 | } |
| 37 | |
|
| 38 | |
public void setOut(PrintStream out) { |
| 39 | 0 | this.out = out; |
| 40 | 0 | } |
| 41 | |
|
| 42 | |
public void setIndent(int indent) { |
| 43 | 0 | this.indent = indent; |
| 44 | 0 | } |
| 45 | |
|
| 46 | |
public int getIndent() { |
| 47 | 0 | return indent; |
| 48 | |
} |
| 49 | |
|
| 50 | |
public void incrementIndent() { |
| 51 | 0 | indent++; |
| 52 | 0 | } |
| 53 | |
|
| 54 | |
public void decrementIndent() { |
| 55 | 0 | indent--; |
| 56 | 0 | } |
| 57 | |
|
| 58 | |
public PrintStream getOut() { |
| 59 | 0 | return out; |
| 60 | |
} |
| 61 | |
|
| 62 | |
public void indent() { |
| 63 | 0 | indent(out, indentChar); |
| 64 | 0 | } |
| 65 | |
|
| 66 | |
public void indent(PrintStream o, char indentCharacter) { |
| 67 | 0 | for (int i = 0; i < indent; i++) { |
| 68 | 0 | o.print(indentCharacter); |
| 69 | |
} |
| 70 | 0 | } |
| 71 | |
|
| 72 | |
public void println(String str) { |
| 73 | 0 | out.println(str); |
| 74 | 0 | } |
| 75 | |
|
| 76 | |
public void indentPrintln(String str) { |
| 77 | 0 | indent(); |
| 78 | 0 | out.println(str); |
| 79 | 0 | } |
| 80 | |
|
| 81 | |
public void indentPrint(String str) { |
| 82 | 0 | indent(); |
| 83 | 0 | out.print(str); |
| 84 | 0 | } |
| 85 | |
|
| 86 | |
public void print(String str) { |
| 87 | 0 | out.print(str); |
| 88 | 0 | } |
| 89 | |
|
| 90 | |
public void writeAttribute(String attribute, String value) { |
| 91 | 0 | if (value == null) { |
| 92 | 0 | return; |
| 93 | |
} |
| 94 | 0 | if (value.equals("")) { |
| 95 | 0 | return; |
| 96 | |
} |
| 97 | 0 | out.print(" "); |
| 98 | 0 | out.print(attribute); |
| 99 | 0 | out.print("=\""); |
| 100 | 0 | out.print(value); |
| 101 | 0 | out.print("\""); |
| 102 | 0 | } |
| 103 | |
|
| 104 | |
public void writeTag(String tag, String value) { |
| 105 | 0 | writeTag(tag, null, value); |
| 106 | 0 | } |
| 107 | |
|
| 108 | |
public void writeTag(String tag, String modifiers, String value) { |
| 109 | 0 | if (value == null) { |
| 110 | 0 | return; |
| 111 | |
} |
| 112 | 0 | if (value.equals("")) { |
| 113 | 0 | return; |
| 114 | |
} |
| 115 | 0 | indent(); |
| 116 | 0 | out.print("<" + tag); |
| 117 | 0 | if (modifiers != null && !modifiers.isEmpty()) { |
| 118 | 0 | out.print(" " + modifiers); |
| 119 | |
} |
| 120 | 0 | out.print(">"); |
| 121 | |
|
| 122 | 0 | out.print(value); |
| 123 | 0 | out.print("</" + tag + ">"); |
| 124 | 0 | out.println(""); |
| 125 | 0 | } |
| 126 | |
|
| 127 | |
public void writeComment(String comment) { |
| 128 | 0 | if (comment == null) { |
| 129 | 0 | return; |
| 130 | |
} |
| 131 | 0 | if (comment.equals("")) { |
| 132 | 0 | return; |
| 133 | |
} |
| 134 | 0 | indent(); |
| 135 | 0 | out.print("<!-- "); |
| 136 | 0 | out.print(comment); |
| 137 | 0 | out.println(" -->"); |
| 138 | 0 | } |
| 139 | |
|
| 140 | |
public String escapeXML(String s) { |
| 141 | 0 | StringBuffer sb = new StringBuffer(); |
| 142 | 0 | int n = s.length(); |
| 143 | 0 | for (int i = 0; i < n; i++) { |
| 144 | |
|
| 145 | 0 | char c = s.charAt(i); |
| 146 | 0 | switch (c) { |
| 147 | |
case '"': |
| 148 | 0 | sb.append("""); |
| 149 | 0 | break; |
| 150 | |
case '\'': |
| 151 | 0 | sb.append("'"); |
| 152 | 0 | break; |
| 153 | |
case '&': |
| 154 | 0 | sb.append("&"); |
| 155 | 0 | break; |
| 156 | |
case '<': |
| 157 | 0 | sb.append("<"); |
| 158 | 0 | break; |
| 159 | |
case '>': |
| 160 | 0 | sb.append(">"); |
| 161 | 0 | break; |
| 162 | |
|
| 163 | |
|
| 164 | |
|
| 165 | |
|
| 166 | |
default: |
| 167 | 0 | sb.append(c); |
| 168 | |
break; |
| 169 | |
} |
| 170 | |
} |
| 171 | 0 | return sb.toString(); |
| 172 | |
} |
| 173 | |
|
| 174 | |
public void writeCommentBox(String comment) { |
| 175 | 0 | String border = |
| 176 | |
"***************************************************************"; |
| 177 | 0 | while (border.length() < comment.length()) { |
| 178 | 0 | border = border + border; |
| 179 | |
} |
| 180 | 0 | border = border.substring(0, comment.length()); |
| 181 | 0 | writeComment(border); |
| 182 | 0 | writeComment(comment); |
| 183 | 0 | writeComment(border); |
| 184 | 0 | } |
| 185 | |
} |