📄 marshallingcontext.java
字号:
* Initializes the context to use the same marshalled text destination and
* parameters as another marshalling context. This method is designed for
* use when an initial context needs to create and invoke a secondary
* context (generally from a different binding) in the course of an
* marshalling operation. Note that once the secondary context has been used
* it's generally necessary to do a {@link XMLWriterBase#flush()} operation
* on the writer used by the that context before resuming output on the
* parent.
*
* @param parent context supplying target for marshalled document text
* @throws IOException on error writing output
*/
public void setFromContext(MarshallingContext parent) throws IOException {
reset();
m_indentCount = parent.m_indentCount;
m_newLine = parent.m_newLine;
m_indentChar = parent.m_indentChar;
if (parent.m_writer instanceof XMLWriterBase) {
XMLWriterBase base = (XMLWriterBase)parent.m_writer;
base.flagContent();
base.flush();
m_writer = base.createChildWriter(m_uris);
} else if (parent.m_writer instanceof StAXWriter) {
m_writer = ((StAXWriter)parent.m_writer).createChildWriter(m_uris);
} else {
m_writer = parent.m_writer;
}
}
/**
* Reset to initial state for reuse. The context is serially reusable,
* as long as this method is called to clear any retained state information
* between uses. It is automatically called when output is set.
*/
public void reset() {
if (m_writer != null) {
m_writer.reset();
}
for (int i = m_globalCount; i < m_marshallers.length; i++) {
m_marshallers[i] = null;
}
for (int i = 0; i < m_objectStack.length; i++) {
m_objectStack[i] = null;
}
m_stackDepth = 0;
}
/**
* Return the binding factory used to create this unmarshaller.
*
* @return binding factory
*/
public IBindingFactory getFactory() {
return m_factory;
}
/**
* Get namespace URIs for mapping. This gets the full ordered array of
* namespaces known in the binding used for this marshalling, where the
* index number of each namespace URI is the namespace index used to lookup
* the prefix when marshalling a name in that namespace. The returned array
* must not be modified.
*
* @return array of namespaces
*/
public String[] getNamespaces() {
return m_uris;
}
/**
* Start document. This can only be validly called immediately following
* one of the set output methods; otherwise the output document will be
* corrupt.
*
* @param enc document encoding, <code>null</code> if not specified
* @param alone standalone document flag, <code>null</code> if not
* specified
* @throws JiBXException on any error (possibly wrapping other exception)
*/
public void startDocument(String enc, Boolean alone) throws JiBXException {
try {
String atext = null;
if (alone != null) {
atext = alone.booleanValue() ? "yes" : "no";
}
m_writer.writeXMLDecl("1.0", enc, atext);
} catch (IOException ex) {
throw new JiBXException("Error writing marshalled document", ex);
}
}
/**
* Start document with output stream and encoding. The effect is the same
* as from first setting the output stream and encoding, then making the
* call to start document.
*
* @param enc document encoding, <code>null</code> if not specified
* @param alone standalone document flag, <code>null</code> if not
* specified
* @param outs stream for document data output
* @throws JiBXException on any error (possibly wrapping other exception)
*/
public void startDocument(String enc, Boolean alone, OutputStream outs)
throws JiBXException {
setOutput(outs, enc);
startDocument(enc, alone);
}
/**
* Start document with writer. The effect is the same as from first
* setting the writer, then making the call to start document.
*
* @param enc document encoding, <code>null</code> if not specified
* @param alone standalone document flag, <code>null</code> if not
* specified
* @param outw writer for document data output
* @throws JiBXException on any error (possibly wrapping other exception)
*/
public void startDocument(String enc, Boolean alone, Writer outw)
throws JiBXException {
setOutput(outw);
startDocument(enc, alone);
}
/**
* End document. Finishes all output and closes the document. Note that if
* this is called with an imcomplete marshalling the result will not be
* well-formed XML.
*
* @throws JiBXException on any error (possibly wrapping other exception)
*/
public void endDocument() throws JiBXException {
try {
m_writer.close();
} catch (IOException ex) {
throw new JiBXException("Error writing marshalled document", ex);
}
}
/**
* Build name with optional namespace. Just returns the appropriate
* name format.
*
* @param index namespace URI index number
* @param name local name part of name
* @return formatted name string
*/
public String buildNameString(int index, String name) {
String ns = m_writer.getNamespaceUri(index);
if (ns == null || "".equals(ns)) {
return "\"" + name + "\"";
} else {
return "\"{" + ns + "}" + name + "\"";
}
}
/**
* Generate start tag for element without attributes.
*
* @param index namespace URI index number
* @param name element name
* @return this context (to allow chained calls)
* @throws JiBXException on any error (possibly wrapping other exception)
*/
public MarshallingContext startTag(int index, String name)
throws JiBXException {
try {
m_writer.startTagClosed(index, name);
return this;
} catch (IOException ex) {
throw new JiBXException("Error writing marshalled document", ex);
}
}
/**
* Generate start tag for element with attributes. This only opens the start
* tag, allowing attributes to be added immediately following this call.
*
* @param index namespace URI index number
* @param name element name
* @return this context (to allow chained calls)
* @throws JiBXException on any error (possibly wrapping other exception)
*/
public MarshallingContext startTagAttributes(int index, String name)
throws JiBXException {
try {
m_writer.startTagOpen(index, name);
return this;
} catch (IOException ex) {
throw new JiBXException("Error writing marshalled document", ex);
}
}
/**
* Generate text attribute. This can only be used following an open start
* tag with attributes.
*
* @param index namespace URI index number
* @param name attribute name
* @param value text value for attribute (cannot be <code>null</code>)
* @return this context (to allow chained calls)
* @throws JiBXException on any error (possibly wrapping other exception)
*/
public MarshallingContext attribute(int index, String name, String value)
throws JiBXException {
try {
m_writer.addAttribute(index, name, value);
return this;
} catch (IOException ex) {
throw new JiBXException("Error writing marshalled document", ex);
} catch (Exception ex) {
String text = buildNameString(index, name);
if (value == null) {
throw new JiBXException("null value for attribute " +
text + " from object of type " +
getStackTop().getClass().getName());
} else {
throw new JiBXException
("Exception while marshalling attribute " + text, ex);
}
}
}
/**
* Generate integer attribute. This can only be used following an open start
* tag.
*
* @param index namespace URI index number
* @param name attribute name
* @param value integer value for attribute
* @return this context (to allow chained calls)
* @throws JiBXException on any error (possibly wrapping other exception)
*/
public MarshallingContext attribute(int index, String name, int value)
throws JiBXException {
return attribute(index, name, Integer.toString(value));
}
/**
* Generate enumeration attribute. The actual text to be written is obtained
* by indexing into the supplied array of values. This can only be used
* following an open start tag.
*
* @param index namespace URI index number
* @param name attribute name
* @param value integer enumeration value (zero-based)
* @param table text values in enumeration
* @return this context (to allow chained calls)
* @throws JiBXException on any error (possibly wrapping other exception)
*/
public MarshallingContext attribute(int index, String name, int value,
String[] table) throws JiBXException {
try {
return attribute(index, name, table[value]);
} catch (ArrayIndexOutOfBoundsException ex) {
throw new JiBXException("Enumeration value of " + value +
" is outside to allowed range of 0 to " + table.length);
}
}
/**
* Close start tag with content to follow.
*
* @return this context (to allow chained calls)
* @throws JiBXException on any error (possibly wrapping other exception)
*/
public MarshallingContext closeStartContent() throws JiBXException {
try {
m_writer.closeStartTag();
return this;
} catch (IOException ex) {
throw new JiBXException("Error writing marshalled document", ex);
}
}
/**
* Close start tag with no content (empty tag).
*
* @return this context (to allow chained calls)
* @throws JiBXException on any error (possibly wrapping other exception)
*/
public MarshallingContext closeStartEmpty() throws JiBXException {
try {
m_writer.closeEmptyTag();
return this;
} catch (IOException ex) {
throw new JiBXException("Error writing marshalled document", ex);
}
}
/**
* Add text content to current element.
*
* @param value text element content
* @return this context (to allow chained calls)
* @throws JiBXException on any error (possibly wrapping other exception)
*/
public MarshallingContext content(String value) throws JiBXException {
try {
m_writer.writeTextContent(value);
return this;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -