|
|
|
|
|
*/ |
|
/* |
|
* Licensed to the Apache Software Foundation (ASF) under one |
|
* or more contributor license agreements. See the NOTICE file |
|
* distributed with this work for additional information |
|
* regarding copyright ownership. The ASF licenses this file |
|
* to you under the Apache License, Version 2.0 (the "License"); |
|
* you may not use this file except in compliance with the License. |
|
* You may obtain a copy of the License at |
|
* |
|
* http://www.apache.org/licenses/LICENSE-2.0 |
|
* |
|
* Unless required by applicable law or agreed to in writing, software |
|
* distributed under the License is distributed on an "AS IS" BASIS, |
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
* See the License for the specific language governing permissions and |
|
* limitations under the License. |
|
*/ |
|
|
|
package com.sun.org.apache.xml.internal.serializer.dom3; |
|
|
|
import java.io.File; |
|
import java.io.FileOutputStream; |
|
import java.io.OutputStream; |
|
import java.io.OutputStreamWriter; |
|
import java.io.StringWriter; |
|
import java.io.UnsupportedEncodingException; |
|
import java.io.Writer; |
|
import java.net.HttpURLConnection; |
|
import java.net.URL; |
|
import java.net.URLConnection; |
|
import java.util.Properties; |
|
import jdk.xml.internal.SecuritySupport; |
|
import com.sun.org.apache.xml.internal.serializer.DOM3Serializer; |
|
import com.sun.org.apache.xml.internal.serializer.Encodings; |
|
import com.sun.org.apache.xml.internal.serializer.Serializer; |
|
import com.sun.org.apache.xml.internal.serializer.ToXMLStream; |
|
import com.sun.org.apache.xml.internal.serializer.OutputPropertiesFactory; |
|
import com.sun.org.apache.xml.internal.serializer.utils.MsgKey; |
|
import com.sun.org.apache.xml.internal.serializer.utils.Utils; |
|
import com.sun.org.apache.xml.internal.serializer.utils.SystemIDResolver; |
|
import jdk.xml.internal.JdkConstants; |
|
import jdk.xml.internal.JdkProperty; |
|
import jdk.xml.internal.JdkProperty.ImplPropMap; |
|
import jdk.xml.internal.JdkProperty.State; |
|
import org.w3c.dom.DOMConfiguration; |
|
import org.w3c.dom.DOMError; |
|
import org.w3c.dom.DOMErrorHandler; |
|
import org.w3c.dom.DOMException; |
|
import org.w3c.dom.DOMStringList; |
|
import org.w3c.dom.Document; |
|
import org.w3c.dom.Node; |
|
import org.w3c.dom.ls.LSException; |
|
import org.w3c.dom.ls.LSOutput; |
|
import org.w3c.dom.ls.LSSerializer; |
|
import org.w3c.dom.ls.LSSerializerFilter; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
final public class LSSerializerImpl implements DOMConfiguration, LSSerializer { |
|
|
|
|
|
private Serializer fXMLSerializer = null; |
|
|
|
|
|
protected int fFeatures = 0; |
|
|
|
|
|
private DOM3Serializer fDOMSerializer = null; |
|
|
|
|
|
private LSSerializerFilter fSerializerFilter = null; |
|
|
|
|
|
private Node fVisitedNode = null; |
|
|
|
|
|
private String fEndOfLine = "\n"; |
|
|
|
|
|
private DOMErrorHandler fDOMErrorHandler = null; |
|
|
|
|
|
private Properties fDOMConfigProperties = null; |
|
|
|
|
|
private String fEncoding; |
|
|
|
|
|
private JdkProperty<Boolean> fIsStandalone; |
|
|
|
// ************************************************************************ |
|
// DOM Level 3 DOM Configuration parameter names |
|
// ************************************************************************ |
|
|
|
private final static int CANONICAL = 0x1 << 0; |
|
|
|
|
|
private final static int CDATA = 0x1 << 1; |
|
|
|
|
|
private final static int CHARNORMALIZE = 0x1 << 2; |
|
|
|
|
|
private final static int COMMENTS = 0x1 << 3; |
|
|
|
|
|
private final static int DTNORMALIZE = 0x1 << 4; |
|
|
|
|
|
private final static int ELEM_CONTENT_WHITESPACE = 0x1 << 5; |
|
|
|
|
|
private final static int ENTITIES = 0x1 << 6; |
|
|
|
|
|
private final static int INFOSET = 0x1 << 7; |
|
|
|
|
|
private final static int NAMESPACES = 0x1 << 8; |
|
|
|
|
|
private final static int NAMESPACEDECLS = 0x1 << 9; |
|
|
|
|
|
private final static int NORMALIZECHARS = 0x1 << 10; |
|
|
|
|
|
private final static int SPLITCDATA = 0x1 << 11; |
|
|
|
|
|
private final static int VALIDATE = 0x1 << 12; |
|
|
|
|
|
private final static int SCHEMAVALIDATE = 0x1 << 13; |
|
|
|
|
|
private final static int WELLFORMED = 0x1 << 14; |
|
|
|
// Parameter discard-default-content, true [required] (default) |
|
|
|
private final static int DISCARDDEFAULT = 0x1 << 15; |
|
|
|
|
|
private final static int PRETTY_PRINT = 0x1 << 16; |
|
|
|
// Parameter ignore-unknown-character-denormalizations, true [required] (default) |
|
|
|
private final static int IGNORE_CHAR_DENORMALIZE = 0x1 << 17; |
|
|
|
|
|
private final static int XMLDECL = 0x1 << 18; |
|
|
|
|
|
private final static int IS_STANDALONE = 0x1 << 19; |
|
|
|
// ************************************************************************ |
|
|
|
|
|
@SuppressWarnings("deprecation") |
|
private String fRecognizedParameters [] = { |
|
DOMConstants.DOM_CANONICAL_FORM, |
|
DOMConstants.DOM_CDATA_SECTIONS, |
|
DOMConstants.DOM_CHECK_CHAR_NORMALIZATION, |
|
DOMConstants.DOM_COMMENTS, |
|
DOMConstants.DOM_DATATYPE_NORMALIZATION, |
|
DOMConstants.DOM_ELEMENT_CONTENT_WHITESPACE, |
|
DOMConstants.DOM_ENTITIES, |
|
DOMConstants.DOM_INFOSET, |
|
DOMConstants.DOM_NAMESPACES, |
|
DOMConstants.DOM_NAMESPACE_DECLARATIONS, |
|
|
|
DOMConstants.DOM_SPLIT_CDATA, |
|
DOMConstants.DOM_VALIDATE, |
|
DOMConstants.DOM_VALIDATE_IF_SCHEMA, |
|
DOMConstants.DOM_WELLFORMED, |
|
DOMConstants.DOM_DISCARD_DEFAULT_CONTENT, |
|
DOMConstants.DOM_FORMAT_PRETTY_PRINT, |
|
DOMConstants.DOM_IGNORE_UNKNOWN_CHARACTER_DENORMALIZATIONS, |
|
DOMConstants.DOM_XMLDECL, |
|
JdkConstants.FQ_IS_STANDALONE, |
|
JdkConstants.SP_IS_STANDALONE, |
|
DOMConstants.DOM_ERROR_HANDLER |
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public LSSerializerImpl () { |
|
|
|
fFeatures |= CDATA; |
|
fFeatures |= COMMENTS; |
|
fFeatures |= ELEM_CONTENT_WHITESPACE; |
|
fFeatures |= ENTITIES; |
|
fFeatures |= NAMESPACES; |
|
fFeatures |= NAMESPACEDECLS; |
|
fFeatures |= SPLITCDATA; |
|
fFeatures |= WELLFORMED; |
|
fFeatures |= DISCARDDEFAULT; |
|
fFeatures |= XMLDECL; |
|
|
|
|
|
fDOMConfigProperties = new Properties(); |
|
|
|
|
|
initializeSerializerProps(); |
|
|
|
|
|
Properties configProps = OutputPropertiesFactory.getDefaultMethodProperties("xml"); |
|
|
|
// change xml version from 1.0 to 1.1 |
|
//configProps.setProperty("version", "1.1"); |
|
|
|
// Get a serializer that seriailizes according to the properties, |
|
|
|
fXMLSerializer = new ToXMLStream(null); |
|
fXMLSerializer.setOutputFormat(configProps); |
|
|
|
|
|
fXMLSerializer.setOutputFormat(fDOMConfigProperties); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public void initializeSerializerProps () { |
|
|
|
fDOMConfigProperties.setProperty(DOMConstants.S_DOM3_PROPERTIES_NS |
|
+ DOMConstants.DOM_CANONICAL_FORM, DOMConstants.DOM3_DEFAULT_FALSE); |
|
|
|
|
|
fDOMConfigProperties.setProperty(DOMConstants.S_DOM3_PROPERTIES_NS |
|
+ DOMConstants.DOM_CDATA_SECTIONS, DOMConstants.DOM3_DEFAULT_TRUE); |
|
|
|
|
|
fDOMConfigProperties.setProperty(DOMConstants.S_DOM3_PROPERTIES_NS |
|
+ DOMConstants.DOM_CHECK_CHAR_NORMALIZATION, |
|
DOMConstants.DOM3_DEFAULT_FALSE); |
|
|
|
|
|
fDOMConfigProperties.setProperty(DOMConstants.S_DOM3_PROPERTIES_NS |
|
+ DOMConstants.DOM_COMMENTS, DOMConstants.DOM3_DEFAULT_TRUE); |
|
|
|
|
|
fDOMConfigProperties.setProperty(DOMConstants.S_DOM3_PROPERTIES_NS |
|
+ DOMConstants.DOM_DATATYPE_NORMALIZATION, |
|
DOMConstants.DOM3_DEFAULT_FALSE); |
|
|
|
|
|
fDOMConfigProperties.setProperty(DOMConstants.S_DOM3_PROPERTIES_NS |
|
+ DOMConstants.DOM_ELEMENT_CONTENT_WHITESPACE, |
|
DOMConstants.DOM3_DEFAULT_TRUE); |
|
|
|
|
|
fDOMConfigProperties.setProperty(DOMConstants.S_DOM3_PROPERTIES_NS |
|
+ DOMConstants.DOM_ENTITIES, DOMConstants.DOM3_DEFAULT_TRUE); |
|
|
|
// error-handler |
|
// Should we set our default ErrorHandler |
|
/* |
|
* if (fDOMConfig.getParameter(Constants.DOM_ERROR_HANDLER) != null) { |
|
* fDOMErrorHandler = |
|
* (DOMErrorHandler)fDOMConfig.getParameter(Constants.DOM_ERROR_HANDLER); } |
|
*/ |
|
|
|
|
|
if ((fFeatures & INFOSET) != 0) { |
|
fDOMConfigProperties.setProperty(DOMConstants.S_DOM3_PROPERTIES_NS |
|
+ DOMConstants.DOM_NAMESPACES, DOMConstants.DOM3_DEFAULT_TRUE); |
|
fDOMConfigProperties.setProperty(DOMConstants.S_DOM3_PROPERTIES_NS |
|
+ DOMConstants.DOM_NAMESPACE_DECLARATIONS, |
|
DOMConstants.DOM3_DEFAULT_TRUE); |
|
fDOMConfigProperties.setProperty(DOMConstants.S_DOM3_PROPERTIES_NS |
|
+ DOMConstants.DOM_COMMENTS, DOMConstants.DOM3_DEFAULT_TRUE); |
|
fDOMConfigProperties.setProperty(DOMConstants.S_DOM3_PROPERTIES_NS |
|
+ DOMConstants.DOM_ELEMENT_CONTENT_WHITESPACE, |
|
DOMConstants.DOM3_DEFAULT_TRUE); |
|
fDOMConfigProperties.setProperty(DOMConstants.S_DOM3_PROPERTIES_NS |
|
+ DOMConstants.DOM_WELLFORMED, DOMConstants.DOM3_DEFAULT_TRUE); |
|
fDOMConfigProperties.setProperty(DOMConstants.S_DOM3_PROPERTIES_NS |
|
+ DOMConstants.DOM_ENTITIES, DOMConstants.DOM3_DEFAULT_FALSE); |
|
fDOMConfigProperties.setProperty(DOMConstants.S_DOM3_PROPERTIES_NS |
|
+ DOMConstants.DOM_CDATA_SECTIONS, |
|
DOMConstants.DOM3_DEFAULT_FALSE); |
|
fDOMConfigProperties.setProperty(DOMConstants.S_DOM3_PROPERTIES_NS |
|
+ DOMConstants.DOM_VALIDATE_IF_SCHEMA, |
|
DOMConstants.DOM3_DEFAULT_FALSE); |
|
fDOMConfigProperties.setProperty(DOMConstants.S_DOM3_PROPERTIES_NS |
|
+ DOMConstants.DOM_DATATYPE_NORMALIZATION, |
|
DOMConstants.DOM3_DEFAULT_FALSE); |
|
} |
|
|
|
|
|
fDOMConfigProperties.setProperty(DOMConstants.S_DOM3_PROPERTIES_NS |
|
+ DOMConstants.DOM_NAMESPACES, DOMConstants.DOM3_DEFAULT_TRUE); |
|
|
|
|
|
fDOMConfigProperties.setProperty(DOMConstants.S_DOM3_PROPERTIES_NS |
|
+ DOMConstants.DOM_NAMESPACE_DECLARATIONS, |
|
DOMConstants.DOM3_DEFAULT_TRUE); |
|
|
|
// normalize-characters |
|
/* |
|
fDOMConfigProperties.setProperty(DOMConstants.S_DOM3_PROPERTIES_NS |
|
+ DOMConstants.DOM_NORMALIZE_CHARACTERS, |
|
DOMConstants.DOM3_DEFAULT_FALSE); |
|
*/ |
|
|
|
|
|
fDOMConfigProperties.setProperty(DOMConstants.S_DOM3_PROPERTIES_NS |
|
+ DOMConstants.DOM_SPLIT_CDATA, DOMConstants.DOM3_DEFAULT_TRUE); |
|
|
|
|
|
fDOMConfigProperties.setProperty(DOMConstants.S_DOM3_PROPERTIES_NS |
|
+ DOMConstants.DOM_VALIDATE, DOMConstants.DOM3_DEFAULT_FALSE); |
|
|
|
|
|
fDOMConfigProperties.setProperty(DOMConstants.S_DOM3_PROPERTIES_NS |
|
+ DOMConstants.DOM_VALIDATE_IF_SCHEMA, |
|
DOMConstants.DOM3_DEFAULT_FALSE); |
|
|
|
|
|
fDOMConfigProperties.setProperty(DOMConstants.S_DOM3_PROPERTIES_NS |
|
+ DOMConstants.DOM_WELLFORMED, DOMConstants.DOM3_DEFAULT_TRUE); |
|
|
|
|
|
fDOMConfigProperties.setProperty( |
|
DOMConstants.S_XSL_OUTPUT_INDENT, |
|
DOMConstants.DOM3_DEFAULT_FALSE); |
|
fDOMConfigProperties.setProperty( |
|
OutputPropertiesFactory.S_KEY_INDENT_AMOUNT, Integer.toString(4)); |
|
|
|
// |
|
|
|
|
|
fDOMConfigProperties.setProperty(DOMConstants.S_DOM3_PROPERTIES_NS |
|
+ DOMConstants.DOM_DISCARD_DEFAULT_CONTENT, |
|
DOMConstants.DOM3_DEFAULT_TRUE); |
|
|
|
|
|
fDOMConfigProperties.setProperty(DOMConstants.S_XSL_OUTPUT_OMIT_XML_DECL, "no"); |
|
|
|
|
|
boolean isStandalone = SecuritySupport.getJAXPSystemProperty( |
|
Boolean.class, JdkConstants.SP_IS_STANDALONE, "false"); |
|
|
|
fIsStandalone = new JdkProperty<>(ImplPropMap.ISSTANDALONE, isStandalone, State.DEFAULT); |
|
|
|
if (isStandalone) { |
|
fFeatures |= IS_STANDALONE; |
|
fDOMConfigProperties.setProperty(DOMConstants.NS_IS_STANDALONE, |
|
DOMConstants.DOM3_EXPLICIT_TRUE); |
|
} else { |
|
fDOMConfigProperties.setProperty(DOMConstants.NS_IS_STANDALONE, |
|
DOMConstants.DOM3_DEFAULT_FALSE); |
|
} |
|
} |
|
|
|
// ************************************************************************ |
|
// DOMConfiguraiton implementation |
|
// ************************************************************************ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
@SuppressWarnings("deprecation") |
|
public boolean canSetParameter(String name, Object value) { |
|
if (value instanceof Boolean){ |
|
if ( name.equalsIgnoreCase(DOMConstants.DOM_CDATA_SECTIONS) |
|
|| name.equalsIgnoreCase(DOMConstants.DOM_COMMENTS) |
|
|| name.equalsIgnoreCase(DOMConstants.DOM_ENTITIES) |
|
|| name.equalsIgnoreCase(DOMConstants.DOM_INFOSET) |
|
|| name.equalsIgnoreCase(DOMConstants.DOM_ELEMENT_CONTENT_WHITESPACE) |
|
|| name.equalsIgnoreCase(DOMConstants.DOM_NAMESPACES) |
|
|| name.equalsIgnoreCase(DOMConstants.DOM_NAMESPACE_DECLARATIONS) |
|
|| name.equalsIgnoreCase(DOMConstants.DOM_SPLIT_CDATA) |
|
|| name.equalsIgnoreCase(DOMConstants.DOM_WELLFORMED) |
|
|| name.equalsIgnoreCase(DOMConstants.DOM_DISCARD_DEFAULT_CONTENT) |
|
|| name.equalsIgnoreCase(DOMConstants.DOM_FORMAT_PRETTY_PRINT) |
|
|| name.equalsIgnoreCase(DOMConstants.DOM_XMLDECL) |
|
|| name.equalsIgnoreCase(JdkConstants.FQ_IS_STANDALONE) |
|
|| name.equalsIgnoreCase(JdkConstants.SP_IS_STANDALONE)){ |
|
|
|
return true; |
|
} |
|
else if (name.equalsIgnoreCase(DOMConstants.DOM_CANONICAL_FORM) |
|
|| name.equalsIgnoreCase(DOMConstants.DOM_CHECK_CHAR_NORMALIZATION) |
|
|| name.equalsIgnoreCase(DOMConstants.DOM_DATATYPE_NORMALIZATION) |
|
|| name.equalsIgnoreCase(DOMConstants.DOM_VALIDATE_IF_SCHEMA) |
|
|| name.equalsIgnoreCase(DOMConstants.DOM_VALIDATE) |
|
|
|
) { |
|
|
|
return !((Boolean)value); |
|
} |
|
else if (name.equalsIgnoreCase(DOMConstants.DOM_IGNORE_UNKNOWN_CHARACTER_DENORMALIZATIONS)) { |
|
|
|
return ((Boolean)value); |
|
} |
|
} |
|
else if (name.equalsIgnoreCase(DOMConstants.DOM_ERROR_HANDLER) && |
|
value == null || value instanceof DOMErrorHandler){ |
|
return true; |
|
} |
|
return false; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public Object getParameter(String name) throws DOMException { |
|
|
|
if(name.equalsIgnoreCase(DOMConstants.DOM_NORMALIZE_CHARACTERS)){ |
|
return null; |
|
} else if (name.equalsIgnoreCase(DOMConstants.DOM_COMMENTS)) { |
|
return ((fFeatures & COMMENTS) != 0) ? Boolean.TRUE : Boolean.FALSE; |
|
} else if (name.equalsIgnoreCase(DOMConstants.DOM_CDATA_SECTIONS)) { |
|
return ((fFeatures & CDATA) != 0) ? Boolean.TRUE : Boolean.FALSE; |
|
} else if (name.equalsIgnoreCase(DOMConstants.DOM_ENTITIES)) { |
|
return ((fFeatures & ENTITIES) != 0) ? Boolean.TRUE : Boolean.FALSE; |
|
} else if (name.equalsIgnoreCase(DOMConstants.DOM_NAMESPACES)) { |
|
return ((fFeatures & NAMESPACES) != 0) ? Boolean.TRUE : Boolean.FALSE; |
|
} else if (name.equalsIgnoreCase(DOMConstants.DOM_NAMESPACE_DECLARATIONS)) { |
|
return ((fFeatures & NAMESPACEDECLS) != 0) ? Boolean.TRUE : Boolean.FALSE; |
|
} else if (name.equalsIgnoreCase(DOMConstants.DOM_SPLIT_CDATA)) { |
|
return ((fFeatures & SPLITCDATA) != 0) ? Boolean.TRUE : Boolean.FALSE; |
|
} else if (name.equalsIgnoreCase(DOMConstants.DOM_WELLFORMED)) { |
|
return ((fFeatures & WELLFORMED) != 0) ? Boolean.TRUE : Boolean.FALSE; |
|
} else if (name.equalsIgnoreCase(DOMConstants.DOM_DISCARD_DEFAULT_CONTENT)) { |
|
return ((fFeatures & DISCARDDEFAULT) != 0) ? Boolean.TRUE : Boolean.FALSE; |
|
} else if (name.equalsIgnoreCase(DOMConstants.DOM_FORMAT_PRETTY_PRINT)) { |
|
return ((fFeatures & PRETTY_PRINT) != 0) ? Boolean.TRUE : Boolean.FALSE; |
|
} else if (name.equalsIgnoreCase(DOMConstants.DOM_XMLDECL)) { |
|
return ((fFeatures & XMLDECL) != 0) ? Boolean.TRUE : Boolean.FALSE; |
|
} else if (ImplPropMap.ISSTANDALONE.is(name)) { |
|
return ((fFeatures & IS_STANDALONE) != 0) ? Boolean.TRUE : Boolean.FALSE; |
|
} else if (name.equalsIgnoreCase(DOMConstants.DOM_ELEMENT_CONTENT_WHITESPACE)) { |
|
return ((fFeatures & ELEM_CONTENT_WHITESPACE) != 0) ? Boolean.TRUE : Boolean.FALSE; |
|
} else if (name.equalsIgnoreCase(DOMConstants.DOM_IGNORE_UNKNOWN_CHARACTER_DENORMALIZATIONS)) { |
|
return Boolean.TRUE; |
|
} else if (name.equalsIgnoreCase(DOMConstants.DOM_CANONICAL_FORM) |
|
|| name.equalsIgnoreCase(DOMConstants.DOM_CHECK_CHAR_NORMALIZATION) |
|
|| name.equalsIgnoreCase(DOMConstants.DOM_DATATYPE_NORMALIZATION) |
|
|
|
|| name.equalsIgnoreCase(DOMConstants.DOM_VALIDATE) |
|
|| name.equalsIgnoreCase(DOMConstants.DOM_VALIDATE_IF_SCHEMA)) { |
|
return Boolean.FALSE; |
|
} else if (name.equalsIgnoreCase(DOMConstants.DOM_INFOSET)){ |
|
if ((fFeatures & ENTITIES) == 0 && |
|
(fFeatures & CDATA) == 0 && |
|
(fFeatures & ELEM_CONTENT_WHITESPACE) != 0 && |
|
(fFeatures & NAMESPACES) != 0 && |
|
(fFeatures & NAMESPACEDECLS) != 0 && |
|
(fFeatures & WELLFORMED) != 0 && |
|
(fFeatures & COMMENTS) != 0) { |
|
return Boolean.TRUE; |
|
} |
|
return Boolean.FALSE; |
|
} else if (name.equalsIgnoreCase(DOMConstants.DOM_ERROR_HANDLER)) { |
|
return fDOMErrorHandler; |
|
} else if ( |
|
name.equalsIgnoreCase(DOMConstants.DOM_SCHEMA_LOCATION) |
|
|| name.equalsIgnoreCase(DOMConstants.DOM_SCHEMA_TYPE)) { |
|
return null; |
|
} else { |
|
|
|
String msg = Utils.messages.createMessage( |
|
MsgKey.ER_FEATURE_NOT_FOUND, |
|
new Object[] { name }); |
|
throw new DOMException(DOMException.NOT_FOUND_ERR, msg); |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public DOMStringList getParameterNames() { |
|
return new DOMStringListImpl(fRecognizedParameters); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public void setParameter(String name, Object value) throws DOMException { |
|
|
|
if (value instanceof Boolean) { |
|
boolean bValue = ((Boolean) value); |
|
|
|
if (name.equalsIgnoreCase(DOMConstants.DOM_COMMENTS)) { |
|
fFeatures = bValue ? fFeatures | COMMENTS : fFeatures |
|
& ~COMMENTS; |
|
|
|
if (bValue) { |
|
fDOMConfigProperties.setProperty(DOMConstants.S_DOM3_PROPERTIES_NS |
|
+ DOMConstants.DOM_COMMENTS, DOMConstants.DOM3_EXPLICIT_TRUE); |
|
} else { |
|
fDOMConfigProperties.setProperty(DOMConstants.S_DOM3_PROPERTIES_NS |
|
+ DOMConstants.DOM_COMMENTS, DOMConstants.DOM3_EXPLICIT_FALSE); |
|
} |
|
} else if (name.equalsIgnoreCase(DOMConstants.DOM_CDATA_SECTIONS)) { |
|
fFeatures = bValue ? fFeatures | CDATA : fFeatures |
|
& ~CDATA; |
|
|
|
if (bValue) { |
|
fDOMConfigProperties.setProperty(DOMConstants.S_DOM3_PROPERTIES_NS |
|
+ DOMConstants.DOM_CDATA_SECTIONS, DOMConstants.DOM3_EXPLICIT_TRUE); |
|
} else { |
|
fDOMConfigProperties.setProperty(DOMConstants.S_DOM3_PROPERTIES_NS |
|
+ DOMConstants.DOM_CDATA_SECTIONS, DOMConstants.DOM3_EXPLICIT_FALSE); |
|
} |
|
} else if (name.equalsIgnoreCase(DOMConstants.DOM_ENTITIES)) { |
|
fFeatures = bValue ? fFeatures | ENTITIES : fFeatures |
|
& ~ENTITIES; |
|
|
|
if (bValue) { |
|
fDOMConfigProperties.setProperty(DOMConstants.S_DOM3_PROPERTIES_NS |
|
+ DOMConstants.DOM_ENTITIES, DOMConstants.DOM3_EXPLICIT_TRUE); |
|
} else { |
|
fDOMConfigProperties.setProperty(DOMConstants.S_DOM3_PROPERTIES_NS |
|
+ DOMConstants.DOM_ENTITIES, DOMConstants.DOM3_EXPLICIT_FALSE); |
|
} |
|
} else if (name.equalsIgnoreCase(DOMConstants.DOM_NAMESPACES)) { |
|
fFeatures = bValue ? fFeatures | NAMESPACES : fFeatures |
|
& ~NAMESPACES; |
|
|
|
if (bValue) { |
|
fDOMConfigProperties.setProperty(DOMConstants.S_DOM3_PROPERTIES_NS |
|
+ DOMConstants.DOM_NAMESPACES, DOMConstants.DOM3_EXPLICIT_TRUE); |
|
} else { |
|
fDOMConfigProperties.setProperty(DOMConstants.S_DOM3_PROPERTIES_NS |
|
+ DOMConstants.DOM_NAMESPACES, DOMConstants.DOM3_EXPLICIT_FALSE); |
|
} |
|
} else if (name |
|
.equalsIgnoreCase(DOMConstants.DOM_NAMESPACE_DECLARATIONS)) { |
|
fFeatures = bValue ? fFeatures | NAMESPACEDECLS |
|
: fFeatures & ~NAMESPACEDECLS; |
|
|
|
if (bValue) { |
|
fDOMConfigProperties.setProperty(DOMConstants.S_DOM3_PROPERTIES_NS |
|
+ DOMConstants.DOM_NAMESPACE_DECLARATIONS, DOMConstants.DOM3_EXPLICIT_TRUE); |
|
} else { |
|
fDOMConfigProperties.setProperty(DOMConstants.S_DOM3_PROPERTIES_NS |
|
+ DOMConstants.DOM_NAMESPACE_DECLARATIONS, DOMConstants.DOM3_EXPLICIT_FALSE); |
|
} |
|
} else if (name.equalsIgnoreCase(DOMConstants.DOM_SPLIT_CDATA)) { |
|
fFeatures = bValue ? fFeatures | SPLITCDATA : fFeatures |
|
& ~SPLITCDATA; |
|
|
|
if (bValue) { |
|
fDOMConfigProperties.setProperty(DOMConstants.S_DOM3_PROPERTIES_NS |
|
+ DOMConstants.DOM_SPLIT_CDATA, DOMConstants.DOM3_EXPLICIT_TRUE); |
|
} else { |
|
fDOMConfigProperties.setProperty(DOMConstants.S_DOM3_PROPERTIES_NS |
|
+ DOMConstants.DOM_SPLIT_CDATA, DOMConstants.DOM3_EXPLICIT_FALSE); |
|
} |
|
} else if (name.equalsIgnoreCase(DOMConstants.DOM_WELLFORMED)) { |
|
fFeatures = bValue ? fFeatures | WELLFORMED : fFeatures |
|
& ~WELLFORMED; |
|
|
|
if (bValue) { |
|
fDOMConfigProperties.setProperty(DOMConstants.S_DOM3_PROPERTIES_NS |
|
+ DOMConstants.DOM_WELLFORMED, DOMConstants.DOM3_EXPLICIT_TRUE); |
|
} else { |
|
fDOMConfigProperties.setProperty(DOMConstants.S_DOM3_PROPERTIES_NS |
|
+ DOMConstants.DOM_WELLFORMED, DOMConstants.DOM3_EXPLICIT_FALSE); |
|
} |
|
} else if (name |
|
.equalsIgnoreCase(DOMConstants.DOM_DISCARD_DEFAULT_CONTENT)) { |
|
fFeatures = bValue ? fFeatures | DISCARDDEFAULT |
|
: fFeatures & ~DISCARDDEFAULT; |
|
|
|
if (bValue) { |
|
fDOMConfigProperties.setProperty(DOMConstants.S_DOM3_PROPERTIES_NS |
|
+ DOMConstants.DOM_DISCARD_DEFAULT_CONTENT, DOMConstants.DOM3_EXPLICIT_TRUE); |
|
} else { |
|
fDOMConfigProperties.setProperty(DOMConstants.S_DOM3_PROPERTIES_NS |
|
+ DOMConstants.DOM_DISCARD_DEFAULT_CONTENT, DOMConstants.DOM3_EXPLICIT_FALSE); |
|
} |
|
} else if (name.equalsIgnoreCase(DOMConstants.DOM_FORMAT_PRETTY_PRINT)) { |
|
fFeatures = bValue ? fFeatures | PRETTY_PRINT : fFeatures |
|
& ~PRETTY_PRINT; |
|
if (bValue) { |
|
fDOMConfigProperties.setProperty(DOMConstants.S_XSL_OUTPUT_INDENT,DOMConstants.DOM3_EXPLICIT_TRUE); |
|
fDOMConfigProperties.setProperty(OutputPropertiesFactory.S_KEY_INDENT_AMOUNT, Integer.toString(4)); |
|
} else { |
|
fDOMConfigProperties.setProperty(DOMConstants.S_XSL_OUTPUT_INDENT,DOMConstants.DOM3_EXPLICIT_FALSE); |
|
} |
|
} else if (name.equalsIgnoreCase(DOMConstants.DOM_XMLDECL)) { |
|
fFeatures = bValue ? fFeatures | XMLDECL : fFeatures |
|
& ~XMLDECL; |
|
if (bValue) { |
|
fDOMConfigProperties.setProperty(DOMConstants.S_XSL_OUTPUT_OMIT_XML_DECL, "no"); |
|
} else { |
|
fDOMConfigProperties.setProperty(DOMConstants.S_XSL_OUTPUT_OMIT_XML_DECL, "yes"); |
|
} |
|
} else if (ImplPropMap.ISSTANDALONE.is(name)) { |
|
fIsStandalone.setValue(name, bValue, State.APIPROPERTY); |
|
fFeatures = fIsStandalone.getValue() ? fFeatures | IS_STANDALONE : fFeatures & ~IS_STANDALONE; |
|
fDOMConfigProperties.setProperty(DOMConstants.NS_IS_STANDALONE, |
|
fIsStandalone.getValue() ? DOMConstants.DOM3_EXPLICIT_TRUE : DOMConstants.DOM3_EXPLICIT_FALSE); |
|
} else if (name.equalsIgnoreCase(DOMConstants.DOM_ELEMENT_CONTENT_WHITESPACE)) { |
|
fFeatures = bValue ? fFeatures | ELEM_CONTENT_WHITESPACE : fFeatures |
|
& ~ELEM_CONTENT_WHITESPACE; |
|
|
|
if (bValue) { |
|
fDOMConfigProperties.setProperty(DOMConstants.S_DOM3_PROPERTIES_NS |
|
+ DOMConstants.DOM_ELEMENT_CONTENT_WHITESPACE, DOMConstants.DOM3_EXPLICIT_TRUE); |
|
} else { |
|
fDOMConfigProperties.setProperty(DOMConstants.S_DOM3_PROPERTIES_NS |
|
+ DOMConstants.DOM_ELEMENT_CONTENT_WHITESPACE, DOMConstants.DOM3_EXPLICIT_FALSE); |
|
} |
|
} else if (name.equalsIgnoreCase(DOMConstants.DOM_IGNORE_UNKNOWN_CHARACTER_DENORMALIZATIONS)) { |
|
|
|
if (!bValue) { |
|
|
|
String msg = Utils.messages.createMessage( |
|
MsgKey.ER_FEATURE_NOT_SUPPORTED, |
|
new Object[] { name }); |
|
throw new DOMException(DOMException.NOT_SUPPORTED_ERR, msg); |
|
} else { |
|
fDOMConfigProperties.setProperty(DOMConstants.S_DOM3_PROPERTIES_NS |
|
+ DOMConstants.DOM_IGNORE_UNKNOWN_CHARACTER_DENORMALIZATIONS, DOMConstants.DOM3_EXPLICIT_TRUE); |
|
} |
|
} else if (name.equalsIgnoreCase(DOMConstants.DOM_CANONICAL_FORM) |
|
|| name.equalsIgnoreCase(DOMConstants.DOM_VALIDATE_IF_SCHEMA) |
|
|| name.equalsIgnoreCase(DOMConstants.DOM_VALIDATE) |
|
|| name.equalsIgnoreCase(DOMConstants.DOM_CHECK_CHAR_NORMALIZATION) |
|
|| name.equalsIgnoreCase(DOMConstants.DOM_DATATYPE_NORMALIZATION) |
|
|
|
) { |
|
|
|
if (bValue) { |
|
String msg = Utils.messages.createMessage( |
|
MsgKey.ER_FEATURE_NOT_SUPPORTED, |
|
new Object[] { name }); |
|
throw new DOMException(DOMException.NOT_SUPPORTED_ERR, msg); |
|
} else { |
|
if (name.equalsIgnoreCase(DOMConstants.DOM_CANONICAL_FORM)) { |
|
fDOMConfigProperties.setProperty(DOMConstants.S_DOM3_PROPERTIES_NS |
|
+ DOMConstants.DOM_CANONICAL_FORM, DOMConstants.DOM3_EXPLICIT_FALSE); |
|
} else if (name.equalsIgnoreCase(DOMConstants.DOM_VALIDATE_IF_SCHEMA)) { |
|
fDOMConfigProperties.setProperty(DOMConstants.S_DOM3_PROPERTIES_NS |
|
+ DOMConstants.DOM_VALIDATE_IF_SCHEMA, DOMConstants.DOM3_EXPLICIT_FALSE); |
|
} else if (name.equalsIgnoreCase(DOMConstants.DOM_VALIDATE)) { |
|
fDOMConfigProperties.setProperty(DOMConstants.S_DOM3_PROPERTIES_NS |
|
+ DOMConstants.DOM_VALIDATE, DOMConstants.DOM3_EXPLICIT_FALSE); |
|
} else if (name.equalsIgnoreCase(DOMConstants.DOM_VALIDATE_IF_SCHEMA)) { |
|
fDOMConfigProperties.setProperty(DOMConstants.DOM_CHECK_CHAR_NORMALIZATION |
|
+ DOMConstants.DOM_CHECK_CHAR_NORMALIZATION, DOMConstants.DOM3_EXPLICIT_FALSE); |
|
} else if (name.equalsIgnoreCase(DOMConstants.DOM_DATATYPE_NORMALIZATION)) { |
|
fDOMConfigProperties.setProperty(DOMConstants.S_DOM3_PROPERTIES_NS |
|
+ DOMConstants.DOM_DATATYPE_NORMALIZATION, DOMConstants.DOM3_EXPLICIT_FALSE); |
|
} /* else if (name.equalsIgnoreCase(DOMConstants.DOM_NORMALIZE_CHARACTERS)) { |
|
fDOMConfigProperties.setProperty(DOMConstants.S_DOM3_PROPERTIES_NS |
|
+ DOMConstants.DOM_NORMALIZE_CHARACTERS, DOMConstants.DOM3_EXPLICIT_FALSE); |
|
} */ |
|
} |
|
} else if (name.equalsIgnoreCase(DOMConstants.DOM_INFOSET)) { |
|
if (bValue) { |
|
fFeatures &= ~ENTITIES; |
|
fFeatures &= ~CDATA; |
|
fFeatures &= ~SCHEMAVALIDATE; |
|
fFeatures &= ~DTNORMALIZE; |
|
fFeatures |= NAMESPACES; |
|
fFeatures |= NAMESPACEDECLS; |
|
fFeatures |= WELLFORMED; |
|
fFeatures |= ELEM_CONTENT_WHITESPACE; |
|
fFeatures |= COMMENTS; |
|
|
|
|
|
fDOMConfigProperties.setProperty(DOMConstants.S_DOM3_PROPERTIES_NS |
|
+ DOMConstants.DOM_NAMESPACES, DOMConstants.DOM3_EXPLICIT_TRUE); |
|
fDOMConfigProperties.setProperty(DOMConstants.S_DOM3_PROPERTIES_NS |
|
+ DOMConstants.DOM_NAMESPACE_DECLARATIONS, DOMConstants.DOM3_EXPLICIT_TRUE); |
|
fDOMConfigProperties.setProperty(DOMConstants.S_DOM3_PROPERTIES_NS |
|
+ DOMConstants.DOM_COMMENTS, DOMConstants.DOM3_EXPLICIT_TRUE); |
|
fDOMConfigProperties.setProperty(DOMConstants.S_DOM3_PROPERTIES_NS |
|
+ DOMConstants.DOM_ELEMENT_CONTENT_WHITESPACE, DOMConstants.DOM3_EXPLICIT_TRUE); |
|
fDOMConfigProperties.setProperty(DOMConstants.S_DOM3_PROPERTIES_NS |
|
+ DOMConstants.DOM_WELLFORMED, DOMConstants.DOM3_EXPLICIT_TRUE); |
|
|
|
fDOMConfigProperties.setProperty(DOMConstants.S_DOM3_PROPERTIES_NS |
|
+ DOMConstants.DOM_ENTITIES, DOMConstants.DOM3_EXPLICIT_FALSE); |
|
|
|
fDOMConfigProperties.setProperty(DOMConstants.S_DOM3_PROPERTIES_NS |
|
+ DOMConstants.DOM_CDATA_SECTIONS, DOMConstants.DOM3_EXPLICIT_FALSE); |
|
fDOMConfigProperties.setProperty(DOMConstants.S_DOM3_PROPERTIES_NS |
|
+ DOMConstants.DOM_VALIDATE_IF_SCHEMA, DOMConstants.DOM3_EXPLICIT_FALSE); |
|
fDOMConfigProperties.setProperty(DOMConstants.S_DOM3_PROPERTIES_NS |
|
+ DOMConstants.DOM_DATATYPE_NORMALIZATION, DOMConstants.DOM3_EXPLICIT_FALSE); |
|
} |
|
} else if (name.equalsIgnoreCase(DOMConstants.DOM_NORMALIZE_CHARACTERS)) { |
|
String msg = Utils.messages.createMessage( |
|
MsgKey.ER_FEATURE_NOT_SUPPORTED, |
|
new Object[] { name }); |
|
throw new DOMException(DOMException.NOT_SUPPORTED_ERR, msg); |
|
} else { |
|
// Setting this to false has no effect |
|
} |
|
} |
|
else if (name.equalsIgnoreCase(DOMConstants.DOM_ERROR_HANDLER)) { |
|
if (value == null || value instanceof DOMErrorHandler) { |
|
fDOMErrorHandler = (DOMErrorHandler)value; |
|
} else { |
|
String msg = Utils.messages.createMessage( |
|
MsgKey.ER_TYPE_MISMATCH_ERR, |
|
new Object[] { name }); |
|
throw new DOMException(DOMException.TYPE_MISMATCH_ERR, msg); |
|
} |
|
} else if ( |
|
name.equalsIgnoreCase(DOMConstants.DOM_SCHEMA_LOCATION) |
|
|| name.equalsIgnoreCase(DOMConstants.DOM_SCHEMA_TYPE) |
|
|| name.equalsIgnoreCase(DOMConstants.DOM_NORMALIZE_CHARACTERS) |
|
&& value != null) { |
|
String msg = Utils.messages.createMessage( |
|
MsgKey.ER_FEATURE_NOT_SUPPORTED, |
|
new Object[] { name }); |
|
throw new DOMException(DOMException.NOT_SUPPORTED_ERR, msg); |
|
} else { |
|
String msg = Utils.messages.createMessage( |
|
MsgKey.ER_FEATURE_NOT_FOUND, |
|
new Object[] { name }); |
|
throw new DOMException(DOMException.NOT_FOUND_ERR, msg); |
|
} |
|
} |
|
// ************************************************************************ |
|
|
|
|
|
// ************************************************************************ |
|
// DOMConfiguraiton implementation |
|
// ************************************************************************ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public DOMConfiguration getDomConfig() { |
|
return (DOMConfiguration)this; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public LSSerializerFilter getFilter() { |
|
return fSerializerFilter; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public String getNewLine() { |
|
return fEndOfLine; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public void setFilter(LSSerializerFilter filter) { |
|
fSerializerFilter = filter; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public void setNewLine(String newLine) { |
|
fEndOfLine = newLine !=null? newLine: fEndOfLine; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public boolean write(Node nodeArg, LSOutput destination) throws LSException { |
|
|
|
if (destination == null) { |
|
String msg = Utils.messages |
|
.createMessage( |
|
MsgKey.ER_NO_OUTPUT_SPECIFIED, |
|
null); |
|
if (fDOMErrorHandler != null) { |
|
fDOMErrorHandler.handleError(new DOMErrorImpl( |
|
DOMError.SEVERITY_FATAL_ERROR, msg, |
|
MsgKey.ER_NO_OUTPUT_SPECIFIED)); |
|
} |
|
throw new LSException(LSException.SERIALIZE_ERR, msg); |
|
} |
|
|
|
|
|
if (nodeArg == null ) { |
|
return false; |
|
} |
|
|
|
// Obtain a reference to the serializer to use |
|
|
|
Serializer serializer = fXMLSerializer; |
|
serializer.reset(); |
|
|
|
|
|
if ( nodeArg != fVisitedNode) { |
|
|
|
String xmlVersion = getXMLVersion(nodeArg); |
|
|
|
|
|
fEncoding = destination.getEncoding(); |
|
if (fEncoding == null ) { |
|
fEncoding = getInputEncoding(nodeArg); |
|
fEncoding = fEncoding != null ? fEncoding : getXMLEncoding(nodeArg) == null? "UTF-8": getXMLEncoding(nodeArg); |
|
} |
|
|
|
// If the encoding is not recognized throw an exception. |
|
|
|
if (!Encodings.isRecognizedEncoding(fEncoding)) { |
|
String msg = Utils.messages |
|
.createMessage( |
|
MsgKey.ER_UNSUPPORTED_ENCODING, |
|
null); |
|
if (fDOMErrorHandler != null) { |
|
fDOMErrorHandler.handleError(new DOMErrorImpl( |
|
DOMError.SEVERITY_FATAL_ERROR, msg, |
|
MsgKey.ER_UNSUPPORTED_ENCODING)); |
|
} |
|
throw new LSException(LSException.SERIALIZE_ERR, msg); |
|
} |
|
|
|
serializer.getOutputFormat().setProperty("version", xmlVersion); |
|
|
|
|
|
fDOMConfigProperties.setProperty(DOMConstants.S_XERCES_PROPERTIES_NS + DOMConstants.S_XML_VERSION, xmlVersion); |
|
fDOMConfigProperties.setProperty(DOMConstants.S_XSL_OUTPUT_ENCODING, fEncoding); |
|
|
|
// If the node to be serialized is not a Document, Element, or Entity |
|
// node |
|
// then the XML declaration, or text declaration, should be never be |
|
|
|
if ( (nodeArg.getNodeType() != Node.DOCUMENT_NODE |
|
|| nodeArg.getNodeType() != Node.ELEMENT_NODE |
|
|| nodeArg.getNodeType() != Node.ENTITY_NODE) |
|
&& ((fFeatures & XMLDECL) != 0)) { |
|
fDOMConfigProperties.setProperty( |
|
DOMConstants.S_XSL_OUTPUT_OMIT_XML_DECL, |
|
DOMConstants.DOM3_DEFAULT_FALSE); |
|
} |
|
|
|
fVisitedNode = nodeArg; |
|
} |
|
|
|
|
|
fXMLSerializer.setOutputFormat(fDOMConfigProperties); |
|
|
|
|
|
try { |
|
|
|
// The LSSerializer will use the LSOutput object to determine |
|
// where to serialize the output to in the following order the |
|
// first one that is not null and not an empty string will be |
|
// used: 1.LSOutput.characterStream, 2.LSOutput.byteStream, |
|
// 3. LSOutput.systemId |
|
|
|
Writer writer = destination.getCharacterStream(); |
|
if (writer == null ) { |
|
|
|
|
|
OutputStream outputStream = destination.getByteStream(); |
|
if ( outputStream == null) { |
|
|
|
|
|
String uri = destination.getSystemId(); |
|
if (uri == null) { |
|
String msg = Utils.messages |
|
.createMessage( |
|
MsgKey.ER_NO_OUTPUT_SPECIFIED, |
|
null); |
|
if (fDOMErrorHandler != null) { |
|
fDOMErrorHandler.handleError(new DOMErrorImpl( |
|
DOMError.SEVERITY_FATAL_ERROR, msg, |
|
MsgKey.ER_NO_OUTPUT_SPECIFIED)); |
|
} |
|
throw new LSException(LSException.SERIALIZE_ERR, msg); |
|
|
|
} else { |
|
|
|
String absoluteURI = SystemIDResolver.getAbsoluteURI(uri); |
|
|
|
URL url = new URL(absoluteURI); |
|
OutputStream urlOutStream = null; |
|
String protocol = url.getProtocol(); |
|
String host = url.getHost(); |
|
|
|
// For file protocols, there is no need to use a URL to get its |
|
// corresponding OutputStream |
|
|
|
// Scheme names consist of a sequence of characters. The lower case |
|
// letters "a"--"z", digits, and the characters plus ("+"), period |
|
// ("."), and hyphen ("-") are allowed. For resiliency, programs |
|
// interpreting URLs should treat upper case letters as equivalent to |
|
|
|
if (protocol.equalsIgnoreCase("file") |
|
&& (host == null || host.length() == 0 || host.equals("localhost"))) { |
|
|
|
urlOutStream = new FileOutputStream(new File(url.getPath())); |
|
|
|
} else { |
|
// This should support URL's whose schemes are mentioned in |
|
// RFC1738 other than file |
|
|
|
URLConnection urlCon = url.openConnection(); |
|
urlCon.setDoInput(false); |
|
urlCon.setDoOutput(true); |
|
urlCon.setUseCaches(false); |
|
urlCon.setAllowUserInteraction(false); |
|
|
|
|
|
if (urlCon instanceof HttpURLConnection) { |
|
HttpURLConnection httpCon = (HttpURLConnection) urlCon; |
|
httpCon.setRequestMethod("PUT"); |
|
} |
|
urlOutStream = urlCon.getOutputStream(); |
|
} |
|
|
|
serializer.setWriter(new OutputStreamWriter(urlOutStream)); |
|
} |
|
} else { |
|
|
|
serializer.setWriter(new OutputStreamWriter(outputStream, fEncoding)); |
|
} |
|
} else { |
|
|
|
serializer.setWriter(writer); |
|
} |
|
|
|
// The associated media type by default is set to text/xml on |
|
// org.apache.xml.serializer.SerializerBase. |
|
|
|
// Get a reference to the serializer then lets you serilize a DOM |
|
|
|
if (fDOMSerializer == null) { |
|
fDOMSerializer = (DOM3Serializer)serializer.asDOM3Serializer(); |
|
} |
|
|
|
|
|
if (fDOMErrorHandler != null) { |
|
fDOMSerializer.setErrorHandler(fDOMErrorHandler); |
|
} |
|
|
|
|
|
if (fSerializerFilter != null) { |
|
fDOMSerializer.setNodeFilter(fSerializerFilter); |
|
} |
|
|
|
|
|
fDOMSerializer.setNewLine(fEndOfLine); |
|
|
|
// Serializer your DOM, where node is an org.w3c.dom.Node |
|
|
|
fDOMSerializer.serializeDOM3(nodeArg); |
|
|
|
} catch( UnsupportedEncodingException ue) { |
|
|
|
String msg = Utils.messages |
|
.createMessage( |
|
MsgKey.ER_UNSUPPORTED_ENCODING, |
|
null); |
|
if (fDOMErrorHandler != null) { |
|
fDOMErrorHandler.handleError(new DOMErrorImpl( |
|
DOMError.SEVERITY_FATAL_ERROR, msg, |
|
MsgKey.ER_UNSUPPORTED_ENCODING, ue)); |
|
} |
|
throw new LSException(LSException.SERIALIZE_ERR, ue.getMessage()); |
|
} catch (LSException lse) { |
|
|
|
throw lse; |
|
} catch (RuntimeException e) { |
|
e.printStackTrace(); |
|
throw new LSException(LSException.SERIALIZE_ERR, e!=null?e.getMessage():"NULL Exception") ; |
|
} catch (Exception e) { |
|
if (fDOMErrorHandler != null) { |
|
fDOMErrorHandler.handleError(new DOMErrorImpl( |
|
DOMError.SEVERITY_FATAL_ERROR, e.getMessage(), |
|
null, e)); |
|
} |
|
e.printStackTrace(); |
|
throw new LSException(LSException.SERIALIZE_ERR, e.toString()); |
|
} |
|
return true; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public String writeToString(Node nodeArg) throws DOMException, LSException { |
|
|
|
if (nodeArg == null) { |
|
return null; |
|
} |
|
|
|
// Should we reset the serializer configuration before each write operation? |
|
|
|
Serializer serializer = fXMLSerializer; |
|
serializer.reset(); |
|
|
|
if (nodeArg != fVisitedNode){ |
|
|
|
String xmlVersion = getXMLVersion(nodeArg); |
|
|
|
serializer.getOutputFormat().setProperty("version", xmlVersion); |
|
|
|
|
|
fDOMConfigProperties.setProperty(DOMConstants.S_XERCES_PROPERTIES_NS + DOMConstants.S_XML_VERSION, xmlVersion); |
|
fDOMConfigProperties.setProperty(DOMConstants.S_XSL_OUTPUT_ENCODING, "UTF-16"); |
|
|
|
// If the node to be serialized is not a Document, Element, or Entity |
|
// node |
|
// then the XML declaration, or text declaration, should be never be |
|
|
|
if ((nodeArg.getNodeType() != Node.DOCUMENT_NODE |
|
|| nodeArg.getNodeType() != Node.ELEMENT_NODE |
|
|| nodeArg.getNodeType() != Node.ENTITY_NODE) |
|
&& ((fFeatures & XMLDECL) != 0)) { |
|
fDOMConfigProperties.setProperty( |
|
DOMConstants.S_XSL_OUTPUT_OMIT_XML_DECL, |
|
DOMConstants.DOM3_DEFAULT_FALSE); |
|
} |
|
|
|
fVisitedNode = nodeArg; |
|
} |
|
|
|
fXMLSerializer.setOutputFormat(fDOMConfigProperties); |
|
|
|
|
|
StringWriter output = new StringWriter(); |
|
|
|
|
|
try { |
|
|
|
|
|
serializer.setWriter(output); |
|
|
|
// Get a reference to the serializer then lets you serilize a DOM |
|
|
|
if (fDOMSerializer == null) { |
|
fDOMSerializer = (DOM3Serializer)serializer.asDOM3Serializer(); |
|
} |
|
|
|
|
|
if (fDOMErrorHandler != null) { |
|
fDOMSerializer.setErrorHandler(fDOMErrorHandler); |
|
} |
|
|
|
|
|
if (fSerializerFilter != null) { |
|
fDOMSerializer.setNodeFilter(fSerializerFilter); |
|
} |
|
|
|
|
|
fDOMSerializer.setNewLine(fEndOfLine); |
|
|
|
|
|
fDOMSerializer.serializeDOM3(nodeArg); |
|
} catch (LSException lse) { |
|
|
|
throw lse; |
|
} catch (RuntimeException e) { |
|
e.printStackTrace(); |
|
throw new LSException(LSException.SERIALIZE_ERR, e.toString()); |
|
} catch (Exception e) { |
|
if (fDOMErrorHandler != null) { |
|
fDOMErrorHandler.handleError(new DOMErrorImpl( |
|
DOMError.SEVERITY_FATAL_ERROR, e.getMessage(), |
|
null, e)); |
|
} |
|
e.printStackTrace(); |
|
throw new LSException(LSException.SERIALIZE_ERR, e.toString()); |
|
} |
|
|
|
|
|
return output.toString(); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public boolean writeToURI(Node nodeArg, String uri) throws LSException { |
|
|
|
if (nodeArg == null ) { |
|
return false; |
|
} |
|
|
|
|
|
Serializer serializer = fXMLSerializer; |
|
serializer.reset(); |
|
|
|
if (nodeArg != fVisitedNode) { |
|
|
|
String xmlVersion = getXMLVersion(nodeArg); |
|
|
|
// Determine the encoding: 1.LSOutput.encoding, |
|
|
|
fEncoding = getInputEncoding(nodeArg); |
|
if (fEncoding == null ) { |
|
fEncoding = fEncoding != null ? fEncoding : getXMLEncoding(nodeArg) == null? "UTF-8": getXMLEncoding(nodeArg); |
|
} |
|
|
|
serializer.getOutputFormat().setProperty("version", xmlVersion); |
|
|
|
|
|
fDOMConfigProperties.setProperty(DOMConstants.S_XERCES_PROPERTIES_NS + DOMConstants.S_XML_VERSION, xmlVersion); |
|
fDOMConfigProperties.setProperty(DOMConstants.S_XSL_OUTPUT_ENCODING, fEncoding); |
|
|
|
// If the node to be serialized is not a Document, Element, or Entity |
|
// node |
|
// then the XML declaration, or text declaration, should be never be |
|
|
|
if ( (nodeArg.getNodeType() != Node.DOCUMENT_NODE |
|
|| nodeArg.getNodeType() != Node.ELEMENT_NODE |
|
|| nodeArg.getNodeType() != Node.ENTITY_NODE) |
|
&& ((fFeatures & XMLDECL) != 0)) { |
|
fDOMConfigProperties.setProperty( |
|
DOMConstants.S_XSL_OUTPUT_OMIT_XML_DECL, |
|
DOMConstants.DOM3_DEFAULT_FALSE); |
|
} |
|
|
|
fVisitedNode = nodeArg; |
|
} |
|
|
|
|
|
fXMLSerializer.setOutputFormat(fDOMConfigProperties); |
|
|
|
|
|
try { |
|
// If the specified encoding is not supported an |
|
|
|
if (uri == null) { |
|
String msg = Utils.messages.createMessage( |
|
MsgKey.ER_NO_OUTPUT_SPECIFIED, null); |
|
if (fDOMErrorHandler != null) { |
|
fDOMErrorHandler.handleError(new DOMErrorImpl( |
|
DOMError.SEVERITY_FATAL_ERROR, msg, |
|
MsgKey.ER_NO_OUTPUT_SPECIFIED)); |
|
} |
|
throw new LSException(LSException.SERIALIZE_ERR, msg); |
|
|
|
} else { |
|
|
|
String absoluteURI = SystemIDResolver.getAbsoluteURI(uri); |
|
|
|
URL url = new URL(absoluteURI); |
|
OutputStream urlOutStream = null; |
|
String protocol = url.getProtocol(); |
|
String host = url.getHost(); |
|
|
|
// For file protocols, there is no need to use a URL to get its |
|
// corresponding OutputStream |
|
|
|
// Scheme names consist of a sequence of characters. The lower |
|
// case letters "a"--"z", digits, and the characters plus ("+"), |
|
// period ("."), and hyphen ("-") are allowed. For resiliency, |
|
// programs interpreting URLs should treat upper case letters as |
|
// equivalent to lower case in scheme names |
|
|
|
if (protocol.equalsIgnoreCase("file") |
|
&& (host == null || host.length() == 0 || host |
|
.equals("localhost"))) { |
|
|
|
urlOutStream = new FileOutputStream(new File(url.getPath())); |
|
|
|
} else { |
|
// This should support URL's whose schemes are mentioned in |
|
// RFC1738 other than file |
|
|
|
URLConnection urlCon = url.openConnection(); |
|
urlCon.setDoInput(false); |
|
urlCon.setDoOutput(true); |
|
urlCon.setUseCaches(false); |
|
urlCon.setAllowUserInteraction(false); |
|
|
|
|
|
if (urlCon instanceof HttpURLConnection) { |
|
HttpURLConnection httpCon = (HttpURLConnection) urlCon; |
|
httpCon.setRequestMethod("PUT"); |
|
} |
|
urlOutStream = urlCon.getOutputStream(); |
|
} |
|
|
|
serializer.setWriter(new OutputStreamWriter(urlOutStream, fEncoding)); |
|
} |
|
|
|
// Get a reference to the serializer then lets you serilize a DOM |
|
|
|
if (fDOMSerializer == null) { |
|
fDOMSerializer = (DOM3Serializer)serializer.asDOM3Serializer(); |
|
} |
|
|
|
|
|
if (fDOMErrorHandler != null) { |
|
fDOMSerializer.setErrorHandler(fDOMErrorHandler); |
|
} |
|
|
|
|
|
if (fSerializerFilter != null) { |
|
fDOMSerializer.setNodeFilter(fSerializerFilter); |
|
} |
|
|
|
|
|
fDOMSerializer.setNewLine(fEndOfLine); |
|
|
|
// Serializer your DOM, where node is an org.w3c.dom.Node |
|
// Assuming that Xalan's serializer can serialize any type of DOM |
|
|
|
fDOMSerializer.serializeDOM3(nodeArg); |
|
|
|
} catch (LSException lse) { |
|
|
|
throw lse; |
|
} catch (RuntimeException e) { |
|
e.printStackTrace(); |
|
throw new LSException(LSException.SERIALIZE_ERR, e.toString()); |
|
} catch (Exception e) { |
|
if (fDOMErrorHandler != null) { |
|
fDOMErrorHandler.handleError(new DOMErrorImpl( |
|
DOMError.SEVERITY_FATAL_ERROR, e.getMessage(), |
|
null, e)); |
|
} |
|
e.printStackTrace(); |
|
throw new LSException(LSException.SERIALIZE_ERR, e.toString()); |
|
} |
|
|
|
return true; |
|
} |
|
// ************************************************************************ |
|
|
|
|
|
// ************************************************************************ |
|
// Implementaion methods |
|
// ************************************************************************ |
|
|
|
/** |
|
* Determines the XML Version of the Document Node to serialize. If the Document Node |
|
* is not a DOM Level 3 Node, then the default version returned is 1.0. |
|
* |
|
* @param nodeArg The Node to serialize |
|
* @return A String containing the version pseudo-attribute of the XMLDecl. |
|
* @throws Throwable if the DOM implementation does not implement Document.getXmlVersion() |
|
*/ |
|
|
|
protected String getXMLVersion(Node nodeArg) { |
|
Document doc = null; |
|
|
|
|
|
if (nodeArg != null) { |
|
if (nodeArg.getNodeType() == Node.DOCUMENT_NODE) { |
|
|
|
doc = (Document)nodeArg; |
|
} else { |
|
|
|
doc = nodeArg.getOwnerDocument(); |
|
} |
|
|
|
|
|
if (doc != null && doc.getImplementation().hasFeature("Core","3.0")) { |
|
try { |
|
return doc.getXmlVersion(); |
|
} catch (AbstractMethodError e) { |
|
//ignore, impl does not support the method |
|
} |
|
} |
|
} |
|
// The version will be treated as "1.0" which may result in |
|
// an ill-formed document being serialized. |
|
|
|
return "1.0"; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
protected String getXMLEncoding(Node nodeArg) { |
|
Document doc = null; |
|
|
|
|
|
if (nodeArg != null) { |
|
if (nodeArg.getNodeType() == Node.DOCUMENT_NODE) { |
|
|
|
doc = (Document)nodeArg; |
|
} else { |
|
|
|
doc = nodeArg.getOwnerDocument(); |
|
} |
|
|
|
|
|
if (doc != null && doc.getImplementation().hasFeature("Core","3.0")) { |
|
return doc.getXmlEncoding(); |
|
} |
|
} |
|
|
|
return "UTF-8"; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
protected String getInputEncoding(Node nodeArg) { |
|
Document doc = null; |
|
|
|
|
|
if (nodeArg != null) { |
|
if (nodeArg.getNodeType() == Node.DOCUMENT_NODE) { |
|
|
|
doc = (Document)nodeArg; |
|
} else { |
|
|
|
doc = nodeArg.getOwnerDocument(); |
|
} |
|
|
|
|
|
if (doc != null && doc.getImplementation().hasFeature("Core","3.0")) { |
|
return doc.getInputEncoding(); |
|
} |
|
} |
|
|
|
return null; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public DOMErrorHandler getErrorHandler() { |
|
return fDOMErrorHandler; |
|
} |
|
|
|
} |