Class SMOutputContext
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
FieldsModifier and TypeFieldDescription(package private) int
Number of bound namespaces in_nsStack
protected boolean
Configuration flag that specifies whether by default namespaces should bind as the default namespaces for elements or not.protected final boolean
(package private) SMNamespace
Namespace of the last START_ELEMENT output.protected SMNamespace
Currently 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
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.(package private) int
Current offset within indentation String, if indenting.(package private) int
Number of characters to add to_indentOffset
when adding a new indentation level (and conversely, subtract when closing such level).(package private) String
This String is null when not doing (heuristic) indentation.(package private) int
Counter used to suppress indentation, for levels where text has been output (indicating either pure-text or mixed content).protected HashMap
<String, SMNamespace> Map that contains all local namespaces, that is, namespaces that have been created for use with documents output using this context.protected String
Prefix to use for creating automatic namespace prefixes.protected int
(package private) SMNamespace[]
Stack of bound non-default namespaces.protected final NamespaceContext
protected final org.codehaus.stax2.XMLStreamWriter2
protected static final int
protected static final SMOTypedValue
protected static final SMNamespace
protected static final SMNamespace
protected static final SMNamespace
protected static final HashMap
<String, SMNamespace> protected static final SMOTypedValue
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprotected
SMOutputContext
(org.codehaus.stax2.XMLStreamWriter2 sw, NamespaceContext rootNsCtxt) -
Method Summary
Modifier and TypeMethodDescriptionprotected String
private void
bindAndWriteNs
(SMNamespace ns, String prefix) Method for establishing binding between given namespace and a non-empty prefix, as well as writing resulting namespace declaration out.void
Method that can be called to force full closing of the underlying stream writer as well as output target it uses (usually aOutputStream
orWriter
).createAttribute
(SMNamespace ns, String localName, byte[] value) createAttribute
(SMNamespace ns, String localName, int value) createAttribute
(SMNamespace ns, String localName, String value) createCData
(char[] buf, int offset, int len) createCData
(String text) createCharacters
(char[] buf, int offset, int len) createCharacters
(String text) createComment
(String text) Method used to create a StaxMate output fragment that corresponds to a single well-formed XML document.createDocument
(String version, String encoding) Method used to create a StaxMate output fragment that corresponds to a single well-formed XML document.createDocument
(String version, String encoding, boolean standalone) createEntityRef
(String name) static SMOutputContext
createInstance
(org.codehaus.stax2.XMLStreamWriter2 sw) static SMOutputContext
createInstance
(org.codehaus.stax2.XMLStreamWriter2 sw, NamespaceContext rootNsCtxt) createNamespace
(SMNamespace ns, SMNamespace parentDefaultNS, int parentNsCount) Method called bySMOutputElement
to add buffered namespace pre-declaration.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.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.createProcessingInstruction
(String target, String data) 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.createValue
(boolean value) createValue
(byte[] buffer) createValue
(byte[] buffer, int offset, int length) createValue
(double value) createValue
(int value) createValue
(long value) void
static final SMNamespace
final SMNamespace
getNamespace
(String uri) final SMNamespace
getNamespace
(String uri, String prefPrefix) (package private) int
final org.codehaus.stax2.XMLStreamWriter2
(package private) boolean
boolean
isPrefixBound
(String prefix) boolean
isPrefixBoundLocally
(String prefix, int parentNsCount) Similar toisPrefixBound(java.lang.String)
, but only considers bindings added by the current start element.final boolean
private void
void
predeclareNamespace
(SMNamespace ns, SMNamespace parentDefaultNS, int parentNsCount) Method called to try to pre-declare given namespacevoid
setIndentation
(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
(SMNamespace ns, String localName, byte[] value) void
writeAttribute
(SMNamespace ns, String localName, int value) void
writeAttribute
(SMNamespace ns, String localName, String value) void
writeCData
(char[] buf, int offset, int len) void
writeCData
(String text) void
writeCharacters
(char[] buf, int offset, int len) void
writeCharacters
(String text) void
writeComment
(String text) void
writeDoctypeDecl
(String rootName, String systemId, String publicId, String intSubset) void
void
writeEndElement
(int parentNsCount, SMNamespace parentDefNs) void
writeEntityRef
(String name) void
writeProcessingInstruction
(String target, String data) void
void
writeStartDocument
(String version, String encoding) void
writeStartDocument
(String version, String encoding, boolean standalone) writeStartElement
(SMNamespace ns, String localName) Method called by the element object when it is about to get written out.void
writeValue
(boolean v) void
writeValue
(byte[] data) void
writeValue
(byte[] data, int offset, int length) void
writeValue
(double d) void
writeValue
(int v) void
writeValue
(long v)
-
Field Details
-
DEF_NS_STACK_SIZE
protected static final int DEF_NS_STACK_SIZE- See Also:
-
NS_EMPTY
-
NS_XML
-
NS_XMLNS
-
sGlobalNsMap
-
FALSE_VALUE
-
TRUE_VALUE
-
_streamWriter
protected final org.codehaus.stax2.XMLStreamWriter2 _streamWriter -
_rootNsContext
-
_cfgRepairing
protected final boolean _cfgRepairing -
_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 _cfgPreferDefaultNsConfiguration 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
Map that contains all local namespaces, that is, namespaces that have been created for use with documents output using this context. -
_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[] _nsStackStack of bound non-default namespaces. -
_boundNsCount
int _boundNsCountNumber of bound namespaces in_nsStack
-
_currElemNS
SMNamespace _currElemNSNamespace of the last START_ELEMENT output. -
_indentString
String _indentStringThis 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 _indentOffsetCurrent offset within indentation String, if indenting. Basically offset of the first character after end of indentation String. -
_indentStep
int _indentStepNumber of characters to add to_indentOffset
when adding a new indentation level (and conversely, subtract when closing such level). -
_indentSuppress
int _indentSuppressCounter 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 _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.
-
-
Constructor Details
-
SMOutputContext
-
-
Method Details
-
setIndentation
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 fromindentStr
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, NamespaceContext rootNsCtxt) throws XMLStreamException - Throws:
XMLStreamException
-
createInstance
public static SMOutputContext createInstance(org.codehaus.stax2.XMLStreamWriter2 sw) throws XMLStreamException - Throws:
XMLStreamException
-
createDocument
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:
XMLStreamException
-
createDocument
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:
XMLStreamException
-
createDocument
public SMOutputDocument createDocument(String version, String encoding, boolean standalone) throws XMLStreamException - Throws:
XMLStreamException
-
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. Resulting fragment is not buffered, and will thus be fully streamed (except for buffering caused by adding buffered children)- Throws:
XMLStreamException
-
createBufferedFragment
- Throws:
XMLStreamException
-
createAttribute
-
createAttribute
-
createAttribute
-
createNamespace
public SMOutputtable createNamespace(SMNamespace ns, SMNamespace parentDefaultNS, int parentNsCount) Method called bySMOutputElement
to add buffered namespace pre-declaration. -
createCharacters
-
createCharacters
-
createCData
-
createCData
-
createComment
-
createEntityRef
-
createProcessingInstruction
-
createValue
-
createValue
-
createValue
-
createValue
-
createValue
-
createValue
-
getNamespace
-
getNamespace
-
getEmptyNamespace
-
getWriter
public final org.codehaus.stax2.XMLStreamWriter2 getWriter() -
isWriterRepairing
public final boolean isWriterRepairing() -
writeCharacters
- Throws:
XMLStreamException
-
writeCharacters
- Throws:
XMLStreamException
-
writeCData
- Throws:
XMLStreamException
-
writeCData
- Throws:
XMLStreamException
-
writeComment
- Throws:
XMLStreamException
-
writeEntityRef
- Throws:
XMLStreamException
-
writeProcessingInstruction
- Throws:
XMLStreamException
-
writeAttribute
public void writeAttribute(SMNamespace ns, String localName, String value) throws XMLStreamException - Throws:
XMLStreamException
-
writeAttribute
- Throws:
XMLStreamException
-
writeAttribute
public void writeAttribute(SMNamespace ns, String localName, byte[] value) throws XMLStreamException - Throws:
XMLStreamException
-
_ensureBindingForAttribute
- Throws:
XMLStreamException
-
predeclareNamespace
public void predeclareNamespace(SMNamespace ns, SMNamespace parentDefaultNS, int parentNsCount) throws XMLStreamException Method called to try to pre-declare given namespace- Throws:
XMLStreamException
-
writeStartElement
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:
XMLStreamException
-
writeEndElement
- Throws:
XMLStreamException
-
writeStartDocument
- Throws:
XMLStreamException
-
writeStartDocument
- Throws:
XMLStreamException
-
writeStartDocument
public void writeStartDocument(String version, String encoding, boolean standalone) throws XMLStreamException - Throws:
XMLStreamException
-
writeEndDocument
- Throws:
XMLStreamException
-
writeDoctypeDecl
public void writeDoctypeDecl(String rootName, String systemId, String publicId, String intSubset) throws XMLStreamException - Throws:
XMLStreamException
-
writeValue
- Throws:
XMLStreamException
-
writeValue
- Throws:
XMLStreamException
-
writeValue
- Throws:
XMLStreamException
-
writeValue
- Throws:
XMLStreamException
-
writeValue
- Throws:
XMLStreamException
-
writeValue
- Throws:
XMLStreamException
-
flushWriter
- Throws:
XMLStreamException
-
closeWriterCompletely
Method that can be called to force full closing of the underlying stream writer as well as output target it uses (usually aOutputStream
orWriter
). Latter is done by callingXMLStreamWriter2.closeCompletely()
on stream writer.- Throws:
XMLStreamException
-
generateUnboundPrefix
-
isPrefixBound
-
isPrefixBoundLocally
Similar toisPrefixBound(java.lang.String)
, but only considers bindings added by the current start element. -
findRootPrefix
-
getNamespaceCount
int getNamespaceCount()- Returns:
- Number of bound non-default namespaces (ones with explicit prefix) currently
-
isDefaultNs
-
bindAndWriteNs
Method for establishing binding between given namespace and a non-empty prefix, as well as writing resulting namespace declaration out.- Throws:
XMLStreamException
-
outputIndentation
- Throws:
XMLStreamException
-