/*
* reserved comment block
* DO NOT REMOVE OR ALTER!
*/
/*
* Copyright 1999-2005 The Apache Software Foundation.
*
* Licensed 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
*
*
* 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.
*/
/**
* EXPERIMENTAL: Implemenatation of DOM Level 3 org.w3c.ls.LSSerializer by delegating serialization
* calls to <CODE>XMLSerializer</CODE>.
* LSSerializer provides an API for serializing (writing) a DOM document out in an
* XML document. The XML data is written to an output stream.
* During serialization of XML data, namespace fixup is done when possible as
* defined in DOM Level 3 Core, Appendix B.
*
* @author Elena Litani, IBM
* @author Gopal Sharma, Sun Microsystems
* @author Arun Yadav, Sun Microsystems
* @author Sunitha Reddy, Sun Microsystems
* @version $Id: DOMSerializerImpl.java,v 1.11 2010-11-01 04:40:36 joehw Exp $
*/
// TODO: When DOM Level 3 goes to REC replace method calls using
// reflection for: getXmlEncoding, getInputEncoding and getXmlEncoding
// with regular static calls on the Document object.
// data
// serializer
// XML 1.1 serializer
//Recognized parameters
/** REVISIT: Currently we handle 3 different configurations, would be nice just have one configuration
*/
// well-formness checking
/**
* Constructs a new LSSerializer.
* The constructor turns on the namespace support in <code>XMLSerializer</code> and
* initializes the following fields: fNSBinder, fLocalNSBinder, fSymbolTable,
* fEmptySymbol, fXmlSymbol, fXmlnsSymbol, fNamespaceCounter, fFeatures.
*/
public DOMSerializerImpl() {
// set default features
features |= NAMESPACES;
features |= SPLITCDATA;
features |= WELLFORMED;
serializer = new XMLSerializer();
}
//
// LSSerializer methods
//
return this;
}
/** DOM L3-EXPERIMENTAL:
* Setter for boolean and object parameters
*/
if (state){
features |= NAMESPACES;
features |= WELLFORMED;
}
// false does not have any effect
features =
features =
(short) (state
? features | NAMESPACES
: features & ~NAMESPACES);
features =
(short) (state
? features | SPLITCDATA
: features & ~SPLITCDATA);
features =
(short) (state
: features & ~DISCARDDEFAULT);
features =
(short) (state
? features | WELLFORMED
: features & ~WELLFORMED);
features =
(short) (state
}
features =
(short) (state
}
features =
(short) (state
}
features =
(short) (state
: features & ~FORMAT_PRETTY_PRINT);
}
// || name.equalsIgnoreCase(Constants.DOM_NORMALIZE_CHARACTERS)) {
// true is not supported
if (state) {
"FEATURE_NOT_SUPPORTED",
}
}else if (
//namespace-declaration has effect only if namespaces is true
features =
(short) (state
// false is not supported
if (!state) {
"FEATURE_NOT_SUPPORTED",
}
} else {
"FEATURE_NOT_FOUND",
}
} else {
"TYPE_MISMATCH_ERR",
}
} else if (
"FEATURE_NOT_SUPPORTED",
} else {
"FEATURE_NOT_FOUND",
}
}
/** DOM L3-EXPERIMENTAL:
* Check if parameter can be set
*/
return true;
}
// both values supported
return true;
// || name.equalsIgnoreCase(Constants.DOM_NORMALIZE_CHARACTERS)) {
// true is not supported
return !value;
// false is not supported
return value;
}
return true;
}
return false;
}
/**
* DOM Level 3 Core CR - Experimental.
*
* The list of the parameters supported by this
* <code>DOMConfiguration</code> object and for which at least one value
* can be set by the application. Note that this list can also contain
* parameter names defined outside this specification.
*/
if (fRecognizedParameters == null){
//Add DOM recognized parameters
//REVISIT: Would have been nice to have a list of
//recognized parameters.
//parameters.add(Constants.DOM_NORMALIZE_CHARACTERS);
//parameters.add(Constants.DOM_SCHEMA_LOCATION);
//parameters.add(Constants.DOM_SCHEMA_TYPE);
//Add recognized xerces features and properties
}
return fRecognizedParameters;
}
/** DOM L3-EXPERIMENTAL:
* Getter for boolean and object parameters
*/
return null;
}
return fErrorHandler;
} else if (
"FEATURE_NOT_SUPPORTED",
} else {
"FEATURE_NOT_FOUND",
}
}
/**
* DOM L3 EXPERIMENTAL:
* Serialize the specified node as described above in the description of
* <code>LSSerializer</code>. The result of serializing the node is
* returned as a string. Writing a Document or Entity node produces a
* serialized form that is well formed XML. Writing other node types
* produces a fragment of text in a form that is not fully defined by
* this document, but that should be useful to a human for debugging or
* diagnostic purposes.
* @param wnode The node to be written.
* @return Returns the serialized data
* @exception DOMException
* DOMSTRING_SIZE_ERR: The resulting string is too long to fit in a
* <code>DOMString</code>.
* @exception LSException
* SERIALIZE_ERR: Unable to serialize the node. DOM applications should
* attach a <code>DOMErrorHandler</code> using the parameter
* "<i>error-handler</i>" to get details on error.
*/
// determine which serializer to use:
Document doc = (wnode.getNodeType() == Node.DOCUMENT_NODE)?(Document)wnode:wnode.getOwnerDocument();
// this should run under JDK 1.1.8...
try {
if(getVersion != null ) {
}
} catch (Exception e) {
// no way to test the version...
// ignore the exception
}
if(xml11Serializer == null) {
xml11Serializer = new XML11Serializer();
}
// copy setting from "main" serializer to XML 1.1 serializer
} else {
ser = serializer;
}
try {
}
}
}
}
else {
"unable-to-serialize-node", null);
}
}
} catch (LSException lse) {
// Rethrow LSException.
throw lse;
} catch (RuntimeException e) {
if (e == DOMNormalizer.abort){
// stopped at user request
return null;
}
} catch (IOException ioe) {
// REVISIT: A generic IOException doesn't provide enough information
// to determine that the serialized document is too large to fit
// into a string. This could have thrown for some other reason. -- mrglavas
"STRING_TOO_LONG",
}
return destination.toString();
}
/**
* DOM L3 EXPERIMENTAL:
* The end-of-line sequence of characters to be used in the XML being
* written out. The only permitted values are these:
* <dl>
* <dt><code>null</code></dt>
* <dd>
* Use a default end-of-line sequence. DOM implementations should choose
* the default to match the usual convention for text files in the
* environment being used. Implementations must choose a default
* sequence that matches one of those allowed by 2.11 "End-of-Line
* Handling". </dd>
* <dt>CR</dt>
* <dd>The carriage-return character (#xD).</dd>
* <dt>CR-LF</dt>
* <dd> The
* carriage-return and line-feed characters (#xD #xA). </dd>
* <dt>LF</dt>
* <dd> The line-feed
* character (#xA). </dd>
* </dl>
* <br>The default value for this attribute is <code>null</code>.
*/
}
/**
* DOM L3 EXPERIMENTAL:
* The end-of-line sequence of characters to be used in the XML being
* written out. The only permitted values are these:
* <dl>
* <dt><code>null</code></dt>
* <dd>
* Use a default end-of-line sequence. DOM implementations should choose
* the default to match the usual convention for text files in the
* environment being used. Implementations must choose a default
* sequence that matches one of those allowed by 2.11 "End-of-Line
* Handling". </dd>
* <dt>CR</dt>
* <dd>The carriage-return character (#xD).</dd>
* <dt>CR-LF</dt>
* <dd> The
* carriage-return and line-feed characters (#xD #xA). </dd>
* <dt>LF</dt>
* <dd> The line-feed
* character (#xA). </dd>
* </dl>
* <br>The default value for this attribute is <code>null</code>.
*/
}
/**
* When the application provides a filter, the serializer will call out
* to the filter before serializing each Node. Attribute nodes are never
* passed to the filter. The filter implementation can choose to remove
* the node from the stream or to terminate the serialization early.
*/
return serializer.fDOMFilter;
}
/**
* When the application provides a filter, the serializer will call out
* to the filter before serializing each Node. Attribute nodes are never
* passed to the filter. The filter implementation can choose to remove
* the node from the stream or to terminate the serialization early.
*/
}
// this initializes a newly-created serializer
}
// copies all settings that could have been modified
// by calls to LSSerializer methods from one serializer to another.
// IMPORTANT: if new methods are implemented or more settings of
// the serializer are made alterable, this must be
// reflected in this method!
}//copysettings
/**
* Serialize the specified node as described above in the general
* description of the <code>LSSerializer</code> interface. The output
* is written to the supplied <code>LSOutput</code>.
* <br> When writing to a <code>LSOutput</code>, the encoding is found by
* looking at the encoding information that is reachable through the
* <code>LSOutput</code> and the item to be written (or its owner
* document) in this order:
* <ol>
* <li> <code>LSOutput.encoding</code>,
* </li>
* <li>
* <code>Document.actualEncoding</code>,
* </li>
* <li>
* <code>Document.xmlEncoding</code>.
* </li>
* </ol>
* <br> If no encoding is reachable through the above properties, a
* default encoding of "UTF-8" will be used.
* <br> If the specified encoding is not supported an
* "unsupported-encoding" error is raised.
* <br> If no output is specified in the <code>LSOutput</code>, a
* "no-output-specified" error is raised.
* @param node The node to serialize.
* @param destination The destination for the serialized DOM.
* @return Returns <code>true</code> if <code>node</code> was
* successfully serialized and <code>false</code> in case the node
* couldn't be serialized.
*/
return false;
: node.getOwnerDocument();
// this should run under JDK 1.1.8...
try {
if (getVersion != null) {
}
} catch (Exception e) {
//no way to test the version...
//ignore the exception
}
//determine which serializer to use:
if (xml11Serializer == null) {
xml11Serializer = new XML11Serializer();
}
//copy setting from "main" serializer to XML 1.1 serializer
} else {
ser = serializer;
}
try {
if (getEncoding != null) {
}
} catch (Exception e) {
// ignore the exception
}
try {
if (getEncoding != null) {
}
} catch (Exception e) {
// ignore the exception
}
encoding = "UTF-8";
}
}
}
try {
if (outputStream == null) {
"no-output-specified", null);
}
}
else {
// URI was specified. Handle relative URIs.
// Use FileOutputStream if this URI is for a local file.
}
// Try to write to some other kind of URI. Some protocols
// won't support this, though HTTP should work.
else {
urlCon.setDoInput(false);
urlCon.setDoOutput(true);
if (urlCon instanceof HttpURLConnection) {
// The DOM L3 LS CR says if we are writing to an HTTP URI
// it is to be done with an HTTP PUT.
}
}
}
}
else {
// byte stream was specified
}
}
else {
// character stream is specified
}
}
else
return false;
} catch( UnsupportedEncodingException ue) {
}
"unsupported-encoding", null));
//return false;
} catch (LSException lse) {
// Rethrow LSException.
throw lse;
} catch (RuntimeException e) {
if (e == DOMNormalizer.abort){
// stopped at user request
return false;
}
} catch (Exception e) {
error.fException = e;
}
e.printStackTrace();
}
return true;
} //write
/**
* Serialize the specified node as described above in the general
* description of the <code>LSSerializer</code> interface. The output
* is written to the supplied URI.
* <br> When writing to a URI, the encoding is found by looking at the
* encoding information that is reachable through the item to be written
* (or its owner document) in this order:
* <ol>
* <li>
* <code>Document.inputEncoding</code>,
* </li>
* <li>
* <code>Document.xmlEncoding</code>.
* </li>
* </ol>
* <br> If no encoding is reachable through the above properties, a
* default encoding of "UTF-8" will be used.
* <br> If the specified encoding is not supported an
* "unsupported-encoding" error is raised.
* @param node The node to serialize.
* @param URI The URI to write to.
* @return Returns <code>true</code> if <code>node</code> was
* successfully serialized and <code>false</code> in case the node
* couldn't be serialized.
*/
return false;
}
: node.getOwnerDocument();
// this should run under JDK 1.1.8...
try {
if (getXmlVersion != null) {
}
} catch (Exception e) {
// no way to test the version...
// ignore the exception
}
if (xml11Serializer == null) {
xml11Serializer = new XML11Serializer();
}
// copy setting from "main" serializer to XML 1.1 serializer
} else {
ser = serializer;
}
try {
if (getEncoding != null) {
}
} catch (Exception e) {
// ignore the exception
}
try {
if (getEncoding != null) {
}
} catch (Exception e) {
// ignore the exception
}
encoding = "UTF-8";
}
}
try {
// URI was specified. Handle relative URIs.
// Use FileOutputStream if this URI is for a local file.
}
// Try to write to some other kind of URI. Some protocols
// won't support this, though HTTP should work.
else {
urlCon.setDoInput(false);
urlCon.setDoOutput(true);
if (urlCon instanceof HttpURLConnection) {
// The DOM L3 LS CR says if we are writing to an HTTP URI
// it is to be done with an HTTP PUT.
}
}
}
else
return false;
} catch (LSException lse) {
// Rethrow LSException.
throw lse;
} catch (RuntimeException e) {
if (e == DOMNormalizer.abort){
// stopped at user request
return false;
}
} catch (Exception e) {
error.fException = e;
}
}
return true;
} //writeURI
//
// Private methods
//
// REVISIT: this is inefficient implementation of well-formness. Instead, we should check
// well-formness as we serialize the tree
boolean verifyNames = true;
: node.getOwnerDocument();
try {
if (versionChanged != null) {
}
} catch (Exception e) {
//no way to test the version...
//ignore the exception
}
// Move down to first child
// No child nodes, so walk tree
// Move to sibling if possible.
break;
}
}
}
}
}
else {
}
}
}
boolean wellformed;
switch (type) {
case Node.DOCUMENT_NODE:{
break;
}
case Node.DOCUMENT_TYPE_NODE:{
break;
}
case Node.ELEMENT_NODE:{
if (verifyNames){
}
else{
}
if (!wellformed){
if (!wellformed){
if (fErrorHandler != null) {
"wf-invalid-character-in-node-name",
"wf-invalid-character-in-node-name");
}
}
}
}
if (attributes != null) {
if (verifyNames) {
if (!wellformed) {
"wf-invalid-character-in-node-name",
"wf-invalid-character-in-node-name");
}
}
}
}
break;
}
case Node.COMMENT_NODE: {
// only verify well-formness if comments included in the tree
DOMNormalizer.isCommentWF(fErrorHandler, fError, fLocator, ((Comment)node).getData(), xml11Version);
break;
}
case Node.ENTITY_REFERENCE_NODE: {
// only if entity is preserved in the tree
}
break;
}
case Node.CDATA_SECTION_NODE: {
// verify content
// the ]]> string will be checked during serialization
break;
}
break;
}
case Node.PROCESSING_INSTRUCTION_NODE:{
if (verifyNames) {
if (xml11Version) {
} else {
}
if (!wellformed) {
"wf-invalid-character-in-node-name",
msg,
"wf-invalid-character-in-node-name");
}
}
break;
}
}
}
// Locate the escape characters
for(int i = 1; i < size; ++i) {
// Decode the 2 digit hexadecimal number following % in '%nn'
}
}
return origPath;
}
}//DOMSerializerImpl