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 | |
|
27 | 0 | private char indentChar = '\t'; |
28 | |
private int indent; |
29 | |
private PrintStream out; |
30 | |
|
31 | |
public XmlWriter () |
32 | 0 | { |
33 | 0 | } |
34 | |
|
35 | |
public XmlWriter (PrintStream out, int indent) |
36 | 0 | { |
37 | 0 | this.indent = indent; |
38 | 0 | this.out = out; |
39 | 0 | } |
40 | |
|
41 | |
public void setOut (PrintStream out) |
42 | |
{ |
43 | 0 | this.out = out; |
44 | 0 | } |
45 | |
|
46 | |
public void setIndent (int indent) |
47 | |
{ |
48 | 0 | this.indent = indent; |
49 | 0 | } |
50 | |
|
51 | |
public int getIndent () |
52 | |
{ |
53 | 0 | return indent; |
54 | |
} |
55 | |
|
56 | |
public void incrementIndent () |
57 | |
{ |
58 | 0 | indent ++; |
59 | 0 | } |
60 | |
|
61 | |
public void decrementIndent () |
62 | |
{ |
63 | 0 | indent --; |
64 | 0 | } |
65 | |
|
66 | |
public PrintStream getOut () |
67 | |
{ |
68 | 0 | return out; |
69 | |
} |
70 | |
|
71 | |
public void indent () |
72 | |
{ |
73 | 0 | indent (out, indentChar); |
74 | 0 | } |
75 | |
|
76 | |
public void indent (PrintStream o, char indentCharacter) |
77 | |
{ |
78 | 0 | for (int i = 0; i < indent; i ++) |
79 | |
{ |
80 | 0 | o.print (indentCharacter); |
81 | |
} |
82 | 0 | } |
83 | |
|
84 | |
public void println (String str) |
85 | |
{ |
86 | 0 | out.println (str); |
87 | 0 | } |
88 | |
|
89 | |
public void indentPrintln (String str) |
90 | |
{ |
91 | 0 | indent (); |
92 | 0 | out.println (str); |
93 | 0 | } |
94 | |
|
95 | |
public void indentPrint (String str) |
96 | |
{ |
97 | 0 | indent (); |
98 | 0 | out.print (str); |
99 | 0 | } |
100 | |
|
101 | |
public void print (String str) |
102 | |
{ |
103 | 0 | out.print (str); |
104 | 0 | } |
105 | |
|
106 | |
public void writeAttribute (String attribute, String value) |
107 | |
{ |
108 | 0 | if (value == null) |
109 | |
{ |
110 | 0 | return; |
111 | |
} |
112 | 0 | if (value.equals ("")) |
113 | |
{ |
114 | 0 | return; |
115 | |
} |
116 | 0 | out.print (" "); |
117 | 0 | out.print (attribute); |
118 | 0 | out.print ("=\""); |
119 | 0 | out.print (value); |
120 | 0 | out.print ("\""); |
121 | 0 | } |
122 | |
|
123 | |
public void writeTag (String tag, String value) |
124 | |
{ |
125 | 0 | writeTag (tag, null, value); |
126 | 0 | } |
127 | |
|
128 | |
public void writeTag (String tag, String modifiers, String value) |
129 | |
{ |
130 | 0 | if (value == null) |
131 | |
{ |
132 | 0 | return; |
133 | |
} |
134 | 0 | if (value.equals ("")) |
135 | |
{ |
136 | 0 | return; |
137 | |
} |
138 | 0 | indent (); |
139 | 0 | out.print ("<" + tag); |
140 | 0 | if (modifiers != null && ! modifiers.isEmpty ()) |
141 | |
{ |
142 | 0 | out.print (" " + modifiers); |
143 | |
} |
144 | 0 | out.print (">"); |
145 | 0 | out.print (escapeXML (value)); |
146 | 0 | out.print ("</" + tag + ">"); |
147 | 0 | out.println (""); |
148 | 0 | } |
149 | |
|
150 | |
public void writeComment (String comment) |
151 | |
{ |
152 | 0 | if (comment == null) |
153 | |
{ |
154 | 0 | return; |
155 | |
} |
156 | 0 | if (comment.equals ("")) |
157 | |
{ |
158 | 0 | return; |
159 | |
} |
160 | 0 | indent (); |
161 | 0 | out.print ("<!-- "); |
162 | 0 | out.print (comment); |
163 | 0 | out.println (" -->"); |
164 | 0 | } |
165 | |
|
166 | |
public String escapeXML (String s) |
167 | |
{ |
168 | 0 | StringBuffer sb = new StringBuffer (); |
169 | 0 | int n = s.length (); |
170 | 0 | for (int i = 0; i < n; i ++) |
171 | |
{ |
172 | |
|
173 | 0 | char c = s.charAt (i); |
174 | 0 | switch (c) |
175 | |
{ |
176 | |
case '"': |
177 | 0 | sb.append ("""); |
178 | 0 | break; |
179 | |
case '\'': |
180 | 0 | sb.append ("'"); |
181 | 0 | break; |
182 | |
case '&': |
183 | 0 | sb.append ("&"); |
184 | 0 | break; |
185 | |
case '<': |
186 | 0 | sb.append ("<"); |
187 | 0 | break; |
188 | |
case '>': |
189 | 0 | sb.append (">"); |
190 | 0 | break; |
191 | |
|
192 | |
|
193 | |
|
194 | |
|
195 | |
default: |
196 | 0 | sb.append (c); |
197 | |
break; |
198 | |
} |
199 | |
} |
200 | 0 | return sb.toString (); |
201 | |
} |
202 | |
|
203 | |
public void writeCommentBox (String comment) |
204 | |
{ |
205 | 0 | String border = |
206 | |
"***************************************************************"; |
207 | 0 | while (border.length () < comment.length ()) |
208 | |
{ |
209 | 0 | border = border + border; |
210 | |
} |
211 | 0 | border = border.substring (0, comment.length ()); |
212 | 0 | writeComment (border); |
213 | 0 | writeComment (comment); |
214 | 0 | writeComment (border); |
215 | 0 | } |
216 | |
} |