12005
|
1 |
/*
|
|
2 |
* reserved comment block
|
|
3 |
* DO NOT REMOVE OR ALTER!
|
|
4 |
*/
|
|
5 |
/*
|
|
6 |
* Copyright 1999-2005 The Apache Software Foundation.
|
|
7 |
*
|
|
8 |
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
9 |
* you may not use this file except in compliance with the License.
|
|
10 |
* You may obtain a copy of the License at
|
|
11 |
*
|
|
12 |
* http://www.apache.org/licenses/LICENSE-2.0
|
|
13 |
*
|
|
14 |
* Unless required by applicable law or agreed to in writing, software
|
|
15 |
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
16 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
17 |
* See the License for the specific language governing permissions and
|
|
18 |
* limitations under the License.
|
|
19 |
*/
|
|
20 |
|
|
21 |
package com.sun.org.apache.xml.internal.serialize;
|
|
22 |
|
|
23 |
import java.io.FileOutputStream;
|
|
24 |
import java.io.IOException;
|
|
25 |
import java.io.OutputStream;
|
|
26 |
import java.io.StringWriter;
|
|
27 |
import java.io.UnsupportedEncodingException;
|
|
28 |
import java.io.Writer;
|
|
29 |
import java.lang.reflect.Method;
|
|
30 |
import java.net.HttpURLConnection;
|
|
31 |
import java.net.URL;
|
|
32 |
import java.net.URLConnection;
|
|
33 |
import java.util.StringTokenizer;
|
|
34 |
import java.util.Vector;
|
|
35 |
|
|
36 |
import com.sun.org.apache.xerces.internal.dom.CoreDocumentImpl;
|
|
37 |
import com.sun.org.apache.xerces.internal.dom.DOMErrorImpl;
|
|
38 |
import com.sun.org.apache.xerces.internal.dom.DOMLocatorImpl;
|
|
39 |
import com.sun.org.apache.xerces.internal.dom.DOMMessageFormatter;
|
|
40 |
import com.sun.org.apache.xerces.internal.dom.DOMNormalizer;
|
|
41 |
import com.sun.org.apache.xerces.internal.dom.DOMStringListImpl;
|
|
42 |
import org.w3c.dom.DOMConfiguration;
|
|
43 |
import org.w3c.dom.DOMError;
|
|
44 |
import org.w3c.dom.DOMErrorHandler;
|
|
45 |
import org.w3c.dom.DOMStringList;
|
|
46 |
import com.sun.org.apache.xerces.internal.impl.Constants;
|
|
47 |
import com.sun.org.apache.xerces.internal.impl.XMLEntityManager;
|
|
48 |
import com.sun.org.apache.xerces.internal.util.DOMUtil;
|
|
49 |
import com.sun.org.apache.xerces.internal.util.NamespaceSupport;
|
|
50 |
import com.sun.org.apache.xerces.internal.util.SymbolTable;
|
|
51 |
import com.sun.org.apache.xerces.internal.util.XML11Char;
|
|
52 |
import com.sun.org.apache.xerces.internal.util.XMLChar;
|
|
53 |
import org.w3c.dom.Attr;
|
|
54 |
import org.w3c.dom.Comment;
|
|
55 |
import org.w3c.dom.DOMException;
|
|
56 |
import org.w3c.dom.Document;
|
|
57 |
import org.w3c.dom.DocumentFragment;
|
|
58 |
import org.w3c.dom.Element;
|
|
59 |
import org.w3c.dom.NamedNodeMap;
|
|
60 |
import org.w3c.dom.Node;
|
|
61 |
import org.w3c.dom.ProcessingInstruction;
|
|
62 |
import org.w3c.dom.ls.LSException;
|
|
63 |
import org.w3c.dom.ls.LSOutput;
|
|
64 |
import org.w3c.dom.ls.LSSerializer;
|
|
65 |
import org.w3c.dom.ls.LSSerializerFilter;
|
|
66 |
|
|
67 |
|
|
68 |
/**
|
|
69 |
* EXPERIMENTAL: Implemenatation of DOM Level 3 org.w3c.ls.LSSerializer by delegating serialization
|
|
70 |
* calls to <CODE>XMLSerializer</CODE>.
|
|
71 |
* LSSerializer provides an API for serializing (writing) a DOM document out in an
|
|
72 |
* XML document. The XML data is written to an output stream.
|
|
73 |
* During serialization of XML data, namespace fixup is done when possible as
|
|
74 |
* defined in DOM Level 3 Core, Appendix B.
|
|
75 |
*
|
|
76 |
* @author Elena Litani, IBM
|
|
77 |
* @author Gopal Sharma, Sun Microsystems
|
|
78 |
* @author Arun Yadav, Sun Microsystems
|
|
79 |
* @author Sunitha Reddy, Sun Microsystems
|
|
80 |
* @version $Id: DOMSerializerImpl.java,v 1.11 2010-11-01 04:40:36 joehw Exp $
|
|
81 |
*/
|
|
82 |
public class DOMSerializerImpl implements LSSerializer, DOMConfiguration {
|
|
83 |
|
|
84 |
// TODO: When DOM Level 3 goes to REC replace method calls using
|
|
85 |
// reflection for: getXmlEncoding, getInputEncoding and getXmlEncoding
|
|
86 |
// with regular static calls on the Document object.
|
|
87 |
|
|
88 |
// data
|
|
89 |
// serializer
|
|
90 |
private XMLSerializer serializer;
|
|
91 |
|
|
92 |
// XML 1.1 serializer
|
|
93 |
private XML11Serializer xml11Serializer;
|
|
94 |
|
|
95 |
//Recognized parameters
|
|
96 |
private DOMStringList fRecognizedParameters;
|
|
97 |
|
|
98 |
/** REVISIT: Currently we handle 3 different configurations, would be nice just have one configuration
|
|
99 |
* that has different recognized parameters depending if it is used in Core/LS.
|
|
100 |
*/
|
|
101 |
protected short features = 0;
|
|
102 |
|
|
103 |
protected final static short NAMESPACES = 0x1<<0;
|
|
104 |
protected final static short WELLFORMED = 0x1<<1;
|
|
105 |
protected final static short ENTITIES = 0x1<<2;
|
|
106 |
protected final static short CDATA = 0x1<<3;
|
|
107 |
protected final static short SPLITCDATA = 0x1<<4;
|
|
108 |
protected final static short COMMENTS = 0x1<<5;
|
|
109 |
protected final static short DISCARDDEFAULT = 0x1<<6;
|
|
110 |
protected final static short INFOSET = 0x1<<7;
|
|
111 |
protected final static short XMLDECL = 0x1<<8;
|
|
112 |
protected final static short NSDECL = 0x1<<9;
|
|
113 |
protected final static short DOM_ELEMENT_CONTENT_WHITESPACE = 0x1<<10;
|
|
114 |
protected final static short FORMAT_PRETTY_PRINT = 0x1<<11;
|
|
115 |
|
|
116 |
// well-formness checking
|
|
117 |
private DOMErrorHandler fErrorHandler = null;
|
|
118 |
private final DOMErrorImpl fError = new DOMErrorImpl();
|
|
119 |
private final DOMLocatorImpl fLocator = new DOMLocatorImpl();
|
|
120 |
private static final RuntimeException abort = new RuntimeException();
|
|
121 |
|
|
122 |
/**
|
|
123 |
* Constructs a new LSSerializer.
|
|
124 |
* The constructor turns on the namespace support in <code>XMLSerializer</code> and
|
|
125 |
* initializes the following fields: fNSBinder, fLocalNSBinder, fSymbolTable,
|
|
126 |
* fEmptySymbol, fXmlSymbol, fXmlnsSymbol, fNamespaceCounter, fFeatures.
|
|
127 |
*/
|
|
128 |
public DOMSerializerImpl() {
|
|
129 |
// set default features
|
|
130 |
features |= NAMESPACES;
|
|
131 |
features |= ENTITIES;
|
|
132 |
features |= COMMENTS;
|
|
133 |
features |= CDATA;
|
|
134 |
features |= SPLITCDATA;
|
|
135 |
features |= WELLFORMED;
|
|
136 |
features |= NSDECL;
|
|
137 |
features |= DOM_ELEMENT_CONTENT_WHITESPACE;
|
|
138 |
features |= DISCARDDEFAULT;
|
|
139 |
features |= XMLDECL;
|
|
140 |
|
|
141 |
serializer = new XMLSerializer();
|
|
142 |
initSerializer(serializer);
|
|
143 |
}
|
|
144 |
|
|
145 |
|
|
146 |
|
|
147 |
//
|
|
148 |
// LSSerializer methods
|
|
149 |
//
|
|
150 |
|
|
151 |
public DOMConfiguration getDomConfig(){
|
|
152 |
return this;
|
|
153 |
}
|
|
154 |
|
|
155 |
/** DOM L3-EXPERIMENTAL:
|
|
156 |
* Setter for boolean and object parameters
|
|
157 |
*/
|
|
158 |
public void setParameter(String name, Object value) throws DOMException {
|
|
159 |
if (value instanceof Boolean) {
|
|
160 |
boolean state = ((Boolean) value).booleanValue();
|
|
161 |
if (name.equalsIgnoreCase(Constants.DOM_INFOSET)){
|
|
162 |
if (state){
|
|
163 |
features &= ~ENTITIES;
|
|
164 |
features &= ~CDATA;
|
|
165 |
features |= NAMESPACES;
|
|
166 |
features |= NSDECL;
|
|
167 |
features |= WELLFORMED;
|
|
168 |
features |= COMMENTS;
|
|
169 |
}
|
|
170 |
// false does not have any effect
|
|
171 |
} else if (name.equalsIgnoreCase(Constants.DOM_XMLDECL)) {
|
|
172 |
features =
|
|
173 |
(short) (state ? features | XMLDECL : features & ~XMLDECL);
|
|
174 |
} else if (name.equalsIgnoreCase(Constants.DOM_NAMESPACES)) {
|
|
175 |
features =
|
|
176 |
(short) (state
|
|
177 |
? features | NAMESPACES
|
|
178 |
: features & ~NAMESPACES);
|
|
179 |
serializer.fNamespaces = state;
|
|
180 |
} else if (name.equalsIgnoreCase(Constants.DOM_SPLIT_CDATA)) {
|
|
181 |
features =
|
|
182 |
(short) (state
|
|
183 |
? features | SPLITCDATA
|
|
184 |
: features & ~SPLITCDATA);
|
|
185 |
} else if (name.equalsIgnoreCase(Constants.DOM_DISCARD_DEFAULT_CONTENT)) {
|
|
186 |
features =
|
|
187 |
(short) (state
|
|
188 |
? features | DISCARDDEFAULT
|
|
189 |
: features & ~DISCARDDEFAULT);
|
|
190 |
} else if (name.equalsIgnoreCase(Constants.DOM_WELLFORMED)) {
|
|
191 |
features =
|
|
192 |
(short) (state
|
|
193 |
? features | WELLFORMED
|
|
194 |
: features & ~WELLFORMED);
|
|
195 |
} else if (name.equalsIgnoreCase(Constants.DOM_ENTITIES)){
|
|
196 |
features =
|
|
197 |
(short) (state
|
|
198 |
? features | ENTITIES
|
|
199 |
: features & ~ENTITIES);
|
|
200 |
}
|
|
201 |
else if (name.equalsIgnoreCase(Constants.DOM_CDATA_SECTIONS)){
|
|
202 |
features =
|
|
203 |
(short) (state
|
|
204 |
? features | CDATA
|
|
205 |
: features & ~CDATA);
|
|
206 |
}
|
|
207 |
else if (name.equalsIgnoreCase(Constants.DOM_COMMENTS)){
|
|
208 |
features =
|
|
209 |
(short) (state
|
|
210 |
? features | COMMENTS
|
|
211 |
: features & ~COMMENTS);
|
|
212 |
}
|
|
213 |
else if (name.equalsIgnoreCase(Constants.DOM_FORMAT_PRETTY_PRINT)){
|
|
214 |
features =
|
|
215 |
(short) (state
|
|
216 |
? features | FORMAT_PRETTY_PRINT
|
|
217 |
: features & ~FORMAT_PRETTY_PRINT);
|
|
218 |
}
|
|
219 |
else if (name.equalsIgnoreCase(Constants.DOM_CANONICAL_FORM)
|
|
220 |
|| name.equalsIgnoreCase(Constants.DOM_VALIDATE_IF_SCHEMA)
|
|
221 |
|| name.equalsIgnoreCase(Constants.DOM_VALIDATE)
|
|
222 |
|| name.equalsIgnoreCase(Constants.DOM_CHECK_CHAR_NORMALIZATION)
|
|
223 |
|| name.equalsIgnoreCase(Constants.DOM_DATATYPE_NORMALIZATION)) {
|
|
224 |
// || name.equalsIgnoreCase(Constants.DOM_NORMALIZE_CHARACTERS)) {
|
|
225 |
// true is not supported
|
|
226 |
if (state) {
|
|
227 |
String msg =
|
|
228 |
DOMMessageFormatter.formatMessage(
|
|
229 |
DOMMessageFormatter.DOM_DOMAIN,
|
|
230 |
"FEATURE_NOT_SUPPORTED",
|
|
231 |
new Object[] { name });
|
|
232 |
throw new DOMException(DOMException.NOT_SUPPORTED_ERR, msg);
|
|
233 |
}
|
|
234 |
}else if (
|
|
235 |
name.equalsIgnoreCase(Constants.DOM_NAMESPACE_DECLARATIONS)) {
|
|
236 |
//namespace-declaration has effect only if namespaces is true
|
|
237 |
features =
|
|
238 |
(short) (state
|
|
239 |
? features | NSDECL
|
|
240 |
: features & ~NSDECL);
|
|
241 |
serializer.fNamespacePrefixes = state;
|
|
242 |
} else if (name.equalsIgnoreCase(Constants.DOM_ELEMENT_CONTENT_WHITESPACE)
|
|
243 |
|| name.equalsIgnoreCase(Constants.DOM_IGNORE_UNKNOWN_CHARACTER_DENORMALIZATIONS)) {
|
|
244 |
// false is not supported
|
|
245 |
if (!state) {
|
|
246 |
String msg =
|
|
247 |
DOMMessageFormatter.formatMessage(
|
|
248 |
DOMMessageFormatter.DOM_DOMAIN,
|
|
249 |
"FEATURE_NOT_SUPPORTED",
|
|
250 |
new Object[] { name });
|
|
251 |
throw new DOMException(DOMException.NOT_SUPPORTED_ERR, msg);
|
|
252 |
}
|
|
253 |
} else {
|
|
254 |
String msg =
|
|
255 |
DOMMessageFormatter.formatMessage(
|
|
256 |
DOMMessageFormatter.DOM_DOMAIN,
|
|
257 |
"FEATURE_NOT_FOUND",
|
|
258 |
new Object[] { name });
|
|
259 |
throw new DOMException(DOMException.NOT_SUPPORTED_ERR, msg);
|
|
260 |
}
|
|
261 |
} else if (name.equalsIgnoreCase(Constants.DOM_ERROR_HANDLER)) {
|
|
262 |
if (value == null || value instanceof DOMErrorHandler) {
|
|
263 |
fErrorHandler = (DOMErrorHandler)value;
|
|
264 |
} else {
|
|
265 |
String msg =
|
|
266 |
DOMMessageFormatter.formatMessage(
|
|
267 |
DOMMessageFormatter.DOM_DOMAIN,
|
|
268 |
"TYPE_MISMATCH_ERR",
|
|
269 |
new Object[] { name });
|
|
270 |
throw new DOMException(DOMException.TYPE_MISMATCH_ERR, msg);
|
|
271 |
}
|
|
272 |
} else if (
|
|
273 |
name.equalsIgnoreCase(Constants.DOM_RESOURCE_RESOLVER)
|
|
274 |
|| name.equalsIgnoreCase(Constants.DOM_SCHEMA_LOCATION)
|
|
275 |
|| name.equalsIgnoreCase(Constants.DOM_SCHEMA_TYPE)
|
|
276 |
|| name.equalsIgnoreCase(Constants.DOM_NORMALIZE_CHARACTERS)
|
|
277 |
&& value != null) {
|
|
278 |
String msg =
|
|
279 |
DOMMessageFormatter.formatMessage(
|
|
280 |
DOMMessageFormatter.DOM_DOMAIN,
|
|
281 |
"FEATURE_NOT_SUPPORTED",
|
|
282 |
new Object[] { name });
|
|
283 |
throw new DOMException(DOMException.NOT_SUPPORTED_ERR, msg);
|
|
284 |
} else {
|
|
285 |
String msg =
|
|
286 |
DOMMessageFormatter.formatMessage(
|
|
287 |
DOMMessageFormatter.DOM_DOMAIN,
|
|
288 |
"FEATURE_NOT_FOUND",
|
|
289 |
new Object[] { name });
|
|
290 |
throw new DOMException(DOMException.NOT_FOUND_ERR, msg);
|
|
291 |
}
|
|
292 |
}
|
|
293 |
|
|
294 |
/** DOM L3-EXPERIMENTAL:
|
|
295 |
* Check if parameter can be set
|
|
296 |
*/
|
|
297 |
public boolean canSetParameter(String name, Object state) {
|
|
298 |
|
|
299 |
if (state == null) {
|
|
300 |
return true;
|
|
301 |
}
|
|
302 |
|
|
303 |
if (state instanceof Boolean) {
|
|
304 |
boolean value = ((Boolean) state).booleanValue();
|
|
305 |
|
|
306 |
if (name.equalsIgnoreCase(Constants.DOM_NAMESPACES)
|
|
307 |
|| name.equalsIgnoreCase(Constants.DOM_SPLIT_CDATA)
|
|
308 |
|| name.equalsIgnoreCase(Constants.DOM_DISCARD_DEFAULT_CONTENT)
|
|
309 |
|| name.equalsIgnoreCase(Constants.DOM_XMLDECL)
|
|
310 |
|| name.equalsIgnoreCase(Constants.DOM_WELLFORMED)
|
|
311 |
|| name.equalsIgnoreCase(Constants.DOM_INFOSET)
|
|
312 |
|| name.equalsIgnoreCase(Constants.DOM_ENTITIES)
|
|
313 |
|| name.equalsIgnoreCase(Constants.DOM_CDATA_SECTIONS)
|
|
314 |
|| name.equalsIgnoreCase(Constants.DOM_COMMENTS)
|
|
315 |
|| name.equalsIgnoreCase(Constants.DOM_NAMESPACE_DECLARATIONS)
|
|
316 |
|| name.equalsIgnoreCase(Constants.DOM_FORMAT_PRETTY_PRINT)) {
|
|
317 |
// both values supported
|
|
318 |
return true;
|
|
319 |
} else if (name.equalsIgnoreCase(Constants.DOM_CANONICAL_FORM)
|
|
320 |
|| name.equalsIgnoreCase(Constants.DOM_VALIDATE_IF_SCHEMA)
|
|
321 |
|| name.equalsIgnoreCase(Constants.DOM_VALIDATE)
|
|
322 |
|| name.equalsIgnoreCase(Constants.DOM_CHECK_CHAR_NORMALIZATION)
|
|
323 |
|| name.equalsIgnoreCase(Constants.DOM_DATATYPE_NORMALIZATION)) {
|
|
324 |
// || name.equalsIgnoreCase(Constants.DOM_NORMALIZE_CHARACTERS)) {
|
|
325 |
// true is not supported
|
|
326 |
return !value;
|
|
327 |
} else if (name.equalsIgnoreCase(Constants.DOM_ELEMENT_CONTENT_WHITESPACE)
|
|
328 |
|| name.equalsIgnoreCase(Constants.DOM_IGNORE_UNKNOWN_CHARACTER_DENORMALIZATIONS)) {
|
|
329 |
// false is not supported
|
|
330 |
return value;
|
|
331 |
}
|
|
332 |
} else if (name.equalsIgnoreCase(Constants.DOM_ERROR_HANDLER) &&
|
|
333 |
state == null || state instanceof DOMErrorHandler) {
|
|
334 |
return true;
|
|
335 |
}
|
|
336 |
|
|
337 |
return false;
|
|
338 |
}
|
|
339 |
|
|
340 |
/**
|
|
341 |
* DOM Level 3 Core CR - Experimental.
|
|
342 |
*
|
|
343 |
* The list of the parameters supported by this
|
|
344 |
* <code>DOMConfiguration</code> object and for which at least one value
|
|
345 |
* can be set by the application. Note that this list can also contain
|
|
346 |
* parameter names defined outside this specification.
|
|
347 |
*/
|
|
348 |
public DOMStringList getParameterNames() {
|
|
349 |
|
|
350 |
if (fRecognizedParameters == null){
|
|
351 |
Vector parameters = new Vector();
|
|
352 |
|
|
353 |
//Add DOM recognized parameters
|
|
354 |
//REVISIT: Would have been nice to have a list of
|
|
355 |
//recognized parameters.
|
|
356 |
parameters.add(Constants.DOM_NAMESPACES);
|
|
357 |
parameters.add(Constants.DOM_SPLIT_CDATA);
|
|
358 |
parameters.add(Constants.DOM_DISCARD_DEFAULT_CONTENT);
|
|
359 |
parameters.add(Constants.DOM_XMLDECL);
|
|
360 |
parameters.add(Constants.DOM_CANONICAL_FORM);
|
|
361 |
parameters.add(Constants.DOM_VALIDATE_IF_SCHEMA);
|
|
362 |
parameters.add(Constants.DOM_VALIDATE);
|
|
363 |
parameters.add(Constants.DOM_CHECK_CHAR_NORMALIZATION);
|
|
364 |
parameters.add(Constants.DOM_DATATYPE_NORMALIZATION);
|
|
365 |
parameters.add(Constants.DOM_FORMAT_PRETTY_PRINT);
|
|
366 |
//parameters.add(Constants.DOM_NORMALIZE_CHARACTERS);
|
|
367 |
parameters.add(Constants.DOM_WELLFORMED);
|
|
368 |
parameters.add(Constants.DOM_INFOSET);
|
|
369 |
parameters.add(Constants.DOM_NAMESPACE_DECLARATIONS);
|
|
370 |
parameters.add(Constants.DOM_ELEMENT_CONTENT_WHITESPACE);
|
|
371 |
parameters.add(Constants.DOM_ENTITIES);
|
|
372 |
parameters.add(Constants.DOM_CDATA_SECTIONS);
|
|
373 |
parameters.add(Constants.DOM_COMMENTS);
|
|
374 |
parameters.add(Constants.DOM_IGNORE_UNKNOWN_CHARACTER_DENORMALIZATIONS);
|
|
375 |
parameters.add(Constants.DOM_ERROR_HANDLER);
|
|
376 |
//parameters.add(Constants.DOM_SCHEMA_LOCATION);
|
|
377 |
//parameters.add(Constants.DOM_SCHEMA_TYPE);
|
|
378 |
|
|
379 |
//Add recognized xerces features and properties
|
|
380 |
|
|
381 |
fRecognizedParameters = new DOMStringListImpl(parameters);
|
|
382 |
|
|
383 |
}
|
|
384 |
|
|
385 |
return fRecognizedParameters;
|
|
386 |
}
|
|
387 |
|
|
388 |
/** DOM L3-EXPERIMENTAL:
|
|
389 |
* Getter for boolean and object parameters
|
|
390 |
*/
|
|
391 |
public Object getParameter(String name) throws DOMException {
|
|
392 |
|
|
393 |
if(name.equalsIgnoreCase(Constants.DOM_NORMALIZE_CHARACTERS)){
|
|
394 |
return null;
|
|
395 |
} else if (name.equalsIgnoreCase(Constants.DOM_COMMENTS)) {
|
|
396 |
return ((features & COMMENTS) != 0) ? Boolean.TRUE : Boolean.FALSE;
|
|
397 |
} else if (name.equalsIgnoreCase(Constants.DOM_NAMESPACES)) {
|
|
398 |
return (features & NAMESPACES) != 0 ? Boolean.TRUE : Boolean.FALSE;
|
|
399 |
} else if (name.equalsIgnoreCase(Constants.DOM_XMLDECL)) {
|
|
400 |
return (features & XMLDECL) != 0 ? Boolean.TRUE : Boolean.FALSE;
|
|
401 |
} else if (name.equalsIgnoreCase(Constants.DOM_CDATA_SECTIONS)) {
|
|
402 |
return (features & CDATA) != 0 ? Boolean.TRUE : Boolean.FALSE;
|
|
403 |
} else if (name.equalsIgnoreCase(Constants.DOM_ENTITIES)) {
|
|
404 |
return (features & ENTITIES) != 0 ? Boolean.TRUE : Boolean.FALSE;
|
|
405 |
} else if (name.equalsIgnoreCase(Constants.DOM_SPLIT_CDATA)) {
|
|
406 |
return (features & SPLITCDATA) != 0 ? Boolean.TRUE : Boolean.FALSE;
|
|
407 |
} else if (name.equalsIgnoreCase(Constants.DOM_WELLFORMED)) {
|
|
408 |
return (features & WELLFORMED) != 0 ? Boolean.TRUE : Boolean.FALSE;
|
|
409 |
} else if (name.equalsIgnoreCase(Constants.DOM_NAMESPACE_DECLARATIONS)) {
|
|
410 |
return (features & NSDECL) != 0 ? Boolean.TRUE : Boolean.FALSE;
|
|
411 |
} else if (name.equalsIgnoreCase(Constants.DOM_FORMAT_PRETTY_PRINT)) {
|
|
412 |
return (features & FORMAT_PRETTY_PRINT) != 0 ? Boolean.TRUE : Boolean.FALSE;
|
|
413 |
} else if (name.equalsIgnoreCase(Constants.DOM_ELEMENT_CONTENT_WHITESPACE) ||
|
|
414 |
name.equalsIgnoreCase(Constants.DOM_IGNORE_UNKNOWN_CHARACTER_DENORMALIZATIONS)) {
|
|
415 |
return Boolean.TRUE;
|
|
416 |
}else if (name.equalsIgnoreCase(Constants.DOM_DISCARD_DEFAULT_CONTENT)){
|
|
417 |
return ((features & DISCARDDEFAULT)!=0)?Boolean.TRUE:Boolean.FALSE;
|
|
418 |
}else if (name.equalsIgnoreCase(Constants.DOM_INFOSET)){
|
|
419 |
if ((features & ENTITIES) == 0 &&
|
|
420 |
(features & CDATA) == 0 &&
|
|
421 |
(features & NAMESPACES) != 0 &&
|
|
422 |
(features & NSDECL) != 0 &&
|
|
423 |
(features & WELLFORMED) != 0 &&
|
|
424 |
(features & COMMENTS) != 0) {
|
|
425 |
return Boolean.TRUE;
|
|
426 |
}
|
|
427 |
return Boolean.FALSE;
|
|
428 |
} else if (name.equalsIgnoreCase(Constants.DOM_CANONICAL_FORM)
|
|
429 |
|| name.equalsIgnoreCase(Constants.DOM_VALIDATE_IF_SCHEMA)
|
|
430 |
|| name.equalsIgnoreCase(Constants.DOM_CHECK_CHAR_NORMALIZATION)
|
|
431 |
|| name.equalsIgnoreCase(Constants.DOM_VALIDATE)
|
|
432 |
|| name.equalsIgnoreCase(Constants.DOM_VALIDATE_IF_SCHEMA)
|
|
433 |
|| name.equalsIgnoreCase(Constants.DOM_DATATYPE_NORMALIZATION)) {
|
|
434 |
return Boolean.FALSE;
|
|
435 |
} else if (name.equalsIgnoreCase(Constants.DOM_ERROR_HANDLER)) {
|
|
436 |
return fErrorHandler;
|
|
437 |
} else if (
|
|
438 |
name.equalsIgnoreCase(Constants.DOM_RESOURCE_RESOLVER)
|
|
439 |
|| name.equalsIgnoreCase(Constants.DOM_SCHEMA_LOCATION)
|
|
440 |
|| name.equalsIgnoreCase(Constants.DOM_SCHEMA_TYPE)) {
|
|
441 |
String msg =
|
|
442 |
DOMMessageFormatter.formatMessage(
|
|
443 |
DOMMessageFormatter.DOM_DOMAIN,
|
|
444 |
"FEATURE_NOT_SUPPORTED",
|
|
445 |
new Object[] { name });
|
|
446 |
throw new DOMException(DOMException.NOT_SUPPORTED_ERR, msg);
|
|
447 |
} else {
|
|
448 |
String msg =
|
|
449 |
DOMMessageFormatter.formatMessage(
|
|
450 |
DOMMessageFormatter.DOM_DOMAIN,
|
|
451 |
"FEATURE_NOT_FOUND",
|
|
452 |
new Object[] { name });
|
|
453 |
throw new DOMException(DOMException.NOT_FOUND_ERR, msg);
|
|
454 |
}
|
|
455 |
}
|
|
456 |
|
|
457 |
|
|
458 |
/**
|
|
459 |
* DOM L3 EXPERIMENTAL:
|
|
460 |
* Serialize the specified node as described above in the description of
|
|
461 |
* <code>LSSerializer</code>. The result of serializing the node is
|
|
462 |
* returned as a string. Writing a Document or Entity node produces a
|
|
463 |
* serialized form that is well formed XML. Writing other node types
|
|
464 |
* produces a fragment of text in a form that is not fully defined by
|
|
465 |
* this document, but that should be useful to a human for debugging or
|
|
466 |
* diagnostic purposes.
|
|
467 |
* @param wnode The node to be written.
|
|
468 |
* @return Returns the serialized data
|
|
469 |
* @exception DOMException
|
|
470 |
* DOMSTRING_SIZE_ERR: The resulting string is too long to fit in a
|
|
471 |
* <code>DOMString</code>.
|
|
472 |
* @exception LSException
|
|
473 |
* SERIALIZE_ERR: Unable to serialize the node. DOM applications should
|
|
474 |
* attach a <code>DOMErrorHandler</code> using the parameter
|
|
475 |
* "<i>error-handler</i>" to get details on error.
|
|
476 |
*/
|
|
477 |
public String writeToString(Node wnode) throws DOMException, LSException {
|
|
478 |
// determine which serializer to use:
|
|
479 |
Document doc = (wnode.getNodeType() == Node.DOCUMENT_NODE)?(Document)wnode:wnode.getOwnerDocument();
|
|
480 |
Method getVersion = null;
|
|
481 |
XMLSerializer ser = null;
|
|
482 |
String ver = null;
|
|
483 |
// this should run under JDK 1.1.8...
|
|
484 |
try {
|
|
485 |
getVersion = doc.getClass().getMethod("getXmlVersion", new Class[]{});
|
|
486 |
if(getVersion != null ) {
|
|
487 |
ver = (String)getVersion.invoke(doc, (Object[]) null);
|
|
488 |
}
|
|
489 |
} catch (Exception e) {
|
|
490 |
// no way to test the version...
|
|
491 |
// ignore the exception
|
|
492 |
}
|
|
493 |
if(ver != null && ver.equals("1.1")) {
|
|
494 |
if(xml11Serializer == null) {
|
|
495 |
xml11Serializer = new XML11Serializer();
|
|
496 |
initSerializer(xml11Serializer);
|
|
497 |
}
|
|
498 |
// copy setting from "main" serializer to XML 1.1 serializer
|
|
499 |
copySettings(serializer, xml11Serializer);
|
|
500 |
ser = xml11Serializer;
|
|
501 |
} else {
|
|
502 |
ser = serializer;
|
|
503 |
}
|
|
504 |
|
|
505 |
StringWriter destination = new StringWriter();
|
|
506 |
try {
|
|
507 |
prepareForSerialization(ser, wnode);
|
|
508 |
ser._format.setEncoding("UTF-16");
|
|
509 |
ser.setOutputCharStream(destination);
|
|
510 |
if (wnode.getNodeType() == Node.DOCUMENT_NODE) {
|
|
511 |
ser.serialize((Document)wnode);
|
|
512 |
}
|
|
513 |
else if (wnode.getNodeType() == Node.DOCUMENT_FRAGMENT_NODE) {
|
|
514 |
ser.serialize((DocumentFragment)wnode);
|
|
515 |
}
|
|
516 |
else if (wnode.getNodeType() == Node.ELEMENT_NODE) {
|
|
517 |
ser.serialize((Element)wnode);
|
|
518 |
}
|
|
519 |
else if (wnode.getNodeType() == Node.TEXT_NODE ||
|
|
520 |
wnode.getNodeType() == Node.COMMENT_NODE ||
|
|
521 |
wnode.getNodeType() == Node.ENTITY_REFERENCE_NODE ||
|
|
522 |
wnode.getNodeType() == Node.CDATA_SECTION_NODE ||
|
|
523 |
wnode.getNodeType() == Node.PROCESSING_INSTRUCTION_NODE ) {
|
|
524 |
ser.serialize(wnode);
|
|
525 |
}
|
|
526 |
else {
|
|
527 |
String msg = DOMMessageFormatter.formatMessage(
|
|
528 |
DOMMessageFormatter.SERIALIZER_DOMAIN,
|
|
529 |
"unable-to-serialize-node", null);
|
|
530 |
if (ser.fDOMErrorHandler != null) {
|
|
531 |
DOMErrorImpl error = new DOMErrorImpl();
|
|
532 |
error.fType = "unable-to-serialize-node";
|
|
533 |
error.fMessage = msg;
|
|
534 |
error.fSeverity = DOMError.SEVERITY_FATAL_ERROR;
|
|
535 |
ser.fDOMErrorHandler.handleError(error);
|
|
536 |
}
|
|
537 |
throw new LSException(LSException.SERIALIZE_ERR, msg);
|
|
538 |
}
|
|
539 |
} catch (LSException lse) {
|
|
540 |
// Rethrow LSException.
|
|
541 |
throw lse;
|
|
542 |
} catch (RuntimeException e) {
|
|
543 |
if (e == DOMNormalizer.abort){
|
|
544 |
// stopped at user request
|
|
545 |
return null;
|
|
546 |
}
|
|
547 |
throw (LSException) new LSException(LSException.SERIALIZE_ERR, e.toString()).initCause(e);
|
|
548 |
} catch (IOException ioe) {
|
|
549 |
// REVISIT: A generic IOException doesn't provide enough information
|
|
550 |
// to determine that the serialized document is too large to fit
|
|
551 |
// into a string. This could have thrown for some other reason. -- mrglavas
|
|
552 |
String msg = DOMMessageFormatter.formatMessage(
|
|
553 |
DOMMessageFormatter.DOM_DOMAIN,
|
|
554 |
"STRING_TOO_LONG",
|
|
555 |
new Object[] { ioe.getMessage()});
|
|
556 |
throw (DOMException) new DOMException(DOMException.DOMSTRING_SIZE_ERR, msg).initCause(ioe);
|
|
557 |
}
|
|
558 |
|
|
559 |
return destination.toString();
|
|
560 |
}
|
|
561 |
|
|
562 |
/**
|
|
563 |
* DOM L3 EXPERIMENTAL:
|
|
564 |
* The end-of-line sequence of characters to be used in the XML being
|
|
565 |
* written out. The only permitted values are these:
|
|
566 |
* <dl>
|
|
567 |
* <dt><code>null</code></dt>
|
|
568 |
* <dd>
|
|
569 |
* Use a default end-of-line sequence. DOM implementations should choose
|
|
570 |
* the default to match the usual convention for text files in the
|
|
571 |
* environment being used. Implementations must choose a default
|
|
572 |
* sequence that matches one of those allowed by 2.11 "End-of-Line
|
|
573 |
* Handling". </dd>
|
|
574 |
* <dt>CR</dt>
|
|
575 |
* <dd>The carriage-return character (#xD).</dd>
|
|
576 |
* <dt>CR-LF</dt>
|
|
577 |
* <dd> The
|
|
578 |
* carriage-return and line-feed characters (#xD #xA). </dd>
|
|
579 |
* <dt>LF</dt>
|
|
580 |
* <dd> The line-feed
|
|
581 |
* character (#xA). </dd>
|
|
582 |
* </dl>
|
|
583 |
* <br>The default value for this attribute is <code>null</code>.
|
|
584 |
*/
|
|
585 |
public void setNewLine(String newLine) {
|
|
586 |
serializer._format.setLineSeparator(newLine);
|
|
587 |
}
|
|
588 |
|
|
589 |
|
|
590 |
/**
|
|
591 |
* DOM L3 EXPERIMENTAL:
|
|
592 |
* The end-of-line sequence of characters to be used in the XML being
|
|
593 |
* written out. The only permitted values are these:
|
|
594 |
* <dl>
|
|
595 |
* <dt><code>null</code></dt>
|
|
596 |
* <dd>
|
|
597 |
* Use a default end-of-line sequence. DOM implementations should choose
|
|
598 |
* the default to match the usual convention for text files in the
|
|
599 |
* environment being used. Implementations must choose a default
|
|
600 |
* sequence that matches one of those allowed by 2.11 "End-of-Line
|
|
601 |
* Handling". </dd>
|
|
602 |
* <dt>CR</dt>
|
|
603 |
* <dd>The carriage-return character (#xD).</dd>
|
|
604 |
* <dt>CR-LF</dt>
|
|
605 |
* <dd> The
|
|
606 |
* carriage-return and line-feed characters (#xD #xA). </dd>
|
|
607 |
* <dt>LF</dt>
|
|
608 |
* <dd> The line-feed
|
|
609 |
* character (#xA). </dd>
|
|
610 |
* </dl>
|
|
611 |
* <br>The default value for this attribute is <code>null</code>.
|
|
612 |
*/
|
|
613 |
public String getNewLine() {
|
|
614 |
return serializer._format.getLineSeparator();
|
|
615 |
}
|
|
616 |
|
|
617 |
|
|
618 |
/**
|
|
619 |
* When the application provides a filter, the serializer will call out
|
|
620 |
* to the filter before serializing each Node. Attribute nodes are never
|
|
621 |
* passed to the filter. The filter implementation can choose to remove
|
|
622 |
* the node from the stream or to terminate the serialization early.
|
|
623 |
*/
|
|
624 |
public LSSerializerFilter getFilter(){
|
|
625 |
return serializer.fDOMFilter;
|
|
626 |
}
|
|
627 |
/**
|
|
628 |
* When the application provides a filter, the serializer will call out
|
|
629 |
* to the filter before serializing each Node. Attribute nodes are never
|
|
630 |
* passed to the filter. The filter implementation can choose to remove
|
|
631 |
* the node from the stream or to terminate the serialization early.
|
|
632 |
*/
|
|
633 |
public void setFilter(LSSerializerFilter filter){
|
|
634 |
serializer.fDOMFilter = filter;
|
|
635 |
}
|
|
636 |
|
|
637 |
// this initializes a newly-created serializer
|
|
638 |
private void initSerializer(XMLSerializer ser) {
|
|
639 |
ser.fNSBinder = new NamespaceSupport();
|
|
640 |
ser.fLocalNSBinder = new NamespaceSupport();
|
|
641 |
ser.fSymbolTable = new SymbolTable();
|
|
642 |
}
|
|
643 |
|
|
644 |
// copies all settings that could have been modified
|
|
645 |
// by calls to LSSerializer methods from one serializer to another.
|
|
646 |
// IMPORTANT: if new methods are implemented or more settings of
|
|
647 |
// the serializer are made alterable, this must be
|
|
648 |
// reflected in this method!
|
|
649 |
private void copySettings(XMLSerializer src, XMLSerializer dest) {
|
|
650 |
dest.fDOMErrorHandler = fErrorHandler;
|
|
651 |
dest._format.setEncoding(src._format.getEncoding());
|
|
652 |
dest._format.setLineSeparator(src._format.getLineSeparator());
|
|
653 |
dest.fDOMFilter = src.fDOMFilter;
|
|
654 |
}//copysettings
|
|
655 |
|
|
656 |
/**
|
|
657 |
* Serialize the specified node as described above in the general
|
|
658 |
* description of the <code>LSSerializer</code> interface. The output
|
|
659 |
* is written to the supplied <code>LSOutput</code>.
|
|
660 |
* <br> When writing to a <code>LSOutput</code>, the encoding is found by
|
|
661 |
* looking at the encoding information that is reachable through the
|
|
662 |
* <code>LSOutput</code> and the item to be written (or its owner
|
|
663 |
* document) in this order:
|
|
664 |
* <ol>
|
|
665 |
* <li> <code>LSOutput.encoding</code>,
|
|
666 |
* </li>
|
|
667 |
* <li>
|
|
668 |
* <code>Document.actualEncoding</code>,
|
|
669 |
* </li>
|
|
670 |
* <li>
|
|
671 |
* <code>Document.xmlEncoding</code>.
|
|
672 |
* </li>
|
|
673 |
* </ol>
|
|
674 |
* <br> If no encoding is reachable through the above properties, a
|
|
675 |
* default encoding of "UTF-8" will be used.
|
|
676 |
* <br> If the specified encoding is not supported an
|
|
677 |
* "unsupported-encoding" error is raised.
|
|
678 |
* <br> If no output is specified in the <code>LSOutput</code>, a
|
|
679 |
* "no-output-specified" error is raised.
|
|
680 |
* @param node The node to serialize.
|
|
681 |
* @param destination The destination for the serialized DOM.
|
|
682 |
* @return Returns <code>true</code> if <code>node</code> was
|
|
683 |
* successfully serialized and <code>false</code> in case the node
|
|
684 |
* couldn't be serialized.
|
|
685 |
*/
|
|
686 |
public boolean write(Node node, LSOutput destination) throws LSException{
|
|
687 |
|
|
688 |
if (node == null)
|
|
689 |
return false;
|
|
690 |
|
|
691 |
Method getVersion = null;
|
|
692 |
XMLSerializer ser = null;
|
|
693 |
String ver = null;
|
|
694 |
Document fDocument =(node.getNodeType() == Node.DOCUMENT_NODE)
|
|
695 |
? (Document) node
|
|
696 |
: node.getOwnerDocument();
|
|
697 |
// this should run under JDK 1.1.8...
|
|
698 |
try {
|
|
699 |
getVersion = fDocument.getClass().getMethod("getXmlVersion", new Class[] {});
|
|
700 |
if (getVersion != null) {
|
|
701 |
ver = (String) getVersion.invoke(fDocument, (Object[]) null);
|
|
702 |
}
|
|
703 |
} catch (Exception e) {
|
|
704 |
//no way to test the version...
|
|
705 |
//ignore the exception
|
|
706 |
}
|
|
707 |
//determine which serializer to use:
|
|
708 |
if (ver != null && ver.equals("1.1")) {
|
|
709 |
if (xml11Serializer == null) {
|
|
710 |
xml11Serializer = new XML11Serializer();
|
|
711 |
initSerializer(xml11Serializer);
|
|
712 |
}
|
|
713 |
//copy setting from "main" serializer to XML 1.1 serializer
|
|
714 |
copySettings(serializer, xml11Serializer);
|
|
715 |
ser = xml11Serializer;
|
|
716 |
} else {
|
|
717 |
ser = serializer;
|
|
718 |
}
|
|
719 |
|
|
720 |
String encoding = null;
|
|
721 |
if ((encoding = destination.getEncoding()) == null) {
|
|
722 |
try {
|
|
723 |
Method getEncoding =
|
|
724 |
fDocument.getClass().getMethod("getInputEncoding", new Class[] {});
|
|
725 |
if (getEncoding != null) {
|
|
726 |
encoding = (String) getEncoding.invoke(fDocument, (Object[]) null);
|
|
727 |
}
|
|
728 |
} catch (Exception e) {
|
|
729 |
// ignore the exception
|
|
730 |
}
|
|
731 |
if (encoding == null) {
|
|
732 |
try {
|
|
733 |
Method getEncoding =
|
|
734 |
fDocument.getClass().getMethod("getXmlEncoding", new Class[] {});
|
|
735 |
if (getEncoding != null) {
|
|
736 |
encoding = (String) getEncoding.invoke(fDocument, (Object[]) null);
|
|
737 |
}
|
|
738 |
} catch (Exception e) {
|
|
739 |
// ignore the exception
|
|
740 |
}
|
|
741 |
if (encoding == null) {
|
|
742 |
encoding = "UTF-8";
|
|
743 |
}
|
|
744 |
}
|
|
745 |
}
|
|
746 |
try {
|
|
747 |
prepareForSerialization(ser, node);
|
|
748 |
ser._format.setEncoding(encoding);
|
|
749 |
OutputStream outputStream = destination.getByteStream();
|
|
750 |
Writer writer = destination.getCharacterStream();
|
|
751 |
String uri = destination.getSystemId();
|
|
752 |
if (writer == null) {
|
|
753 |
if (outputStream == null) {
|
|
754 |
if (uri == null) {
|
|
755 |
String msg = DOMMessageFormatter.formatMessage(
|
|
756 |
DOMMessageFormatter.SERIALIZER_DOMAIN,
|
|
757 |
"no-output-specified", null);
|
|
758 |
if (ser.fDOMErrorHandler != null) {
|
|
759 |
DOMErrorImpl error = new DOMErrorImpl();
|
|
760 |
error.fType = "no-output-specified";
|
|
761 |
error.fMessage = msg;
|
|
762 |
error.fSeverity = DOMError.SEVERITY_FATAL_ERROR;
|
|
763 |
ser.fDOMErrorHandler.handleError(error);
|
|
764 |
}
|
|
765 |
throw new LSException(LSException.SERIALIZE_ERR, msg);
|
|
766 |
}
|
|
767 |
else {
|
|
768 |
// URI was specified. Handle relative URIs.
|
|
769 |
String expanded = XMLEntityManager.expandSystemId(uri, null, true);
|
|
770 |
URL url = new URL(expanded != null ? expanded : uri);
|
|
771 |
OutputStream out = null;
|
|
772 |
String protocol = url.getProtocol();
|
|
773 |
String host = url.getHost();
|
|
774 |
// Use FileOutputStream if this URI is for a local file.
|
|
775 |
if (protocol.equals("file")
|
|
776 |
&& (host == null || host.length() == 0 || host.equals("localhost"))) {
|
|
777 |
out = new FileOutputStream(getPathWithoutEscapes(url.getFile()));
|
|
778 |
}
|
|
779 |
// Try to write to some other kind of URI. Some protocols
|
|
780 |
// won't support this, though HTTP should work.
|
|
781 |
else {
|
|
782 |
URLConnection urlCon = url.openConnection();
|
|
783 |
urlCon.setDoInput(false);
|
|
784 |
urlCon.setDoOutput(true);
|
|
785 |
urlCon.setUseCaches(false); // Enable tunneling.
|
|
786 |
if (urlCon instanceof HttpURLConnection) {
|
|
787 |
// The DOM L3 LS CR says if we are writing to an HTTP URI
|
|
788 |
// it is to be done with an HTTP PUT.
|
|
789 |
HttpURLConnection httpCon = (HttpURLConnection) urlCon;
|
|
790 |
httpCon.setRequestMethod("PUT");
|
|
791 |
}
|
|
792 |
out = urlCon.getOutputStream();
|
|
793 |
}
|
|
794 |
ser.setOutputByteStream(out);
|
|
795 |
}
|
|
796 |
}
|
|
797 |
else {
|
|
798 |
// byte stream was specified
|
|
799 |
ser.setOutputByteStream(outputStream);
|
|
800 |
}
|
|
801 |
}
|
|
802 |
else {
|
|
803 |
// character stream is specified
|
|
804 |
ser.setOutputCharStream(writer);
|
|
805 |
}
|
|
806 |
|
|
807 |
if (node.getNodeType() == Node.DOCUMENT_NODE)
|
|
808 |
ser.serialize((Document) node);
|
|
809 |
else if (node.getNodeType() == Node.DOCUMENT_FRAGMENT_NODE)
|
|
810 |
ser.serialize((DocumentFragment) node);
|
|
811 |
else if (node.getNodeType() == Node.ELEMENT_NODE)
|
|
812 |
ser.serialize((Element) node);
|
|
813 |
else if (node.getNodeType() == Node.TEXT_NODE ||
|
|
814 |
node.getNodeType() == Node.COMMENT_NODE ||
|
|
815 |
node.getNodeType() == Node.ENTITY_REFERENCE_NODE ||
|
|
816 |
node.getNodeType() == Node.CDATA_SECTION_NODE ||
|
|
817 |
node.getNodeType() == Node.PROCESSING_INSTRUCTION_NODE ) {
|
|
818 |
ser.serialize(node);
|
|
819 |
}
|
|
820 |
else
|
|
821 |
return false;
|
|
822 |
} catch( UnsupportedEncodingException ue) {
|
|
823 |
if (ser.fDOMErrorHandler != null) {
|
|
824 |
DOMErrorImpl error = new DOMErrorImpl();
|
|
825 |
error.fException = ue;
|
|
826 |
error.fType = "unsupported-encoding";
|
|
827 |
error.fMessage = ue.getMessage();
|
|
828 |
error.fSeverity = DOMError.SEVERITY_FATAL_ERROR;
|
|
829 |
ser.fDOMErrorHandler.handleError(error);
|
|
830 |
}
|
|
831 |
throw new LSException(LSException.SERIALIZE_ERR,
|
|
832 |
DOMMessageFormatter.formatMessage(
|
|
833 |
DOMMessageFormatter.SERIALIZER_DOMAIN,
|
|
834 |
"unsupported-encoding", null));
|
|
835 |
//return false;
|
|
836 |
} catch (LSException lse) {
|
|
837 |
// Rethrow LSException.
|
|
838 |
throw lse;
|
|
839 |
} catch (RuntimeException e) {
|
|
840 |
if (e == DOMNormalizer.abort){
|
|
841 |
// stopped at user request
|
|
842 |
return false;
|
|
843 |
}
|
|
844 |
throw (LSException) DOMUtil.createLSException(LSException.SERIALIZE_ERR, e).fillInStackTrace();
|
|
845 |
} catch (Exception e) {
|
|
846 |
if (ser.fDOMErrorHandler != null) {
|
|
847 |
DOMErrorImpl error = new DOMErrorImpl();
|
|
848 |
error.fException = e;
|
|
849 |
error.fMessage = e.getMessage();
|
|
850 |
error.fSeverity = DOMError.SEVERITY_ERROR;
|
|
851 |
ser.fDOMErrorHandler.handleError(error);
|
|
852 |
|
|
853 |
}
|
|
854 |
e.printStackTrace();
|
|
855 |
throw (LSException) DOMUtil.createLSException(LSException.SERIALIZE_ERR, e).fillInStackTrace();
|
|
856 |
}
|
|
857 |
return true;
|
|
858 |
|
|
859 |
} //write
|
|
860 |
|
|
861 |
/**
|
|
862 |
* Serialize the specified node as described above in the general
|
|
863 |
* description of the <code>LSSerializer</code> interface. The output
|
|
864 |
* is written to the supplied URI.
|
|
865 |
* <br> When writing to a URI, the encoding is found by looking at the
|
|
866 |
* encoding information that is reachable through the item to be written
|
|
867 |
* (or its owner document) in this order:
|
|
868 |
* <ol>
|
|
869 |
* <li>
|
|
870 |
* <code>Document.inputEncoding</code>,
|
|
871 |
* </li>
|
|
872 |
* <li>
|
|
873 |
* <code>Document.xmlEncoding</code>.
|
|
874 |
* </li>
|
|
875 |
* </ol>
|
|
876 |
* <br> If no encoding is reachable through the above properties, a
|
|
877 |
* default encoding of "UTF-8" will be used.
|
|
878 |
* <br> If the specified encoding is not supported an
|
|
879 |
* "unsupported-encoding" error is raised.
|
|
880 |
* @param node The node to serialize.
|
|
881 |
* @param URI The URI to write to.
|
|
882 |
* @return Returns <code>true</code> if <code>node</code> was
|
|
883 |
* successfully serialized and <code>false</code> in case the node
|
|
884 |
* couldn't be serialized.
|
|
885 |
*/
|
|
886 |
public boolean writeToURI(Node node, String URI) throws LSException{
|
|
887 |
if (node == null){
|
|
888 |
return false;
|
|
889 |
}
|
|
890 |
|
|
891 |
Method getXmlVersion = null;
|
|
892 |
XMLSerializer ser = null;
|
|
893 |
String ver = null;
|
|
894 |
String encoding = null;
|
|
895 |
|
|
896 |
Document fDocument =(node.getNodeType() == Node.DOCUMENT_NODE)
|
|
897 |
? (Document) node
|
|
898 |
: node.getOwnerDocument();
|
|
899 |
// this should run under JDK 1.1.8...
|
|
900 |
try {
|
|
901 |
getXmlVersion =
|
|
902 |
fDocument.getClass().getMethod("getXmlVersion", new Class[] {});
|
|
903 |
if (getXmlVersion != null) {
|
|
904 |
ver = (String) getXmlVersion.invoke(fDocument, (Object[]) null);
|
|
905 |
}
|
|
906 |
} catch (Exception e) {
|
|
907 |
// no way to test the version...
|
|
908 |
// ignore the exception
|
|
909 |
}
|
|
910 |
if (ver != null && ver.equals("1.1")) {
|
|
911 |
if (xml11Serializer == null) {
|
|
912 |
xml11Serializer = new XML11Serializer();
|
|
913 |
initSerializer(xml11Serializer);
|
|
914 |
}
|
|
915 |
// copy setting from "main" serializer to XML 1.1 serializer
|
|
916 |
copySettings(serializer, xml11Serializer);
|
|
917 |
ser = xml11Serializer;
|
|
918 |
} else {
|
|
919 |
ser = serializer;
|
|
920 |
}
|
|
921 |
|
|
922 |
try {
|
|
923 |
Method getEncoding =
|
|
924 |
fDocument.getClass().getMethod("getInputEncoding", new Class[] {});
|
|
925 |
if (getEncoding != null) {
|
|
926 |
encoding = (String) getEncoding.invoke(fDocument, (Object[]) null);
|
|
927 |
}
|
|
928 |
} catch (Exception e) {
|
|
929 |
// ignore the exception
|
|
930 |
}
|
|
931 |
if (encoding == null) {
|
|
932 |
try {
|
|
933 |
Method getEncoding =
|
|
934 |
fDocument.getClass().getMethod("getXmlEncoding", new Class[] {});
|
|
935 |
if (getEncoding != null) {
|
|
936 |
encoding = (String) getEncoding.invoke(fDocument, (Object[]) null);
|
|
937 |
}
|
|
938 |
} catch (Exception e) {
|
|
939 |
// ignore the exception
|
|
940 |
}
|
|
941 |
if (encoding == null) {
|
|
942 |
encoding = "UTF-8";
|
|
943 |
}
|
|
944 |
}
|
|
945 |
|
|
946 |
try {
|
|
947 |
prepareForSerialization(ser, node);
|
|
948 |
ser._format.setEncoding(encoding);
|
|
949 |
|
|
950 |
// URI was specified. Handle relative URIs.
|
|
951 |
String expanded = XMLEntityManager.expandSystemId(URI, null, true);
|
|
952 |
URL url = new URL(expanded != null ? expanded : URI);
|
|
953 |
OutputStream out = null;
|
|
954 |
String protocol = url.getProtocol();
|
|
955 |
String host = url.getHost();
|
|
956 |
// Use FileOutputStream if this URI is for a local file.
|
|
957 |
if (protocol.equals("file")
|
|
958 |
&& (host == null || host.length() == 0 || host.equals("localhost"))) {
|
|
959 |
out = new FileOutputStream(getPathWithoutEscapes(url.getFile()));
|
|
960 |
}
|
|
961 |
// Try to write to some other kind of URI. Some protocols
|
|
962 |
// won't support this, though HTTP should work.
|
|
963 |
else {
|
|
964 |
URLConnection urlCon = url.openConnection();
|
|
965 |
urlCon.setDoInput(false);
|
|
966 |
urlCon.setDoOutput(true);
|
|
967 |
urlCon.setUseCaches(false); // Enable tunneling.
|
|
968 |
if (urlCon instanceof HttpURLConnection) {
|
|
969 |
// The DOM L3 LS CR says if we are writing to an HTTP URI
|
|
970 |
// it is to be done with an HTTP PUT.
|
|
971 |
HttpURLConnection httpCon = (HttpURLConnection) urlCon;
|
|
972 |
httpCon.setRequestMethod("PUT");
|
|
973 |
}
|
|
974 |
out = urlCon.getOutputStream();
|
|
975 |
}
|
|
976 |
ser.setOutputByteStream(out);
|
|
977 |
|
|
978 |
if (node.getNodeType() == Node.DOCUMENT_NODE)
|
|
979 |
ser.serialize((Document) node);
|
|
980 |
else if (node.getNodeType() == Node.DOCUMENT_FRAGMENT_NODE)
|
|
981 |
ser.serialize((DocumentFragment) node);
|
|
982 |
else if (node.getNodeType() == Node.ELEMENT_NODE)
|
|
983 |
ser.serialize((Element) node);
|
|
984 |
else if (node.getNodeType() == Node.TEXT_NODE ||
|
|
985 |
node.getNodeType() == Node.COMMENT_NODE ||
|
|
986 |
node.getNodeType() == Node.ENTITY_REFERENCE_NODE ||
|
|
987 |
node.getNodeType() == Node.CDATA_SECTION_NODE ||
|
|
988 |
node.getNodeType() == Node.PROCESSING_INSTRUCTION_NODE ) {
|
|
989 |
ser.serialize(node);
|
|
990 |
}
|
|
991 |
else
|
|
992 |
return false;
|
|
993 |
} catch (LSException lse) {
|
|
994 |
// Rethrow LSException.
|
|
995 |
throw lse;
|
|
996 |
} catch (RuntimeException e) {
|
|
997 |
if (e == DOMNormalizer.abort){
|
|
998 |
// stopped at user request
|
|
999 |
return false;
|
|
1000 |
}
|
|
1001 |
throw (LSException) DOMUtil.createLSException(LSException.SERIALIZE_ERR, e).fillInStackTrace();
|
|
1002 |
} catch (Exception e) {
|
|
1003 |
if (ser.fDOMErrorHandler != null) {
|
|
1004 |
DOMErrorImpl error = new DOMErrorImpl();
|
|
1005 |
error.fException = e;
|
|
1006 |
error.fMessage = e.getMessage();
|
|
1007 |
error.fSeverity = DOMError.SEVERITY_ERROR;
|
|
1008 |
ser.fDOMErrorHandler.handleError(error);
|
|
1009 |
}
|
|
1010 |
throw (LSException) DOMUtil.createLSException(LSException.SERIALIZE_ERR, e).fillInStackTrace();
|
|
1011 |
}
|
|
1012 |
return true;
|
|
1013 |
} //writeURI
|
|
1014 |
|
|
1015 |
|
|
1016 |
//
|
|
1017 |
// Private methods
|
|
1018 |
//
|
|
1019 |
|
|
1020 |
private void prepareForSerialization(XMLSerializer ser, Node node) {
|
|
1021 |
ser.reset();
|
|
1022 |
ser.features = features;
|
|
1023 |
ser.fDOMErrorHandler = fErrorHandler;
|
|
1024 |
ser.fNamespaces = (features & NAMESPACES) != 0;
|
|
1025 |
ser.fNamespacePrefixes = (features & NSDECL) != 0;
|
|
1026 |
ser._format.setOmitComments((features & COMMENTS)==0);
|
|
1027 |
ser._format.setOmitXMLDeclaration((features & XMLDECL) == 0);
|
|
1028 |
ser._format.setIndenting((features & FORMAT_PRETTY_PRINT) != 0);
|
|
1029 |
|
|
1030 |
if ((features & WELLFORMED) != 0) {
|
|
1031 |
// REVISIT: this is inefficient implementation of well-formness. Instead, we should check
|
|
1032 |
// well-formness as we serialize the tree
|
|
1033 |
Node next, root;
|
|
1034 |
root = node;
|
|
1035 |
Method versionChanged;
|
|
1036 |
boolean verifyNames = true;
|
|
1037 |
Document document =(node.getNodeType() == Node.DOCUMENT_NODE)
|
|
1038 |
? (Document) node
|
|
1039 |
: node.getOwnerDocument();
|
|
1040 |
try {
|
|
1041 |
versionChanged = document.getClass().getMethod("isXMLVersionChanged()", new Class[] {});
|
|
1042 |
if (versionChanged != null) {
|
|
1043 |
verifyNames = ((Boolean)versionChanged.invoke(document, (Object[]) null)).booleanValue();
|
|
1044 |
}
|
|
1045 |
} catch (Exception e) {
|
|
1046 |
//no way to test the version...
|
|
1047 |
//ignore the exception
|
|
1048 |
}
|
|
1049 |
if (node.getFirstChild() != null) {
|
|
1050 |
while (node != null) {
|
|
1051 |
verify(node, verifyNames, false);
|
|
1052 |
// Move down to first child
|
|
1053 |
next = node.getFirstChild();
|
|
1054 |
// No child nodes, so walk tree
|
|
1055 |
while (next == null) {
|
|
1056 |
// Move to sibling if possible.
|
|
1057 |
next = node.getNextSibling();
|
|
1058 |
if (next == null) {
|
|
1059 |
node = node.getParentNode();
|
|
1060 |
if (root == node){
|
|
1061 |
next = null;
|
|
1062 |
break;
|
|
1063 |
}
|
|
1064 |
next = node.getNextSibling();
|
|
1065 |
}
|
|
1066 |
}
|
|
1067 |
node = next;
|
|
1068 |
}
|
|
1069 |
}
|
|
1070 |
else {
|
|
1071 |
verify(node, verifyNames, false);
|
|
1072 |
}
|
|
1073 |
}
|
|
1074 |
}
|
|
1075 |
|
|
1076 |
|
|
1077 |
private void verify (Node node, boolean verifyNames, boolean xml11Version){
|
|
1078 |
|
|
1079 |
int type = node.getNodeType();
|
|
1080 |
fLocator.fRelatedNode = node;
|
|
1081 |
boolean wellformed;
|
|
1082 |
switch (type) {
|
|
1083 |
case Node.DOCUMENT_NODE:{
|
|
1084 |
break;
|
|
1085 |
}
|
|
1086 |
case Node.DOCUMENT_TYPE_NODE:{
|
|
1087 |
break;
|
|
1088 |
}
|
|
1089 |
case Node.ELEMENT_NODE:{
|
|
1090 |
if (verifyNames){
|
|
1091 |
if((features & NAMESPACES) != 0){
|
|
1092 |
wellformed = CoreDocumentImpl.isValidQName(node.getPrefix() , node.getLocalName(), xml11Version) ;
|
|
1093 |
}
|
|
1094 |
else{
|
|
1095 |
wellformed = CoreDocumentImpl.isXMLName(node.getNodeName() , xml11Version);
|
|
1096 |
}
|
|
1097 |
if (!wellformed){
|
|
1098 |
if (!wellformed){
|
|
1099 |
if (fErrorHandler != null) {
|
|
1100 |
String msg = DOMMessageFormatter.formatMessage(
|
|
1101 |
DOMMessageFormatter.DOM_DOMAIN,
|
|
1102 |
"wf-invalid-character-in-node-name",
|
|
1103 |
new Object[]{"Element", node.getNodeName()});
|
|
1104 |
DOMNormalizer.reportDOMError(fErrorHandler, fError, fLocator, msg, DOMError.SEVERITY_FATAL_ERROR,
|
|
1105 |
"wf-invalid-character-in-node-name");
|
|
1106 |
}
|
|
1107 |
|
|
1108 |
}
|
|
1109 |
}
|
|
1110 |
}
|
|
1111 |
|
|
1112 |
NamedNodeMap attributes = (node.hasAttributes()) ? node.getAttributes() : null;
|
|
1113 |
if (attributes != null) {
|
|
1114 |
for (int i = 0; i < attributes.getLength(); ++i) {
|
|
1115 |
Attr attr = (Attr) attributes.item(i);
|
|
1116 |
fLocator.fRelatedNode = attr;
|
|
1117 |
DOMNormalizer.isAttrValueWF( fErrorHandler, fError, fLocator,
|
|
1118 |
attributes, attr, attr.getValue(), xml11Version);
|
|
1119 |
if (verifyNames) {
|
|
1120 |
wellformed = CoreDocumentImpl.isXMLName( attr.getNodeName(), xml11Version);
|
|
1121 |
if (!wellformed) {
|
|
1122 |
String msg =
|
|
1123 |
DOMMessageFormatter.formatMessage(
|
|
1124 |
DOMMessageFormatter.DOM_DOMAIN,
|
|
1125 |
"wf-invalid-character-in-node-name",
|
|
1126 |
new Object[] { "Attr", node.getNodeName()});
|
|
1127 |
DOMNormalizer.reportDOMError( fErrorHandler, fError, fLocator, msg, DOMError.SEVERITY_FATAL_ERROR,
|
|
1128 |
"wf-invalid-character-in-node-name");
|
|
1129 |
}
|
|
1130 |
}
|
|
1131 |
}
|
|
1132 |
|
|
1133 |
}
|
|
1134 |
|
|
1135 |
break;
|
|
1136 |
}
|
|
1137 |
|
|
1138 |
case Node.COMMENT_NODE: {
|
|
1139 |
// only verify well-formness if comments included in the tree
|
|
1140 |
if ((features & COMMENTS) != 0)
|
|
1141 |
DOMNormalizer.isCommentWF(fErrorHandler, fError, fLocator, ((Comment)node).getData(), xml11Version);
|
|
1142 |
break;
|
|
1143 |
}
|
|
1144 |
case Node.ENTITY_REFERENCE_NODE: {
|
|
1145 |
// only if entity is preserved in the tree
|
|
1146 |
if (verifyNames && (features & ENTITIES) != 0){
|
|
1147 |
CoreDocumentImpl.isXMLName(node.getNodeName() , xml11Version);
|
|
1148 |
}
|
|
1149 |
break;
|
|
1150 |
|
|
1151 |
}
|
|
1152 |
case Node.CDATA_SECTION_NODE: {
|
|
1153 |
// verify content
|
|
1154 |
DOMNormalizer.isXMLCharWF(fErrorHandler, fError, fLocator, node.getNodeValue(), xml11Version);
|
|
1155 |
// the ]]> string will be checked during serialization
|
|
1156 |
break;
|
|
1157 |
}
|
|
1158 |
case Node.TEXT_NODE:{
|
|
1159 |
DOMNormalizer.isXMLCharWF(fErrorHandler, fError, fLocator, node.getNodeValue(), xml11Version);
|
|
1160 |
break;
|
|
1161 |
}
|
|
1162 |
case Node.PROCESSING_INSTRUCTION_NODE:{
|
|
1163 |
ProcessingInstruction pinode = (ProcessingInstruction)node ;
|
|
1164 |
String target = pinode.getTarget();
|
|
1165 |
if (verifyNames) {
|
|
1166 |
if (xml11Version) {
|
|
1167 |
wellformed = XML11Char.isXML11ValidName(target);
|
|
1168 |
} else {
|
|
1169 |
wellformed = XMLChar.isValidName(target);
|
|
1170 |
}
|
|
1171 |
|
|
1172 |
if (!wellformed) {
|
|
1173 |
String msg =
|
|
1174 |
DOMMessageFormatter.formatMessage(
|
|
1175 |
DOMMessageFormatter.DOM_DOMAIN,
|
|
1176 |
"wf-invalid-character-in-node-name",
|
|
1177 |
new Object[] { "Element", node.getNodeName()});
|
|
1178 |
DOMNormalizer.reportDOMError(
|
|
1179 |
fErrorHandler,
|
|
1180 |
fError,
|
|
1181 |
fLocator,
|
|
1182 |
msg,
|
|
1183 |
DOMError.SEVERITY_FATAL_ERROR,
|
|
1184 |
"wf-invalid-character-in-node-name");
|
|
1185 |
}
|
|
1186 |
}
|
|
1187 |
DOMNormalizer.isXMLCharWF(fErrorHandler, fError, fLocator, pinode.getData(), xml11Version);
|
|
1188 |
break;
|
|
1189 |
}
|
|
1190 |
}
|
|
1191 |
|
|
1192 |
}
|
|
1193 |
|
|
1194 |
private String getPathWithoutEscapes(String origPath) {
|
|
1195 |
if (origPath != null && origPath.length() != 0 && origPath.indexOf('%') != -1) {
|
|
1196 |
// Locate the escape characters
|
|
1197 |
StringTokenizer tokenizer = new StringTokenizer(origPath, "%");
|
|
1198 |
StringBuffer result = new StringBuffer(origPath.length());
|
|
1199 |
int size = tokenizer.countTokens();
|
|
1200 |
result.append(tokenizer.nextToken());
|
|
1201 |
for(int i = 1; i < size; ++i) {
|
|
1202 |
String token = tokenizer.nextToken();
|
|
1203 |
// Decode the 2 digit hexadecimal number following % in '%nn'
|
|
1204 |
result.append((char)Integer.valueOf(token.substring(0, 2), 16).intValue());
|
|
1205 |
result.append(token.substring(2));
|
|
1206 |
}
|
|
1207 |
return result.toString();
|
|
1208 |
}
|
|
1209 |
return origPath;
|
|
1210 |
}
|
|
1211 |
|
|
1212 |
}//DOMSerializerImpl
|