1 | |
package org.apache.maven.surefire.util; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
import org.codehaus.plexus.util.xml.XMLWriter; |
23 | |
|
24 | |
import java.io.PrintWriter; |
25 | |
import java.util.LinkedList; |
26 | |
|
27 | |
public class PrettyPrintXMLWriter |
28 | |
implements XMLWriter |
29 | |
{ |
30 | |
private final PrintWriter writer; |
31 | |
|
32 | 0 | private final LinkedList elementStack = new LinkedList(); |
33 | |
|
34 | |
private boolean tagInProgress; |
35 | |
|
36 | |
private int depth; |
37 | |
|
38 | |
private final String lineIndenter; |
39 | |
|
40 | |
private final String encoding; |
41 | |
|
42 | |
private final String docType; |
43 | |
|
44 | |
private boolean readyForNewLine; |
45 | |
|
46 | |
private boolean tagIsEmpty; |
47 | |
|
48 | |
public PrettyPrintXMLWriter( PrintWriter writer ) |
49 | |
{ |
50 | 0 | this( writer, null, null ); |
51 | 0 | } |
52 | |
|
53 | |
private PrettyPrintXMLWriter( PrintWriter writer, String lineIndenter, String encoding, String doctype ) |
54 | 0 | { |
55 | 0 | this.writer = writer; |
56 | |
|
57 | 0 | this.lineIndenter = lineIndenter; |
58 | |
|
59 | 0 | this.encoding = encoding; |
60 | |
|
61 | 0 | this.docType = doctype; |
62 | |
|
63 | 0 | if ( docType != null || encoding != null ) |
64 | |
{ |
65 | 0 | writeDocumentHeaders(); |
66 | |
} |
67 | 0 | } |
68 | |
|
69 | |
private PrettyPrintXMLWriter( PrintWriter writer, String encoding, String doctype ) |
70 | |
{ |
71 | 0 | this( writer, " ", encoding, doctype ); |
72 | 0 | } |
73 | |
|
74 | |
public void startElement( String name ) |
75 | |
{ |
76 | 0 | tagIsEmpty = false; |
77 | |
|
78 | 0 | finishTag(); |
79 | |
|
80 | 0 | write( "<" ); |
81 | |
|
82 | 0 | write( name ); |
83 | |
|
84 | 0 | elementStack.addLast( name ); |
85 | |
|
86 | 0 | tagInProgress = true; |
87 | |
|
88 | 0 | depth++; |
89 | |
|
90 | 0 | readyForNewLine = true; |
91 | |
|
92 | 0 | tagIsEmpty = true; |
93 | 0 | } |
94 | |
|
95 | |
public void writeText( String text ) |
96 | |
{ |
97 | 0 | writeText( text, true ); |
98 | 0 | } |
99 | |
|
100 | |
public void writeMarkup( String text ) |
101 | |
{ |
102 | 0 | writeText( text, false ); |
103 | 0 | } |
104 | |
|
105 | |
private void writeText( String text, boolean escapeXml ) |
106 | |
{ |
107 | 0 | readyForNewLine = false; |
108 | |
|
109 | 0 | tagIsEmpty = false; |
110 | |
|
111 | 0 | finishTag(); |
112 | |
|
113 | 0 | if ( escapeXml ) |
114 | |
{ |
115 | 0 | text = escapeXml( text ); |
116 | |
} |
117 | |
|
118 | 0 | write( text ); |
119 | 0 | } |
120 | |
|
121 | |
private static String escapeXml( String text ) |
122 | |
{ |
123 | 0 | StringBuffer sb = new StringBuffer ( text.length() * 2 ); |
124 | 0 | for ( int i = 0; i < text.length(); i++ ) |
125 | |
{ |
126 | 0 | char c = text.charAt( i ); |
127 | 0 | if ( c < 32 ) |
128 | |
{ |
129 | 0 | if ( c == '\n' || c == '\r' || c == '\t' ) |
130 | |
{ |
131 | 0 | sb.append( c ); |
132 | |
} |
133 | |
else |
134 | |
{ |
135 | |
|
136 | |
|
137 | |
|
138 | |
|
139 | |
|
140 | 0 | sb.append( "&#" ).append( (int) c ).append( ';' ); |
141 | |
} |
142 | |
} |
143 | 0 | else if ( c == '<' ) |
144 | |
{ |
145 | 0 | sb.append( "<" ); |
146 | |
} |
147 | 0 | else if ( c == '>' ) |
148 | |
{ |
149 | 0 | sb.append( ">" ); |
150 | |
} |
151 | 0 | else if ( c == '&' ) |
152 | |
{ |
153 | 0 | sb.append( "&" ); |
154 | |
} |
155 | 0 | else if ( c == '"' ) |
156 | |
{ |
157 | 0 | sb.append( """ ); |
158 | |
} |
159 | 0 | else if ( c == '\'' ) |
160 | |
{ |
161 | 0 | sb.append( "'" ); |
162 | |
} |
163 | |
else |
164 | |
{ |
165 | 0 | sb.append( c ); |
166 | |
} |
167 | |
} |
168 | 0 | return sb.toString(); |
169 | |
} |
170 | |
|
171 | |
public void addAttribute( String key, String value ) |
172 | |
{ |
173 | 0 | write( " " ); |
174 | |
|
175 | 0 | write( key ); |
176 | |
|
177 | 0 | write( "=\"" ); |
178 | |
|
179 | 0 | write( escapeXml( value ) ); |
180 | |
|
181 | 0 | write( "\"" ); |
182 | 0 | } |
183 | |
|
184 | |
public void endElement() |
185 | |
{ |
186 | 0 | depth--; |
187 | |
|
188 | 0 | if ( tagIsEmpty ) |
189 | |
{ |
190 | 0 | write( "/" ); |
191 | |
|
192 | 0 | readyForNewLine = false; |
193 | |
|
194 | 0 | finishTag(); |
195 | |
|
196 | 0 | elementStack.removeLast(); |
197 | |
} |
198 | |
else |
199 | |
{ |
200 | 0 | finishTag(); |
201 | |
|
202 | 0 | write( "</" + elementStack.removeLast() + ">" ); |
203 | |
} |
204 | |
|
205 | 0 | readyForNewLine = true; |
206 | 0 | } |
207 | |
|
208 | |
private void write( String str ) |
209 | |
{ |
210 | 0 | writer.write( str ); |
211 | 0 | } |
212 | |
|
213 | |
private void finishTag() |
214 | |
{ |
215 | 0 | if ( tagInProgress ) |
216 | |
{ |
217 | 0 | write( ">" ); |
218 | |
} |
219 | |
|
220 | 0 | tagInProgress = false; |
221 | |
|
222 | 0 | if ( readyForNewLine ) |
223 | |
{ |
224 | 0 | endOfLine(); |
225 | |
} |
226 | 0 | readyForNewLine = false; |
227 | |
|
228 | 0 | tagIsEmpty = false; |
229 | 0 | } |
230 | |
|
231 | |
protected void endOfLine() |
232 | |
{ |
233 | 0 | write( "\n" ); |
234 | |
|
235 | 0 | for ( int i = 0; i < depth; i++ ) |
236 | |
{ |
237 | 0 | write( lineIndenter ); |
238 | |
} |
239 | 0 | } |
240 | |
|
241 | |
private void writeDocumentHeaders() |
242 | |
{ |
243 | 0 | write( "<?xml version=\"1.0\"" ); |
244 | |
|
245 | 0 | if ( encoding != null ) |
246 | |
{ |
247 | 0 | write( " encoding=\"" + encoding + "\"" ); |
248 | |
} |
249 | |
|
250 | 0 | write( "?>" ); |
251 | |
|
252 | 0 | endOfLine(); |
253 | |
|
254 | 0 | if ( docType != null ) |
255 | |
{ |
256 | 0 | write( "<!DOCTYPE " ); |
257 | |
|
258 | 0 | write( docType ); |
259 | |
|
260 | 0 | write( ">" ); |
261 | |
|
262 | 0 | endOfLine(); |
263 | |
} |
264 | 0 | } |
265 | |
} |