You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@xerces.apache.org by ne...@apache.org on 2003/07/30 13:14:08 UTC
cvs commit: xml-xerces/java/src/org/apache/xml/serialize DOMSerializerImpl.java BaseMarkupSerializer.java XML11Serializer.java XMLSerializer.java DOMWriterImpl.java
neeraj 2003/07/30 04:14:08
Modified: java/src/org/apache/xml/serialize BaseMarkupSerializer.java
XML11Serializer.java XMLSerializer.java
Added: java/src/org/apache/xerces/parsers DOMParserImpl.java
java/src/org/apache/xml/serialize DOMSerializerImpl.java
Removed: java/src/org/apache/xerces/parsers DOMBuilderImpl.java
java/src/org/apache/xml/serialize DOMWriterImpl.java
Log:
Applying patches to upgrade to latest DOM L3 LS draft. Thanks Gopal.
DOMBuilderImpl.java is changed to DOMParserImpl.java
DOMWriterImpl.java is changed to DOMSerializerImpl.java
Revision Changes Path
1.1 xml-xerces/java/src/org/apache/xerces/parsers/DOMParserImpl.java
Index: DOMParserImpl.java
===================================================================
/*
* The Apache Software License, Version 1.1
*
*
* Copyright (c) 2000-2003 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Xerces" and "Apache Software Foundation" must
* not be used to endorse or promote products derived from this
* software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache",
* nor may "Apache" appear in their name, without prior written
* permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation and was
* originally based on software copyright (c) 2001, International
* Business Machines, Inc., http://www.apache.org. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
package org.apache.xerces.parsers;
import java.io.StringReader;
import java.util.Stack;
import java.util.StringTokenizer;
import java.util.Vector;
import org.apache.xerces.dom.DOMErrorImpl;
import org.apache.xerces.dom.DOMMessageFormatter;
import org.apache.xerces.dom3.DOMConfiguration;
import org.apache.xerces.dom3.DOMError;
import org.apache.xerces.dom3.DOMErrorHandler;
import org.apache.xerces.impl.Constants;
import org.apache.xerces.util.DOMEntityResolverWrapper;
import org.apache.xerces.util.DOMErrorHandlerWrapper;
import org.apache.xerces.util.ObjectFactory;
import org.apache.xerces.util.SymbolTable;
import org.apache.xerces.xni.grammars.XMLGrammarPool;
import org.apache.xerces.xni.parser.XMLConfigurationException;
import org.apache.xerces.xni.parser.XMLEntityResolver;
import org.apache.xerces.xni.parser.XMLInputSource;
import org.apache.xerces.xni.parser.XMLParserConfiguration;
import org.w3c.dom.DOMException;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.ls.DOMBuilder;
import org.w3c.dom.ls.DOMBuilderFilter;
import org.w3c.dom.ls.DOMEntityResolver;
import org.w3c.dom.ls.DOMInputSource;
/**
* This is Xerces DOM Builder class. It uses the abstract DOM
* parser with a document scanner, a dtd scanner, and a validator, as
* well as a grammar pool.
*
* @author Pavani Mukthipudi, Sun Microsystems Inc.
* @author Elena Litani, IBM
* @author Rahul Srivastava, Sun Microsystems Inc.
* @version $Id: DOMParserImpl.java,v 1.1 2003/07/30 11:14:07 neeraj Exp $
*/
public class DOMBuilderImpl
extends AbstractDOMParser implements DOMBuilder, DOMConfiguration {
// SAX & Xerces feature ids
/** Feature identifier: namespaces. */
protected static final String NAMESPACES =
Constants.SAX_FEATURE_PREFIX + Constants.NAMESPACES_FEATURE;
/** Feature id: validation. */
protected static final String VALIDATION_FEATURE =
Constants.SAX_FEATURE_PREFIX+Constants.VALIDATION_FEATURE;
/** XML Schema validation */
protected static final String XMLSCHEMA =
Constants.XERCES_FEATURE_PREFIX + Constants.SCHEMA_VALIDATION_FEATURE;
/** Dynamic validation */
protected static final String DYNAMIC_VALIDATION =
Constants.XERCES_FEATURE_PREFIX + Constants.DYNAMIC_VALIDATION_FEATURE;
/** Feature identifier: expose schema normalized value */
protected static final String NORMALIZE_DATA =
Constants.XERCES_FEATURE_PREFIX + Constants.SCHEMA_NORMALIZED_VALUE;
// internal properties
protected static final String SYMBOL_TABLE =
Constants.XERCES_PROPERTY_PREFIX + Constants.SYMBOL_TABLE_PROPERTY;
protected static final String PSVI_AUGMENT =
Constants.XERCES_FEATURE_PREFIX +Constants.SCHEMA_AUGMENT_PSVI;
//
// Data
//
// REVISIT: this value should be null by default and should be set during creation of
// DOMBuilder
protected String fSchemaType = null;
protected boolean fBusy = false;
protected final static boolean DEBUG = false;
private Vector fSchemaLocations = new Vector();
private String fSchemaLocation = null;
//
// Constructors
//
/**
* Constructs a DOM Builder using the standard parser configuration.
*/
public DOMBuilderImpl(String configuration, String schemaType) {
this(
(XMLParserConfiguration) ObjectFactory.createObject(
"org.apache.xerces.xni.parser.XMLParserConfiguration",
configuration));
if (schemaType != null) {
if (schemaType.equals(Constants.NS_DTD)) {
fConfiguration.setFeature(
Constants.XERCES_FEATURE_PREFIX + Constants.SCHEMA_VALIDATION_FEATURE,
false);
fConfiguration.setProperty(
Constants.JAXP_PROPERTY_PREFIX + Constants.SCHEMA_LANGUAGE,
Constants.NS_DTD);
fSchemaType = Constants.NS_DTD;
}
else if (schemaType.equals(Constants.NS_XMLSCHEMA)) {
// XML Schem validation
fConfiguration.setProperty(
Constants.JAXP_PROPERTY_PREFIX + Constants.SCHEMA_LANGUAGE,
Constants.NS_XMLSCHEMA);
}
}
}
/**
* Constructs a DOM Builder using the specified parser configuration.
*/
public DOMBuilderImpl(XMLParserConfiguration config) {
super(config);
// add recognized features
final String[] domRecognizedFeatures = {
Constants.DOM_CANONICAL_FORM,
Constants.DOM_CDATA_SECTIONS,
Constants.DOM_CHARSET_OVERRIDES_XML_ENCODING,
Constants.DOM_INFOSET,
Constants.DOM_NAMESPACE_DECLARATIONS,
Constants.DOM_SUPPORTED_MEDIATYPES_ONLY,
Constants.DOM_CERTIFIED,
Constants.DOM_WELLFORMED,
};
fConfiguration.addRecognizedFeatures(domRecognizedFeatures);
// turn off deferred DOM
fConfiguration.setFeature(DEFER_NODE_EXPANSION, false);
// set default values
fConfiguration.setFeature(Constants.DOM_CANONICAL_FORM, false);
fConfiguration.setFeature(Constants.DOM_CDATA_SECTIONS, true);
fConfiguration.setFeature(Constants.DOM_CHARSET_OVERRIDES_XML_ENCODING, true);
fConfiguration.setFeature(Constants.DOM_INFOSET, false);
fConfiguration.setFeature(Constants.DOM_NAMESPACE_DECLARATIONS, true);
fConfiguration.setFeature(Constants.DOM_SUPPORTED_MEDIATYPES_ONLY, false);
fConfiguration.setFeature(Constants.DOM_WELLFORMED, true);
// REVISIT: by default Xerces assumes that input is certified.
// default is different from the one specified in the DOM spec
fConfiguration.setFeature(Constants.DOM_CERTIFIED, true);
// Xerces datatype-normalization feature is on by default
fConfiguration.setFeature( NORMALIZE_DATA, false );
} // <init>(XMLParserConfiguration)
/**
* Constructs a DOM Builder using the specified symbol table.
*/
public DOMBuilderImpl(SymbolTable symbolTable) {
this(
(XMLParserConfiguration) ObjectFactory.createObject(
"org.apache.xerces.xni.parser.XMLParserConfiguration",
"org.apache.xerces.parsers.XML11Configuration"));
fConfiguration.setProperty(
Constants.XERCES_PROPERTY_PREFIX + Constants.SYMBOL_TABLE_PROPERTY,
symbolTable);
} // <init>(SymbolTable)
/**
* Constructs a DOM Builder using the specified symbol table and
* grammar pool.
*/
public DOMBuilderImpl(SymbolTable symbolTable, XMLGrammarPool grammarPool) {
this(
(XMLParserConfiguration) ObjectFactory.createObject(
"org.apache.xerces.xni.parser.XMLParserConfiguration",
"org.apache.xerces.parsers.XML11Configuration"));
fConfiguration.setProperty(
Constants.XERCES_PROPERTY_PREFIX + Constants.SYMBOL_TABLE_PROPERTY,
symbolTable);
fConfiguration.setProperty(
Constants.XERCES_PROPERTY_PREFIX
+ Constants.XMLGRAMMAR_POOL_PROPERTY,
grammarPool);
}
/**
* Resets the parser state.
*
* @throws SAXException Thrown on initialization error.
*/
public void reset() {
super.reset();
// DOM Filter
if (fSkippedElemStack!=null) {
fSkippedElemStack.removeAllElements();
}
fSchemaLocations.clear();
fRejectedElement.clear();
fFilterReject = false;
fSchemaType = null;
} // reset()
//
// DOMBuilder methods
//
public DOMConfiguration getConfig(){
return this;
}
/**
* When the application provides a filter, the parser will call out to
* the filter at the completion of the construction of each
* <code>Element</code> node. The filter implementation can choose to
* remove the element from the document being constructed (unless the
* element is the document element) or to terminate the parse early. If
* the document is being validated when it's loaded the validation
* happens before the filter is called.
*/
public DOMBuilderFilter getFilter() {
return fDOMFilter;
}
/**
* When the application provides a filter, the parser will call out to
* the filter at the completion of the construction of each
* <code>Element</code> node. The filter implementation can choose to
* remove the element from the document being constructed (unless the
* element is the document element) or to terminate the parse early. If
* the document is being validated when it's loaded the validation
* happens before the filter is called.
*/
public void setFilter(DOMBuilderFilter filter) {
fDOMFilter = filter;
if (fSkippedElemStack == null) {
fSkippedElemStack = new Stack();
}
}
/**
* Set parameters and properties
*/
public void setParameter(String name, Object value) throws DOMException {
// set features
if(value instanceof Boolean){
boolean state = ((Boolean)value).booleanValue();
try {
if (name.equals(Constants.DOM_COMMENTS)) {
fConfiguration.setFeature(INCLUDE_COMMENTS_FEATURE, state);
}
else if (name.equals(Constants.DOM_DATATYPE_NORMALIZATION)) {
fConfiguration.setFeature(NORMALIZE_DATA, state);
}
else if (name.equals(Constants.DOM_ENTITIES)) {
fConfiguration.setFeature(CREATE_ENTITY_REF_NODES, state);
}
else if (name.equals(Constants.DOM_INFOSET)
|| name.equals(Constants.DOM_SUPPORTED_MEDIATYPES_ONLY)
|| name.equals(Constants.DOM_CANONICAL_FORM)) {
if (state) { // true is not supported
String msg =
DOMMessageFormatter.formatMessage(
DOMMessageFormatter.DOM_DOMAIN,
"FEATURE_NOT_SUPPORTED",
new Object[] { name });
throw new DOMException(DOMException.NOT_SUPPORTED_ERR, msg);
}
// setting those features to false is no-op
}
else if (name.equals(Constants.DOM_NAMESPACES)) {
fConfiguration.setFeature(NAMESPACES, state);
}
else if ( name.equals(Constants.DOM_CDATA_SECTIONS)
|| name.equals(Constants.DOM_NAMESPACE_DECLARATIONS)
|| name.equals(Constants.DOM_WELLFORMED) ) {
if (!state) { // false is not supported
String msg =
DOMMessageFormatter.formatMessage(
DOMMessageFormatter.DOM_DOMAIN,
"FEATURE_NOT_SUPPORTED",
new Object[] { name });
throw new DOMException(DOMException.NOT_SUPPORTED_ERR, msg);
}
// setting these features to true is no-op
// REVISIT: implement "namespace-declaration" feature
}
else if (name.equals(Constants.DOM_VALIDATE)) {
fConfiguration.setFeature(VALIDATION_FEATURE, state);
if (fSchemaType != Constants.NS_DTD) {
fConfiguration.setFeature(XMLSCHEMA, state);
}
if (state){
fConfiguration.setFeature(DYNAMIC_VALIDATION, false);
}
}
else if (name.equals(Constants.DOM_VALIDATE_IF_SCHEMA)) {
fConfiguration.setFeature(DYNAMIC_VALIDATION, state);
// Note: validation and dynamic validation are mutually exclusive
if (state){
fConfiguration.setFeature(VALIDATION_FEATURE, false);
}
}
else if (name.equals(Constants.DOM_WHITESPACE_IN_ELEMENT_CONTENT)) {
fConfiguration.setFeature(INCLUDE_IGNORABLE_WHITESPACE, state);
}
else if (name.equals(Constants.DOM_PSVI)){
//XSModel - turn on PSVI augmentation
fConfiguration.setFeature(PSVI_AUGMENT, true);
fConfiguration.setProperty(DOCUMENT_CLASS_NAME,
"org.apache.xerces.dom.PSVIDocumentImpl");
}
else {
// Constants.DOM_CHARSET_OVERRIDES_XML_ENCODING feature
// or any Xerces feature
fConfiguration.setFeature(name, state);
}
}
catch (XMLConfigurationException e) {
String msg =
DOMMessageFormatter.formatMessage(
DOMMessageFormatter.DOM_DOMAIN,
"FEATURE_NOT_FOUND",
new Object[] { name });
throw new DOMException(DOMException.NOT_FOUND_ERR, msg);
}
}
else { // set properties
if (name.equals(Constants.DOM_ERROR_HANDLER)) {
if (value instanceof DOMErrorHandler) {
try {
fErrorHandler = new DOMErrorHandlerWrapper((DOMErrorHandler) value);
fConfiguration.setProperty(ERROR_HANDLER, fErrorHandler);
}
catch (XMLConfigurationException e) {}
}
else {
// REVISIT: type mismatch
String msg =
DOMMessageFormatter.formatMessage(
DOMMessageFormatter.DOM_DOMAIN,
"FEATURE_NOT_SUPPORTED",
new Object[] { name });
throw new DOMException(DOMException.NOT_SUPPORTED_ERR, msg);
}
}
else if (name.equals(Constants.DOM_ENTITY_RESOLVER)) {
if (value instanceof DOMEntityResolver) {
try {
fConfiguration.setProperty(ENTITY_RESOLVER, new DOMEntityResolverWrapper((DOMEntityResolver) value));
}
catch (XMLConfigurationException e) {}
}
else {
// REVISIT: type mismatch
String msg =
DOMMessageFormatter.formatMessage(
DOMMessageFormatter.DOM_DOMAIN,
"FEATURE_NOT_SUPPORTED",
new Object[] { name });
throw new DOMException(DOMException.NOT_SUPPORTED_ERR, msg);
}
}
else if (name.equals(Constants.DOM_SCHEMA_LOCATION)) {
if (value instanceof String) {
try {
if (fSchemaType == Constants.NS_XMLSCHEMA) {
fSchemaLocation = (String)value;
// map DOM schema-location to JAXP schemaSource property
// tokenize location string
StringTokenizer t = new StringTokenizer(fSchemaLocation, " \n\t\r");
if (t.hasMoreTokens()){
fSchemaLocations.clear();
fSchemaLocations.add(t.nextToken ());
while (t.hasMoreTokens()) {
fSchemaLocations.add(t.nextToken ());
}
fConfiguration.setProperty(
Constants.JAXP_PROPERTY_PREFIX + Constants.SCHEMA_SOURCE,
fSchemaLocations.toArray());
}
else {
fConfiguration.setProperty(
Constants.JAXP_PROPERTY_PREFIX + Constants.SCHEMA_SOURCE,
value);
}
}
else {
// REVISIT: allow pre-parsing DTD grammars
String msg =
DOMMessageFormatter.formatMessage(
DOMMessageFormatter.DOM_DOMAIN,
"FEATURE_NOT_SUPPORTED",
new Object[] { name });
throw new DOMException(DOMException.NOT_SUPPORTED_ERR, msg);
}
}
catch (XMLConfigurationException e) {}
}
else {
// REVISIT: type mismatch
String msg =
DOMMessageFormatter.formatMessage(
DOMMessageFormatter.DOM_DOMAIN,
"FEATURE_NOT_SUPPORTED",
new Object[] { name });
throw new DOMException(DOMException.NOT_SUPPORTED_ERR, msg);
}
}
else if (name.equals(Constants.DOM_SCHEMA_TYPE)) {
// REVISIT: should null value be supported?
if (value instanceof String) {
try {
if (value.equals(Constants.NS_XMLSCHEMA)) {
// turn on schema feature
fConfiguration.setFeature(Constants.XERCES_FEATURE_PREFIX
+ Constants.SCHEMA_VALIDATION_FEATURE,
true);
// map to JAXP schemaLanguage
fConfiguration.setProperty( Constants.JAXP_PROPERTY_PREFIX
+ Constants.SCHEMA_LANGUAGE,
Constants.NS_XMLSCHEMA);
fSchemaType = Constants.NS_XMLSCHEMA;
}
else if (value.equals(Constants.NS_DTD)) {
fConfiguration.setFeature(
Constants.XERCES_FEATURE_PREFIX
+ Constants.SCHEMA_VALIDATION_FEATURE,
false);
fConfiguration.setProperty( Constants.JAXP_PROPERTY_PREFIX
+ Constants.SCHEMA_LANGUAGE,
Constants.NS_DTD);
fSchemaType = Constants.NS_DTD;
}
}
catch (XMLConfigurationException e) {}
}
else {
String msg =
DOMMessageFormatter.formatMessage(
DOMMessageFormatter.DOM_DOMAIN,
"FEATURE_NOT_SUPPORTED",
new Object[] { name });
throw new DOMException(DOMException.NOT_SUPPORTED_ERR, msg);
}
}
else if (name.equals(DOCUMENT_CLASS_NAME)) {
fConfiguration.setProperty(DOCUMENT_CLASS_NAME, value);
}
else {
// REVISIT: check if this is a boolean parameter -- type mismatch should be thrown.
//parameter is not recognized
String msg =
DOMMessageFormatter.formatMessage(
DOMMessageFormatter.DOM_DOMAIN,
"FEATURE_NOT_FOUND",
new Object[] { name });
throw new DOMException(DOMException.NOT_FOUND_ERR, msg);
}
}
}
/**
* Look up the value of a feature or a property.
*/
public Object getParameter(String name) throws DOMException {
if (name.equals(Constants.DOM_COMMENTS)) {
return (fConfiguration.getFeature(INCLUDE_COMMENTS_FEATURE))
? Boolean.TRUE
: Boolean.FALSE;
}
else if (name.equals(Constants.DOM_DATATYPE_NORMALIZATION)) {
return (fConfiguration.getFeature(NORMALIZE_DATA))
? Boolean.TRUE
: Boolean.FALSE;
}
else if (name.equals(Constants.DOM_ENTITIES)) {
return (fConfiguration.getFeature(CREATE_ENTITY_REF_NODES))
? Boolean.TRUE
: Boolean.FALSE;
}
else if (name.equals(Constants.DOM_NAMESPACES)) {
return (fConfiguration.getFeature(NAMESPACES))
? Boolean.TRUE
: Boolean.FALSE;
}
else if (name.equals(Constants.DOM_VALIDATE)) {
return (fConfiguration.getFeature(VALIDATION_FEATURE))
? Boolean.TRUE
: Boolean.FALSE;
}
else if (name.equals(Constants.DOM_VALIDATE_IF_SCHEMA)) {
return (fConfiguration.getFeature(DYNAMIC_VALIDATION))
? Boolean.TRUE
: Boolean.FALSE;
}
else if (name.equals(Constants.DOM_WHITESPACE_IN_ELEMENT_CONTENT)) {
return (fConfiguration.getFeature(INCLUDE_IGNORABLE_WHITESPACE))
? Boolean.TRUE
: Boolean.FALSE;
}
else if (
name.equals(Constants.DOM_NAMESPACE_DECLARATIONS)
|| name.equals(Constants.DOM_CDATA_SECTIONS)
|| name.equals(Constants.DOM_WELLFORMED)
|| name.equals(Constants.DOM_CANONICAL_FORM)
|| name.equals(Constants.DOM_SUPPORTED_MEDIATYPES_ONLY)
|| name.equals(Constants.DOM_INFOSET)
|| name.equals(Constants.DOM_CHARSET_OVERRIDES_XML_ENCODING)) {
return (fConfiguration.getFeature(name))
? Boolean.TRUE
: Boolean.FALSE;
}
else if (name.equals(Constants.DOM_ERROR_HANDLER)) {
if (fErrorHandler != null) {
return fErrorHandler.getErrorHandler();
}
return null;
}
else if (name.equals(Constants.DOM_ENTITY_RESOLVER)) {
try {
XMLEntityResolver entityResolver =
(XMLEntityResolver) fConfiguration.getProperty(ENTITY_RESOLVER);
if (entityResolver != null
&& entityResolver instanceof DOMEntityResolverWrapper) {
return ((DOMEntityResolverWrapper) entityResolver).getEntityResolver();
}
return null;
}
catch (XMLConfigurationException e) {}
}
else if (name.equals(Constants.DOM_SCHEMA_TYPE)) {
return fConfiguration.getProperty(
Constants.JAXP_PROPERTY_PREFIX + Constants.SCHEMA_LANGUAGE);
}
else if (name.equals(Constants.DOM_SCHEMA_LOCATION)) {
return fSchemaLocation;
}
else if (name.equals(SYMBOL_TABLE)){
return fConfiguration.getProperty(SYMBOL_TABLE);
}
else if (name.equals(DOCUMENT_CLASS_NAME)) {
return fConfiguration.getProperty(DOCUMENT_CLASS_NAME);
}
else {
String msg =
DOMMessageFormatter.formatMessage(
DOMMessageFormatter.DOM_DOMAIN,
"FEATURE_NOT_FOUND",
new Object[] { name });
throw new DOMException(DOMException.NOT_FOUND_ERR, msg);
}
return null;
}
public boolean canSetParameter(String name, Object value) {
if(value instanceof Boolean){
boolean state = ((Boolean)value).booleanValue();
if (name.equals(Constants.DOM_INFOSET)
|| name.equals(Constants.DOM_SUPPORTED_MEDIATYPES_ONLY)
|| name.equals(Constants.DOM_CANONICAL_FORM)) {
// true is not supported
return (state) ? false : true;
}
else if (
name.equals(Constants.DOM_CDATA_SECTIONS)
|| name.equals(Constants.DOM_NAMESPACE_DECLARATIONS)
|| name.equals(Constants.DOM_WELLFORMED) ) {
// false is not supported
return (state) ? true : false;
}
else if (
name.equals(Constants.DOM_CHARSET_OVERRIDES_XML_ENCODING)
|| name.equals(Constants.DOM_COMMENTS)
|| name.equals(Constants.DOM_DATATYPE_NORMALIZATION)
|| name.equals(Constants.DOM_ENTITIES)
|| name.equals(Constants.DOM_NAMESPACES)
|| name.equals(Constants.DOM_VALIDATE)
|| name.equals(Constants.DOM_VALIDATE_IF_SCHEMA)
|| name.equals(Constants.DOM_WHITESPACE_IN_ELEMENT_CONTENT)) {
return true;
}
// Recognize Xerces features.
try {
fConfiguration.getFeature(name);
return true;
}
catch (XMLConfigurationException e) {
return false;
}
}
else { // check properties
if (name.equals(Constants.DOM_ERROR_HANDLER)) {
if (value instanceof DOMErrorHandler) {
return true;
}
return false;
}
else if (name.equals(Constants.DOM_ENTITY_RESOLVER)) {
if (value instanceof DOMEntityResolver) {
return true;
}
return false;
}
else if (name.equals(Constants.DOM_SCHEMA_TYPE)) {
if (value instanceof String
&& (value.equals(Constants.NS_XMLSCHEMA)
|| value.equals(Constants.NS_DTD))) {
return true;
}
return false;
}
else if (name.equals(Constants.DOM_SCHEMA_LOCATION)) {
if (value instanceof String)
return true;
return false;
}
else if (name.equals(DOCUMENT_CLASS_NAME)){
return true;
}
return false;
}
}
/**
* Parse an XML document from a location identified by an URI reference.
* If the URI contains a fragment identifier (see section 4.1 in ), the
* behavior is not defined by this specification.
*
*/
public Document parseURI(String uri) {
XMLInputSource source = new XMLInputSource(null, uri, null);
fBusy = true;
try {
parse(source);
fBusy = false;
} catch (Exception e){
fBusy = false;
if (fErrorHandler != null) {
DOMErrorImpl error = new DOMErrorImpl();
error.fException = e;
error.fMessage = e.getMessage();
error.fSeverity = DOMError.SEVERITY_FATAL_ERROR;
fErrorHandler.getErrorHandler().handleError(error);
}
if (DEBUG) {
e.printStackTrace();
}
}
return getDocument();
}
/**
* Parse an XML document from a resource identified by an
* <code>DOMInputSource</code>.
*
*/
public Document parse(DOMInputSource is) {
// need to wrap the DOMInputSource with an XMLInputSource
XMLInputSource xmlInputSource = dom2xmlInputSource(is);
fBusy = true;
try {
parse(xmlInputSource);
fBusy = false;
} catch (Exception e) {
fBusy = false;
if (fErrorHandler != null) {
DOMErrorImpl error = new DOMErrorImpl();
error.fException = e;
error.fMessage = e.getMessage();
error.fSeverity = DOMError.SEVERITY_FATAL_ERROR;
fErrorHandler.getErrorHandler().handleError(error);
}
if (DEBUG) {
e.printStackTrace();
}
}
return getDocument();
}
/**
* Parse an XML document or fragment from a resource identified by an
* <code>DOMInputSource</code> and insert the content into an existing
* document at the position epcified with the <code>contextNode</code>
* and <code>action</code> arguments. When parsing the input stream the
* context node is used for resolving unbound namespace prefixes.
*
* @param is The <code>DOMInputSource</code> from which the source
* document is to be read.
* @param cnode The <code>Node</code> that is used as the context for
* the data that is being parsed.
* @param action This parameter describes which action should be taken
* between the new set of node being inserted and the existing
* children of the context node. The set of possible actions is
* defined above.
* @exception DOMException
* HIERARCHY_REQUEST_ERR: Thrown if this action results in an invalid
* hierarchy (i.e. a Document with more than one document element).
*/
public Node parseWithContext(DOMInputSource is, Node cnode,
short action) throws DOMException {
// REVISIT: need to implement.
throw new DOMException(DOMException.NOT_SUPPORTED_ERR, "Not supported");
}
/**
* NON-DOM: convert DOMInputSource to XNIInputSource
*
* @param is
* @return
*/
XMLInputSource dom2xmlInputSource(DOMInputSource is) {
// need to wrap the DOMInputSource with an XMLInputSource
XMLInputSource xis = null;
// if there is a string data, use a StringReader
// according to DOM, we need to treat such data as "UTF-16".
if (is.getStringData() != null) {
xis = new XMLInputSource(is.getPublicId(), is.getSystemId(),
is.getBaseURI(), new StringReader(is.getStringData()),
"UTF-16");
}
// check whether there is a Reader
// according to DOM, we need to treat such reader as "UTF-16".
else if (is.getCharacterStream() != null) {
xis = new XMLInputSource(is.getPublicId(), is.getSystemId(),
is.getBaseURI(), is.getCharacterStream(),
"UTF-16");
}
// check whether there is an InputStream
else if (is.getByteStream() != null) {
xis = new XMLInputSource(is.getPublicId(), is.getSystemId(),
is.getBaseURI(), is.getByteStream(),
is.getEncoding());
}
// otherwise, just use the public/system/base Ids
else {
xis = new XMLInputSource(is.getPublicId(), is.getSystemId(),
is.getBaseURI());
}
return xis;
}
/**
* @see org.w3c.dom.ls.DOMBuilder#getAsync()
*/
public boolean getAsync() {
return false;
}
/**
* @see org.w3c.dom.ls.DOMBuilder#getBusy()
*/
public boolean getBusy() {
return fBusy;
}
} // class DOMBuilderImpl
1.46 +3 -3 xml-xerces/java/src/org/apache/xml/serialize/BaseMarkupSerializer.java
Index: BaseMarkupSerializer.java
===================================================================
RCS file: /home/cvs/xml-xerces/java/src/org/apache/xml/serialize/BaseMarkupSerializer.java,v
retrieving revision 1.45
retrieving revision 1.46
diff -u -r1.45 -r1.46
--- BaseMarkupSerializer.java 26 May 2003 17:53:38 -0000 1.45
+++ BaseMarkupSerializer.java 30 Jul 2003 11:14:08 -0000 1.46
@@ -106,7 +106,7 @@
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.Notation;
-import org.w3c.dom.ls.DOMWriterFilter;
+import org.w3c.dom.ls.DOMSerializerFilter;
import org.w3c.dom.traversal.NodeFilter;
import org.xml.sax.ContentHandler;
import org.xml.sax.DTDHandler;
@@ -174,7 +174,7 @@
protected Hashtable fFeatures;
protected DOMErrorHandler fDOMErrorHandler;
protected final DOMErrorImpl fDOMError = new DOMErrorImpl();
- protected DOMWriterFilter fDOMFilter;
+ protected DOMSerializerFilter fDOMFilter;
protected EncodingInfo _encodingInfo;
1.5 +5 -5 xml-xerces/java/src/org/apache/xml/serialize/XML11Serializer.java
Index: XML11Serializer.java
===================================================================
RCS file: /home/cvs/xml-xerces/java/src/org/apache/xml/serialize/XML11Serializer.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- XML11Serializer.java 30 May 2003 13:35:44 -0000 1.4
+++ XML11Serializer.java 30 Jul 2003 11:14:08 -0000 1.5
@@ -99,9 +99,9 @@
* The serializer supports both DOM and SAX. SAX serializing is done by firing
* SAX events and using the serializer as a document handler. DOM serializing is done
* by calling {@link #serialize(Document)} or by using DOM Level 3
- * {@link org.w3c.dom.ls.DOMWriter} and
- * serializing with {@link org.w3c.dom.ls.DOMWriter#writeNode},
- * {@link org.w3c.dom.ls.DOMWriter#writeToString}.
+ * {@link org.w3c.dom.ls.DOMSerializer} and
+ * serializing with {@link org.w3c.dom.ls.DOMSerializer#write},
+ * {@link org.w3c.dom.ls.DOMSerializer#writeToString}.
* <p>
* If an I/O exception occurs while serializing, the serializer
* will not throw an exception directly, but only throw it
@@ -133,7 +133,7 @@
//
//
- // DOM Level 3 implementation: variables intialized in DOMWriterImpl
+ // DOM Level 3 implementation: variables intialized in DOMSerializerImpl
//
/** stores namespaces in scope */
1.53 +6 -6 xml-xerces/java/src/org/apache/xml/serialize/XMLSerializer.java
Index: XMLSerializer.java
===================================================================
RCS file: /home/cvs/xml-xerces/java/src/org/apache/xml/serialize/XMLSerializer.java,v
retrieving revision 1.52
retrieving revision 1.53
diff -u -r1.52 -r1.53
--- XMLSerializer.java 10 Jun 2003 13:41:29 -0000 1.52
+++ XMLSerializer.java 30 Jul 2003 11:14:08 -0000 1.53
@@ -108,9 +108,9 @@
* The serializer supports both DOM and SAX. SAX serializing is done by firing
* SAX events and using the serializer as a document handler. DOM serializing is done
* by calling {@link #serialize(Document)} or by using DOM Level 3
- * {@link org.w3c.dom.ls.DOMWriter} and
- * serializing with {@link org.w3c.dom.ls.DOMWriter#writeNode},
- * {@link org.w3c.dom.ls.DOMWriter#writeToString}.
+ * {@link org.w3c.dom.ls.DOMSerializer} and
+ * serializing with {@link org.w3c.dom.ls.DOMSerializer#write},
+ * {@link org.w3c.dom.ls.DOMSerializer#writeToString}.
* <p>
* If an I/O exception occurs while serializing, the serializer
* will not throw an exception directly, but only throw it
@@ -142,7 +142,7 @@
//
//
- // DOM Level 3 implementation: variables intialized in DOMWriterImpl
+ // DOM Level 3 implementation: variables intialized in DOMSerializerImpl
//
/** stores namespaces in scope */
@@ -228,7 +228,7 @@
/**
* This methods turns on namespace fixup algorithm during
* DOM serialization.
- * @see org.w3c.dom.ls.DOMWriter
+ * @see org.w3c.dom.ls.DOMSerializer
*
* @param namespaces
*/
1.1 xml-xerces/java/src/org/apache/xml/serialize/DOMSerializerImpl.java
Index: DOMSerializerImpl.java
===================================================================
/*
* The Apache Software License, Version 1.1
*
*
* Copyright (c) 1999-2002 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Xerces" and "Apache Software Foundation" must
* not be used to endorse or promote products derived from this
* software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache",
* nor may "Apache" appear in their name, without prior written
* permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation and was
* originally based on software copyright (c) 2002, International
* Business Machines, Inc., http://www.apache.org. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
package org.apache.xml.serialize;
import java.io.IOException;
import java.io.StringWriter;
import java.io.OutputStream;
import java.io.Writer;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.File;
import java.lang.reflect.Method;
import java.util.Enumeration;
import java.util.Hashtable;
import org.apache.xerces.dom.DOMErrorImpl;
import org.apache.xerces.dom.DOMMessageFormatter;
import org.apache.xerces.dom3.DOMConfiguration;
import org.apache.xerces.dom3.DOMError;
import org.apache.xerces.dom3.DOMErrorHandler;
import org.apache.xerces.impl.Constants;
import org.apache.xerces.util.NamespaceSupport;
import org.apache.xerces.util.SymbolTable;
import org.w3c.dom.DOMException;
import org.w3c.dom.Document;
import org.w3c.dom.DocumentFragment;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.ls.DOMSerializer;
import org.w3c.dom.ls.DOMSerializerFilter;
import org.w3c.dom.ls.DOMOutput;
/**
* Implemenatation of DOM Level 3 org.w3c.ls.DOMSerializer by delegating serialization
* calls to <CODE>XMLSerializer</CODE>.
* DOMSerializer 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
* @version $Id: DOMSerializerImpl.java,v 1.1 2003/07/30 11:14:08 neeraj Exp $
*/
public class DOMSerializerImpl implements DOMSerializer, DOMConfiguration {
// data
private String fEncoding;
// serializer
private XMLSerializer serializer;
// XML 1.1 serializer
private XML11Serializer xml11Serializer;
/**
* Constructs a new DOMSerializer.
* 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() {
serializer = new XMLSerializer();
initSerializer(serializer);
}
//
// DOMSerializer methods
//
public DOMConfiguration getConfig(){
return this;
}
/** DOM L3-EXPERIMENTAL:
* Setter for boolean and object parameters
*/
public void setParameter(String name, Object value) throws DOMException {
if (serializer.fFeatures.containsKey(name)) {
// This is a feature
if (value instanceof Boolean){
boolean state = ((Boolean)value).booleanValue();
if (name.equals(Constants.DOM_XMLDECL)){
serializer._format.setOmitXMLDeclaration(!state);
serializer.fFeatures.put(name, value);
}
else if (name.equals(Constants.DOM_NAMESPACES)){
serializer.fNamespaces = state;
serializer.fFeatures.put(name, value);
}
else if (name.equals(Constants.DOM_SPLIT_CDATA)
|| name.equals(Constants.DOM_DISCARD_DEFAULT_CONTENT)){
// both values supported
serializer.fFeatures.put(name, value);
}
else if (name.equals(Constants.DOM_CANONICAL_FORM)
|| name.equals(Constants.DOM_VALIDATE_IF_SCHEMA)
|| name.equals(Constants.DOM_VALIDATE)
|| name.equals(Constants.DOM_CHECK_CHAR_NORMALIZATION)
|| name.equals(Constants.DOM_DATATYPE_NORMALIZATION)
|| name.equals(Constants.DOM_FORMAT_PRETTY_PRINT)
|| name.equals(Constants.DOM_NORMALIZE_CHARACTERS)
// REVISIT: these must be supported
|| name.equals(Constants.DOM_WELLFORMED)) {
// true is not supported
if (state){
String msg = DOMMessageFormatter.formatMessage(
DOMMessageFormatter.DOM_DOMAIN,
"FEATURE_NOT_SUPPORTED",
new Object[] { name });
throw new DOMException(DOMException.NOT_SUPPORTED_ERR, msg);
}
}
else if (name.equals(Constants.DOM_INFOSET)
|| name.equals(Constants.DOM_NAMESPACE_DECLARATIONS)
|| name.equals(Constants.DOM_WHITESPACE_IN_ELEMENT_CONTENT)
|| name.equals(Constants.DOM_IGNORE_CHAR_DENORMALIZATION)
// REVISIT: these must be supported
|| name.equals(Constants.DOM_ENTITIES)
|| name.equals(Constants.DOM_CDATA_SECTIONS)
|| name.equals(Constants.DOM_COMMENTS)) {
// false is not supported
if (!state){
String msg = DOMMessageFormatter.formatMessage(
DOMMessageFormatter.DOM_DOMAIN,
"FEATURE_NOT_SUPPORTED",
new Object[] { name });
throw new DOMException(DOMException.NOT_SUPPORTED_ERR, msg);
}
}
else {
String msg = DOMMessageFormatter.formatMessage(
DOMMessageFormatter.DOM_DOMAIN,
"FEATURE_NOT_FOUND",
new Object[] { name });
throw new DOMException(DOMException.NOT_SUPPORTED_ERR, msg);
}
}
else {
// REVISIT: modify error exception to TYPE_MISMATCH
String msg = DOMMessageFormatter.formatMessage(
DOMMessageFormatter.DOM_DOMAIN,
"FEATURE_NOT_SUPPORTED",
new Object[] { name });
throw new DOMException(DOMException.NOT_FOUND_ERR, msg);
}
}
else if (name.equals(Constants.DOM_ERROR_HANDLER)) {
if (value instanceof DOMErrorHandler) {
serializer.fDOMErrorHandler = (DOMErrorHandler) value;
}
else {
// REVISIT: modify error exception to TYPE_MISMATCH
String msg = DOMMessageFormatter.formatMessage(
DOMMessageFormatter.DOM_DOMAIN,
"FEATURE_NOT_SUPPORTED",
new Object[] { name });
throw new DOMException(DOMException.NOT_FOUND_ERR, msg);
}
}
else if (name.equals(Constants.DOM_ENTITY_RESOLVER)
|| name.equals(Constants.DOM_SCHEMA_LOCATION)
|| name.equals(Constants.DOM_SCHEMA_TYPE)) {
String msg = DOMMessageFormatter.formatMessage(
DOMMessageFormatter.DOM_DOMAIN,
"FEATURE_NOT_SUPPORTED",
new Object[] { name });
throw new DOMException(DOMException.NOT_SUPPORTED_ERR, msg);
}
else {
String msg = DOMMessageFormatter.formatMessage(
DOMMessageFormatter.DOM_DOMAIN,
"FEATURE_NOT_FOUND",
new Object[] { name });
throw new DOMException(DOMException.NOT_FOUND_ERR, msg);
}
}
/** DOM L3-EXPERIMENTAL:
* Check if parameter can be set
*/
public boolean canSetParameter(String name, Object state) {
if (state instanceof Boolean){
boolean value = ((Boolean)state).booleanValue();
if (name.equals(Constants.DOM_NAMESPACES)
|| name.equals(Constants.DOM_SPLIT_CDATA)
|| name.equals(Constants.DOM_DISCARD_DEFAULT_CONTENT)
|| name.equals(Constants.DOM_XMLDECL)){
// both values supported
return true;
}
else if (name.equals(Constants.DOM_CANONICAL_FORM)
|| name.equals(Constants.DOM_VALIDATE_IF_SCHEMA)
|| name.equals(Constants.DOM_VALIDATE)
|| name.equals(Constants.DOM_CHECK_CHAR_NORMALIZATION)
|| name.equals(Constants.DOM_DATATYPE_NORMALIZATION)
|| name.equals(Constants.DOM_FORMAT_PRETTY_PRINT)
|| name.equals(Constants.DOM_NORMALIZE_CHARACTERS)
// REVISIT: these must be supported
|| name.equals(Constants.DOM_WELLFORMED)) {
// true is not supported
return !value;
}
else if (name.equals(Constants.DOM_INFOSET)
|| name.equals(Constants.DOM_NAMESPACE_DECLARATIONS)
|| name.equals(Constants.DOM_WHITESPACE_IN_ELEMENT_CONTENT)
|| name.equals(Constants.DOM_IGNORE_CHAR_DENORMALIZATION)
// REVISIT: these must be supported
|| name.equals(Constants.DOM_ENTITIES)
|| name.equals(Constants.DOM_CDATA_SECTIONS)
|| name.equals(Constants.DOM_COMMENTS)) {
// false is not supported
return value;
}
}
else if (name.equals(Constants.DOM_ERROR_HANDLER)){
return true;
}
return false;
}
/** DOM L3-EXPERIMENTAL:
* Getter for boolean and object parameters
*/
public Object getParameter(String name) throws DOMException {
Object state = serializer.fFeatures.get(name);
if (state == null) {
if (name.equals(Constants.DOM_ERROR_HANDLER)) {
return serializer.fDOMErrorHandler;
}
else if (name.equals(Constants.DOM_ENTITY_RESOLVER)
|| name.equals(Constants.DOM_SCHEMA_LOCATION)
|| name.equals(Constants.DOM_SCHEMA_TYPE)) {
String msg =
DOMMessageFormatter.formatMessage(
DOMMessageFormatter.DOM_DOMAIN,
"FEATURE_NOT_SUPPORTED",
new Object[] { name });
throw new DOMException(DOMException.NOT_SUPPORTED_ERR, msg);
}
else {
String msg =
DOMMessageFormatter.formatMessage(
DOMMessageFormatter.DOM_DOMAIN,
"FEATURE_NOT_FOUND",
new Object[] { name });
throw new DOMException(DOMException.NOT_FOUND_ERR, msg);
}
}
return ((Boolean) state);
}
/**
* DOM L3 EXPERIMENTAL:
* The character encoding in which the output will be written.
* <br> The encoding to use when writing is determined as follows: If the
* encoding attribute has been set, that value will be used.If the
* encoding attribute is <code>null</code> or empty, but the item to be
* written includes an encoding declaration, that value will be used.If
* neither of the above provides an encoding name, a default encoding of
* "UTF-8" will be used.
* <br>The default value is <code>null</code>.
*/
public String getEncoding() {
return fEncoding;
}
/**
* DOM L3 EXPERIMENTAL:
* The character encoding in which the output will be written.
* <br> The encoding to use when writing is determined as follows: If the
* encoding attribute has been set, that value will be used.If the
* encoding attribute is <code>null</code> or empty, but the item to be
* written includes an encoding declaration, that value will be used.If
* neither of the above provides an encoding name, a default encoding of
* "UTF-8" will be used.
* <br>The default value is <code>null</code>.
*/
public void setEncoding(String encoding) {
serializer._format.setEncoding(encoding);
fEncoding = serializer._format.getEncoding();
}
/**
* DOM L3 EXPERIMENTAL:
* Write out the specified node as described above in the description of
* <code>DOMSerializer</code>. 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 destination The destination for the data to be written.
* @param wnode The <code>Document</code> or <code>Entity</code> node to
* be written. For other node types, something sensible should be
* written, but the exact serialized form is not specified.
* @return Returns <code>true</code> if <code>node</code> was
* successfully serialized and <code>false</code> in case a failure
* occured and the failure wasn't canceled by the error handler.
* @exception none
*/
public boolean writeNode(java.io.OutputStream destination,
Node wnode) {
// determine which serializer to use:
Document doc = (wnode.getNodeType()== Node.DOCUMENT_NODE)?(Document)wnode:wnode.getOwnerDocument();
Method getVersion = null;
XMLSerializer ser = null;
String ver = null;
// this should run under JDK 1.1.8...
try {
getVersion = doc.getClass().getMethod("getVersion", new Class[]{});
if(getVersion != null ) {
ver = (String)getVersion.invoke(doc, null);
}
} catch (Exception e) {
// no way to test the version...
// ignore the exception
}
if(ver != null && ver.equals("1.1")) {
if(xml11Serializer == null) {
xml11Serializer = new XML11Serializer();
initSerializer(xml11Serializer);
}
// copy setting from "main" serializer to XML 1.1 serializer
copySettings(serializer, xml11Serializer);
ser = xml11Serializer;
} else {
ser = serializer;
}
checkAllFeatures(ser);
try {
ser.reset();
ser.setOutputByteStream(destination);
if (wnode == null)
return false;
else if (wnode.getNodeType() == Node.DOCUMENT_NODE)
ser.serialize((Document)wnode);
else if (wnode.getNodeType() == Node.DOCUMENT_FRAGMENT_NODE)
ser.serialize((DocumentFragment)wnode);
else if (wnode.getNodeType() == Node.ELEMENT_NODE)
ser.serialize((Element)wnode);
else
return false;
} catch (Exception e) {
if (ser.fDOMErrorHandler != null) {
DOMErrorImpl error = new DOMErrorImpl();
error.fException = e;
error.fMessage = e.getMessage();
error.fSeverity = DOMError.SEVERITY_ERROR;
ser.fDOMErrorHandler.handleError(error);
}
}
return true;
}
/**
* DOM L3 EXPERIMENTAL:
* Serialize the specified node as described above in the description of
* <code>DOMSerializer</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, or <code>null</code> in case a
* failure occured and the failure wasn't canceled by the error
* handler.
* @exception DOMException
* DOMSTRING_SIZE_ERR: The resulting string is too long to fit in a
* <code>DOMString</code>.
*/
public String writeToString(Node wnode)
throws DOMException {
// determine which serializer to use:
Document doc = (wnode.getNodeType() == Node.DOCUMENT_NODE)?(Document)wnode:wnode.getOwnerDocument();
Method getVersion = null;
XMLSerializer ser = null;
String ver = null;
// this should run under JDK 1.1.8...
try {
getVersion = doc.getClass().getMethod("getVersion", new Class[]{});
if(getVersion != null ) {
ver = (String)getVersion.invoke(doc, null);
}
} catch (Exception e) {
// no way to test the version...
// ignore the exception
}
if(ver != null && ver.equals("1.1")) {
if(xml11Serializer == null) {
xml11Serializer = new XML11Serializer();
initSerializer(xml11Serializer);
}
// copy setting from "main" serializer to XML 1.1 serializer
copySettings(serializer, xml11Serializer);
ser = xml11Serializer;
} else {
ser = serializer;
}
checkAllFeatures(ser);
StringWriter destination = new StringWriter();
try {
ser.reset();
ser.setOutputCharStream(destination);
if (wnode == null)
return null;
else if (wnode.getNodeType() == Node.DOCUMENT_NODE)
ser.serialize((Document)wnode);
else if (wnode.getNodeType() == Node.DOCUMENT_FRAGMENT_NODE)
ser.serialize((DocumentFragment)wnode);
else if (wnode.getNodeType() == Node.ELEMENT_NODE)
ser.serialize((Element)wnode);
else
return null;
} catch (IOException ioe) {
String msg = DOMMessageFormatter.formatMessage(
DOMMessageFormatter.DOM_DOMAIN,
"STRING_TOO_LONG",
new Object[] { ioe.getMessage()});
throw new DOMException(DOMException.DOMSTRING_SIZE_ERR,msg);
}
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>.
*/
public void setNewLine(String newLine) {
serializer._format.setLineSeparator(newLine);
}
/**
* 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>.
*/
public String getNewLine() {
return serializer._format.getLineSeparator();
}
/**
* 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.
*/
public DOMSerializerFilter getFilter(){
return null;
}
/**
* 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.
*/
public void setFilter(DOMSerializerFilter filter){
serializer.fDOMFilter = filter;
}
private void checkAllFeatures(XMLSerializer ser) {
if (getParameter(Constants.DOM_WHITESPACE_IN_ELEMENT_CONTENT) == Boolean.TRUE)
ser._format.setPreserveSpace(true);
else
ser._format.setPreserveSpace(false);
}
// this initializes a newly-created serializer
private void initSerializer(XMLSerializer ser) {
ser.fNamespaces = true;
ser.fNSBinder = new NamespaceSupport();
ser.fLocalNSBinder = new NamespaceSupport();
ser.fSymbolTable = new SymbolTable();
ser.fFeatures = new Hashtable();
ser.fFeatures.put(Constants.DOM_NAMESPACES, Boolean.TRUE);
ser.fFeatures.put(Constants.DOM_NORMALIZE_CHARACTERS, Boolean.FALSE);
ser.fFeatures.put(Constants.DOM_VALIDATE_IF_SCHEMA, Boolean.FALSE);
ser.fFeatures.put(Constants.DOM_VALIDATE, Boolean.FALSE);
ser.fFeatures.put(Constants.DOM_ENTITIES, Boolean.TRUE);
ser.fFeatures.put(Constants.DOM_SPLIT_CDATA, Boolean.TRUE);
ser.fFeatures.put(Constants.DOM_CDATA_SECTIONS, Boolean.TRUE);
ser.fFeatures.put(Constants.DOM_COMMENTS, Boolean.TRUE);
ser.fFeatures.put(Constants.DOM_WHITESPACE_IN_ELEMENT_CONTENT, Boolean.TRUE);
ser.fFeatures.put(Constants.DOM_DISCARD_DEFAULT_CONTENT, Boolean.TRUE);
ser.fFeatures.put(Constants.DOM_CANONICAL_FORM, Boolean.FALSE);
ser.fFeatures.put(Constants.DOM_FORMAT_PRETTY_PRINT, Boolean.FALSE);
ser.fFeatures.put(Constants.DOM_XMLDECL, Boolean.TRUE);
ser.fFeatures.put(Constants.DOM_CHECK_CHAR_NORMALIZATION, Boolean.FALSE);
ser.fFeatures.put(Constants.DOM_DATATYPE_NORMALIZATION, Boolean.FALSE);
ser.fFeatures.put(Constants.DOM_NORMALIZE_CHARACTERS, Boolean.FALSE);
ser.fFeatures.put(Constants.DOM_WELLFORMED, Boolean.FALSE);
ser.fFeatures.put(Constants.DOM_INFOSET, Boolean.FALSE);
ser.fFeatures.put(Constants.DOM_NAMESPACE_DECLARATIONS, Boolean.TRUE);
ser.fFeatures.put(Constants.DOM_WHITESPACE_IN_ELEMENT_CONTENT, Boolean.TRUE);
ser.fFeatures.put(Constants.DOM_IGNORE_CHAR_DENORMALIZATION, Boolean.TRUE);
}
// copies all settings that could have been modified
// by calls to DOMSerializer 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!
private void copySettings(XMLSerializer src, XMLSerializer dest) {
dest._format.setOmitXMLDeclaration(src._format.getOmitXMLDeclaration());
dest.fNamespaces = src.fNamespaces;
dest.fDOMErrorHandler = src.fDOMErrorHandler;
dest._format.setEncoding(src._format.getEncoding());
dest._format.setLineSeparator(src._format.getLineSeparator());
dest.fDOMFilter = src.fDOMFilter;
// and copy over all the entries in fFeatures:
Enumeration keys = src.fFeatures.keys();
while(keys.hasMoreElements()) {
Object key = keys.nextElement();
Object val = src.fFeatures.get(key);
dest.fFeatures.put(key,val);
}
}//copysettings
/**
* Serialize the specified node as described above in the general
* description of the <code>DOMSerializer</code> interface. The output
* is written to the supplied <code>DOMOutput</code>.
* <br> When writing to a <code>DOMOutput</code>, the encoding is found by
* looking at the encoding information that is reachable through the
* <code>DOMOutput</code> and the item to be written (or its owner
* document) in this order:
* <ol>
* <li> <code>DOMOutput.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>DOMOutput</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.
*/
public boolean write(Node node,
DOMOutput destination){
Method getVersion = null;
XMLSerializer ser = null;
String ver = null;
OutputStream fOutputStream = null;
String fEncoding = null;
Document fDocument = (node.getNodeType()== Node.DOCUMENT_NODE)?(Document)node:node.getOwnerDocument();
// this should run under JDK 1.1.8...
try {
getVersion = fDocument.getClass().getMethod("getVersion", new Class[]{});
if(getVersion != null ) {
ver = (String)getVersion.invoke(fDocument, null);
}
} catch (Exception e) {
// no way to test the version...
// ignore the exception
}
// determine which serializer to use:
if(ver != null && ver.equals("1.1")) {
if(xml11Serializer == null) {
xml11Serializer = new XML11Serializer();
initSerializer(xml11Serializer);
}
// copy setting from "main" serializer to XML 1.1 serializer
copySettings(serializer, xml11Serializer);
ser = xml11Serializer;
} else {
ser = serializer;
}
checkAllFeatures(ser);
Writer fWriter = destination.getCharacterStream();
if(fWriter==null){
fOutputStream = destination.getByteStream();
if( fOutputStream ==null){
if (ser.fDOMErrorHandler != null) {
DOMErrorImpl error = new DOMErrorImpl();
error.fMessage = "no-output-specified";
error.fSeverity = DOMError.SEVERITY_FATAL_ERROR;
ser.fDOMErrorHandler.handleError(error);
}
return false;
}
if( (fEncoding = destination.getEncoding())== null){
fDocument = node.getOwnerDocument();
if( (fEncoding = fDocument.getActualEncoding()) == null)
if((fEncoding = fDocument.getXmlEncoding()) == null)
fEncoding = "UTF-8";
}
}
try {
ser.reset();
if(fWriter!=null)
ser.setOutputCharStream(fWriter);
else if ( fOutputStream !=null){
serializer._format.setEncoding(fEncoding);
ser.setOutputByteStream(fOutputStream);
}
if (node == null)
return false;
else if (node.getNodeType() == Node.DOCUMENT_NODE)
ser.serialize((Document)node);
else if (node.getNodeType() == Node.DOCUMENT_FRAGMENT_NODE)
ser.serialize((DocumentFragment)node);
else if (node.getNodeType() == Node.ELEMENT_NODE)
ser.serialize((Element)node);
else
return false;
} catch (Exception e) {
if (ser.fDOMErrorHandler != null) {
DOMErrorImpl error = new DOMErrorImpl();
error.fException = e;
error.fMessage = e.getMessage();
error.fSeverity = DOMError.SEVERITY_ERROR;
ser.fDOMErrorHandler.handleError(error);
}
}
return true;
} //write
/**
* Serialize the specified node as described above in the general
* description of the <code>DOMSerializer</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.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.
* @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.
*/
public boolean writeURI(Node node,
String URI){
// determine which serializer to use:
Method getVersion = null;
XMLSerializer ser = null;
String ver = null;
String fEncoding = null;
Document fDocument = (node.getNodeType()== Node.DOCUMENT_NODE)?(Document)node:node.getOwnerDocument();
// this should run under JDK 1.1.8...
try {
getVersion = fDocument.getClass().getMethod("getVersion", new Class[]{});
if(getVersion != null ) {
ver = (String)getVersion.invoke(fDocument, null);
}
} catch (Exception e) {
// no way to test the version...
// ignore the exception
}
if(ver != null && ver.equals("1.1")) {
if(xml11Serializer == null) {
xml11Serializer = new XML11Serializer();
initSerializer(xml11Serializer);
}
// copy setting from "main" serializer to XML 1.1 serializer
copySettings(serializer, xml11Serializer);
ser = xml11Serializer;
} else {
ser = serializer;
}
checkAllFeatures(ser);
if((fEncoding = fDocument.getActualEncoding())==null)
if((fEncoding = fDocument.getXmlEncoding())==null)
fEncoding = "UTF-8";
try {
ser.reset();
FileOutputStream fileOut = new FileOutputStream(new File(URI));
OutputStreamWriter fWriter = new OutputStreamWriter(fileOut, fEncoding);
if(fWriter!=null)
ser.setOutputCharStream(fWriter);
else
return false;
if (node == null)
return false;
else if (node.getNodeType() == Node.DOCUMENT_NODE)
ser.serialize((Document)node);
else if (node.getNodeType() == Node.DOCUMENT_FRAGMENT_NODE)
ser.serialize((DocumentFragment)node);
else if (node.getNodeType() == Node.ELEMENT_NODE)
ser.serialize((Element)node);
else
return false;
} catch (Exception e) {
if (ser.fDOMErrorHandler != null) {
DOMErrorImpl error = new DOMErrorImpl();
error.fException = e;
error.fMessage = e.getMessage();
error.fSeverity = DOMError.SEVERITY_ERROR;
ser.fDOMErrorHandler.handleError(error);
}
}
return true;
} //writeURI
}//DOMSerializerImpl
---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-cvs-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-cvs-help@xml.apache.org