org.codehaus.staxmate.out
Class SMOutputContext

java.lang.Object
  extended by org.codehaus.staxmate.out.SMOutputContext

public final class SMOutputContext
extends java.lang.Object

Class that encapsulates details about context in which StaxMate output is done. The most important of the details is the stream writer to use (since that is eventually invoked to do the real output), and its properties.

Usually the process of outputting XML content with StaxMate starts by instantiating an SMOutputContext. It can then be used to create output fragments; all of which bind to that context. Context is thus what "connects" various fragments when they are buffered (when there may or may not be child/parent relationships yet defined).

Context is also used (in addition to storing output relevant settings and acting as a fragment factory) as the owner of various other objects, most notable namespaces. All local namespaces are owned by one and only one context.


Field Summary
protected static SMNamespace sNsEmpty
           
protected static SMNamespace sNsXml
           
protected static SMNamespace sNsXmlns
           
 
Constructor Summary
protected SMOutputContext(org.codehaus.stax2.XMLStreamWriter2 sw, javax.xml.namespace.NamespaceContext rootNsCtxt)
           
 
Method Summary
 SMBufferedFragment createBufferedFragment()
           
 SMOutputtable createCData(char[] buf, int offset, int len)
           
 SMOutputtable createCData(java.lang.String text)
           
 SMOutputtable createCharacters(char[] buf, int offset, int len)
           
 SMOutputtable createCharacters(java.lang.String text)
           
 SMOutputtable createComment(java.lang.String text)
           
 SMOutputDocument createDocument()
          Method used to create a StaxMate output fragment that corresponds to a single well-formed XML document.
 SMOutputDocument createDocument(java.lang.String version, java.lang.String encoding)
          Method used to create a StaxMate output fragment that corresponds to a single well-formed XML document.
 SMOutputDocument createDocument(java.lang.String version, java.lang.String encoding, boolean standalone)
           
 SMOutputtable createEntityRef(java.lang.String name)
           
static SMOutputContext createInstance(org.codehaus.stax2.XMLStreamWriter2 sw)
           
static SMOutputContext createInstance(org.codehaus.stax2.XMLStreamWriter2 sw, javax.xml.namespace.NamespaceContext rootNsCtxt)
           
 SMOutputtable createNonSharedCData(char[] buf, int offset, int len)
          Specialized alternative to {link #createCData(char[],int,int)} that can count on the passed char array NOT being shared.
 SMOutputtable createNonSharedCharacters(char[] buf, int offset, int len)
          Specialized alternative to {link #createCharacters(char[],int,int)} that can count on the passed char array NOT being shared.
 SMOutputtable createProcessingInstruction(java.lang.String target, java.lang.String data)
           
 SMRootFragment createRootFragment()
          Method to use when outputting an XML sub-tree, in which case the underlying stream writer may be (or has been) used for outputting XML content in addition to content that is output using StaxMate.
 java.lang.String findRootPrefix(SMNamespace ns)
           
 void flushWriter()
           
 java.lang.String generateUnboundPrefix()
           
static SMNamespace getEmptyNamespace()
           
 SMNamespace getNamespace(java.lang.String uri)
           
 SMNamespace getNamespace(java.lang.String uri, java.lang.String prefPrefix)
           
 org.codehaus.stax2.XMLStreamWriter2 getWriter()
           
 boolean isPrefixBound(java.lang.String prefix)
           
 boolean isWriterRepairing()
           
 void setIndentation(java.lang.String indentStr, int startOffset, int step)
          This method can be called to enable or disable heuristic indentation for the output done using this output context.
 void writeAttribute(java.lang.String localName, SMNamespace ns, java.lang.String value)
           
 void writeCData(char[] buf, int offset, int len)
           
 void writeCData(java.lang.String text)
           
 void writeCharacters(char[] buf, int offset, int len)
           
 void writeCharacters(java.lang.String text)
           
 void writeComment(java.lang.String text)
           
 void writeDoctypeDecl(java.lang.String rootName, java.lang.String systemId, java.lang.String publicId, java.lang.String intSubset)
           
 void writeEndDocument()
           
 void writeEndElement(int parentNsCount, SMNamespace parentDefNs)
           
 void writeEntityRef(java.lang.String name)
           
 void writeProcessingInstruction(java.lang.String target, java.lang.String data)
           
 void writeStartDocument()
           
 void writeStartDocument(java.lang.String version, java.lang.String encoding)
           
 void writeStartDocument(java.lang.String version, java.lang.String encoding, boolean standalone)
           
 SMNamespace writeStartElement(SMNamespace ns, java.lang.String localName)
          Method called by the element object when it is about to get written out.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

sNsEmpty

protected static final SMNamespace sNsEmpty

sNsXml

protected static final SMNamespace sNsXml

sNsXmlns

protected static final SMNamespace sNsXmlns
Constructor Detail

SMOutputContext

protected SMOutputContext(org.codehaus.stax2.XMLStreamWriter2 sw,
                          javax.xml.namespace.NamespaceContext rootNsCtxt)
Method Detail

setIndentation

public void setIndentation(java.lang.String indentStr,
                           int startOffset,
                           int step)
This method can be called to enable or disable heuristic indentation for the output done using this output context.

Here are some example calls:

context.setIndentation("\n ", 1, 2); // indent by lf and 2 spaces per level context.setIndentation(null, 0, 0); // disable indentation context.setIndentation("\r\n\t\t\t\t\t\t\t\t", 2, 1); // indent by windows lf and 1 tab per level

Parameters:
indentStr - String to use for indentation; if non-null, will enable indentation, if null, will disable it. Used in conjunction with the other arguments
startOffset - Initial character offset for the first level of indentation (current context; usually root context): basically, number of leading characters from indentStr to output.
step - Number of characters to add from the indentation String for each new level (and to subtract when closing levels).

createInstance

public static SMOutputContext createInstance(org.codehaus.stax2.XMLStreamWriter2 sw,
                                             javax.xml.namespace.NamespaceContext rootNsCtxt)
                                      throws javax.xml.stream.XMLStreamException
Throws:
javax.xml.stream.XMLStreamException

createInstance

public static SMOutputContext createInstance(org.codehaus.stax2.XMLStreamWriter2 sw)
                                      throws javax.xml.stream.XMLStreamException
Throws:
javax.xml.stream.XMLStreamException

createDocument

public SMOutputDocument createDocument()
                                throws javax.xml.stream.XMLStreamException
Method used to create a StaxMate output fragment that corresponds to a single well-formed XML document. Assumption, then, is that the underlying stream writer has only been created, but no writes have yet been done.

This version of the method calls the matching no-arguments method in the stream writer.

Throws:
javax.xml.stream.XMLStreamException

createDocument

public SMOutputDocument createDocument(java.lang.String version,
                                       java.lang.String encoding)
                                throws javax.xml.stream.XMLStreamException
Method used to create a StaxMate output fragment that corresponds to a single well-formed XML document. Assumption, then, is that the underlying stream writer has only been created, but no writes have yet been done.

This version of the method calls the matching stream writer method which takes full xml declaration information.

Throws:
javax.xml.stream.XMLStreamException

createDocument

public SMOutputDocument createDocument(java.lang.String version,
                                       java.lang.String encoding,
                                       boolean standalone)
                                throws javax.xml.stream.XMLStreamException
Throws:
javax.xml.stream.XMLStreamException

createRootFragment

public SMRootFragment createRootFragment()
                                  throws javax.xml.stream.XMLStreamException
Method to use when outputting an XML sub-tree, in which case the underlying stream writer may be (or has been) used for outputting XML content in addition to content that is output using StaxMate. Resulting fragment is not buffered, and will thus be fully streamed (except for buffering caused by adding buffered children)

Throws:
javax.xml.stream.XMLStreamException

createBufferedFragment

public SMBufferedFragment createBufferedFragment()
                                          throws javax.xml.stream.XMLStreamException
Throws:
javax.xml.stream.XMLStreamException

createCharacters

public SMOutputtable createCharacters(java.lang.String text)

createCharacters

public SMOutputtable createCharacters(char[] buf,
                                      int offset,
                                      int len)

createNonSharedCharacters

public SMOutputtable createNonSharedCharacters(char[] buf,
                                               int offset,
                                               int len)
Specialized alternative to {link #createCharacters(char[],int,int)} that can count on the passed char array NOT being shared. This means that no intermediate copy needs to be done -- instance can just use the passed in reference knowing it will not be messed by other threads.


createCData

public SMOutputtable createCData(java.lang.String text)

createCData

public SMOutputtable createCData(char[] buf,
                                 int offset,
                                 int len)

createNonSharedCData

public SMOutputtable createNonSharedCData(char[] buf,
                                          int offset,
                                          int len)
Specialized alternative to {link #createCData(char[],int,int)} that can count on the passed char array NOT being shared. This means that no intermediate copy needs to be done -- instance can just use the passed in reference knowing it will not be messed by other threads.


createComment

public SMOutputtable createComment(java.lang.String text)

createEntityRef

public SMOutputtable createEntityRef(java.lang.String name)

createProcessingInstruction

public SMOutputtable createProcessingInstruction(java.lang.String target,
                                                 java.lang.String data)

getNamespace

public final SMNamespace getNamespace(java.lang.String uri)

getNamespace

public final SMNamespace getNamespace(java.lang.String uri,
                                      java.lang.String prefPrefix)

getEmptyNamespace

public static final SMNamespace getEmptyNamespace()

getWriter

public final org.codehaus.stax2.XMLStreamWriter2 getWriter()

isWriterRepairing

public final boolean isWriterRepairing()

flushWriter

public void flushWriter()
                 throws javax.xml.stream.XMLStreamException
Throws:
javax.xml.stream.XMLStreamException

writeCharacters

public void writeCharacters(java.lang.String text)
                     throws javax.xml.stream.XMLStreamException
Throws:
javax.xml.stream.XMLStreamException

writeCharacters

public void writeCharacters(char[] buf,
                            int offset,
                            int len)
                     throws javax.xml.stream.XMLStreamException
Throws:
javax.xml.stream.XMLStreamException

writeCData

public void writeCData(java.lang.String text)
                throws javax.xml.stream.XMLStreamException
Throws:
javax.xml.stream.XMLStreamException

writeCData

public void writeCData(char[] buf,
                       int offset,
                       int len)
                throws javax.xml.stream.XMLStreamException
Throws:
javax.xml.stream.XMLStreamException

writeComment

public void writeComment(java.lang.String text)
                  throws javax.xml.stream.XMLStreamException
Throws:
javax.xml.stream.XMLStreamException

writeEntityRef

public void writeEntityRef(java.lang.String name)
                    throws javax.xml.stream.XMLStreamException
Throws:
javax.xml.stream.XMLStreamException

writeProcessingInstruction

public void writeProcessingInstruction(java.lang.String target,
                                       java.lang.String data)
                                throws javax.xml.stream.XMLStreamException
Throws:
javax.xml.stream.XMLStreamException

writeAttribute

public void writeAttribute(java.lang.String localName,
                           SMNamespace ns,
                           java.lang.String value)
                    throws javax.xml.stream.XMLStreamException
Throws:
javax.xml.stream.XMLStreamException

writeStartElement

public SMNamespace writeStartElement(SMNamespace ns,
                                     java.lang.String localName)
                              throws javax.xml.stream.XMLStreamException
Method called by the element object when it is about to get written out. In this case, element will keep track of part of namespace context information for this context object (to save allocation of separate namespace context object).

Returns:
Namespace that was the active namespace in parent scope of this element. Will be different from the default namespace if a new default namespace was declared to be used by this element.
Throws:
javax.xml.stream.XMLStreamException

writeEndElement

public void writeEndElement(int parentNsCount,
                            SMNamespace parentDefNs)
                     throws javax.xml.stream.XMLStreamException
Throws:
javax.xml.stream.XMLStreamException

writeStartDocument

public void writeStartDocument()
                        throws javax.xml.stream.XMLStreamException
Throws:
javax.xml.stream.XMLStreamException

writeStartDocument

public void writeStartDocument(java.lang.String version,
                               java.lang.String encoding)
                        throws javax.xml.stream.XMLStreamException
Throws:
javax.xml.stream.XMLStreamException

writeStartDocument

public void writeStartDocument(java.lang.String version,
                               java.lang.String encoding,
                               boolean standalone)
                        throws javax.xml.stream.XMLStreamException
Throws:
javax.xml.stream.XMLStreamException

writeEndDocument

public void writeEndDocument()
                      throws javax.xml.stream.XMLStreamException
Throws:
javax.xml.stream.XMLStreamException

writeDoctypeDecl

public void writeDoctypeDecl(java.lang.String rootName,
                             java.lang.String systemId,
                             java.lang.String publicId,
                             java.lang.String intSubset)
                      throws javax.xml.stream.XMLStreamException
Throws:
javax.xml.stream.XMLStreamException

generateUnboundPrefix

public java.lang.String generateUnboundPrefix()

isPrefixBound

public boolean isPrefixBound(java.lang.String prefix)

findRootPrefix

public java.lang.String findRootPrefix(SMNamespace ns)