author | aefimov |
Mon, 31 Mar 2014 19:03:41 +0400 | |
changeset 23777 | ce87cedb71cf |
parent 12457 | c348e06f0e82 |
child 25834 | aba3efbf4ec5 |
permissions | -rw-r--r-- |
6 | 1 |
/* |
2 |
* reserved comment block |
|
3 |
* DO NOT REMOVE OR ALTER! |
|
4 |
*/ |
|
5 |
/* |
|
23777
ce87cedb71cf
8035437: Xerces Update: xml/serialize/DOMSerializerImpl
aefimov
parents:
12457
diff
changeset
|
6 |
* Licensed to the Apache Software Foundation (ASF) under one or more |
ce87cedb71cf
8035437: Xerces Update: xml/serialize/DOMSerializerImpl
aefimov
parents:
12457
diff
changeset
|
7 |
* contributor license agreements. See the NOTICE file distributed with |
ce87cedb71cf
8035437: Xerces Update: xml/serialize/DOMSerializerImpl
aefimov
parents:
12457
diff
changeset
|
8 |
* this work for additional information regarding copyright ownership. |
ce87cedb71cf
8035437: Xerces Update: xml/serialize/DOMSerializerImpl
aefimov
parents:
12457
diff
changeset
|
9 |
* The ASF licenses this file to You under the Apache License, Version 2.0 |
ce87cedb71cf
8035437: Xerces Update: xml/serialize/DOMSerializerImpl
aefimov
parents:
12457
diff
changeset
|
10 |
* (the "License"); you may not use this file except in compliance with |
ce87cedb71cf
8035437: Xerces Update: xml/serialize/DOMSerializerImpl
aefimov
parents:
12457
diff
changeset
|
11 |
* the License. You may obtain a copy of the License at |
6 | 12 |
* |
13 |
* http://www.apache.org/licenses/LICENSE-2.0 |
|
14 |
* |
|
15 |
* Unless required by applicable law or agreed to in writing, software |
|
16 |
* distributed under the License is distributed on an "AS IS" BASIS, |
|
17 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
18 |
* See the License for the specific language governing permissions and |
|
19 |
* limitations under the License. |
|
20 |
*/ |
|
21 |
||
22 |
// Sep 14, 2000: |
|
23 |
// Fixed comments to preserve whitespaces and add a line break |
|
24 |
// when indenting. Reported by Gervase Markham <gerv@gerv.net> |
|
25 |
// Sep 14, 2000: |
|
26 |
// Fixed serializer to report IO exception directly, instead at |
|
27 |
// the end of document processing. |
|
28 |
// Reported by Patrick Higgins <phiggins@transzap.com> |
|
29 |
// Sep 13, 2000: |
|
30 |
// CR in character data will print as �D; |
|
31 |
// Aug 25, 2000: |
|
32 |
// Fixed processing instruction printing inside element content |
|
33 |
// to not escape content. Reported by Mikael Staldal |
|
34 |
// <d96-mst@d.kth.se> |
|
35 |
// Aug 25, 2000: |
|
36 |
// Added ability to omit comments. |
|
37 |
// Contributed by Anupam Bagchi <abagchi@jtcsv.com> |
|
38 |
// Aug 26, 2000: |
|
39 |
// Fixed bug in newline handling when preserving spaces. |
|
40 |
// Contributed by Mike Dusseault <mdusseault@home.com> |
|
41 |
// Aug 29, 2000: |
|
42 |
// Fixed state.unescaped not being set to false when |
|
43 |
// entering element state. |
|
44 |
// Reported by Lowell Vaughn <lvaughn@agillion.com> |
|
45 |
||
46 |
||
47 |
package com.sun.org.apache.xml.internal.serialize; |
|
48 |
||
49 |
||
50 |
import java.io.IOException; |
|
51 |
import java.io.OutputStream; |
|
52 |
import java.io.Writer; |
|
53 |
import java.util.Hashtable; |
|
54 |
import java.util.Vector; |
|
55 |
||
56 |
import com.sun.org.apache.xerces.internal.dom.DOMErrorImpl; |
|
57 |
import com.sun.org.apache.xerces.internal.dom.DOMLocatorImpl; |
|
58 |
import com.sun.org.apache.xerces.internal.dom.DOMMessageFormatter; |
|
59 |
import com.sun.org.apache.xerces.internal.util.XMLChar; |
|
23777
ce87cedb71cf
8035437: Xerces Update: xml/serialize/DOMSerializerImpl
aefimov
parents:
12457
diff
changeset
|
60 |
import org.w3c.dom.DOMError; |
ce87cedb71cf
8035437: Xerces Update: xml/serialize/DOMSerializerImpl
aefimov
parents:
12457
diff
changeset
|
61 |
import org.w3c.dom.DOMErrorHandler; |
6 | 62 |
import org.w3c.dom.Document; |
63 |
import org.w3c.dom.DocumentFragment; |
|
64 |
import org.w3c.dom.DocumentType; |
|
65 |
import org.w3c.dom.Element; |
|
66 |
import org.w3c.dom.Node; |
|
67 |
import org.w3c.dom.ls.LSException; |
|
68 |
import org.w3c.dom.ls.LSSerializerFilter; |
|
69 |
import org.w3c.dom.traversal.NodeFilter; |
|
70 |
import org.xml.sax.ContentHandler; |
|
71 |
import org.xml.sax.DTDHandler; |
|
72 |
import org.xml.sax.DocumentHandler; |
|
73 |
import org.xml.sax.Locator; |
|
74 |
import org.xml.sax.SAXException; |
|
75 |
import org.xml.sax.ext.DeclHandler; |
|
76 |
import org.xml.sax.ext.LexicalHandler; |
|
77 |
||
78 |
/** |
|
79 |
* Base class for a serializer supporting both DOM and SAX pretty |
|
80 |
* serializing of XML/HTML/XHTML documents. Derives classes perform |
|
81 |
* the method-specific serializing, this class provides the common |
|
82 |
* serializing mechanisms. |
|
83 |
* <p> |
|
84 |
* The serializer must be initialized with the proper writer and |
|
85 |
* output format before it can be used by calling {@link #setOutputCharStream} |
|
86 |
* or {@link #setOutputByteStream} for the writer and {@link #setOutputFormat} |
|
87 |
* for the output format. |
|
88 |
* <p> |
|
89 |
* The serializer can be reused any number of times, but cannot |
|
90 |
* be used concurrently by two threads. |
|
91 |
* <p> |
|
92 |
* If an output stream is used, the encoding is taken from the |
|
93 |
* output format (defaults to <tt>UTF-8</tt>). If a writer is |
|
94 |
* used, make sure the writer uses the same encoding (if applies) |
|
95 |
* as specified in the output format. |
|
96 |
* <p> |
|
97 |
* The serializer supports both DOM and SAX. DOM serializing is done |
|
98 |
* by calling {@link #serialize(Document)} and SAX serializing is done by firing |
|
99 |
* SAX events and using the serializer as a document handler. |
|
100 |
* This also applies to derived class. |
|
101 |
* <p> |
|
102 |
* If an I/O exception occurs while serializing, the serializer |
|
103 |
* will not throw an exception directly, but only throw it |
|
104 |
* at the end of serializing (either DOM or SAX's {@link |
|
105 |
* org.xml.sax.DocumentHandler#endDocument}. |
|
106 |
* <p> |
|
107 |
* For elements that are not specified as whitespace preserving, |
|
108 |
* the serializer will potentially break long text lines at space |
|
109 |
* boundaries, indent lines, and serialize elements on separate |
|
110 |
* lines. Line terminators will be regarded as spaces, and |
|
111 |
* spaces at beginning of line will be stripped. |
|
112 |
* <p> |
|
113 |
* When indenting, the serializer is capable of detecting seemingly |
|
114 |
* element content, and serializing these elements indented on separate |
|
115 |
* lines. An element is serialized indented when it is the first or |
|
116 |
* last child of an element, or immediate following or preceding |
|
117 |
* another element. |
|
118 |
* |
|
119 |
* |
|
120 |
* @author <a href="mailto:arkin@intalio.com">Assaf Arkin</a> |
|
121 |
* @author <a href="mailto:rahul.srivastava@sun.com">Rahul Srivastava</a> |
|
122 |
* @author Elena Litani, IBM |
|
123 |
* @author Sunitha Reddy, Sun Microsystems |
|
124 |
* @see Serializer |
|
23777
ce87cedb71cf
8035437: Xerces Update: xml/serialize/DOMSerializerImpl
aefimov
parents:
12457
diff
changeset
|
125 |
* @see org.w3c.dom.ls.LSSerializer |
6 | 126 |
*/ |
127 |
public abstract class BaseMarkupSerializer |
|
128 |
implements ContentHandler, DocumentHandler, LexicalHandler, |
|
129 |
DTDHandler, DeclHandler, DOMSerializer, Serializer |
|
130 |
{ |
|
131 |
||
132 |
// DOM L3 implementation |
|
133 |
protected short features = 0xFFFFFFFF; |
|
134 |
protected DOMErrorHandler fDOMErrorHandler; |
|
135 |
protected final DOMErrorImpl fDOMError = new DOMErrorImpl(); |
|
136 |
protected LSSerializerFilter fDOMFilter; |
|
137 |
||
138 |
protected EncodingInfo _encodingInfo; |
|
139 |
||
140 |
||
141 |
/** |
|
142 |
* Holds array of all element states that have been entered. |
|
143 |
* The array is automatically resized. When leaving an element, |
|
144 |
* it's state is not removed but reused when later returning |
|
145 |
* to the same nesting level. |
|
146 |
*/ |
|
147 |
private ElementState[] _elementStates; |
|
148 |
||
149 |
||
150 |
/** |
|
151 |
* The index of the next state to place in the array, |
|
152 |
* or one plus the index of the current state. When zero, |
|
153 |
* we are in no state. |
|
154 |
*/ |
|
155 |
private int _elementStateCount; |
|
156 |
||
157 |
||
158 |
/** |
|
159 |
* Vector holding comments and PIs that come before the root |
|
160 |
* element (even after it), see {@link #serializePreRoot}. |
|
161 |
*/ |
|
162 |
private Vector _preRoot; |
|
163 |
||
164 |
||
165 |
/** |
|
166 |
* If the document has been started (header serialized), this |
|
167 |
* flag is set to true so it's not started twice. |
|
168 |
*/ |
|
169 |
protected boolean _started; |
|
170 |
||
171 |
||
172 |
/** |
|
173 |
* True if the serializer has been prepared. This flag is set |
|
174 |
* to false when the serializer is reset prior to using it, |
|
175 |
* and to true after it has been prepared for usage. |
|
176 |
*/ |
|
177 |
private boolean _prepared; |
|
178 |
||
179 |
||
180 |
/** |
|
181 |
* Association between namespace URIs (keys) and prefixes (values). |
|
182 |
* Accumulated here prior to starting an element and placing this |
|
183 |
* list in the element state. |
|
184 |
*/ |
|
185 |
protected Hashtable _prefixes; |
|
186 |
||
187 |
||
188 |
/** |
|
189 |
* The system identifier of the document type, if known. |
|
190 |
*/ |
|
191 |
protected String _docTypePublicId; |
|
192 |
||
193 |
||
194 |
/** |
|
195 |
* The system identifier of the document type, if known. |
|
196 |
*/ |
|
197 |
protected String _docTypeSystemId; |
|
198 |
||
199 |
||
200 |
/** |
|
201 |
* The output format associated with this serializer. This will never |
|
202 |
* be a null reference. If no format was passed to the constructor, |
|
203 |
* the default one for this document type will be used. The format |
|
204 |
* object is never changed by the serializer. |
|
205 |
*/ |
|
206 |
protected OutputFormat _format; |
|
207 |
||
208 |
||
209 |
/** |
|
210 |
* The printer used for printing text parts. |
|
211 |
*/ |
|
212 |
protected Printer _printer; |
|
213 |
||
214 |
||
215 |
/** |
|
216 |
* True if indenting printer. |
|
217 |
*/ |
|
218 |
protected boolean _indenting; |
|
219 |
||
220 |
/** Temporary buffer to store character data */ |
|
221 |
protected final StringBuffer fStrBuffer = new StringBuffer(40); |
|
222 |
||
223 |
/** |
|
224 |
* The underlying writer. |
|
225 |
*/ |
|
226 |
private Writer _writer; |
|
227 |
||
228 |
||
229 |
/** |
|
230 |
* The output stream. |
|
231 |
*/ |
|
232 |
private OutputStream _output; |
|
233 |
||
234 |
/** Current node that is being processed */ |
|
235 |
protected Node fCurrentNode = null; |
|
236 |
||
237 |
||
238 |
||
239 |
//--------------------------------// |
|
240 |
// Constructor and initialization // |
|
241 |
//--------------------------------// |
|
242 |
||
243 |
||
244 |
/** |
|
245 |
* Protected constructor can only be used by derived class. |
|
246 |
* Must initialize the serializer before serializing any document, |
|
247 |
* by calling {@link #setOutputCharStream} or {@link #setOutputByteStream} |
|
248 |
* first |
|
249 |
*/ |
|
250 |
protected BaseMarkupSerializer( OutputFormat format ) |
|
251 |
{ |
|
252 |
int i; |
|
253 |
||
254 |
_elementStates = new ElementState[ 10 ]; |
|
255 |
for ( i = 0 ; i < _elementStates.length ; ++i ) |
|
256 |
_elementStates[ i ] = new ElementState(); |
|
257 |
_format = format; |
|
258 |
} |
|
259 |
||
260 |
||
261 |
public DocumentHandler asDocumentHandler() |
|
262 |
throws IOException |
|
263 |
{ |
|
264 |
prepare(); |
|
265 |
return this; |
|
266 |
} |
|
267 |
||
268 |
||
269 |
public ContentHandler asContentHandler() |
|
270 |
throws IOException |
|
271 |
{ |
|
272 |
prepare(); |
|
273 |
return this; |
|
274 |
} |
|
275 |
||
276 |
||
277 |
public DOMSerializer asDOMSerializer() |
|
278 |
throws IOException |
|
279 |
{ |
|
280 |
prepare(); |
|
281 |
return this; |
|
282 |
} |
|
283 |
||
284 |
||
285 |
public void setOutputByteStream( OutputStream output ) |
|
286 |
{ |
|
287 |
if ( output == null ) { |
|
288 |
String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.SERIALIZER_DOMAIN, |
|
289 |
"ArgumentIsNull", new Object[]{"output"}); |
|
290 |
throw new NullPointerException(msg); |
|
291 |
} |
|
292 |
_output = output; |
|
293 |
_writer = null; |
|
294 |
reset(); |
|
295 |
} |
|
296 |
||
297 |
||
298 |
public void setOutputCharStream( Writer writer ) |
|
299 |
{ |
|
300 |
if ( writer == null ) { |
|
301 |
String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.SERIALIZER_DOMAIN, |
|
302 |
"ArgumentIsNull", new Object[]{"writer"}); |
|
303 |
throw new NullPointerException(msg); |
|
304 |
} |
|
305 |
_writer = writer; |
|
306 |
_output = null; |
|
307 |
reset(); |
|
308 |
} |
|
309 |
||
310 |
||
311 |
public void setOutputFormat( OutputFormat format ) |
|
312 |
{ |
|
313 |
if ( format == null ) { |
|
314 |
String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.SERIALIZER_DOMAIN, |
|
315 |
"ArgumentIsNull", new Object[]{"format"}); |
|
316 |
throw new NullPointerException(msg); |
|
317 |
} |
|
318 |
_format = format; |
|
319 |
reset(); |
|
320 |
} |
|
321 |
||
322 |
||
323 |
public boolean reset() |
|
324 |
{ |
|
325 |
if ( _elementStateCount > 1 ) { |
|
326 |
String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.SERIALIZER_DOMAIN, |
|
327 |
"ResetInMiddle", null); |
|
328 |
throw new IllegalStateException(msg); |
|
329 |
} |
|
330 |
_prepared = false; |
|
331 |
fCurrentNode = null; |
|
332 |
fStrBuffer.setLength(0); |
|
333 |
return true; |
|
334 |
} |
|
335 |
||
23777
ce87cedb71cf
8035437: Xerces Update: xml/serialize/DOMSerializerImpl
aefimov
parents:
12457
diff
changeset
|
336 |
protected void cleanup() { |
ce87cedb71cf
8035437: Xerces Update: xml/serialize/DOMSerializerImpl
aefimov
parents:
12457
diff
changeset
|
337 |
fCurrentNode = null; |
ce87cedb71cf
8035437: Xerces Update: xml/serialize/DOMSerializerImpl
aefimov
parents:
12457
diff
changeset
|
338 |
} |
6 | 339 |
|
340 |
protected void prepare() |
|
341 |
throws IOException |
|
342 |
{ |
|
343 |
if ( _prepared ) |
|
344 |
return; |
|
345 |
||
346 |
if ( _writer == null && _output == null ) { |
|
347 |
String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.SERIALIZER_DOMAIN, |
|
348 |
"NoWriterSupplied", null); |
|
349 |
throw new IOException(msg); |
|
350 |
} |
|
351 |
// If the output stream has been set, use it to construct |
|
352 |
// the writer. It is possible that the serializer has been |
|
353 |
// reused with the same output stream and different encoding. |
|
354 |
||
355 |
_encodingInfo = _format.getEncodingInfo(); |
|
356 |
||
357 |
if ( _output != null ) { |
|
358 |
_writer = _encodingInfo.getWriter(_output); |
|
359 |
} |
|
360 |
||
361 |
if ( _format.getIndenting() ) { |
|
362 |
_indenting = true; |
|
363 |
_printer = new IndentPrinter( _writer, _format ); |
|
364 |
} else { |
|
365 |
_indenting = false; |
|
366 |
_printer = new Printer( _writer, _format ); |
|
367 |
} |
|
368 |
||
369 |
ElementState state; |
|
370 |
||
371 |
_elementStateCount = 0; |
|
372 |
state = _elementStates[ 0 ]; |
|
373 |
state.namespaceURI = null; |
|
374 |
state.localName = null; |
|
375 |
state.rawName = null; |
|
376 |
state.preserveSpace = _format.getPreserveSpace(); |
|
377 |
state.empty = true; |
|
378 |
state.afterElement = false; |
|
379 |
state.afterComment = false; |
|
380 |
state.doCData = state.inCData = false; |
|
381 |
state.prefixes = null; |
|
382 |
||
383 |
_docTypePublicId = _format.getDoctypePublic(); |
|
384 |
_docTypeSystemId = _format.getDoctypeSystem(); |
|
385 |
_started = false; |
|
386 |
_prepared = true; |
|
387 |
} |
|
388 |
||
389 |
||
390 |
||
391 |
//----------------------------------// |
|
392 |
// DOM document serializing methods // |
|
393 |
//----------------------------------// |
|
394 |
||
395 |
||
396 |
/** |
|
397 |
* Serializes the DOM element using the previously specified |
|
398 |
* writer and output format. Throws an exception only if |
|
399 |
* an I/O exception occured while serializing. |
|
400 |
* |
|
401 |
* @param elem The element to serialize |
|
402 |
* @throws IOException An I/O exception occured while |
|
403 |
* serializing |
|
404 |
*/ |
|
405 |
public void serialize( Element elem ) |
|
406 |
throws IOException |
|
407 |
{ |
|
408 |
reset(); |
|
409 |
prepare(); |
|
410 |
serializeNode( elem ); |
|
23777
ce87cedb71cf
8035437: Xerces Update: xml/serialize/DOMSerializerImpl
aefimov
parents:
12457
diff
changeset
|
411 |
cleanup(); |
6 | 412 |
_printer.flush(); |
413 |
if ( _printer.getException() != null ) |
|
414 |
throw _printer.getException(); |
|
415 |
} |
|
416 |
||
417 |
/** |
|
418 |
* Serializes a node using the previously specified |
|
419 |
* writer and output format. Throws an exception only if |
|
420 |
* an I/O exception occured while serializing. |
|
421 |
* |
|
422 |
* @param node Node to serialize |
|
423 |
* @throws IOException An I/O exception occured while serializing |
|
424 |
*/ |
|
425 |
public void serialize( Node node ) throws IOException { |
|
426 |
reset(); |
|
427 |
prepare(); |
|
428 |
serializeNode( node ); |
|
429 |
//Print any PIs and Comments which appeared in 'node' |
|
430 |
serializePreRoot(); |
|
431 |
_printer.flush(); |
|
432 |
if ( _printer.getException() != null ) |
|
433 |
throw _printer.getException(); |
|
434 |
} |
|
435 |
||
436 |
/** |
|
437 |
* Serializes the DOM document fragmnt using the previously specified |
|
438 |
* writer and output format. Throws an exception only if |
|
439 |
* an I/O exception occured while serializing. |
|
440 |
* |
|
23777
ce87cedb71cf
8035437: Xerces Update: xml/serialize/DOMSerializerImpl
aefimov
parents:
12457
diff
changeset
|
441 |
* @param frag The document fragment to serialize |
6 | 442 |
* @throws IOException An I/O exception occured while |
443 |
* serializing |
|
444 |
*/ |
|
445 |
public void serialize( DocumentFragment frag ) |
|
446 |
throws IOException |
|
447 |
{ |
|
448 |
reset(); |
|
449 |
prepare(); |
|
450 |
serializeNode( frag ); |
|
23777
ce87cedb71cf
8035437: Xerces Update: xml/serialize/DOMSerializerImpl
aefimov
parents:
12457
diff
changeset
|
451 |
cleanup(); |
6 | 452 |
_printer.flush(); |
453 |
if ( _printer.getException() != null ) |
|
454 |
throw _printer.getException(); |
|
455 |
} |
|
456 |
||
457 |
||
458 |
/** |
|
459 |
* Serializes the DOM document using the previously specified |
|
460 |
* writer and output format. Throws an exception only if |
|
461 |
* an I/O exception occured while serializing. |
|
462 |
* |
|
463 |
* @param doc The document to serialize |
|
464 |
* @throws IOException An I/O exception occured while |
|
465 |
* serializing |
|
466 |
*/ |
|
467 |
public void serialize( Document doc ) |
|
468 |
throws IOException |
|
469 |
{ |
|
470 |
reset(); |
|
471 |
prepare(); |
|
472 |
serializeNode( doc ); |
|
473 |
serializePreRoot(); |
|
23777
ce87cedb71cf
8035437: Xerces Update: xml/serialize/DOMSerializerImpl
aefimov
parents:
12457
diff
changeset
|
474 |
cleanup(); |
6 | 475 |
_printer.flush(); |
476 |
if ( _printer.getException() != null ) |
|
477 |
throw _printer.getException(); |
|
478 |
} |
|
479 |
||
480 |
||
481 |
//------------------------------------------// |
|
482 |
// SAX document handler serializing methods // |
|
483 |
//------------------------------------------// |
|
484 |
||
485 |
||
486 |
public void startDocument() |
|
487 |
throws SAXException |
|
488 |
{ |
|
489 |
try { |
|
490 |
prepare(); |
|
491 |
} catch ( IOException except ) { |
|
492 |
throw new SAXException( except.toString() ); |
|
493 |
} |
|
494 |
// Nothing to do here. All the magic happens in startDocument(String) |
|
495 |
} |
|
496 |
||
497 |
||
498 |
public void characters( char[] chars, int start, int length ) |
|
499 |
throws SAXException |
|
500 |
{ |
|
501 |
ElementState state; |
|
502 |
||
503 |
try { |
|
504 |
state = content(); |
|
505 |
||
506 |
// Check if text should be print as CDATA section or unescaped |
|
507 |
// based on elements listed in the output format (the element |
|
508 |
// state) or whether we are inside a CDATA section or entity. |
|
509 |
||
510 |
if ( state.inCData || state.doCData ) { |
|
511 |
int saveIndent; |
|
512 |
||
513 |
// Print a CDATA section. The text is not escaped, but ']]>' |
|
514 |
// appearing in the code must be identified and dealt with. |
|
515 |
// The contents of a text node is considered space preserving. |
|
516 |
if ( ! state.inCData ) { |
|
517 |
_printer.printText( "<![CDATA[" ); |
|
518 |
state.inCData = true; |
|
519 |
} |
|
520 |
saveIndent = _printer.getNextIndent(); |
|
521 |
_printer.setNextIndent( 0 ); |
|
522 |
char ch; |
|
523 |
final int end = start + length; |
|
524 |
for ( int index = start ; index < end; ++index ) { |
|
525 |
ch = chars[index]; |
|
526 |
if ( ch == ']' && index + 2 < end && |
|
527 |
chars[ index + 1 ] == ']' && chars[ index + 2 ] == '>' ) { |
|
528 |
_printer.printText("]]]]><![CDATA[>"); |
|
529 |
index +=2; |
|
530 |
continue; |
|
531 |
} |
|
532 |
if (!XMLChar.isValid(ch)) { |
|
533 |
// check if it is surrogate |
|
534 |
if (++index < end) { |
|
23777
ce87cedb71cf
8035437: Xerces Update: xml/serialize/DOMSerializerImpl
aefimov
parents:
12457
diff
changeset
|
535 |
surrogates(ch, chars[index],true); |
6 | 536 |
} |
537 |
else { |
|
23777
ce87cedb71cf
8035437: Xerces Update: xml/serialize/DOMSerializerImpl
aefimov
parents:
12457
diff
changeset
|
538 |
fatalError("The character '"+ch+"' is an invalid XML character"); |
6 | 539 |
} |
540 |
continue; |
|
23777
ce87cedb71cf
8035437: Xerces Update: xml/serialize/DOMSerializerImpl
aefimov
parents:
12457
diff
changeset
|
541 |
} |
ce87cedb71cf
8035437: Xerces Update: xml/serialize/DOMSerializerImpl
aefimov
parents:
12457
diff
changeset
|
542 |
if ( ( ch >= ' ' && _encodingInfo.isPrintable(ch) && ch != 0x7F ) || |
ce87cedb71cf
8035437: Xerces Update: xml/serialize/DOMSerializerImpl
aefimov
parents:
12457
diff
changeset
|
543 |
ch == '\n' || ch == '\r' || ch == '\t' ) { |
ce87cedb71cf
8035437: Xerces Update: xml/serialize/DOMSerializerImpl
aefimov
parents:
12457
diff
changeset
|
544 |
_printer.printText(ch); |
ce87cedb71cf
8035437: Xerces Update: xml/serialize/DOMSerializerImpl
aefimov
parents:
12457
diff
changeset
|
545 |
} |
ce87cedb71cf
8035437: Xerces Update: xml/serialize/DOMSerializerImpl
aefimov
parents:
12457
diff
changeset
|
546 |
else { |
ce87cedb71cf
8035437: Xerces Update: xml/serialize/DOMSerializerImpl
aefimov
parents:
12457
diff
changeset
|
547 |
// The character is not printable -- split CDATA section |
ce87cedb71cf
8035437: Xerces Update: xml/serialize/DOMSerializerImpl
aefimov
parents:
12457
diff
changeset
|
548 |
_printer.printText("]]>&#x"); |
ce87cedb71cf
8035437: Xerces Update: xml/serialize/DOMSerializerImpl
aefimov
parents:
12457
diff
changeset
|
549 |
_printer.printText(Integer.toHexString(ch)); |
ce87cedb71cf
8035437: Xerces Update: xml/serialize/DOMSerializerImpl
aefimov
parents:
12457
diff
changeset
|
550 |
_printer.printText(";<![CDATA["); |
6 | 551 |
} |
552 |
} |
|
553 |
_printer.setNextIndent( saveIndent ); |
|
554 |
||
555 |
} else { |
|
556 |
||
557 |
int saveIndent; |
|
558 |
||
559 |
if ( state.preserveSpace ) { |
|
560 |
// If preserving space then hold of indentation so no |
|
561 |
// excessive spaces are printed at line breaks, escape |
|
562 |
// the text content without replacing spaces and print |
|
563 |
// the text breaking only at line breaks. |
|
564 |
saveIndent = _printer.getNextIndent(); |
|
565 |
_printer.setNextIndent( 0 ); |
|
566 |
printText( chars, start, length, true, state.unescaped ); |
|
567 |
_printer.setNextIndent( saveIndent ); |
|
568 |
} else { |
|
569 |
printText( chars, start, length, false, state.unescaped ); |
|
570 |
} |
|
571 |
} |
|
572 |
} catch ( IOException except ) { |
|
573 |
throw new SAXException( except ); |
|
574 |
} |
|
575 |
} |
|
576 |
||
577 |
||
578 |
public void ignorableWhitespace( char[] chars, int start, int length ) |
|
579 |
throws SAXException |
|
580 |
{ |
|
581 |
int i; |
|
582 |
||
583 |
try { |
|
584 |
content(); |
|
585 |
||
586 |
// Print ignorable whitespaces only when indenting, after |
|
587 |
// all they are indentation. Cancel the indentation to |
|
588 |
// not indent twice. |
|
589 |
if ( _indenting ) { |
|
590 |
_printer.setThisIndent( 0 ); |
|
591 |
for ( i = start ; length-- > 0 ; ++i ) |
|
592 |
_printer.printText( chars[ i ] ); |
|
593 |
} |
|
594 |
} catch ( IOException except ) { |
|
595 |
throw new SAXException( except ); |
|
596 |
} |
|
597 |
} |
|
598 |
||
599 |
||
600 |
public final void processingInstruction( String target, String code ) |
|
601 |
throws SAXException |
|
602 |
{ |
|
603 |
try { |
|
604 |
processingInstructionIO( target, code ); |
|
605 |
} catch ( IOException except ) { |
|
606 |
throw new SAXException( except ); |
|
607 |
} |
|
608 |
} |
|
609 |
||
610 |
public void processingInstructionIO( String target, String code ) |
|
611 |
throws IOException |
|
612 |
{ |
|
613 |
int index; |
|
614 |
ElementState state; |
|
615 |
||
616 |
state = content(); |
|
617 |
||
618 |
// Create the processing instruction textual representation. |
|
619 |
// Make sure we don't have '?>' inside either target or code. |
|
620 |
index = target.indexOf( "?>" ); |
|
621 |
if ( index >= 0 ) |
|
622 |
fStrBuffer.append( "<?" ).append( target.substring( 0, index ) ); |
|
623 |
else |
|
624 |
fStrBuffer.append( "<?" ).append( target ); |
|
625 |
if ( code != null ) { |
|
626 |
fStrBuffer.append( ' ' ); |
|
627 |
index = code.indexOf( "?>" ); |
|
628 |
if ( index >= 0 ) |
|
629 |
fStrBuffer.append( code.substring( 0, index ) ); |
|
630 |
else |
|
631 |
fStrBuffer.append( code ); |
|
632 |
} |
|
633 |
fStrBuffer.append( "?>" ); |
|
634 |
||
635 |
// If before the root element (or after it), do not print |
|
636 |
// the PI directly but place it in the pre-root vector. |
|
637 |
if ( isDocumentState() ) { |
|
638 |
if ( _preRoot == null ) |
|
639 |
_preRoot = new Vector(); |
|
640 |
_preRoot.addElement( fStrBuffer.toString() ); |
|
641 |
} else { |
|
642 |
_printer.indent(); |
|
643 |
printText( fStrBuffer.toString(), true, true ); |
|
644 |
_printer.unindent(); |
|
645 |
if ( _indenting ) |
|
646 |
state.afterElement = true; |
|
647 |
} |
|
648 |
||
649 |
fStrBuffer.setLength(0); |
|
650 |
} |
|
651 |
||
652 |
||
653 |
public void comment( char[] chars, int start, int length ) |
|
654 |
throws SAXException |
|
655 |
{ |
|
656 |
try { |
|
657 |
comment( new String( chars, start, length ) ); |
|
658 |
} catch ( IOException except ) { |
|
659 |
throw new SAXException( except ); |
|
660 |
} |
|
661 |
} |
|
662 |
||
663 |
||
664 |
public void comment( String text ) |
|
665 |
throws IOException |
|
666 |
{ |
|
667 |
int index; |
|
668 |
ElementState state; |
|
669 |
||
670 |
if ( _format.getOmitComments() ) |
|
671 |
return; |
|
672 |
||
673 |
state = content(); |
|
674 |
// Create the processing comment textual representation. |
|
675 |
// Make sure we don't have '-->' inside the comment. |
|
676 |
index = text.indexOf( "-->" ); |
|
677 |
if ( index >= 0 ) |
|
678 |
fStrBuffer.append( "<!--" ).append( text.substring( 0, index ) ).append( "-->" ); |
|
679 |
else |
|
680 |
fStrBuffer.append( "<!--" ).append( text ).append( "-->" ); |
|
681 |
||
682 |
// If before the root element (or after it), do not print |
|
683 |
// the comment directly but place it in the pre-root vector. |
|
684 |
if ( isDocumentState() ) { |
|
685 |
if ( _preRoot == null ) |
|
686 |
_preRoot = new Vector(); |
|
687 |
_preRoot.addElement( fStrBuffer.toString() ); |
|
688 |
} else { |
|
689 |
// Indent this element on a new line if the first |
|
690 |
// content of the parent element or immediately |
|
691 |
// following an element. |
|
692 |
if ( _indenting && ! state.preserveSpace) |
|
693 |
_printer.breakLine(); |
|
694 |
_printer.indent(); |
|
695 |
printText( fStrBuffer.toString(), true, true ); |
|
696 |
_printer.unindent(); |
|
697 |
if ( _indenting ) |
|
698 |
state.afterElement = true; |
|
699 |
} |
|
700 |
||
701 |
fStrBuffer.setLength(0); |
|
702 |
state.afterComment = true; |
|
703 |
state.afterElement = false; |
|
704 |
} |
|
705 |
||
706 |
||
707 |
public void startCDATA() |
|
708 |
{ |
|
709 |
ElementState state; |
|
710 |
||
711 |
state = getElementState(); |
|
712 |
state.doCData = true; |
|
713 |
} |
|
714 |
||
715 |
||
716 |
public void endCDATA() |
|
717 |
{ |
|
718 |
ElementState state; |
|
719 |
||
720 |
state = getElementState(); |
|
721 |
state.doCData = false; |
|
722 |
} |
|
723 |
||
724 |
||
725 |
public void startNonEscaping() |
|
726 |
{ |
|
727 |
ElementState state; |
|
728 |
||
729 |
state = getElementState(); |
|
730 |
state.unescaped = true; |
|
731 |
} |
|
732 |
||
733 |
||
734 |
public void endNonEscaping() |
|
735 |
{ |
|
736 |
ElementState state; |
|
737 |
||
738 |
state = getElementState(); |
|
739 |
state.unescaped = false; |
|
740 |
} |
|
741 |
||
742 |
||
743 |
public void startPreserving() |
|
744 |
{ |
|
745 |
ElementState state; |
|
746 |
||
747 |
state = getElementState(); |
|
748 |
state.preserveSpace = true; |
|
749 |
} |
|
750 |
||
751 |
||
752 |
public void endPreserving() |
|
753 |
{ |
|
754 |
ElementState state; |
|
755 |
||
756 |
state = getElementState(); |
|
757 |
state.preserveSpace = false; |
|
758 |
} |
|
759 |
||
760 |
||
761 |
/** |
|
762 |
* Called at the end of the document to wrap it up. |
|
763 |
* Will flush the output stream and throw an exception |
|
764 |
* if any I/O error occured while serializing. |
|
765 |
* |
|
766 |
* @throws SAXException An I/O exception occured during |
|
767 |
* serializing |
|
768 |
*/ |
|
769 |
public void endDocument() |
|
770 |
throws SAXException |
|
771 |
{ |
|
772 |
try { |
|
773 |
// Print all the elements accumulated outside of |
|
774 |
// the root element. |
|
775 |
serializePreRoot(); |
|
776 |
// Flush the output, this is necessary for fStrBuffered output. |
|
777 |
_printer.flush(); |
|
778 |
} catch ( IOException except ) { |
|
779 |
throw new SAXException( except ); |
|
780 |
} |
|
781 |
} |
|
782 |
||
783 |
||
784 |
public void startEntity( String name ) |
|
785 |
{ |
|
786 |
// ??? |
|
787 |
} |
|
788 |
||
789 |
||
790 |
public void endEntity( String name ) |
|
791 |
{ |
|
792 |
// ??? |
|
793 |
} |
|
794 |
||
795 |
||
796 |
public void setDocumentLocator( Locator locator ) |
|
797 |
{ |
|
798 |
// Nothing to do |
|
799 |
} |
|
800 |
||
801 |
||
802 |
//-----------------------------------------// |
|
803 |
// SAX content handler serializing methods // |
|
804 |
//-----------------------------------------// |
|
805 |
||
806 |
||
807 |
public void skippedEntity ( String name ) |
|
808 |
throws SAXException |
|
809 |
{ |
|
810 |
try { |
|
811 |
endCDATA(); |
|
812 |
content(); |
|
813 |
_printer.printText( '&' ); |
|
814 |
_printer.printText( name ); |
|
815 |
_printer.printText( ';' ); |
|
816 |
} catch ( IOException except ) { |
|
817 |
throw new SAXException( except ); |
|
818 |
} |
|
819 |
} |
|
820 |
||
821 |
||
822 |
public void startPrefixMapping( String prefix, String uri ) |
|
823 |
throws SAXException |
|
824 |
{ |
|
825 |
if ( _prefixes == null ) |
|
826 |
_prefixes = new Hashtable(); |
|
827 |
_prefixes.put( uri, prefix == null ? "" : prefix ); |
|
828 |
} |
|
829 |
||
830 |
||
831 |
public void endPrefixMapping( String prefix ) |
|
832 |
throws SAXException |
|
833 |
{ |
|
834 |
} |
|
835 |
||
836 |
||
837 |
//------------------------------------------// |
|
838 |
// SAX DTD/Decl handler serializing methods // |
|
839 |
//------------------------------------------// |
|
840 |
||
841 |
||
842 |
public final void startDTD( String name, String publicId, String systemId ) |
|
843 |
throws SAXException |
|
844 |
{ |
|
845 |
try { |
|
846 |
_printer.enterDTD(); |
|
847 |
_docTypePublicId = publicId; |
|
848 |
_docTypeSystemId = systemId; |
|
849 |
||
850 |
} catch ( IOException except ) { |
|
851 |
throw new SAXException( except ); |
|
852 |
} |
|
853 |
} |
|
854 |
||
855 |
||
856 |
public void endDTD() |
|
857 |
{ |
|
858 |
// Nothing to do here, all the magic occurs in startDocument(String). |
|
859 |
} |
|
860 |
||
861 |
||
862 |
public void elementDecl( String name, String model ) |
|
863 |
throws SAXException |
|
864 |
{ |
|
865 |
try { |
|
866 |
_printer.enterDTD(); |
|
867 |
_printer.printText( "<!ELEMENT " ); |
|
868 |
_printer.printText( name ); |
|
869 |
_printer.printText( ' ' ); |
|
870 |
_printer.printText( model ); |
|
871 |
_printer.printText( '>' ); |
|
872 |
if ( _indenting ) |
|
873 |
_printer.breakLine(); |
|
874 |
} catch ( IOException except ) { |
|
875 |
throw new SAXException( except ); |
|
876 |
} |
|
877 |
} |
|
878 |
||
879 |
||
880 |
public void attributeDecl( String eName, String aName, String type, |
|
881 |
String valueDefault, String value ) |
|
882 |
throws SAXException |
|
883 |
{ |
|
884 |
try { |
|
885 |
_printer.enterDTD(); |
|
886 |
_printer.printText( "<!ATTLIST " ); |
|
887 |
_printer.printText( eName ); |
|
888 |
_printer.printText( ' ' ); |
|
889 |
_printer.printText( aName ); |
|
890 |
_printer.printText( ' ' ); |
|
891 |
_printer.printText( type ); |
|
892 |
if ( valueDefault != null ) { |
|
893 |
_printer.printText( ' ' ); |
|
894 |
_printer.printText( valueDefault ); |
|
895 |
} |
|
896 |
if ( value != null ) { |
|
897 |
_printer.printText( " \"" ); |
|
898 |
printEscaped( value ); |
|
899 |
_printer.printText( '"' ); |
|
900 |
} |
|
901 |
_printer.printText( '>' ); |
|
902 |
if ( _indenting ) |
|
903 |
_printer.breakLine(); |
|
904 |
} catch ( IOException except ) { |
|
905 |
throw new SAXException( except ); |
|
906 |
} |
|
907 |
} |
|
908 |
||
909 |
||
910 |
public void internalEntityDecl( String name, String value ) |
|
911 |
throws SAXException |
|
912 |
{ |
|
913 |
try { |
|
914 |
_printer.enterDTD(); |
|
915 |
_printer.printText( "<!ENTITY " ); |
|
916 |
_printer.printText( name ); |
|
917 |
_printer.printText( " \"" ); |
|
918 |
printEscaped( value ); |
|
919 |
_printer.printText( "\">" ); |
|
920 |
if ( _indenting ) |
|
921 |
_printer.breakLine(); |
|
922 |
} catch ( IOException except ) { |
|
923 |
throw new SAXException( except ); |
|
924 |
} |
|
925 |
} |
|
926 |
||
927 |
||
928 |
public void externalEntityDecl( String name, String publicId, String systemId ) |
|
929 |
throws SAXException |
|
930 |
{ |
|
931 |
try { |
|
932 |
_printer.enterDTD(); |
|
933 |
unparsedEntityDecl( name, publicId, systemId, null ); |
|
934 |
} catch ( IOException except ) { |
|
935 |
throw new SAXException( except ); |
|
936 |
} |
|
937 |
} |
|
938 |
||
939 |
||
940 |
public void unparsedEntityDecl( String name, String publicId, |
|
941 |
String systemId, String notationName ) |
|
942 |
throws SAXException |
|
943 |
{ |
|
944 |
try { |
|
945 |
_printer.enterDTD(); |
|
946 |
if ( publicId == null ) { |
|
947 |
_printer.printText( "<!ENTITY " ); |
|
948 |
_printer.printText( name ); |
|
949 |
_printer.printText( " SYSTEM " ); |
|
950 |
printDoctypeURL( systemId ); |
|
951 |
} else { |
|
952 |
_printer.printText( "<!ENTITY " ); |
|
953 |
_printer.printText( name ); |
|
954 |
_printer.printText( " PUBLIC " ); |
|
955 |
printDoctypeURL( publicId ); |
|
956 |
_printer.printText( ' ' ); |
|
957 |
printDoctypeURL( systemId ); |
|
958 |
} |
|
959 |
if ( notationName != null ) { |
|
960 |
_printer.printText( " NDATA " ); |
|
961 |
_printer.printText( notationName ); |
|
962 |
} |
|
963 |
_printer.printText( '>' ); |
|
964 |
if ( _indenting ) |
|
965 |
_printer.breakLine(); |
|
966 |
} catch ( IOException except ) { |
|
967 |
throw new SAXException( except ); |
|
968 |
} |
|
969 |
} |
|
970 |
||
971 |
||
972 |
public void notationDecl( String name, String publicId, String systemId ) |
|
973 |
throws SAXException |
|
974 |
{ |
|
975 |
try { |
|
976 |
_printer.enterDTD(); |
|
977 |
if ( publicId != null ) { |
|
978 |
_printer.printText( "<!NOTATION " ); |
|
979 |
_printer.printText( name ); |
|
980 |
_printer.printText( " PUBLIC " ); |
|
981 |
printDoctypeURL( publicId ); |
|
982 |
if ( systemId != null ) { |
|
983 |
_printer.printText( ' ' ); |
|
984 |
printDoctypeURL( systemId ); |
|
985 |
} |
|
986 |
} else { |
|
987 |
_printer.printText( "<!NOTATION " ); |
|
988 |
_printer.printText( name ); |
|
989 |
_printer.printText( " SYSTEM " ); |
|
990 |
printDoctypeURL( systemId ); |
|
991 |
} |
|
992 |
_printer.printText( '>' ); |
|
993 |
if ( _indenting ) |
|
994 |
_printer.breakLine(); |
|
995 |
} catch ( IOException except ) { |
|
996 |
throw new SAXException( except ); |
|
997 |
} |
|
998 |
} |
|
999 |
||
1000 |
||
1001 |
//------------------------------------------// |
|
1002 |
// Generic node serializing methods methods // |
|
1003 |
//------------------------------------------// |
|
1004 |
||
1005 |
||
1006 |
/** |
|
1007 |
* Serialize the DOM node. This method is shared across XML, HTML and XHTML |
|
1008 |
* serializers and the differences are masked out in a separate {@link |
|
1009 |
* #serializeElement}. |
|
1010 |
* |
|
1011 |
* @param node The node to serialize |
|
1012 |
* @see #serializeElement |
|
1013 |
* @throws IOException An I/O exception occured while |
|
1014 |
* serializing |
|
1015 |
*/ |
|
1016 |
protected void serializeNode( Node node ) |
|
1017 |
throws IOException |
|
1018 |
{ |
|
1019 |
fCurrentNode = node; |
|
1020 |
||
1021 |
// Based on the node type call the suitable SAX handler. |
|
1022 |
// Only comments entities and documents which are not |
|
1023 |
// handled by SAX are serialized directly. |
|
1024 |
switch ( node.getNodeType() ) { |
|
1025 |
case Node.TEXT_NODE : { |
|
1026 |
String text; |
|
1027 |
||
1028 |
text = node.getNodeValue(); |
|
1029 |
if ( text != null ) { |
|
1030 |
if (fDOMFilter !=null && |
|
1031 |
(fDOMFilter.getWhatToShow() & NodeFilter.SHOW_TEXT)!= 0) { |
|
1032 |
short code = fDOMFilter.acceptNode(node); |
|
1033 |
switch (code) { |
|
1034 |
case NodeFilter.FILTER_REJECT: |
|
1035 |
case NodeFilter.FILTER_SKIP: { |
|
1036 |
break; |
|
1037 |
} |
|
1038 |
default: { |
|
1039 |
characters(text); |
|
1040 |
} |
|
1041 |
} |
|
1042 |
} |
|
1043 |
else if ( !_indenting || getElementState().preserveSpace |
|
1044 |
|| (text.replace('\n',' ').trim().length() != 0)) |
|
1045 |
characters( text ); |
|
1046 |
||
1047 |
} |
|
1048 |
break; |
|
1049 |
} |
|
1050 |
||
1051 |
case Node.CDATA_SECTION_NODE : { |
|
1052 |
String text = node.getNodeValue(); |
|
1053 |
if ((features & DOMSerializerImpl.CDATA) != 0) { |
|
1054 |
if (text != null) { |
|
1055 |
if (fDOMFilter != null |
|
1056 |
&& (fDOMFilter.getWhatToShow() |
|
1057 |
& NodeFilter.SHOW_CDATA_SECTION) |
|
1058 |
!= 0) { |
|
1059 |
short code = fDOMFilter.acceptNode(node); |
|
1060 |
switch (code) { |
|
1061 |
case NodeFilter.FILTER_REJECT : |
|
1062 |
case NodeFilter.FILTER_SKIP : |
|
1063 |
{ |
|
1064 |
// skip the CDATA node |
|
1065 |
return; |
|
1066 |
} |
|
1067 |
default : |
|
1068 |
{ |
|
1069 |
//fall through.. |
|
1070 |
} |
|
1071 |
} |
|
1072 |
} |
|
1073 |
startCDATA(); |
|
1074 |
characters(text); |
|
1075 |
endCDATA(); |
|
1076 |
} |
|
1077 |
} else { |
|
1078 |
// transform into a text node |
|
1079 |
characters(text); |
|
1080 |
} |
|
1081 |
break; |
|
1082 |
} |
|
1083 |
case Node.COMMENT_NODE : { |
|
1084 |
String text; |
|
1085 |
||
1086 |
if ( ! _format.getOmitComments() ) { |
|
1087 |
text = node.getNodeValue(); |
|
1088 |
if ( text != null ) { |
|
1089 |
||
1090 |
if (fDOMFilter !=null && |
|
1091 |
(fDOMFilter.getWhatToShow() & NodeFilter.SHOW_COMMENT)!= 0) { |
|
1092 |
short code = fDOMFilter.acceptNode(node); |
|
1093 |
switch (code) { |
|
1094 |
case NodeFilter.FILTER_REJECT: |
|
1095 |
case NodeFilter.FILTER_SKIP: { |
|
1096 |
// skip the comment node |
|
1097 |
return; |
|
1098 |
} |
|
1099 |
default: { |
|
1100 |
// fall through |
|
1101 |
} |
|
1102 |
} |
|
1103 |
} |
|
1104 |
comment( text ); |
|
1105 |
} |
|
1106 |
} |
|
1107 |
break; |
|
1108 |
} |
|
1109 |
||
1110 |
case Node.ENTITY_REFERENCE_NODE : { |
|
1111 |
Node child; |
|
1112 |
||
1113 |
endCDATA(); |
|
1114 |
content(); |
|
1115 |
||
1116 |
if (((features & DOMSerializerImpl.ENTITIES) != 0) |
|
1117 |
|| (node.getFirstChild() == null)) { |
|
1118 |
if (fDOMFilter !=null && |
|
1119 |
(fDOMFilter.getWhatToShow() & NodeFilter.SHOW_ENTITY_REFERENCE)!= 0) { |
|
1120 |
short code = fDOMFilter.acceptNode(node); |
|
1121 |
switch (code) { |
|
1122 |
case NodeFilter.FILTER_REJECT:{ |
|
1123 |
return; // remove the node |
|
1124 |
} |
|
1125 |
case NodeFilter.FILTER_SKIP: { |
|
1126 |
child = node.getFirstChild(); |
|
1127 |
while ( child != null ) { |
|
1128 |
serializeNode( child ); |
|
1129 |
child = child.getNextSibling(); |
|
1130 |
} |
|
1131 |
return; |
|
1132 |
} |
|
1133 |
||
1134 |
default: { |
|
1135 |
// fall through |
|
1136 |
} |
|
1137 |
} |
|
1138 |
} |
|
1139 |
checkUnboundNamespacePrefixedNode(node); |
|
1140 |
||
1141 |
_printer.printText("&"); |
|
1142 |
_printer.printText(node.getNodeName()); |
|
1143 |
_printer.printText(";"); |
|
1144 |
} |
|
1145 |
else { |
|
1146 |
child = node.getFirstChild(); |
|
1147 |
while ( child != null ) { |
|
1148 |
serializeNode( child ); |
|
1149 |
child = child.getNextSibling(); |
|
1150 |
} |
|
1151 |
} |
|
1152 |
||
1153 |
break; |
|
1154 |
} |
|
1155 |
||
1156 |
case Node.PROCESSING_INSTRUCTION_NODE : { |
|
1157 |
||
1158 |
if (fDOMFilter !=null && |
|
1159 |
(fDOMFilter.getWhatToShow() & NodeFilter.SHOW_PROCESSING_INSTRUCTION)!= 0) { |
|
1160 |
short code = fDOMFilter.acceptNode(node); |
|
1161 |
switch (code) { |
|
1162 |
case NodeFilter.FILTER_REJECT: |
|
1163 |
case NodeFilter.FILTER_SKIP: { |
|
1164 |
return; // skip this node |
|
1165 |
} |
|
1166 |
default: { // fall through |
|
1167 |
} |
|
1168 |
} |
|
1169 |
} |
|
1170 |
processingInstructionIO( node.getNodeName(), node.getNodeValue() ); |
|
1171 |
break; |
|
1172 |
} |
|
1173 |
case Node.ELEMENT_NODE : { |
|
1174 |
||
1175 |
if (fDOMFilter !=null && |
|
1176 |
(fDOMFilter.getWhatToShow() & NodeFilter.SHOW_ELEMENT)!= 0) { |
|
1177 |
short code = fDOMFilter.acceptNode(node); |
|
1178 |
switch (code) { |
|
1179 |
case NodeFilter.FILTER_REJECT: { |
|
1180 |
return; |
|
1181 |
} |
|
1182 |
case NodeFilter.FILTER_SKIP: { |
|
1183 |
Node child = node.getFirstChild(); |
|
1184 |
while ( child != null ) { |
|
1185 |
serializeNode( child ); |
|
1186 |
child = child.getNextSibling(); |
|
1187 |
} |
|
1188 |
return; // skip this node |
|
1189 |
} |
|
1190 |
||
1191 |
default: { // fall through |
|
1192 |
} |
|
1193 |
} |
|
1194 |
} |
|
1195 |
serializeElement( (Element) node ); |
|
1196 |
break; |
|
1197 |
} |
|
1198 |
case Node.DOCUMENT_NODE : { |
|
1199 |
DocumentType docType; |
|
1200 |
||
1201 |
serializeDocument(); |
|
1202 |
||
1203 |
// If there is a document type, use the SAX events to |
|
1204 |
// serialize it. |
|
1205 |
docType = ( (Document) node ).getDoctype(); |
|
1206 |
if (docType != null) { |
|
1207 |
// DOM Level 2 (or higher) |
|
1208 |
try { |
|
1209 |
String internal; |
|
1210 |
||
1211 |
_printer.enterDTD(); |
|
1212 |
_docTypePublicId = docType.getPublicId(); |
|
1213 |
_docTypeSystemId = docType.getSystemId(); |
|
1214 |
internal = docType.getInternalSubset(); |
|
1215 |
if ( internal != null && internal.length() > 0 ) |
|
1216 |
_printer.printText( internal ); |
|
1217 |
endDTD(); |
|
1218 |
} |
|
1219 |
// DOM Level 1 -- does implementation have methods? |
|
1220 |
catch (NoSuchMethodError nsme) { |
|
1221 |
Class docTypeClass = docType.getClass(); |
|
1222 |
||
1223 |
String docTypePublicId = null; |
|
1224 |
String docTypeSystemId = null; |
|
1225 |
try { |
|
1226 |
java.lang.reflect.Method getPublicId = docTypeClass.getMethod("getPublicId", (Class[]) null); |
|
1227 |
if (getPublicId.getReturnType().equals(String.class)) { |
|
1228 |
docTypePublicId = (String)getPublicId.invoke(docType, (Object[]) null); |
|
1229 |
} |
|
1230 |
} |
|
1231 |
catch (Exception e) { |
|
1232 |
// ignore |
|
1233 |
} |
|
1234 |
try { |
|
1235 |
java.lang.reflect.Method getSystemId = docTypeClass.getMethod("getSystemId", (Class[]) null); |
|
1236 |
if (getSystemId.getReturnType().equals(String.class)) { |
|
1237 |
docTypeSystemId = (String)getSystemId.invoke(docType, (Object[]) null); |
|
1238 |
} |
|
1239 |
} |
|
1240 |
catch (Exception e) { |
|
1241 |
// ignore |
|
1242 |
} |
|
1243 |
_printer.enterDTD(); |
|
1244 |
_docTypePublicId = docTypePublicId; |
|
1245 |
_docTypeSystemId = docTypeSystemId; |
|
1246 |
endDTD(); |
|
1247 |
} |
|
1248 |
||
1249 |
serializeDTD(docType.getName()); |
|
1250 |
||
1251 |
} |
|
1252 |
_started = true; |
|
1253 |
||
1254 |
// !! Fall through |
|
1255 |
} |
|
1256 |
case Node.DOCUMENT_FRAGMENT_NODE : { |
|
1257 |
Node child; |
|
1258 |
||
1259 |
// By definition this will happen if the node is a document, |
|
1260 |
// document fragment, etc. Just serialize its contents. It will |
|
1261 |
// work well for other nodes that we do not know how to serialize. |
|
1262 |
child = node.getFirstChild(); |
|
1263 |
while ( child != null ) { |
|
1264 |
serializeNode( child ); |
|
1265 |
child = child.getNextSibling(); |
|
1266 |
} |
|
1267 |
break; |
|
1268 |
} |
|
1269 |
||
1270 |
default: |
|
1271 |
break; |
|
1272 |
} |
|
1273 |
} |
|
1274 |
||
1275 |
||
1276 |
/* Serializes XML Declaration, according to 'xml-declaration' property. |
|
1277 |
*/ |
|
1278 |
protected void serializeDocument()throws IOException { |
|
1279 |
int i; |
|
1280 |
||
1281 |
String dtd = _printer.leaveDTD(); |
|
1282 |
if (! _started) { |
|
1283 |
||
1284 |
if (! _format.getOmitXMLDeclaration()) { |
|
1285 |
StringBuffer buffer; |
|
1286 |
||
1287 |
// Serialize the document declaration appreaing at the head |
|
1288 |
// of very XML document (unless asked not to). |
|
1289 |
buffer = new StringBuffer( "<?xml version=\"" ); |
|
1290 |
if (_format.getVersion() != null) |
|
1291 |
buffer.append( _format.getVersion() ); |
|
1292 |
else |
|
1293 |
buffer.append( "1.0" ); |
|
1294 |
buffer.append( '"' ); |
|
1295 |
String format_encoding = _format.getEncoding(); |
|
1296 |
if (format_encoding != null) { |
|
1297 |
buffer.append( " encoding=\"" ); |
|
1298 |
buffer.append( format_encoding ); |
|
1299 |
buffer.append( '"' ); |
|
1300 |
} |
|
1301 |
if (_format.getStandalone() && _docTypeSystemId == null && |
|
1302 |
_docTypePublicId == null) |
|
1303 |
buffer.append( " standalone=\"yes\"" ); |
|
1304 |
buffer.append( "?>" ); |
|
1305 |
_printer.printText( buffer ); |
|
1306 |
_printer.breakLine(); |
|
1307 |
} |
|
1308 |
} |
|
1309 |
||
1310 |
// Always serialize these, even if not te first root element. |
|
1311 |
serializePreRoot(); |
|
1312 |
||
1313 |
} |
|
1314 |
||
1315 |
/* Serializes DTD, if present. |
|
1316 |
*/ |
|
1317 |
protected void serializeDTD(String name) throws IOException{ |
|
1318 |
||
1319 |
String dtd = _printer.leaveDTD(); |
|
1320 |
if (! _format.getOmitDocumentType()) { |
|
1321 |
if (_docTypeSystemId != null) { |
|
1322 |
// System identifier must be specified to print DOCTYPE. |
|
1323 |
// If public identifier is specified print 'PUBLIC |
|
1324 |
// <public> <system>', if not, print 'SYSTEM <system>'. |
|
1325 |
_printer.printText( "<!DOCTYPE " ); |
|
1326 |
_printer.printText( name ); |
|
1327 |
if (_docTypePublicId != null) { |
|
1328 |
_printer.printText( " PUBLIC " ); |
|
1329 |
printDoctypeURL( _docTypePublicId ); |
|
1330 |
if (_indenting) { |
|
1331 |
_printer.breakLine(); |
|
1332 |
for (int i = 0 ; i < 18 + name.length() ; ++i) |
|
1333 |
_printer.printText( " " ); |
|
1334 |
} else |
|
1335 |
_printer.printText( " " ); |
|
1336 |
printDoctypeURL( _docTypeSystemId ); |
|
1337 |
} else { |
|
1338 |
_printer.printText( " SYSTEM " ); |
|
1339 |
printDoctypeURL( _docTypeSystemId ); |
|
1340 |
} |
|
1341 |
||
1342 |
// If we accumulated any DTD contents while printing. |
|
1343 |
// this would be the place to print it. |
|
1344 |
if (dtd != null && dtd.length() > 0) { |
|
1345 |
_printer.printText( " [" ); |
|
1346 |
printText( dtd, true, true ); |
|
1347 |
_printer.printText( ']' ); |
|
1348 |
} |
|
1349 |
||
1350 |
_printer.printText( ">" ); |
|
1351 |
_printer.breakLine(); |
|
1352 |
} else if (dtd != null && dtd.length() > 0) { |
|
1353 |
_printer.printText( "<!DOCTYPE " ); |
|
1354 |
_printer.printText( name ); |
|
1355 |
_printer.printText( " [" ); |
|
1356 |
printText( dtd, true, true ); |
|
1357 |
_printer.printText( "]>" ); |
|
1358 |
_printer.breakLine(); |
|
1359 |
} |
|
1360 |
} |
|
1361 |
} |
|
1362 |
||
1363 |
||
1364 |
/** |
|
1365 |
* Must be called by a method about to print any type of content. |
|
1366 |
* If the element was just opened, the opening tag is closed and |
|
1367 |
* will be matched to a closing tag. Returns the current element |
|
1368 |
* state with <tt>empty</tt> and <tt>afterElement</tt> set to false. |
|
1369 |
* |
|
1370 |
* @return The current element state |
|
23777
ce87cedb71cf
8035437: Xerces Update: xml/serialize/DOMSerializerImpl
aefimov
parents:
12457
diff
changeset
|
1371 |
* @throws IOException An I/O exception occurred while |
6 | 1372 |
* serializing |
1373 |
*/ |
|
1374 |
protected ElementState content() |
|
1375 |
throws IOException |
|
1376 |
{ |
|
1377 |
ElementState state; |
|
1378 |
||
1379 |
state = getElementState(); |
|
1380 |
if ( ! isDocumentState() ) { |
|
1381 |
// Need to close CData section first |
|
1382 |
if ( state.inCData && ! state.doCData ) { |
|
1383 |
_printer.printText( "]]>" ); |
|
1384 |
state.inCData = false; |
|
1385 |
} |
|
1386 |
// If this is the first content in the element, |
|
1387 |
// change the state to not-empty and close the |
|
1388 |
// opening element tag. |
|
1389 |
if ( state.empty ) { |
|
1390 |
_printer.printText( '>' ); |
|
1391 |
state.empty = false; |
|
1392 |
} |
|
1393 |
// Except for one content type, all of them |
|
1394 |
// are not last element. That one content |
|
1395 |
// type will take care of itself. |
|
1396 |
state.afterElement = false; |
|
1397 |
// Except for one content type, all of them |
|
1398 |
// are not last comment. That one content |
|
1399 |
// type will take care of itself. |
|
1400 |
state.afterComment = false; |
|
1401 |
} |
|
1402 |
return state; |
|
1403 |
} |
|
1404 |
||
1405 |
||
1406 |
/** |
|
1407 |
* Called to print the text contents in the prevailing element format. |
|
1408 |
* Since this method is capable of printing text as CDATA, it is used |
|
1409 |
* for that purpose as well. White space handling is determined by the |
|
1410 |
* current element state. In addition, the output format can dictate |
|
1411 |
* whether the text is printed as CDATA or unescaped. |
|
1412 |
* |
|
1413 |
* @param text The text to print |
|
1414 |
* @throws IOException An I/O exception occured while |
|
1415 |
* serializing |
|
1416 |
*/ |
|
1417 |
protected void characters( String text ) |
|
1418 |
throws IOException |
|
1419 |
{ |
|
1420 |
ElementState state; |
|
1421 |
||
1422 |
state = content(); |
|
1423 |
// Check if text should be print as CDATA section or unescaped |
|
1424 |
// based on elements listed in the output format (the element |
|
1425 |
// state) or whether we are inside a CDATA section or entity. |
|
1426 |
||
1427 |
if ( state.inCData || state.doCData ) { |
|
1428 |
// Print a CDATA section. The text is not escaped, but ']]>' |
|
1429 |
// appearing in the code must be identified and dealt with. |
|
1430 |
// The contents of a text node is considered space preserving. |
|
1431 |
if ( ! state.inCData ) { |
|
1432 |
_printer.printText("<![CDATA["); |
|
1433 |
state.inCData = true; |
|
1434 |
} |
|
23777
ce87cedb71cf
8035437: Xerces Update: xml/serialize/DOMSerializerImpl
aefimov
parents:
12457
diff
changeset
|
1435 |
int saveIndent = _printer.getNextIndent(); |
6 | 1436 |
_printer.setNextIndent( 0 ); |
1437 |
printCDATAText( text); |
|
1438 |
_printer.setNextIndent( saveIndent ); |
|
1439 |
||
1440 |
} else { |
|
1441 |
||
1442 |
int saveIndent; |
|
1443 |
||
1444 |
if ( state.preserveSpace ) { |
|
1445 |
// If preserving space then hold of indentation so no |
|
1446 |
// excessive spaces are printed at line breaks, escape |
|
1447 |
// the text content without replacing spaces and print |
|
1448 |
// the text breaking only at line breaks. |
|
1449 |
saveIndent = _printer.getNextIndent(); |
|
1450 |
_printer.setNextIndent( 0 ); |
|
1451 |
printText( text, true, state.unescaped ); |
|
1452 |
_printer.setNextIndent( saveIndent ); |
|
1453 |
} else { |
|
1454 |
printText( text, false, state.unescaped ); |
|
1455 |
} |
|
1456 |
} |
|
1457 |
} |
|
1458 |
||
1459 |
||
1460 |
/** |
|
1461 |
* Returns the suitable entity reference for this character value, |
|
1462 |
* or null if no such entity exists. Calling this method with <tt>'&'</tt> |
|
1463 |
* will return <tt>"&amp;"</tt>. |
|
1464 |
* |
|
1465 |
* @param ch Character value |
|
1466 |
* @return Character entity name, or null |
|
1467 |
*/ |
|
1468 |
protected abstract String getEntityRef( int ch ); |
|
1469 |
||
1470 |
||
1471 |
/** |
|
1472 |
* Called to serializee the DOM element. The element is serialized based on |
|
1473 |
* the serializer's method (XML, HTML, XHTML). |
|
1474 |
* |
|
1475 |
* @param elem The element to serialize |
|
1476 |
* @throws IOException An I/O exception occured while |
|
1477 |
* serializing |
|
1478 |
*/ |
|
1479 |
protected abstract void serializeElement( Element elem ) |
|
1480 |
throws IOException; |
|
1481 |
||
1482 |
||
1483 |
/** |
|
1484 |
* Comments and PIs cannot be serialized before the root element, |
|
1485 |
* because the root element serializes the document type, which |
|
1486 |
* generally comes first. Instead such PIs and comments are |
|
1487 |
* accumulated inside a vector and serialized by calling this |
|
1488 |
* method. Will be called when the root element is serialized |
|
1489 |
* and when the document finished serializing. |
|
1490 |
* |
|
1491 |
* @throws IOException An I/O exception occured while |
|
1492 |
* serializing |
|
1493 |
*/ |
|
1494 |
protected void serializePreRoot() |
|
1495 |
throws IOException |
|
1496 |
{ |
|
1497 |
int i; |
|
1498 |
||
1499 |
if ( _preRoot != null ) { |
|
1500 |
for ( i = 0 ; i < _preRoot.size() ; ++i ) { |
|
1501 |
printText( (String) _preRoot.elementAt( i ), true, true ); |
|
1502 |
if ( _indenting ) |
|
1503 |
_printer.breakLine(); |
|
1504 |
} |
|
1505 |
_preRoot.removeAllElements(); |
|
1506 |
} |
|
1507 |
} |
|
1508 |
||
1509 |
||
1510 |
//---------------------------------------------// |
|
1511 |
// Text pretty printing and formatting methods // |
|
1512 |
//---------------------------------------------// |
|
1513 |
||
1514 |
protected void printCDATAText( String text ) throws IOException { |
|
1515 |
int length = text.length(); |
|
1516 |
char ch; |
|
1517 |
||
1518 |
for ( int index = 0 ; index < length; ++index ) { |
|
1519 |
ch = text.charAt( index ); |
|
1520 |
if (ch == ']' |
|
1521 |
&& index + 2 < length |
|
1522 |
&& text.charAt(index + 1) == ']' |
|
1523 |
&& text.charAt(index + 2) == '>') { // check for ']]>' |
|
1524 |
if (fDOMErrorHandler != null) { |
|
1525 |
// REVISIT: this means that if DOM Error handler is not registered we don't report any |
|
1526 |
// fatal errors and might serialize not wellformed document |
|
1527 |
if ((features & DOMSerializerImpl.SPLITCDATA) == 0) { |
|
1528 |
String msg = DOMMessageFormatter.formatMessage( |
|
1529 |
DOMMessageFormatter.SERIALIZER_DOMAIN, |
|
1530 |
"EndingCDATA", |
|
1531 |
null); |
|
1532 |
if ((features & DOMSerializerImpl.WELLFORMED) != 0) { |
|
1533 |
// issue fatal error |
|
1534 |
modifyDOMError(msg, DOMError.SEVERITY_FATAL_ERROR, "wf-invalid-character", fCurrentNode); |
|
1535 |
fDOMErrorHandler.handleError(fDOMError); |
|
1536 |
throw new LSException(LSException.SERIALIZE_ERR, msg); |
|
1537 |
} |
|
23777
ce87cedb71cf
8035437: Xerces Update: xml/serialize/DOMSerializerImpl
aefimov
parents:
12457
diff
changeset
|
1538 |
// issue error |
ce87cedb71cf
8035437: Xerces Update: xml/serialize/DOMSerializerImpl
aefimov
parents:
12457
diff
changeset
|
1539 |
modifyDOMError(msg, DOMError.SEVERITY_ERROR, "cdata-section-not-splitted", fCurrentNode); |
ce87cedb71cf
8035437: Xerces Update: xml/serialize/DOMSerializerImpl
aefimov
parents:
12457
diff
changeset
|
1540 |
if (!fDOMErrorHandler.handleError(fDOMError)) { |
ce87cedb71cf
8035437: Xerces Update: xml/serialize/DOMSerializerImpl
aefimov
parents:
12457
diff
changeset
|
1541 |
throw new LSException(LSException.SERIALIZE_ERR, msg); |
6 | 1542 |
} |
1543 |
} else { |
|
1544 |
// issue warning |
|
1545 |
String msg = |
|
1546 |
DOMMessageFormatter.formatMessage( |
|
1547 |
DOMMessageFormatter.SERIALIZER_DOMAIN, |
|
1548 |
"SplittingCDATA", |
|
1549 |
null); |
|
1550 |
modifyDOMError( |
|
1551 |
msg, |
|
1552 |
DOMError.SEVERITY_WARNING, |
|
1553 |
null, fCurrentNode); |
|
1554 |
fDOMErrorHandler.handleError(fDOMError); |
|
1555 |
} |
|
1556 |
} |
|
1557 |
// split CDATA section |
|
1558 |
_printer.printText("]]]]><![CDATA[>"); |
|
1559 |
index += 2; |
|
1560 |
continue; |
|
1561 |
} |
|
1562 |
||
1563 |
if (!XMLChar.isValid(ch)) { |
|
1564 |
// check if it is surrogate |
|
1565 |
if (++index <length) { |
|
23777
ce87cedb71cf
8035437: Xerces Update: xml/serialize/DOMSerializerImpl
aefimov
parents:
12457
diff
changeset
|
1566 |
surrogates(ch, text.charAt(index),true); |
6 | 1567 |
} |
1568 |
else { |
|
23777
ce87cedb71cf
8035437: Xerces Update: xml/serialize/DOMSerializerImpl
aefimov
parents:
12457
diff
changeset
|
1569 |
fatalError("The character '"+ch+"' is an invalid XML character"); |
6 | 1570 |
} |
1571 |
continue; |
|
23777
ce87cedb71cf
8035437: Xerces Update: xml/serialize/DOMSerializerImpl
aefimov
parents:
12457
diff
changeset
|
1572 |
} |
ce87cedb71cf
8035437: Xerces Update: xml/serialize/DOMSerializerImpl
aefimov
parents:
12457
diff
changeset
|
1573 |
if ( ( ch >= ' ' && _encodingInfo.isPrintable(ch) && ch != 0x7F ) || |
ce87cedb71cf
8035437: Xerces Update: xml/serialize/DOMSerializerImpl
aefimov
parents:
12457
diff
changeset
|
1574 |
ch == '\n' || ch == '\r' || ch == '\t' ) { |
ce87cedb71cf
8035437: Xerces Update: xml/serialize/DOMSerializerImpl
aefimov
parents:
12457
diff
changeset
|
1575 |
_printer.printText(ch); |
ce87cedb71cf
8035437: Xerces Update: xml/serialize/DOMSerializerImpl
aefimov
parents:
12457
diff
changeset
|
1576 |
} |
ce87cedb71cf
8035437: Xerces Update: xml/serialize/DOMSerializerImpl
aefimov
parents:
12457
diff
changeset
|
1577 |
else { |
6 | 1578 |
|
23777
ce87cedb71cf
8035437: Xerces Update: xml/serialize/DOMSerializerImpl
aefimov
parents:
12457
diff
changeset
|
1579 |
// The character is not printable -- split CDATA section |
ce87cedb71cf
8035437: Xerces Update: xml/serialize/DOMSerializerImpl
aefimov
parents:
12457
diff
changeset
|
1580 |
_printer.printText("]]>&#x"); |
ce87cedb71cf
8035437: Xerces Update: xml/serialize/DOMSerializerImpl
aefimov
parents:
12457
diff
changeset
|
1581 |
_printer.printText(Integer.toHexString(ch)); |
ce87cedb71cf
8035437: Xerces Update: xml/serialize/DOMSerializerImpl
aefimov
parents:
12457
diff
changeset
|
1582 |
_printer.printText(";<![CDATA["); |
6 | 1583 |
} |
1584 |
} |
|
1585 |
} |
|
1586 |
||
1587 |
||
23777
ce87cedb71cf
8035437: Xerces Update: xml/serialize/DOMSerializerImpl
aefimov
parents:
12457
diff
changeset
|
1588 |
protected void surrogates(int high, int low, boolean inContent) throws IOException{ |
6 | 1589 |
if (XMLChar.isHighSurrogate(high)) { |
1590 |
if (!XMLChar.isLowSurrogate(low)) { |
|
1591 |
//Invalid XML |
|
1592 |
fatalError("The character '"+(char)low+"' is an invalid XML character"); |
|
1593 |
} |
|
1594 |
else { |
|
1595 |
int supplemental = XMLChar.supplemental((char)high, (char)low); |
|
1596 |
if (!XMLChar.isValid(supplemental)) { |
|
1597 |
//Invalid XML |
|
1598 |
fatalError("The character '"+(char)supplemental+"' is an invalid XML character"); |
|
1599 |
} |
|
1600 |
else { |
|
23777
ce87cedb71cf
8035437: Xerces Update: xml/serialize/DOMSerializerImpl
aefimov
parents:
12457
diff
changeset
|
1601 |
if (inContent && content().inCData) { |
6 | 1602 |
_printer.printText("]]>&#x"); |
1603 |
_printer.printText(Integer.toHexString(supplemental)); |
|
1604 |
_printer.printText(";<![CDATA["); |
|
1605 |
} |
|
1606 |
else { |
|
1607 |
printHex(supplemental); |
|
1608 |
} |
|
1609 |
} |
|
1610 |
} |
|
1611 |
} else { |
|
1612 |
fatalError("The character '"+(char)high+"' is an invalid XML character"); |
|
1613 |
} |
|
1614 |
||
1615 |
} |
|
1616 |
||
1617 |
/** |
|
1618 |
* Called to print additional text with whitespace handling. |
|
1619 |
* If spaces are preserved, the text is printed as if by calling |
|
1620 |
* {@link #printText(String,boolean,boolean)} with a call to {@link Printer#breakLine} |
|
1621 |
* for each new line. If spaces are not preserved, the text is |
|
1622 |
* broken at space boundaries if longer than the line width; |
|
1623 |
* Multiple spaces are printed as such, but spaces at beginning |
|
1624 |
* of line are removed. |
|
1625 |
* |
|
23777
ce87cedb71cf
8035437: Xerces Update: xml/serialize/DOMSerializerImpl
aefimov
parents:
12457
diff
changeset
|
1626 |
* @param chars The text to print |
ce87cedb71cf
8035437: Xerces Update: xml/serialize/DOMSerializerImpl
aefimov
parents:
12457
diff
changeset
|
1627 |
* @param start The start offset |
ce87cedb71cf
8035437: Xerces Update: xml/serialize/DOMSerializerImpl
aefimov
parents:
12457
diff
changeset
|
1628 |
* @param length The number of characters |
6 | 1629 |
* @param preserveSpace Space preserving flag |
1630 |
* @param unescaped Print unescaped |
|
1631 |
*/ |
|
1632 |
protected void printText( char[] chars, int start, int length, |
|
1633 |
boolean preserveSpace, boolean unescaped ) |
|
1634 |
throws IOException |
|
1635 |
{ |
|
1636 |
||
1637 |
if ( preserveSpace ) { |
|
1638 |
// Preserving spaces: the text must print exactly as it is, |
|
1639 |
// without breaking when spaces appear in the text and without |
|
1640 |
// consolidating spaces. If a line terminator is used, a line |
|
1641 |
// break will occur. |
|
1642 |
while ( length-- > 0 ) { |
|
23777
ce87cedb71cf
8035437: Xerces Update: xml/serialize/DOMSerializerImpl
aefimov
parents:
12457
diff
changeset
|
1643 |
char ch = chars[ start ]; |
6 | 1644 |
++start; |
23777
ce87cedb71cf
8035437: Xerces Update: xml/serialize/DOMSerializerImpl
aefimov
parents:
12457
diff
changeset
|
1645 |
if ( ch == '\n' || ch == '\r' || unescaped ) { |
6 | 1646 |
_printer.printText( ch ); |
23777
ce87cedb71cf
8035437: Xerces Update: xml/serialize/DOMSerializerImpl
aefimov
parents:
12457
diff
changeset
|
1647 |
} |
ce87cedb71cf
8035437: Xerces Update: xml/serialize/DOMSerializerImpl
aefimov
parents:
12457
diff
changeset
|
1648 |
else { |
6 | 1649 |
printEscaped( ch ); |
23777
ce87cedb71cf
8035437: Xerces Update: xml/serialize/DOMSerializerImpl
aefimov
parents:
12457
diff
changeset
|
1650 |
} |
6 | 1651 |
} |
1652 |
} else { |
|
1653 |
// Not preserving spaces: print one part at a time, and |
|
1654 |
// use spaces between parts to break them into different |
|
1655 |
// lines. Spaces at beginning of line will be stripped |
|
1656 |
// by printing mechanism. Line terminator is treated |
|
1657 |
// no different than other text part. |
|
1658 |
while ( length-- > 0 ) { |
|
23777
ce87cedb71cf
8035437: Xerces Update: xml/serialize/DOMSerializerImpl
aefimov
parents:
12457
diff
changeset
|
1659 |
char ch = chars[ start ]; |
6 | 1660 |
++start; |
23777
ce87cedb71cf
8035437: Xerces Update: xml/serialize/DOMSerializerImpl
aefimov
parents:
12457
diff
changeset
|
1661 |
if ( ch == ' ' || ch == '\f' || ch == '\t' || ch == '\n' || ch == '\r' ) { |
6 | 1662 |
_printer.printSpace(); |
23777
ce87cedb71cf
8035437: Xerces Update: xml/serialize/DOMSerializerImpl
aefimov
parents:
12457
diff
changeset
|
1663 |
} |
ce87cedb71cf
8035437: Xerces Update: xml/serialize/DOMSerializerImpl
aefimov
parents:
12457
diff
changeset
|
1664 |
else if ( unescaped ) { |
6 | 1665 |
_printer.printText( ch ); |
23777
ce87cedb71cf
8035437: Xerces Update: xml/serialize/DOMSerializerImpl
aefimov
parents:
12457
diff
changeset
|
1666 |
} |
ce87cedb71cf
8035437: Xerces Update: xml/serialize/DOMSerializerImpl
aefimov
parents:
12457
diff
changeset
|
1667 |
else { |
6 | 1668 |
printEscaped( ch ); |
23777
ce87cedb71cf
8035437: Xerces Update: xml/serialize/DOMSerializerImpl
aefimov
parents:
12457
diff
changeset
|
1669 |
} |
6 | 1670 |
} |
1671 |
} |
|
1672 |
} |
|
1673 |
||
1674 |
||
1675 |
protected void printText( String text, boolean preserveSpace, boolean unescaped ) |
|
1676 |
throws IOException |
|
1677 |
{ |
|
1678 |
int index; |
|
1679 |
char ch; |
|
1680 |
||
1681 |
if ( preserveSpace ) { |
|
1682 |
// Preserving spaces: the text must print exactly as it is, |
|
1683 |
// without breaking when spaces appear in the text and without |
|
1684 |
// consolidating spaces. If a line terminator is used, a line |
|
1685 |
// break will occur. |
|
1686 |
for ( index = 0 ; index < text.length() ; ++index ) { |
|
1687 |
ch = text.charAt( index ); |
|
1688 |
if ( ch == '\n' || ch == '\r' || unescaped ) |
|
1689 |
_printer.printText( ch ); |
|
1690 |
else |
|
1691 |
printEscaped( ch ); |
|
1692 |
} |
|
1693 |
} else { |
|
1694 |
// Not preserving spaces: print one part at a time, and |
|
1695 |
// use spaces between parts to break them into different |
|
1696 |
// lines. Spaces at beginning of line will be stripped |
|
1697 |
// by printing mechanism. Line terminator is treated |
|
1698 |
// no different than other text part. |
|
1699 |
for ( index = 0 ; index < text.length() ; ++index ) { |
|
1700 |
ch = text.charAt( index ); |
|
23777
ce87cedb71cf
8035437: Xerces Update: xml/serialize/DOMSerializerImpl
aefimov
parents:
12457
diff
changeset
|
1701 |
if ( ch == ' ' || ch == '\f' || ch == '\t' || ch == '\n' || ch == '\r' ) { |
6 | 1702 |
_printer.printSpace(); |
23777
ce87cedb71cf
8035437: Xerces Update: xml/serialize/DOMSerializerImpl
aefimov
parents:
12457
diff
changeset
|
1703 |
} |
ce87cedb71cf
8035437: Xerces Update: xml/serialize/DOMSerializerImpl
aefimov
parents:
12457
diff
changeset
|
1704 |
else if ( unescaped ) { |
6 | 1705 |
_printer.printText( ch ); |
23777
ce87cedb71cf
8035437: Xerces Update: xml/serialize/DOMSerializerImpl
aefimov
parents:
12457
diff
changeset
|
1706 |
} |
ce87cedb71cf
8035437: Xerces Update: xml/serialize/DOMSerializerImpl
aefimov
parents:
12457
diff
changeset
|
1707 |
else { |
6 | 1708 |
printEscaped( ch ); |
23777
ce87cedb71cf
8035437: Xerces Update: xml/serialize/DOMSerializerImpl
aefimov
parents:
12457
diff
changeset
|
1709 |
} |
6 | 1710 |
} |
1711 |
} |
|
1712 |
} |
|
1713 |
||
1714 |
||
1715 |
/** |
|
1716 |
* Print a document type public or system identifier URL. |
|
1717 |
* Encapsulates the URL in double quotes, escapes non-printing |
|
1718 |
* characters and print it equivalent to {@link #printText}. |
|
1719 |
* |
|
1720 |
* @param url The document type url to print |
|
1721 |
*/ |
|
1722 |
protected void printDoctypeURL( String url ) |
|
1723 |
throws IOException |
|
1724 |
{ |
|
1725 |
int i; |
|
1726 |
||
1727 |
_printer.printText( '"' ); |
|
1728 |
for( i = 0 ; i < url.length() ; ++i ) { |
|
1729 |
if ( url.charAt( i ) == '"' || url.charAt( i ) < 0x20 || url.charAt( i ) > 0x7F ) { |
|
1730 |
_printer.printText( '%' ); |
|
1731 |
_printer.printText( Integer.toHexString( url.charAt( i ) ) ); |
|
1732 |
} else |
|
1733 |
_printer.printText( url.charAt( i ) ); |
|
1734 |
} |
|
1735 |
_printer.printText( '"' ); |
|
1736 |
} |
|
1737 |
||
1738 |
||
1739 |
protected void printEscaped( int ch ) |
|
1740 |
throws IOException |
|
1741 |
{ |
|
1742 |
String charRef; |
|
1743 |
// If there is a suitable entity reference for this |
|
1744 |
// character, print it. The list of available entity |
|
1745 |
// references is almost but not identical between |
|
1746 |
// XML and HTML. |
|
1747 |
charRef = getEntityRef( ch ); |
|
1748 |
if ( charRef != null ) { |
|
1749 |
_printer.printText( '&' ); |
|
1750 |
_printer.printText( charRef ); |
|
1751 |
_printer.printText( ';' ); |
|
23777
ce87cedb71cf
8035437: Xerces Update: xml/serialize/DOMSerializerImpl
aefimov
parents:
12457
diff
changeset
|
1752 |
} else if ( ( ch >= ' ' && _encodingInfo.isPrintable((char)ch) && ch != 0x7F ) || |
6 | 1753 |
ch == '\n' || ch == '\r' || ch == '\t' ) { |
1754 |
// Non printables are below ASCII space but not tab or line |
|
1755 |
// terminator, ASCII delete, or above a certain Unicode threshold. |
|
1756 |
if (ch < 0x10000) { |
|
1757 |
_printer.printText((char)ch ); |
|
1758 |
} else { |
|
1759 |
_printer.printText((char)(((ch-0x10000)>>10)+0xd800)); |
|
1760 |
_printer.printText((char)(((ch-0x10000)&0x3ff)+0xdc00)); |
|
1761 |
} |
|
1762 |
} else { |
|
1763 |
printHex(ch); |
|
1764 |
} |
|
1765 |
} |
|
1766 |
||
1767 |
/** |
|
1768 |
* Escapes chars |
|
1769 |
*/ |
|
1770 |
final void printHex( int ch) throws IOException { |
|
1771 |
_printer.printText( "&#x" ); |
|
1772 |
_printer.printText(Integer.toHexString(ch)); |
|
1773 |
_printer.printText( ';' ); |
|
1774 |
||
1775 |
} |
|
1776 |
||
1777 |
||
1778 |
/** |
|
1779 |
* Escapes a string so it may be printed as text content or attribute |
|
1780 |
* value. Non printable characters are escaped using character references. |
|
1781 |
* Where the format specifies a deault entity reference, that reference |
|
1782 |
* is used (e.g. <tt>&lt;</tt>). |
|
1783 |
* |
|
1784 |
* @param source The string to escape |
|
1785 |
*/ |
|
1786 |
protected void printEscaped( String source ) |
|
1787 |
throws IOException |
|
1788 |
{ |
|
1789 |
for ( int i = 0 ; i < source.length() ; ++i ) { |
|
1790 |
int ch = source.charAt(i); |
|
1791 |
if ((ch & 0xfc00) == 0xd800 && i+1 < source.length()) { |
|
1792 |
int lowch = source.charAt(i+1); |
|
1793 |
if ((lowch & 0xfc00) == 0xdc00) { |
|
1794 |
ch = 0x10000 + ((ch-0xd800)<<10) + lowch-0xdc00; |
|
1795 |
i++; |
|
1796 |
} |
|
1797 |
} |
|
1798 |
printEscaped(ch); |
|
1799 |
} |
|
1800 |
} |
|
1801 |
||
1802 |
||
1803 |
//--------------------------------// |
|
1804 |
// Element state handling methods // |
|
1805 |
//--------------------------------// |
|
1806 |
||
1807 |
||
1808 |
/** |
|
1809 |
* Return the state of the current element. |
|
1810 |
* |
|
1811 |
* @return Current element state |
|
1812 |
*/ |
|
1813 |
protected ElementState getElementState() |
|
1814 |
{ |
|
1815 |
return _elementStates[ _elementStateCount ]; |
|
1816 |
} |
|
1817 |
||
1818 |
||
1819 |
/** |
|
1820 |
* Enter a new element state for the specified element. |
|
1821 |
* Tag name and space preserving is specified, element |
|
1822 |
* state is initially empty. |
|
1823 |
* |
|
1824 |
* @return Current element state, or null |
|
1825 |
*/ |
|
1826 |
protected ElementState enterElementState( String namespaceURI, String localName, |
|
1827 |
String rawName, boolean preserveSpace ) |
|
1828 |
{ |
|
1829 |
ElementState state; |
|
1830 |
||
1831 |
if ( _elementStateCount + 1 == _elementStates.length ) { |
|
1832 |
ElementState[] newStates; |
|
1833 |
||
1834 |
// Need to create a larger array of states. This does not happen |
|
1835 |
// often, unless the document is really deep. |
|
1836 |
newStates = new ElementState[ _elementStates.length + 10 ]; |
|
1837 |
for ( int i = 0 ; i < _elementStates.length ; ++i ) |
|
1838 |
newStates[ i ] = _elementStates[ i ]; |
|
1839 |
for ( int i = _elementStates.length ; i < newStates.length ; ++i ) |
|
1840 |
newStates[ i ] = new ElementState(); |
|
1841 |
_elementStates = newStates; |
|
1842 |
} |
|
1843 |
||
1844 |
++_elementStateCount; |
|
1845 |
state = _elementStates[ _elementStateCount ]; |
|
1846 |
state.namespaceURI = namespaceURI; |
|
1847 |
state.localName = localName; |
|
1848 |
state.rawName = rawName; |
|
1849 |
state.preserveSpace = preserveSpace; |
|
1850 |
state.empty = true; |
|
1851 |
state.afterElement = false; |
|
1852 |
state.afterComment = false; |
|
1853 |
state.doCData = state.inCData = false; |
|
1854 |
state.unescaped = false; |
|
1855 |
state.prefixes = _prefixes; |
|
1856 |
||
1857 |
_prefixes = null; |
|
1858 |
return state; |
|
1859 |
} |
|
1860 |
||
1861 |
||
1862 |
/** |
|
1863 |
* Leave the current element state and return to the |
|
1864 |
* state of the parent element. If this was the root |
|
1865 |
* element, return to the state of the document. |
|
1866 |
* |
|
1867 |
* @return Previous element state |
|
1868 |
*/ |
|
1869 |
protected ElementState leaveElementState() |
|
1870 |
{ |
|
1871 |
if ( _elementStateCount > 0 ) { |
|
1872 |
/*Corrected by David Blondeau (blondeau@intalio.com)*/ |
|
23777
ce87cedb71cf
8035437: Xerces Update: xml/serialize/DOMSerializerImpl
aefimov
parents:
12457
diff
changeset
|
1873 |
_prefixes = null; |
ce87cedb71cf
8035437: Xerces Update: xml/serialize/DOMSerializerImpl
aefimov
parents:
12457
diff
changeset
|
1874 |
//_prefixes = _elementStates[ _elementStateCount ].prefixes; |
6 | 1875 |
-- _elementStateCount; |
1876 |
return _elementStates[ _elementStateCount ]; |
|
1877 |
} |
|
23777
ce87cedb71cf
8035437: Xerces Update: xml/serialize/DOMSerializerImpl
aefimov
parents:
12457
diff
changeset
|
1878 |
String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.SERIALIZER_DOMAIN, "Internal", null); |
ce87cedb71cf
8035437: Xerces Update: xml/serialize/DOMSerializerImpl
aefimov
parents:
12457
diff
changeset
|
1879 |
throw new IllegalStateException(msg); |
6 | 1880 |
} |
1881 |
||
1882 |
||
1883 |
/** |
|
1884 |
* Returns true if in the state of the document. |
|
1885 |
* Returns true before entering any element and after |
|
1886 |
* leaving the root element. |
|
1887 |
* |
|
1888 |
* @return True if in the state of the document |
|
1889 |
*/ |
|
23777
ce87cedb71cf
8035437: Xerces Update: xml/serialize/DOMSerializerImpl
aefimov
parents:
12457
diff
changeset
|
1890 |
protected boolean isDocumentState() { |
6 | 1891 |
return _elementStateCount == 0; |
1892 |
} |
|
1893 |
||
23777
ce87cedb71cf
8035437: Xerces Update: xml/serialize/DOMSerializerImpl
aefimov
parents:
12457
diff
changeset
|
1894 |
/** Clears document state. **/ |
ce87cedb71cf
8035437: Xerces Update: xml/serialize/DOMSerializerImpl
aefimov
parents:
12457
diff
changeset
|
1895 |
final void clearDocumentState() { |
ce87cedb71cf
8035437: Xerces Update: xml/serialize/DOMSerializerImpl
aefimov
parents:
12457
diff
changeset
|
1896 |
_elementStateCount = 0; |
ce87cedb71cf
8035437: Xerces Update: xml/serialize/DOMSerializerImpl
aefimov
parents:
12457
diff
changeset
|
1897 |
} |
6 | 1898 |
|
1899 |
/** |
|
1900 |
* Returns the namespace prefix for the specified URI. |
|
1901 |
* If the URI has been mapped to a prefix, returns the |
|
1902 |
* prefix, otherwise returns null. |
|
1903 |
* |
|
1904 |
* @param namespaceURI The namespace URI |
|
1905 |
* @return The namespace prefix if known, or null |
|
1906 |
*/ |
|
1907 |
protected String getPrefix( String namespaceURI ) |
|
1908 |
{ |
|
1909 |
String prefix; |
|
1910 |
||
1911 |
if ( _prefixes != null ) { |
|
1912 |
prefix = (String) _prefixes.get( namespaceURI ); |
|
1913 |
if ( prefix != null ) |
|
1914 |
return prefix; |
|
1915 |
} |
|
23777
ce87cedb71cf
8035437: Xerces Update: xml/serialize/DOMSerializerImpl
aefimov
parents:
12457
diff
changeset
|
1916 |
if ( _elementStateCount == 0 ) { |
6 | 1917 |
return null; |
23777
ce87cedb71cf
8035437: Xerces Update: xml/serialize/DOMSerializerImpl
aefimov
parents:
12457
diff
changeset
|
1918 |
} |
ce87cedb71cf
8035437: Xerces Update: xml/serialize/DOMSerializerImpl
aefimov
parents:
12457
diff
changeset
|
1919 |
for ( int i = _elementStateCount ; i > 0 ; --i ) { |
ce87cedb71cf
8035437: Xerces Update: xml/serialize/DOMSerializerImpl
aefimov
parents:
12457
diff
changeset
|
1920 |
if ( _elementStates[ i ].prefixes != null ) { |
ce87cedb71cf
8035437: Xerces Update: xml/serialize/DOMSerializerImpl
aefimov
parents:
12457
diff
changeset
|
1921 |
prefix = (String) _elementStates[ i ].prefixes.get( namespaceURI ); |
ce87cedb71cf
8035437: Xerces Update: xml/serialize/DOMSerializerImpl
aefimov
parents:
12457
diff
changeset
|
1922 |
if ( prefix != null ) |
ce87cedb71cf
8035437: Xerces Update: xml/serialize/DOMSerializerImpl
aefimov
parents:
12457
diff
changeset
|
1923 |
return prefix; |
6 | 1924 |
} |
1925 |
} |
|
1926 |
return null; |
|
1927 |
} |
|
1928 |
||
1929 |
/** |
|
1930 |
* The method modifies global DOM error object |
|
1931 |
* |
|
1932 |
* @param message |
|
1933 |
* @param severity |
|
1934 |
* @param type |
|
1935 |
* @return a DOMError |
|
1936 |
*/ |
|
1937 |
protected DOMError modifyDOMError(String message, short severity, String type, Node node){ |
|
1938 |
fDOMError.reset(); |
|
1939 |
fDOMError.fMessage = message; |
|
1940 |
fDOMError.fType = type; |
|
1941 |
fDOMError.fSeverity = severity; |
|
1942 |
fDOMError.fLocator = new DOMLocatorImpl(-1, -1, -1, node, null); |
|
1943 |
return fDOMError; |
|
1944 |
||
1945 |
} |
|
1946 |
||
1947 |
||
1948 |
protected void fatalError(String message) throws IOException{ |
|
1949 |
if (fDOMErrorHandler != null) { |
|
1950 |
modifyDOMError(message, DOMError.SEVERITY_FATAL_ERROR, null, fCurrentNode); |
|
1951 |
fDOMErrorHandler.handleError(fDOMError); |
|
1952 |
} |
|
1953 |
else { |
|
1954 |
throw new IOException(message); |
|
1955 |
} |
|
1956 |
} |
|
1957 |
||
1958 |
/** |
|
1959 |
* DOM level 3: |
|
1960 |
* Check a node to determine if it contains unbound namespace prefixes. |
|
1961 |
* |
|
1962 |
* @param node The node to check for unbound namespace prefices |
|
1963 |
*/ |
|
1964 |
protected void checkUnboundNamespacePrefixedNode (Node node) throws IOException{ |
|
1965 |
||
1966 |
} |
|
1967 |
} |