Class SMOutputContext
- java.lang.Object
-
- org.codehaus.staxmate.out.SMOutputContext
-
public final class SMOutputContext extends java.lang.ObjectClass 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
Fields Modifier and Type Field Description (package private) int_boundNsCountNumber of bound namespaces in_nsStackprotected boolean_cfgPreferDefaultNsConfiguration flag that specifies whether by default namespaces should bind as the default namespaces for elements or not.protected boolean_cfgRepairing(package private) SMNamespace_currElemNSNamespace of the last START_ELEMENT output.protected SMNamespace_defaultNSCurrently active default namespace; one that is in effect within current scope (inside currently open element, if any; if none, within root level).(package private) boolean_indentLevelEmptyThis flag is used to prevent indentation from being added for empty leaf elements, which should either be output as empty elements, or start/end tag pair, with no intervening spaces.(package private) int_indentOffsetCurrent offset within indentation String, if indenting.(package private) int_indentStepNumber of characters to add to_indentOffsetwhen adding a new indentation level (and conversely, subtract when closing such level).(package private) java.lang.String_indentStringThis String is null when not doing (heuristic) indentation.(package private) int_indentSuppressCounter used to suppress indentation, for levels where text has been output (indicating either pure-text or mixed content).protected java.util.HashMap<java.lang.String,SMNamespace>_localNsMapMap that contains all local namespaces, that is, namespaces that have been created for use with documents output using this context.protected java.lang.String_nsPrefixPrefixPrefix to use for creating automatic namespace prefixes.protected int_nsPrefixSeqNr(package private) SMNamespace[]_nsStackStack of bound non-default namespaces.protected javax.xml.namespace.NamespaceContext_rootNsContextprotected org.codehaus.stax2.XMLStreamWriter2_streamWriterprotected static intDEF_NS_STACK_SIZEprotected static SMOTypedValueFALSE_VALUEprotected static SMNamespaceNS_EMPTYprotected static SMNamespaceNS_XMLprotected static SMNamespaceNS_XMLNSprotected static java.util.HashMap<java.lang.String,SMNamespace>sGlobalNsMapprotected static SMOTypedValueTRUE_VALUE
-
Constructor Summary
Constructors Modifier Constructor Description protectedSMOutputContext(org.codehaus.stax2.XMLStreamWriter2 sw, javax.xml.namespace.NamespaceContext rootNsCtxt)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected java.lang.String_ensureBindingForAttribute(SMNamespace ns)private voidbindAndWriteNs(SMNamespace ns, java.lang.String prefix)Method for establishing binding between given namespace and a non-empty prefix, as well as writing resulting namespace declaration out.voidcloseWriterCompletely()Method that can be called to force full closing of the underlying stream writer as well as output target it uses (usually aOutputStreamorWriter).SMOutputtablecreateAttribute(SMNamespace ns, java.lang.String localName, byte[] value)SMOutputtablecreateAttribute(SMNamespace ns, java.lang.String localName, int value)SMOutputtablecreateAttribute(SMNamespace ns, java.lang.String localName, java.lang.String value)SMBufferedFragmentcreateBufferedFragment()SMOutputtablecreateCData(char[] buf, int offset, int len)SMOutputtablecreateCData(java.lang.String text)SMOutputtablecreateCharacters(char[] buf, int offset, int len)SMOutputtablecreateCharacters(java.lang.String text)SMOutputtablecreateComment(java.lang.String text)SMOutputDocumentcreateDocument()Method used to create a StaxMate output fragment that corresponds to a single well-formed XML document.SMOutputDocumentcreateDocument(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.SMOutputDocumentcreateDocument(java.lang.String version, java.lang.String encoding, boolean standalone)SMOutputtablecreateEntityRef(java.lang.String name)static SMOutputContextcreateInstance(org.codehaus.stax2.XMLStreamWriter2 sw)static SMOutputContextcreateInstance(org.codehaus.stax2.XMLStreamWriter2 sw, javax.xml.namespace.NamespaceContext rootNsCtxt)SMOutputtablecreateNamespace(SMNamespace ns, SMNamespace parentDefaultNS, int parentNsCount)Method called bySMOutputElementto add buffered namespace pre-declaration.SMOutputtablecreateNonSharedCData(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.SMOutputtablecreateNonSharedCharacters(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.SMOutputtablecreateProcessingInstruction(java.lang.String target, java.lang.String data)SMRootFragmentcreateRootFragment()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.SMOutputtablecreateValue(boolean value)SMOutputtablecreateValue(byte[] buffer)SMOutputtablecreateValue(byte[] buffer, int offset, int length)SMOutputtablecreateValue(double value)SMOutputtablecreateValue(int value)SMOutputtablecreateValue(long value)java.lang.StringfindRootPrefix(SMNamespace ns)voidflushWriter()java.lang.StringgenerateUnboundPrefix()static SMNamespacegetEmptyNamespace()SMNamespacegetNamespace(java.lang.String uri)SMNamespacegetNamespace(java.lang.String uri, java.lang.String prefPrefix)(package private) intgetNamespaceCount()org.codehaus.stax2.XMLStreamWriter2getWriter()(package private) booleanisDefaultNs(SMNamespace ns)booleanisPrefixBound(java.lang.String prefix)booleanisPrefixBoundLocally(java.lang.String prefix, int parentNsCount)Similar toisPrefixBound(java.lang.String), but only considers bindings added by the current start element.booleanisWriterRepairing()private voidoutputIndentation()voidpredeclareNamespace(SMNamespace ns, SMNamespace parentDefaultNS, int parentNsCount)Method called to try to pre-declare given namespacevoidsetIndentation(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.voidwriteAttribute(SMNamespace ns, java.lang.String localName, byte[] value)voidwriteAttribute(SMNamespace ns, java.lang.String localName, int value)voidwriteAttribute(SMNamespace ns, java.lang.String localName, java.lang.String value)voidwriteCData(char[] buf, int offset, int len)voidwriteCData(java.lang.String text)voidwriteCharacters(char[] buf, int offset, int len)voidwriteCharacters(java.lang.String text)voidwriteComment(java.lang.String text)voidwriteDoctypeDecl(java.lang.String rootName, java.lang.String systemId, java.lang.String publicId, java.lang.String intSubset)voidwriteEndDocument()voidwriteEndElement(int parentNsCount, SMNamespace parentDefNs)voidwriteEntityRef(java.lang.String name)voidwriteProcessingInstruction(java.lang.String target, java.lang.String data)voidwriteStartDocument()voidwriteStartDocument(java.lang.String version, java.lang.String encoding)voidwriteStartDocument(java.lang.String version, java.lang.String encoding, boolean standalone)SMNamespacewriteStartElement(SMNamespace ns, java.lang.String localName)Method called by the element object when it is about to get written out.voidwriteValue(boolean v)voidwriteValue(byte[] data)voidwriteValue(byte[] data, int offset, int length)voidwriteValue(double d)voidwriteValue(int v)voidwriteValue(long v)
-
-
-
Field Detail
-
DEF_NS_STACK_SIZE
protected static final int DEF_NS_STACK_SIZE
- See Also:
- Constant Field Values
-
NS_EMPTY
protected static final SMNamespace NS_EMPTY
-
NS_XML
protected static final SMNamespace NS_XML
-
NS_XMLNS
protected static final SMNamespace NS_XMLNS
-
sGlobalNsMap
protected static final java.util.HashMap<java.lang.String,SMNamespace> sGlobalNsMap
-
FALSE_VALUE
protected static final SMOTypedValue FALSE_VALUE
-
TRUE_VALUE
protected static final SMOTypedValue TRUE_VALUE
-
_streamWriter
protected final org.codehaus.stax2.XMLStreamWriter2 _streamWriter
-
_rootNsContext
protected final javax.xml.namespace.NamespaceContext _rootNsContext
-
_cfgRepairing
protected final boolean _cfgRepairing
-
_nsPrefixPrefix
protected java.lang.String _nsPrefixPrefix
Prefix to use for creating automatic namespace prefixes. For example, setting this to "ns" would result in automatic prefixes of form "ns1", "ns2" and so on.
-
_nsPrefixSeqNr
protected int _nsPrefixSeqNr
-
_cfgPreferDefaultNs
protected boolean _cfgPreferDefaultNs
Configuration flag that specifies whether by default namespaces should bind as the default namespaces for elements or not. If true, all unbound namespaces are always bound as the default namespace, when elements are output: if false, more complicated logics is used (which considers preferred prefixes, past bindings etc).
-
_localNsMap
protected java.util.HashMap<java.lang.String,SMNamespace> _localNsMap
Map that contains all local namespaces, that is, namespaces that have been created for use with documents output using this context.
-
_defaultNS
protected SMNamespace _defaultNS
Currently active default namespace; one that is in effect within current scope (inside currently open element, if any; if none, within root level).
-
_nsStack
SMNamespace[] _nsStack
Stack of bound non-default namespaces.
-
_boundNsCount
int _boundNsCount
Number of bound namespaces in_nsStack
-
_currElemNS
SMNamespace _currElemNS
Namespace of the last START_ELEMENT output.
-
_indentString
java.lang.String _indentString
This String is null when not doing (heuristic) indentation. Otherwise it defines the longest possible indentation String to use; subset by the offset indexes as necessary.
-
_indentOffset
int _indentOffset
Current offset within indentation String, if indenting. Basically offset of the first character after end of indentation String.
-
_indentStep
int _indentStep
Number of characters to add to_indentOffsetwhen adding a new indentation level (and conversely, subtract when closing such level).
-
_indentSuppress
int _indentSuppress
Counter used to suppress indentation, for levels where text has been output (indicating either pure-text or mixed content). Set to -1 when indentation is disabled. This remains 0 when no explicit text output has been done, and is set to 1 from such a state. After becoming non-zero, it will be incremented by one for each new level (start element output), and subtracted by one for close elements.Since this needs to be 0 for any indentation to be output, it is also used as a 'flag' to see if indentation is enabled.
-
_indentLevelEmpty
boolean _indentLevelEmpty
This flag is used to prevent indentation from being added for empty leaf elements, which should either be output as empty elements, or start/end tag pair, with no intervening spaces.
-
-
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 argumentsstartOffset- Initial character offset for the first level of indentation (current context; usually root context): basically, number of leading characters fromindentStrto 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
-
createAttribute
public SMOutputtable createAttribute(SMNamespace ns, java.lang.String localName, java.lang.String value)
-
createAttribute
public SMOutputtable createAttribute(SMNamespace ns, java.lang.String localName, int value)
-
createAttribute
public SMOutputtable createAttribute(SMNamespace ns, java.lang.String localName, byte[] value)
-
createNamespace
public SMOutputtable createNamespace(SMNamespace ns, SMNamespace parentDefaultNS, int parentNsCount)
Method called bySMOutputElementto add buffered namespace pre-declaration.
-
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)
-
createValue
public SMOutputtable createValue(boolean value)
-
createValue
public SMOutputtable createValue(int value)
-
createValue
public SMOutputtable createValue(long value)
-
createValue
public SMOutputtable createValue(double value)
-
createValue
public SMOutputtable createValue(byte[] buffer)
-
createValue
public SMOutputtable createValue(byte[] buffer, int offset, int length)
-
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()
-
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(SMNamespace ns, java.lang.String localName, java.lang.String value) throws javax.xml.stream.XMLStreamException
- Throws:
javax.xml.stream.XMLStreamException
-
writeAttribute
public void writeAttribute(SMNamespace ns, java.lang.String localName, int value) throws javax.xml.stream.XMLStreamException
- Throws:
javax.xml.stream.XMLStreamException
-
writeAttribute
public void writeAttribute(SMNamespace ns, java.lang.String localName, byte[] value) throws javax.xml.stream.XMLStreamException
- Throws:
javax.xml.stream.XMLStreamException
-
_ensureBindingForAttribute
protected java.lang.String _ensureBindingForAttribute(SMNamespace ns) throws javax.xml.stream.XMLStreamException
- Throws:
javax.xml.stream.XMLStreamException
-
predeclareNamespace
public void predeclareNamespace(SMNamespace ns, SMNamespace parentDefaultNS, int parentNsCount) throws javax.xml.stream.XMLStreamException
Method called to try to pre-declare given namespace- 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
-
writeValue
public void writeValue(boolean v) throws javax.xml.stream.XMLStreamException- Throws:
javax.xml.stream.XMLStreamException
-
writeValue
public void writeValue(int v) throws javax.xml.stream.XMLStreamException- Throws:
javax.xml.stream.XMLStreamException
-
writeValue
public void writeValue(long v) throws javax.xml.stream.XMLStreamException- Throws:
javax.xml.stream.XMLStreamException
-
writeValue
public void writeValue(double d) throws javax.xml.stream.XMLStreamException- Throws:
javax.xml.stream.XMLStreamException
-
writeValue
public void writeValue(byte[] data) throws javax.xml.stream.XMLStreamException- Throws:
javax.xml.stream.XMLStreamException
-
writeValue
public void writeValue(byte[] data, int offset, int length) throws javax.xml.stream.XMLStreamException- Throws:
javax.xml.stream.XMLStreamException
-
flushWriter
public void flushWriter() throws javax.xml.stream.XMLStreamException- Throws:
javax.xml.stream.XMLStreamException
-
closeWriterCompletely
public void closeWriterCompletely() throws javax.xml.stream.XMLStreamExceptionMethod that can be called to force full closing of the underlying stream writer as well as output target it uses (usually aOutputStreamorWriter). Latter is done by callingXMLStreamWriter2.closeCompletely()on stream writer.- Throws:
javax.xml.stream.XMLStreamException
-
generateUnboundPrefix
public java.lang.String generateUnboundPrefix()
-
isPrefixBound
public boolean isPrefixBound(java.lang.String prefix)
-
isPrefixBoundLocally
public boolean isPrefixBoundLocally(java.lang.String prefix, int parentNsCount)Similar toisPrefixBound(java.lang.String), but only considers bindings added by the current start element.
-
findRootPrefix
public java.lang.String findRootPrefix(SMNamespace ns)
-
getNamespaceCount
int getNamespaceCount()
- Returns:
- Number of bound non-default namespaces (ones with explicit prefix) currently
-
isDefaultNs
boolean isDefaultNs(SMNamespace ns)
-
bindAndWriteNs
private void bindAndWriteNs(SMNamespace ns, java.lang.String prefix) throws javax.xml.stream.XMLStreamException
Method for establishing binding between given namespace and a non-empty prefix, as well as writing resulting namespace declaration out.- Throws:
javax.xml.stream.XMLStreamException
-
outputIndentation
private void outputIndentation() throws javax.xml.stream.XMLStreamException- Throws:
javax.xml.stream.XMLStreamException
-
-