File: UTF8XMLWriter.java

package info (click to toggle)
lib-xp-java 0.5-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 1,652 kB
  • ctags: 2,424
  • sloc: java: 8,085; makefile: 53; sh: 17; xml: 7
file content (428 lines) | stat: -rw-r--r-- 10,457 bytes parent folder | download | duplicates (3)
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
package com.jclark.xml.output;

import java.io.Writer;
import java.io.IOException;
import java.io.CharConversionException;
import java.io.OutputStream;

/**
 * An XMLWriter that encodes characters in UTF-8.
 * Methods are not synchronized: wrap this in a SyncXMLWriter
 * if you need to use this concurrently from multiple threads.
 * @version $Revision: 1.7 $ $Date: 1998/06/28 09:12:04 $
 */

public class UTF8XMLWriter extends XMLWriter {
  private OutputStream out;
  private boolean inStartTag = false;
  private boolean quoteWhitespace = false;
  private static final int DEFAULT_BUF_LENGTH = 4*1024;
  private byte[] buf = new byte[DEFAULT_BUF_LENGTH];
  private int bufUsed = 0;
  private String sysLineSeparator;
  private String lineSeparator;
  private boolean minimizeEmptyElements;
  private boolean minimizeEmptyElementsHtml;

  static final public int MINIMIZE_EMPTY_ELEMENTS = 1;
  static final public int MINIMIZE_EMPTY_ELEMENTS_HTML = 2;
  static final private int DEFAULT_OPTIONS = 0;
  /**
   * Create an XML writer that will write in UTF-8 to the specified
   * OutputStream with the specified options.
   */
  public UTF8XMLWriter(OutputStream out, int options) {
    super(out);
    this.out = out;
    if ((options & MINIMIZE_EMPTY_ELEMENTS_HTML) != 0)
      this.minimizeEmptyElements = this.minimizeEmptyElementsHtml = true;
    else if ((options & MINIMIZE_EMPTY_ELEMENTS) != 0)
      this.minimizeEmptyElements = true;
    sysLineSeparator = lineSeparator = System.getProperty("line.separator");
  }

  /**
   * Create an XML writer that will write in UTF-8 to the specified
   * OutputStream with the default options.
   */
  public UTF8XMLWriter(OutputStream out) {
    this(out, DEFAULT_OPTIONS);
  }

  public void writeUTF8(byte[] buf, int off, int len) throws IOException {
    if (inStartTag)
      finishStartTag();
    while (--len >= 0) {
      byte b = buf[off++];
      switch (b) {
      case (byte)'\n':
	writeRaw(lineSeparator);
	break;
      case (byte)'&':
	writeRaw("&");
	break;
      case (byte)'<':
	writeRaw("&lt;");
	break;
      case (byte)'>':
	writeRaw("&gt;");
	break;
      case (byte)'"':
	writeRaw("&quot;");
	break;
      default:
	put(b);
	break;
      }
    }
  }

  public void write(char cbuf[], int off, int len) throws IOException {
    if (len == 0)
      return;
    if (inStartTag)
      finishStartTag();
    do {
      try {
	writeQuote(cbuf[off++]);
      }
      catch (CharConversionException e) {
	if (len-- == 0)
	  throw e;
	writeSurrogatePair(cbuf[off - 1], cbuf[off]);
	off++;
      }
    } while (--len > 0);
  }

  public void write(char c) throws IOException {
    if (inStartTag)
      finishStartTag();
    writeQuote(c);
  }

  public void write(String str) throws IOException {
    int len = str.length();
    if (len == 0)
      return;
    if (inStartTag)
      finishStartTag();
    writeQuote(str, 0, len);
  }

  public void write(String str, int off, int len) throws IOException {
    if (len == 0)
      return;
    if (inStartTag)
      finishStartTag();
    writeQuote(str, off, len);
  }
  
  private final void writeQuote(String str, int off, int len) throws IOException {
    for (; off < len; off++) {
      try {
	writeQuote(str.charAt(off));
      }
      catch (CharConversionException e) {
	if (++off == len)
	  throw e;
	writeSurrogatePair(str.charAt(off - 1), str.charAt(off));
      }
    }
  }

  private final void writeQuote(char c) throws IOException {
    switch (c) {
    case '\n':
      writeRaw(quoteWhitespace ? "&#10;" : lineSeparator);
      break;
    case '&':
      writeRaw("&amp;");
      break;
    case '<':
      writeRaw("&lt;");
      break;
    case  '>':
      writeRaw("&gt;");
      break;
    case '"':
      writeRaw("&quot;");
      break;
    case '\r':
      if (quoteWhitespace || !(out instanceof ReplacementTextOutputStream))
	writeRaw("&#13;");
      else
	put((byte)'\r');
      break;
    case '\t':
      if (quoteWhitespace)
	writeRaw("&#9;");
      else
	put((byte)'\t');
      break;
    default:
      if (c < 0x80)
	put((byte)c);
      else
	writeMB(c);
    }
  }

  private void writeRaw(String str) throws IOException {
    final int n = str.length();
    for (int i = 0; i < n; i++) {
      char c = str.charAt(i);
      if (c < 0x80)
	put((byte)c);
      else {
	try {
	  writeMB(str.charAt(i));
	}
	catch (CharConversionException e) {
	  if (++i == n)
	    throw e;
	  writeSurrogatePair(c, str.charAt(i));
	}
      }
    }
  }

  private final void writeMB(char c) throws IOException {
    switch (c & 0xF800) {
    case 0:
      put((byte)(((c >> 6) & 0x1F) | 0xC0));
      put((byte)((c & 0x3F) | 0x80));
      break;
    default:
      put((byte)(((c >> 12) & 0xF) | 0xE0));
      put((byte)(((c >> 6) & 0x3F) | 0x80));
      put((byte)((c & 0x3F) | 0x80));
      break;
    case 0xD800:
      throw new CharConversionException();
    }
  }
  
  private final void writeSurrogatePair(char c1, char c2) throws IOException {
    if ((c1 & 0xFC00) != 0xD800 || (c2 & 0xFC00) != 0xDC00)
      throw new CharConversionException();
    int c = ((c1 & 0x3FF) << 10) | (c2 & 0x3FF);
    c += 0x10000;
    put((byte)(((c >> 18) & 0x7) | 0xF0));
    put((byte)(((c >> 12) & 0x3F) | 0x80));
    put((byte)(((c >> 6) & 0x3F) | 0x80));
    put((byte)((c & 0x3F) | 0x80));
  }

  public void startElement(String name) throws IOException {
    if (inStartTag)
      finishStartTag();
    put((byte)'<');
    writeRaw(name);
    inStartTag = true;
  }

  private void finishStartTag() throws IOException {
    inStartTag = false;
    put((byte)'>');
  }

  public void attribute(String name, String value) throws IOException {
    if (!inStartTag)
      throw new IllegalStateException("attribute outside of start-tag");
    put((byte)' ');
    writeRaw(name);
    put((byte)'=');
    put((byte)'"');
    quoteWhitespace = true;
    writeQuote(value, 0, value.length());
    quoteWhitespace = false;
    put((byte)'"');
  }

  public void startAttribute(String name) throws IOException {
    if (!inStartTag)
      throw new IllegalStateException("attribute outside of start-tag");
    inStartTag = false;
    quoteWhitespace = true;
    put((byte)' ');
    writeRaw(name);
    put((byte)'=');
    put((byte)'"');
  }

  public void endAttribute() throws IOException {
    put((byte)'"');
    inStartTag = true;
    quoteWhitespace = false;
  }

  public void endElement(String name) throws IOException {
    if (inStartTag) {
      inStartTag = false;
      if (minimizeEmptyElements) {
	if (minimizeEmptyElementsHtml)
	  put((byte)' ');
	put((byte)'/');
	put((byte)'>');
	return;
      }
      put((byte)'>');
    }
    put((byte)'<');
    put((byte)'/');
    writeRaw(name);
    put((byte)'>');
  }

  public void processingInstruction(String target, String data) throws IOException {
    if (inStartTag)
      finishStartTag();
    put((byte)'<');
    put((byte)'?');
    writeRaw(target);
    if (data.length() > 0) {
      put((byte)' ');
      writeMarkup(data);
    }
    put((byte)'?');
    put((byte)'>');
  }

  public void comment(String body) throws IOException {
    if (inStartTag)
      finishStartTag();
    writeRaw("<!--");
    writeMarkup(body);
    writeRaw("-->");
  }

  public void entityReference(boolean isParam, String name) throws IOException {
    if (inStartTag)
      finishStartTag();
    put(isParam ? (byte)'%' : (byte)'&');
    writeRaw(name);
    put((byte)';');
  }

  public void characterReference(int n) throws IOException {
    if (inStartTag)
      finishStartTag();
    writeRaw("&#");
    writeRaw(Integer.toString(n));
    put((byte)';');
  }

  public void cdataSection(String content) throws IOException {
    if (inStartTag)
      finishStartTag();
    writeRaw("<![CDATA[");
    writeMarkup(content);
    writeRaw("]]>");
  }

  public void markup(String str) throws IOException {
    if (inStartTag)
      finishStartTag();
    writeMarkup(str);
  }

  private static class ReplacementTextOutputStream extends OutputStream {
    private OutputStream out;
    ReplacementTextOutputStream(OutputStream out) {
      this.out = out;
    }
    public void write(int b) throws IOException {
      switch (b) {
      case '"':
      case '\'':
      case '%':
      case '&':
      case '\r':
	out.write('&');
	out.write('#');
	{
	  String s = Integer.toString(b);
	  for (int i = 0; i < s.length(); i++)
	    out.write(s.charAt(i));
	}
	out.write(';');
	break;
      default:
	out.write(b);
	break;
      }
    }
    public void close() throws IOException {
      out.close();
    }
    public void flush() throws IOException {
      out.flush();
    }
    OutputStream getOutputStream() {
      return out;
    }
  }

  public void startReplacementText() throws IOException {
    flushBuf();
    out = new ReplacementTextOutputStream(out);
    lineSeparator = "\n";
  }

  public void endReplacementText() throws IOException {
    flushBuf();
    out = ((ReplacementTextOutputStream)out).getOutputStream();
    if (!(out instanceof ReplacementTextOutputStream))
      lineSeparator = sysLineSeparator;
  }

  private void writeMarkup(String str) throws IOException {
    int len = str.length();
    for (int i = 0; i < len; i++) {
      char c = str.charAt(i);
      if (c == '\n')
	writeRaw(lineSeparator);
      else {
	try {
	  if (c < 0x80)
	    put((byte)c);
	  else
	    writeMB(c);
	}
	catch (CharConversionException e) {
	  if (++i == len)
	    throw e;
	  writeSurrogatePair(c, str.charAt(i));
	}
      }
    }
  }

  private final void put(byte b) throws IOException {
    if (bufUsed == buf.length)
      flushBuf();
    buf[bufUsed++] = b;
  }

  private final void flushBuf() throws IOException {
    out.write(buf, 0, bufUsed);
    bufUsed = 0;
  }

  public void flush() throws IOException {
    if (bufUsed != 0)
      flushBuf();
    out.flush();
  }

  public void close() throws IOException {
    if (out != null) {
      if (bufUsed != 0)
	flushBuf();
      out.close();
      out = null;
      buf = null;
    }
  }
}