blob: 60f7e04ea1b15b2e9daf3950e84094dbb8e597da (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
|
// **********************************************************************
//
// Copyright (c) 2003
// ZeroC, Inc.
// Billerica, MA, USA
//
// All Rights Reserved.
//
// Ice is free software; you can redistribute it and/or modify it under
// the terms of the GNU General Public License version 2 as published by
// the Free Software Foundation.
//
// **********************************************************************
package IceUtil;
public class XMLOutput extends OutputBase
{
public
XMLOutput()
{
super();
_se = false;
_text = false;
_sgml = false;
_escape = false;
}
public
XMLOutput(java.io.PrintWriter writer)
{
super(writer);
_se = false;
_text = false;
_sgml = false;
_escape = false;
}
public
XMLOutput(String s)
{
super(s);
_se = false;
_text = false;
_sgml = false;
_escape = false;
}
public void
setSGML(boolean sgml)
{
_sgml = true;
}
public void
print(String s)
{
if(_se)
{
_out.print('>');
_se = false;
}
_text = true;
if(_escape)
{
String escaped = escape(s);
super.print(escaped);
}
else
{
super.print(s);
}
}
public XMLOutput
write(String s)
{
print(s);
return this;
}
public void
nl()
{
if(_se)
{
_se = false;
_out.print('>');
}
super.nl();
}
public XMLOutput
se(String element)
{
nl();
//
// If we're not in SGML mode the output of the '>' character is
// deferred until either the end-element (in which case a /> is
// emitted) or until something is displayed.
//
if(_escape)
{
_out.print('<');
_out.print(escape(element));
}
else
{
_out.print('<');
_out.print(element);
}
_se = true;
_text = false;
int pos = element.indexOf(' ');
if(pos == -1)
{
pos = element.indexOf('\t');
}
if(pos == -1)
{
_elementStack.addFirst(element);
}
else
{
_elementStack.addFirst(element.substring(0, pos));
}
++_pos; // TODO: ???
inc();
_separator = false;
return this;
}
public XMLOutput
ee()
{
String element = (String)_elementStack.removeFirst();
dec();
if(_se)
{
//
// SGML (docbook) doesn't support <foo/>
//
if(_sgml)
{
_out.print("></");
_out.print(element);
_out.print(">");
}
else
{
_out.print("/>");
}
}
else
{
if(!_text)
{
nl();
}
_out.print("</");
_out.print(element);
_out.print(">");
}
--_pos; // TODO: ???
_se = false;
_text = false;
return this;
}
public XMLOutput
attr(String name, String value)
{
//
// Precondition: Attributes can only be attached to elements.
//
assert(_se);
_out.print(" ");
_out.print(name);
_out.print("=\"");
_out.print(escape(value));
_out.print("\"");
return this;
}
public XMLOutput
startEscapes()
{
_escape = true;
return this;
}
public XMLOutput
endEscapes()
{
_escape = false;
return this;
}
public String
currentElement()
{
if(_elementStack.size() > 0)
{
return (String)_elementStack.getFirst();
}
else
{
return "";
}
}
private String
escape(String input)
{
String v = input;
//
// Find out whether there is a reserved character to avoid
// conversion if not necessary.
//
final String allReserved = "<>'\"&";
boolean hasReserved = false;
char[] arr = input.toCharArray();
for(int i = 0; i < arr.length; i++)
{
if(allReserved.indexOf(arr[i]) != -1)
{
hasReserved = true;
break;
}
}
if(hasReserved)
{
//
// First convert all & to &
//
if(v.indexOf('&') != -1)
{
v = v.replaceAll("&", "&");
}
//
// Next convert remaining reserved characters.
//
if(v.indexOf('>') != -1)
{
v = v.replaceAll(">", ">");
}
if(v.indexOf('<') != -1)
{
v = v.replaceAll("<", "<");
}
if(v.indexOf('\'') != -1)
{
v = v.replaceAll("'", "'");
}
if(v.indexOf('"') != -1)
{
v = v.replaceAll("\"", """);
}
}
return v;
}
private java.util.LinkedList _elementStack = new java.util.LinkedList();
boolean _se;
boolean _text;
private boolean _sgml;
private boolean _escape;
}
|