com.ctc.wstx.dom
Class WstxDOMWrappingWriter

java.lang.Object
  extended by org.codehaus.stax2.ri.dom.DOMWrappingWriter
      extended by com.ctc.wstx.dom.WstxDOMWrappingWriter
All Implemented Interfaces:
XMLStreamWriter, TypedXMLStreamWriter, Validatable, XMLStreamWriter2

public class WstxDOMWrappingWriter
extends DOMWrappingWriter

This is an adapter class that allows building a DOM tree using XMLStreamWriter interface.

Note that the implementation is only to be used for use with javax.xml.transform.dom.DOMResult.

Some notes regarding missing/incomplete functionality:

Author:
Tatu Saloranta, Dan Diephouse

Field Summary
protected static String ERR_NSDECL_WRONG_STATE
           
protected  String mAutomaticNsPrefix
           
protected  int[] mAutoNsSeq
          for NsRepairing mode
protected  WriterConfig mConfig
           
protected  DOMOutputElement mCurrElem
          This element is the current context element, under which all other nodes are added, until matching end element is output.
protected  DOMOutputElement mOpenElement
          This element is non-null right after a call to either writeStartElement and writeEmptyElement, and can be used to add attributes and namespace declarations.
protected  String mSuggestedDefNs
           
 
Fields inherited from class org.codehaus.stax2.ri.dom.DOMWrappingWriter
mDocument, mEncoding, mNsAware, mNsContext, mNsRepairing, mValueEncoder
 
Method Summary
protected  void appendLeaf(Node n)
           
static WstxDOMWrappingWriter createFrom(WriterConfig cfg, DOMResult dst)
           
protected  void createStartElem(String nsURI, String prefix, String localName, boolean isEmpty)
          Method called by all start element write methods.
protected  String findElemPrefix(String nsURI, DOMOutputElement elem)
          Method called to find an existing prefix for the given namespace, if any exists in the scope.
protected  String findOrCreateAttrPrefix(String suggPrefix, String nsURI, DOMOutputElement elem)
          Method called to somehow find a prefix for given namespace, to be used for a new start element; either use an existing one, or generate a new one.
protected  String generateElemPrefix(String suggPrefix, String nsURI, DOMOutputElement elem)
          Method called after findElemPrefix(java.lang.String, com.ctc.wstx.dom.DOMOutputElement) has returned null, to create and bind a namespace mapping for specified namespace.
 NamespaceContext getNamespaceContext()
           
 String getPrefix(String uri)
           
 Object getProperty(String name)
           
 boolean isPropertySupported(String name)
          Method similar to XMLOutputFactory.isPropertySupported(java.lang.String), used to determine whether a property is supported by the Writer instance.
protected  void outputAttribute(String nsURI, String prefix, String localName, String value)
           
 void setDefaultNamespace(String uri)
           
 void setPrefix(String prefix, String uri)
           
 boolean setProperty(String name, Object value)
          Method that can be used to set per-writer properties; a subset of properties one can set via matching XMLOutputFactory2 instance.
 void writeAttribute(String localName, String value)
           
 void writeAttribute(String nsURI, String localName, String value)
           
 void writeAttribute(String prefix, String nsURI, String localName, String value)
           
 void writeDefaultNamespace(String nsURI)
           
 void writeDTD(String rootName, String systemId, String publicId, String internalSubset)
           
 void writeEmptyElement(String localName)
           
 void writeEmptyElement(String nsURI, String localName)
           
 void writeEmptyElement(String prefix, String localName, String nsURI)
           
 void writeEndDocument()
           
 void writeEndElement()
           
 void writeNamespace(String prefix, String nsURI)
           
 void writeStartElement(String localName)
           
 void writeStartElement(String nsURI, String localName)
           
 void writeStartElement(String prefix, String localName, String nsURI)
           
 
Methods inherited from class org.codehaus.stax2.ri.dom.DOMWrappingWriter
close, closeCompletely, copyEventFromReader, flush, getEncoding, getLocation, getValueEncoder, reportUnsupported, serializeQNameValue, setNamespaceContext, setValidationProblemHandler, stopValidatingAgainst, stopValidatingAgainst, throwOutputError, throwOutputError, validateAgainst, writeBinary, writeBinary, writeBinaryAttribute, writeBinaryAttribute, writeBoolean, writeBooleanAttribute, writeCData, writeCData, writeCharacters, writeCharacters, writeComment, writeDecimal, writeDecimalAttribute, writeDouble, writeDoubleArray, writeDoubleArrayAttribute, writeDoubleAttribute, writeDTD, writeEntityRef, writeFloat, writeFloatArray, writeFloatArrayAttribute, writeFloatAttribute, writeFullEndElement, writeInt, writeIntArray, writeIntArrayAttribute, writeIntAttribute, writeInteger, writeIntegerAttribute, writeLong, writeLongArray, writeLongArrayAttribute, writeLongAttribute, writeProcessingInstruction, writeProcessingInstruction, writeQName, writeQNameAttribute, writeRaw, writeRaw, writeRaw, writeSpace, writeSpace, writeStartDocument, writeStartDocument, writeStartDocument, writeStartDocument
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

ERR_NSDECL_WRONG_STATE

protected static final String ERR_NSDECL_WRONG_STATE
See Also:
Constant Field Values

mConfig

protected final WriterConfig mConfig

mCurrElem

protected DOMOutputElement mCurrElem
This element is the current context element, under which all other nodes are added, until matching end element is output. Null outside of the main element tree.

Note: explicit empty element (written using writeEmptyElement) will never become current element.


mOpenElement

protected DOMOutputElement mOpenElement
This element is non-null right after a call to either writeStartElement and writeEmptyElement, and can be used to add attributes and namespace declarations.

Note: while this is often the same as mCurrElem, it's not always. Specifically, an empty element (written explicitly using writeEmptyElement) will become open element but NOT current element. Conversely, regular elements will remain current element when non elements are written (text, comments, PI), but not the open element.


mAutoNsSeq

protected int[] mAutoNsSeq
for NsRepairing mode


mSuggestedDefNs

protected String mSuggestedDefNs

mAutomaticNsPrefix

protected String mAutomaticNsPrefix
Method Detail

createFrom

public static WstxDOMWrappingWriter createFrom(WriterConfig cfg,
                                               DOMResult dst)
                                        throws XMLStreamException
Throws:
XMLStreamException

getNamespaceContext

public NamespaceContext getNamespaceContext()
Specified by:
getNamespaceContext in interface XMLStreamWriter
Specified by:
getNamespaceContext in class DOMWrappingWriter

getPrefix

public String getPrefix(String uri)
Specified by:
getPrefix in interface XMLStreamWriter
Specified by:
getPrefix in class DOMWrappingWriter

getProperty

public Object getProperty(String name)
Specified by:
getProperty in interface XMLStreamWriter
Specified by:
getProperty in class DOMWrappingWriter

setDefaultNamespace

public void setDefaultNamespace(String uri)
Specified by:
setDefaultNamespace in interface XMLStreamWriter
Specified by:
setDefaultNamespace in class DOMWrappingWriter

setPrefix

public void setPrefix(String prefix,
                      String uri)
               throws XMLStreamException
Specified by:
setPrefix in interface XMLStreamWriter
Specified by:
setPrefix in class DOMWrappingWriter
Throws:
XMLStreamException

writeAttribute

public void writeAttribute(String localName,
                           String value)
                    throws XMLStreamException
Specified by:
writeAttribute in interface XMLStreamWriter
Specified by:
writeAttribute in class DOMWrappingWriter
Throws:
XMLStreamException

writeAttribute

public void writeAttribute(String nsURI,
                           String localName,
                           String value)
                    throws XMLStreamException
Specified by:
writeAttribute in interface XMLStreamWriter
Specified by:
writeAttribute in class DOMWrappingWriter
Throws:
XMLStreamException

writeAttribute

public void writeAttribute(String prefix,
                           String nsURI,
                           String localName,
                           String value)
                    throws XMLStreamException
Specified by:
writeAttribute in interface XMLStreamWriter
Specified by:
writeAttribute in class DOMWrappingWriter
Throws:
XMLStreamException

writeDefaultNamespace

public void writeDefaultNamespace(String nsURI)
Specified by:
writeDefaultNamespace in interface XMLStreamWriter
Specified by:
writeDefaultNamespace in class DOMWrappingWriter

writeEmptyElement

public void writeEmptyElement(String localName)
                       throws XMLStreamException
Specified by:
writeEmptyElement in interface XMLStreamWriter
Specified by:
writeEmptyElement in class DOMWrappingWriter
Throws:
XMLStreamException

writeEmptyElement

public void writeEmptyElement(String nsURI,
                              String localName)
                       throws XMLStreamException
Specified by:
writeEmptyElement in interface XMLStreamWriter
Specified by:
writeEmptyElement in class DOMWrappingWriter
Throws:
XMLStreamException

writeEmptyElement

public void writeEmptyElement(String prefix,
                              String localName,
                              String nsURI)
                       throws XMLStreamException
Specified by:
writeEmptyElement in interface XMLStreamWriter
Specified by:
writeEmptyElement in class DOMWrappingWriter
Throws:
XMLStreamException

writeEndDocument

public void writeEndDocument()
Specified by:
writeEndDocument in interface XMLStreamWriter
Specified by:
writeEndDocument in class DOMWrappingWriter

writeEndElement

public void writeEndElement()

writeNamespace

public void writeNamespace(String prefix,
                           String nsURI)
                    throws XMLStreamException
Throws:
XMLStreamException

writeStartElement

public void writeStartElement(String localName)
                       throws XMLStreamException
Throws:
XMLStreamException

writeStartElement

public void writeStartElement(String nsURI,
                              String localName)
                       throws XMLStreamException
Throws:
XMLStreamException

writeStartElement

public void writeStartElement(String prefix,
                              String localName,
                              String nsURI)
                       throws XMLStreamException
Throws:
XMLStreamException

isPropertySupported

public boolean isPropertySupported(String name)
Description copied from interface: XMLStreamWriter2
Method similar to XMLOutputFactory.isPropertySupported(java.lang.String), used to determine whether a property is supported by the Writer instance. This means that this method may return false for some properties that the output factory does support: specifically, it should only return true if the value is mutable on per-instance basis. False means that either the property is not recognized, or is not mutable via writer instance.

Specified by:
isPropertySupported in interface XMLStreamWriter2
Specified by:
isPropertySupported in class DOMWrappingWriter

setProperty

public boolean setProperty(String name,
                           Object value)
Description copied from interface: XMLStreamWriter2
Method that can be used to set per-writer properties; a subset of properties one can set via matching XMLOutputFactory2 instance. Exactly which methods are mutable is implementation specific.

Specified by:
setProperty in interface XMLStreamWriter2
Specified by:
setProperty in class DOMWrappingWriter
Parameters:
name - Name of the property to set
value - Value to set property to.
Returns:
True, if the specified property was succesfully set to specified value; false if its value was not changed

writeDTD

public void writeDTD(String rootName,
                     String systemId,
                     String publicId,
                     String internalSubset)
              throws XMLStreamException
Specified by:
writeDTD in interface XMLStreamWriter2
Specified by:
writeDTD in class DOMWrappingWriter
Throws:
XMLStreamException

appendLeaf

protected void appendLeaf(Node n)
                   throws IllegalStateException
Specified by:
appendLeaf in class DOMWrappingWriter
Throws:
IllegalStateException

createStartElem

protected void createStartElem(String nsURI,
                               String prefix,
                               String localName,
                               boolean isEmpty)
                        throws XMLStreamException
Method called by all start element write methods.

Parameters:
nsURI - Namespace URI to use: null and empty String denote 'no namespace'
Throws:
XMLStreamException

outputAttribute

protected void outputAttribute(String nsURI,
                               String prefix,
                               String localName,
                               String value)
                        throws XMLStreamException
Throws:
XMLStreamException

findElemPrefix

protected final String findElemPrefix(String nsURI,
                                      DOMOutputElement elem)
                               throws XMLStreamException
Method called to find an existing prefix for the given namespace, if any exists in the scope. If one is found, it's returned (including "" for the current default namespace); if not, null is returned.

Parameters:
nsURI - URI of namespace for which we need a prefix
Throws:
XMLStreamException

generateElemPrefix

protected final String generateElemPrefix(String suggPrefix,
                                          String nsURI,
                                          DOMOutputElement elem)
                                   throws XMLStreamException
Method called after findElemPrefix(java.lang.String, com.ctc.wstx.dom.DOMOutputElement) has returned null, to create and bind a namespace mapping for specified namespace.

Throws:
XMLStreamException

findOrCreateAttrPrefix

protected final String findOrCreateAttrPrefix(String suggPrefix,
                                              String nsURI,
                                              DOMOutputElement elem)
                                       throws XMLStreamException
Method called to somehow find a prefix for given namespace, to be used for a new start element; either use an existing one, or generate a new one. If a new mapping needs to be generated, it will also be automatically bound, and necessary namespace declaration output.

Parameters:
suggPrefix - Suggested prefix to bind, if any; may be null to indicate "no preference"
nsURI - URI of namespace for which we need a prefix
elem - Currently open start element, on which the attribute will be added.
Throws:
XMLStreamException