public class MarshallingContext extends java.lang.Object implements IMarshallingContext
Modifier and Type | Field and Description |
---|---|
private static int |
INITIAL_STACK_SIZE
Starting size for object stack.
|
private OutByteBuffer |
m_byteBuffer
Buffer used when writing to stream (lazy create,
null if
unused). |
private java.lang.String[] |
m_classes
Names of classes included in mapping definition.
|
private StringIntHashMap |
m_classIndexMap
Map from fully-qualified class name to index in internal tables.
|
private IBindingFactory |
m_factory
Binding factory used to create this unmarshaller.
|
private java.util.HashMap |
m_idMap
Shared map from IDs to objects.
|
private char |
m_indentChar
Character used for indenting.
|
private int |
m_indentCount
Indent character count per level.
|
private IMarshaller[] |
m_marshallers
Marshallers for classes in mapping definition (lazy create of actual
marshaller instances)
|
private java.lang.String |
m_newLine
Character sequence for end of line.
|
private java.lang.Object[] |
m_objectStack
Stack of objects being marshalled.
|
private int |
m_stackDepth
Current marshalling stack depth.
|
private int |
m_transientBase
Index past end of last fixed marshaller class.
|
private java.lang.String[] |
m_transientMarshallerClasses
Transient marshaller classes for mapping definition (
null
for mappings out of context). |
private java.lang.String[] |
m_uris
URIs for namespaces used in binding.
|
protected java.lang.Object |
m_userContext
User context object (not used by JiBX, only for user convenience).
|
private IXMLWriter |
m_writer
Output document handler.
|
static java.lang.String |
XML_NAMESPACE
Fixed XML namespace.
|
Constructor and Description |
---|
MarshallingContext(java.lang.String[] classes,
java.lang.String[] mcs,
java.lang.String[] uris,
IBindingFactory ifact)
Constructor.
|
Modifier and Type | Method and Description |
---|---|
void |
addMarshalling(java.lang.String mapname,
java.lang.String name)
Define marshalling for class.
|
MarshallingContext |
attribute(int index,
java.lang.String name,
int value)
Generate integer attribute.
|
MarshallingContext |
attribute(int index,
java.lang.String name,
int value,
java.lang.String[] table)
Generate enumeration attribute.
|
MarshallingContext |
attribute(int index,
java.lang.String name,
java.lang.String value)
Generate text attribute.
|
java.lang.String |
buildNameString(int index,
java.lang.String name)
Build name with optional namespace.
|
MarshallingContext |
closeStartContent()
Close start tag with content to follow.
|
MarshallingContext |
closeStartEmpty()
Close start tag with no content (empty tag).
|
MarshallingContext |
content(int value)
Add integer content to current element.
|
MarshallingContext |
content(int value,
java.lang.String[] table)
Add enumeration content to current element.
|
MarshallingContext |
content(java.lang.String value)
Add text content to current element.
|
private ICharacterEscaper |
createEscaper(java.lang.String enc)
Create character escaper for encoding.
|
MarshallingContext |
element(int index,
java.lang.String name,
int value)
Generate complete element with integer content.
|
MarshallingContext |
element(int index,
java.lang.String name,
int value,
java.lang.String[] table)
Generate complete element with enumeration content.
|
MarshallingContext |
element(int index,
java.lang.String name,
java.lang.String value)
Generate complete element with text content.
|
void |
endDocument()
End document.
|
MarshallingContext |
endTag(int index,
java.lang.String name)
Generate end tag for element.
|
IBindingFactory |
getFactory()
Return the binding factory used to create this unmarshaller.
|
java.util.HashMap |
getIdMap()
Get shared ID map.
|
int |
getIndent()
Get current nesting indent spaces.
|
IMarshaller |
getMarshaller(java.lang.String mapname)
Find the marshaller for a particular class index in the current context.
|
java.lang.String[] |
getNamespaces()
Get namespace URIs for mapping.
|
int |
getStackDepth()
Get current marshalling object stack depth.
|
java.lang.Object |
getStackObject(int depth)
Get object from marshalling stack.
|
java.lang.Object |
getStackTop()
Get top object on marshalling stack.
|
java.lang.Object |
getUserContext()
Get the user context object.
|
IXMLWriter |
getXmlWriter()
Get the writer being used for output.
|
MarshallingContext |
marshalCollection(java.util.ArrayList col)
Marshal all items in a collection.
|
MarshallingContext |
marshalCollection(java.util.Collection col)
Marshal all items in a collection.
|
MarshallingContext |
marshalCollection(java.util.Vector col)
Marshal all items in a collection.
|
void |
marshalDocument(java.lang.Object root)
Marshal document from root object without XML declaration.
|
void |
marshalDocument(java.lang.Object root,
java.lang.String enc,
java.lang.Boolean alone)
Marshal document from root object.
|
void |
marshalDocument(java.lang.Object root,
java.lang.String enc,
java.lang.Boolean alone,
java.io.OutputStream outs)
Marshal document from root object to output stream with encoding.
|
void |
marshalDocument(java.lang.Object root,
java.lang.String enc,
java.lang.Boolean alone,
java.io.Writer outw)
Marshal document from root object to writer.
|
protected void |
marshalRoot(java.lang.Object root)
Marshal document from root object.
|
void |
popNamespaces()
End use of namespace indexes from a separate binding.
|
void |
popObject()
Pop marshalled object from stack.
|
void |
pushNamespaces(java.lang.String factname)
Use namespace indexes from a separate binding, as identified by that
binding's factory class name.
|
void |
pushObject(java.lang.Object obj)
Push created object to marshalling stack.
|
void |
removeMarshalling(java.lang.String mapname)
Undefine marshalling for element.
|
void |
reset()
Reset to initial state for reuse.
|
void |
setFromContext(MarshallingContext parent)
Initializes the context to use the same marshalled text destination and
parameters as another marshalling context.
|
void |
setIndent(int count)
Set nesting indent spaces.
|
void |
setIndent(int count,
java.lang.String newline,
char indent)
Set nesting indentation.
|
void |
setOutput(java.io.OutputStream outs,
java.lang.String enc)
Set output stream and encoding.
|
void |
setOutput(java.io.OutputStream outs,
java.lang.String enc,
ICharacterEscaper esc)
Set output stream with encoding and escaper.
|
void |
setOutput(java.io.Writer outw)
Set output writer.
|
void |
setOutput(java.io.Writer outw,
ICharacterEscaper esc)
Set output writer and escaper.
|
void |
setUserContext(java.lang.Object obj)
Set a user context object.
|
void |
setXmlWriter(IXMLWriter xwrite)
Set the writer being used for output.
|
void |
startDocument(java.lang.String enc,
java.lang.Boolean alone)
Start document.
|
void |
startDocument(java.lang.String enc,
java.lang.Boolean alone,
java.io.OutputStream outs)
Start document with output stream and encoding.
|
void |
startDocument(java.lang.String enc,
java.lang.Boolean alone,
java.io.Writer outw)
Start document with writer.
|
MarshallingContext |
startTag(int index,
java.lang.String name)
Generate start tag for element without attributes.
|
MarshallingContext |
startTagAttributes(int index,
java.lang.String name)
Generate start tag for element with attributes.
|
MarshallingContext |
startTagNamespaces(int index,
java.lang.String name,
int[] nums,
java.lang.String[] prefs)
Generate start tag for element with namespaces.
|
MarshallingContext |
writeCData(java.lang.String text)
Write CDATA text to document.
|
MarshallingContext |
writeContent(java.lang.String text)
Write content value with character entity substitutions.
|
public static final java.lang.String XML_NAMESPACE
private static final int INITIAL_STACK_SIZE
private IBindingFactory m_factory
private StringIntHashMap m_classIndexMap
private java.lang.String[] m_classes
private int m_transientBase
private java.lang.String[] m_transientMarshallerClasses
null
for mappings out of context).private IMarshaller[] m_marshallers
private java.lang.String[] m_uris
private int m_stackDepth
private java.lang.Object[] m_objectStack
private int m_indentCount
private java.lang.String m_newLine
private char m_indentChar
private java.util.HashMap m_idMap
private IXMLWriter m_writer
private OutByteBuffer m_byteBuffer
null
if
unused).protected java.lang.Object m_userContext
public MarshallingContext(java.lang.String[] classes, java.lang.String[] mcs, java.lang.String[] uris, IBindingFactory ifact)
classes
- ordered array of class names included in mapping
definition (reference kept, must be constant)mcs
- names of marshaller classes for indexes with fixed marshallers
(as opposed to mapping slots, which may be overridden; reference kept,
must be constant)uris
- ordered array of URIs for namespaces used in binding (must
be constant; the value in position 0 must always be the empty string "",
and the value in position 1 must always be the XML namespace
"http://www.w3.org/XML/1998/namespace")ifact
- binding factory creating this unmarshallerprivate ICharacterEscaper createEscaper(java.lang.String enc) throws JiBXException
enc
- document output encoding, or null
for defaultJiBXException
- if error creating setting outputpublic void setOutput(java.io.OutputStream outs, java.lang.String enc, ICharacterEscaper esc) throws JiBXException
setOutput
in interface IMarshallingContext
outs
- stream for document data outputenc
- document output encoding, or null
uses UTF-8
defaultesc
- escaper for writing characters to streamJiBXException
- if error setting outputpublic void setOutput(java.io.OutputStream outs, java.lang.String enc) throws JiBXException
setOutput
in interface IMarshallingContext
outs
- stream for document data outputenc
- document output encoding, or null
for defaultJiBXException
- if error creating setting outputpublic void setOutput(java.io.Writer outw, ICharacterEscaper esc)
setOutput
in interface IMarshallingContext
outw
- writer for document data outputesc
- escaper for writing characterspublic void setOutput(java.io.Writer outw)
setOutput
in interface IMarshallingContext
outw
- writer for document data outputpublic IXMLWriter getXmlWriter()
getXmlWriter
in interface IMarshallingContext
public void setXmlWriter(IXMLWriter xwrite)
setXmlWriter
in interface IMarshallingContext
xwrite
- XML writer used for outputpublic int getIndent()
getIndent
in interface IMarshallingContext
public void setIndent(int count)
setIndent
in interface IMarshallingContext
count
- number of spaces to indent per level, or disable
indentation if negativepublic void setIndent(int count, java.lang.String newline, char indent)
setIndent
in interface IMarshallingContext
count
- number of character to indent per level, or disable
indentation if negative (zero means new line only)newline
- sequence of characters used for a line ending
(null
means use the single character '\n')indent
- whitespace character used for indentationpublic void setFromContext(MarshallingContext parent) throws java.io.IOException
XMLWriterBase.flush()
operation
on the writer used by the that context before resuming output on the
parent.parent
- context supplying target for marshalled document textjava.io.IOException
- on error writing outputpublic void reset()
reset
in interface IMarshallingContext
public IBindingFactory getFactory()
public java.lang.String[] getNamespaces()
public void startDocument(java.lang.String enc, java.lang.Boolean alone) throws JiBXException
startDocument
in interface IMarshallingContext
enc
- document encoding, null
if not specifiedalone
- standalone document flag, null
if not
specifiedJiBXException
- on any error (possibly wrapping other exception)public void startDocument(java.lang.String enc, java.lang.Boolean alone, java.io.OutputStream outs) throws JiBXException
startDocument
in interface IMarshallingContext
enc
- document encoding, null
if not specifiedalone
- standalone document flag, null
if not
specifiedouts
- stream for document data outputJiBXException
- on any error (possibly wrapping other exception)public void startDocument(java.lang.String enc, java.lang.Boolean alone, java.io.Writer outw) throws JiBXException
startDocument
in interface IMarshallingContext
enc
- document encoding, null
if not specifiedalone
- standalone document flag, null
if not
specifiedoutw
- writer for document data outputJiBXException
- on any error (possibly wrapping other exception)public void endDocument() throws JiBXException
endDocument
in interface IMarshallingContext
JiBXException
- on any error (possibly wrapping other exception)public java.lang.String buildNameString(int index, java.lang.String name)
index
- namespace URI index numbername
- local name part of namepublic MarshallingContext startTag(int index, java.lang.String name) throws JiBXException
index
- namespace URI index numbername
- element nameJiBXException
- on any error (possibly wrapping other exception)public MarshallingContext startTagAttributes(int index, java.lang.String name) throws JiBXException
index
- namespace URI index numbername
- element nameJiBXException
- on any error (possibly wrapping other exception)public MarshallingContext attribute(int index, java.lang.String name, java.lang.String value) throws JiBXException
index
- namespace URI index numbername
- attribute namevalue
- text value for attribute (cannot be null
)JiBXException
- on any error (possibly wrapping other exception)public MarshallingContext attribute(int index, java.lang.String name, int value) throws JiBXException
index
- namespace URI index numbername
- attribute namevalue
- integer value for attributeJiBXException
- on any error (possibly wrapping other exception)public MarshallingContext attribute(int index, java.lang.String name, int value, java.lang.String[] table) throws JiBXException
index
- namespace URI index numbername
- attribute namevalue
- integer enumeration value (zero-based)table
- text values in enumerationJiBXException
- on any error (possibly wrapping other exception)public MarshallingContext closeStartContent() throws JiBXException
JiBXException
- on any error (possibly wrapping other exception)public MarshallingContext closeStartEmpty() throws JiBXException
JiBXException
- on any error (possibly wrapping other exception)public MarshallingContext content(java.lang.String value) throws JiBXException
value
- text element contentJiBXException
- on any error (possibly wrapping other exception)public MarshallingContext content(int value) throws JiBXException
value
- integer element contentJiBXException
- on any error (possibly wrapping other exception)public MarshallingContext content(int value, java.lang.String[] table) throws JiBXException
value
- integer enumeration value (zero-based)table
- text values in enumerationJiBXException
- on any error (possibly wrapping other exception)public MarshallingContext endTag(int index, java.lang.String name) throws JiBXException
index
- namespace URI index numbername
- element nameJiBXException
- on any error (possibly wrapping other exception)public MarshallingContext element(int index, java.lang.String name, java.lang.String value) throws JiBXException
index
- namespace URI index numbername
- element namevalue
- text element contentJiBXException
- on any error (possibly wrapping other exception)public MarshallingContext element(int index, java.lang.String name, int value) throws JiBXException
index
- namespace URI index numbername
- element namevalue
- integer element contentJiBXException
- on any error (possibly wrapping other exception)public MarshallingContext element(int index, java.lang.String name, int value, java.lang.String[] table) throws JiBXException
index
- namespace URI index numbername
- element namevalue
- integer enumeration value (zero-based)table
- text values in enumerationJiBXException
- on any error (possibly wrapping other exception)public MarshallingContext writeCData(java.lang.String text) throws java.io.IOException
text
- content value textjava.io.IOException
- on error writing to documentpublic MarshallingContext writeContent(java.lang.String text) throws java.io.IOException
text
- content value textjava.io.IOException
- on error writing to documentpublic MarshallingContext marshalCollection(java.util.Collection col) throws JiBXException
col
- collection of items to be marshalledJiBXException
- on any error (possibly wrapping other exception)public MarshallingContext marshalCollection(java.util.ArrayList col) throws JiBXException
col
- collection of items to be marshalledJiBXException
- on any error (possibly wrapping other exception)public MarshallingContext marshalCollection(java.util.Vector col) throws JiBXException
col
- collection of items to be marshalledJiBXException
- on any error (possibly wrapping other exception)public void addMarshalling(java.lang.String mapname, java.lang.String name) throws JiBXException
mapname
- mapping name associated with unmarshallername
- marshaller class name handlingJiBXException
- if unknown mapping namepublic void removeMarshalling(java.lang.String mapname) throws JiBXException
mapname
- mapping name associated with unmarshallerJiBXException
- if unknown mapping namepublic MarshallingContext startTagNamespaces(int index, java.lang.String name, int[] nums, java.lang.String[] prefs) throws JiBXException
index
- namespace URI index numbername
- element namenums
- array of namespace indexes defined by this element (must
be constant, reference is kept until end of element)prefs
- array of namespace prefixes mapped by this element (no
null
values, use "" for default namespace declaration)JiBXException
- on any error (possibly wrapping other exception)public IMarshaller getMarshaller(java.lang.String mapname) throws JiBXException
getMarshaller
in interface IMarshallingContext
mapname
- marshaller mapping name (generally the class name to be
handled, or abstract mapping type name)JiBXException
- on any error (possibly wrapping other exception)protected void marshalRoot(java.lang.Object root) throws JiBXException
root
- object at root of structure to be marshalled, which must have
a top-level mapping in the bindingJiBXException
- on any error (possibly wrapping other exception)public void marshalDocument(java.lang.Object root) throws JiBXException
endDocument()
.marshalDocument
in interface IMarshallingContext
root
- object at root of structure to be marshalled, which must have
a top-level mapping in the bindingJiBXException
- on any error (possibly wrapping other exception)public void marshalDocument(java.lang.Object root, java.lang.String enc, java.lang.Boolean alone) throws JiBXException
startDocument(java.lang.String, java.lang.Boolean)
, a call to marshal the root
object using this context, and finally a call to endDocument()
.marshalDocument
in interface IMarshallingContext
root
- object at root of structure to be marshalled, which must have
a top-level mapping in the bindingenc
- document encoding, null
if not specifiedalone
- standalone document flag, null
if not
specifiedJiBXException
- on any error (possibly wrapping other exception)public void marshalDocument(java.lang.Object root, java.lang.String enc, java.lang.Boolean alone, java.io.OutputStream outs) throws JiBXException
startDocument(java.lang.String, java.lang.Boolean)
, a call to marshal the root object using this context,
and finally a call to endDocument()
.marshalDocument
in interface IMarshallingContext
root
- object at root of structure to be marshalled, which must have
a top-level mapping in the bindingenc
- document encoding, null
if not specifiedalone
- standalone document flag, null
if not
specifiedouts
- stream for document data outputJiBXException
- on any error (possibly wrapping other exception)public void marshalDocument(java.lang.Object root, java.lang.String enc, java.lang.Boolean alone, java.io.Writer outw) throws JiBXException
startDocument(java.lang.String, java.lang.Boolean)
, a call
to marshal the root object using this context, and finally a call to
endDocument()
.marshalDocument
in interface IMarshallingContext
root
- object at root of structure to be marshalled, which must have
a top-level mapping in the bindingenc
- document encoding, null
if not specifiedalone
- standalone document flag, null
if not
specifiedoutw
- writer for document data outputJiBXException
- on any error (possibly wrapping other exception)public void pushNamespaces(java.lang.String factname)
pushNamespaces
in interface IMarshallingContext
factname
- binding factory class name for binding defining namespacespublic void popNamespaces()
pushNamespaces(String)
,
restoring whatever namespace usage was in effect prior to that call.popNamespaces
in interface IMarshallingContext
public java.util.HashMap getIdMap()
public void setUserContext(java.lang.Object obj)
reset()
method,
so to make use of this you need to first call the appropriate version of
the setOutput()
method, then this method, and finally one of
the marshalDocument
methods which uses the previously-set
output (not the ones which take a stream or writer as parameter, since
they call setOutput()
themselves).setUserContext
in interface IMarshallingContext
obj
- user context object, or null
if clearing existing
context objectgetUserContext()
public java.lang.Object getUserContext()
getUserContext
in interface IMarshallingContext
null
if no context object
setsetUserContext(Object)
public void pushObject(java.lang.Object obj)
pushObject
in interface IMarshallingContext
obj
- object being marshalledpublic void popObject() throws JiBXException
popObject
in interface IMarshallingContext
JiBXException
- if no object on stackpublic int getStackDepth()
getStackDepth
in interface IMarshallingContext
public java.lang.Object getStackObject(int depth)
getStackObject
in interface IMarshallingContext
depth
- object depth in stack to be retrieved (must be in the range
of zero to the current depth minus one).public java.lang.Object getStackTop()
getStackTop
in interface IMarshallingContext
null
if none