Class XMLSerializer
- All Implemented Interfaces:
javax.xml.bind.ValidationEventHandler,ErrorHandler
XmlOutput.
This object coordinates the overall marshalling efforts across different content-tree objects and different target formats.
The following CFG gives the proper sequence of method invocation.
MARSHALLING := ELEMENT
ELEMENT := "startElement" NSDECL* "endNamespaceDecls"
ATTRIBUTE* "endAttributes" BODY "endElement"
NSDECL := "declareNamespace"
ATTRIBUTE := "attribute"
ATTVALUES := "text"*
BODY := ( "text" | ELEMENT )*
A marshalling of one element consists of two stages. The first stage is for marshalling attributes and collecting namespace declarations. The second stage is for marshalling characters/child elements of that element.
Observe that multiple invocation of "text" is allowed.
Also observe that the namespace declarations are allowed only between "startElement" and "endAttributes".
Exceptions in marshaller
IOException, SAXException, and XMLStreamException
are thrown from XmlOutput. They are always considered fatal, and
therefore caught only by MarshallerImpl.
AccessorException can be thrown when an access to a property/field
fails, and this is considered as a recoverable error, so it's caught everywhere.
- Author:
- Kohsuke Kawaguchi
-
Field Summary
FieldsModifier and TypeFieldDescriptionjavax.xml.bind.attachment.AttachmentMarshallerfinal JAXBContextImplfinal int[]final NameList -
Method Summary
Modifier and TypeMethodDescriptionvoidaddInscopeBinding(String nsUri, String prefix) This method can be called afterstartDocument(com.sun.xml.bind.v2.runtime.output.XmlOutput, boolean, java.lang.String, java.lang.String)is called but before the marshalling begins, to set the currently in-scope namespace bindings.voidattribute(Name name, CharSequence value) voidvoidattWildcardAsAttributes(Map<QName, String> attributes, String fieldName) voidattWildcardAsURIs(Map<QName, String> attributes, String fieldName) voidchildAsRoot(Object obj) final voidchildAsSoleContent(Object child, String fieldName) The equivalent of:final voidchildAsXsiType(Object child, String fieldName, JaxBeanInfo expected, boolean nillable) This method is called when a type child object is found.voidTakes care of cleaning the currentProperty.voidclose()voidSwitches to the "marshal child texts/elements" mode.voidvoidEnds marshalling of an element.voidendNamespaceDecls(Object innerPeer) voiderrorMissingId(Object obj) Called when a referenced object doesn't have an ID.Deprecated.javax.xml.bind.ValidationEventLocatorgetCurrentLocation(String fieldName) May return null when the property hasn't been set.booleanstatic XMLSerializerWhen called from within the realm of the marshaller, this method returns the currentXMLSerializerin charge.protected javax.xml.bind.ValidationEventLocatorGets the current location.booleanGets the MIME type with which the binary content shall be printed.booleanbooleanhandleError(Exception e, Object source, String fieldName) booleanhandleEvent(javax.xml.bind.ValidationEvent event) voidleafElement(Name tagName, int data, String fieldName) voidleafElement(Name tagName, Pcdata data, String fieldName) voidleafElement(Name tagName, String data, String fieldName) final voidreportError(String fieldName, Throwable t) Report an error found as an exception.voidreportError(javax.xml.bind.ValidationEvent ve) javax.activation.MimeTypesetExpectedMimeType(javax.activation.MimeType expectedMimeType) This method is used byMimeTypedTransducerto set the expected MIME type for the encapsulatedTransducer.booleansetInlineBinaryFlag(boolean value) voidsetObjectIdentityCycleDetection(boolean val) voidsetPrefixMapper(NamespacePrefixMapper prefixMapper) setSchemaType(QName st) voidstartDocument(XmlOutput out, boolean fragment, String schemaLocation, String noNsSchemaLocation) Reset this object to write to the specified output.voidstartElement(Name tagName, Object outerPeer) voidstartElement(String nsUri, String localName, String preferredPrefix, Object outerPeer) voidstartElementForce(String nsUri, String localName, String forcedPrefix, Object outerPeer) Variation ofstartElement(String, String, String, Object)that forces a specific prefix.voidThetext(String, String)method that takesPcdata.voidMarshalls text.<E> voidwriteDom(E element, javax.xml.bind.annotation.DomHandler<E, ?> domHandler, Object parentBean, String fieldName) final voidShort for the following call sequence:Methods inherited from class com.sun.xml.bind.v2.runtime.Coordinator
_getInstance, containsAdapter, error, fatalError, getAdapter, popCoordinator, pushCoordinator, putAdapter, warning
-
Field Details
-
grammar
-
nameList
-
knownUri2prefixIndexMap
public final int[] knownUri2prefixIndexMap -
attachmentMarshaller
public javax.xml.bind.attachment.AttachmentMarshaller attachmentMarshaller
-
-
Method Details
-
getCachedBase64DataInstance
Deprecated.Base64Datais no longer cached, so that XMLStreamWriterEx impl can retain the data, like JAX-WS does.Gets the cached instance ofBase64Data. -
reportError
- Throws:
SAXException
-
reportError
Report an error found as an exception.- Parameters:
fieldName- the name of the property being processed when an error is found.- Throws:
SAXException
-
startElement
-
startElement
-
startElementForce
public void startElementForce(String nsUri, String localName, String forcedPrefix, Object outerPeer) Variation ofstartElement(String, String, String, Object)that forces a specific prefix. Needed to preserve the prefix when marshalling DOM. -
endNamespaceDecls
- Throws:
IOExceptionXMLStreamException
-
endAttributes
Switches to the "marshal child texts/elements" mode. This method has to be called after the 1st pass is completed. -
endElement
Ends marshalling of an element. Pops the internal stack. -
leafElement
public void leafElement(Name tagName, String data, String fieldName) throws SAXException, IOException, XMLStreamException -
leafElement
public void leafElement(Name tagName, Pcdata data, String fieldName) throws SAXException, IOException, XMLStreamException -
leafElement
public void leafElement(Name tagName, int data, String fieldName) throws SAXException, IOException, XMLStreamException -
text
public void text(String text, String fieldName) throws SAXException, IOException, XMLStreamException Marshalls text.This method can be called after the
endAttributes()method to marshal texts inside elements. If the method is called more than once, those texts are considered as separated by whitespaces. For example,c.startElement("","foo"); c.endAttributes(); c.text("abc"); c.text("def"); c.startElement("","bar"); c.endAttributes(); c.endElement(); c.text("ghi"); c.endElement();will generate<foo>abc def<bar/>ghi</foo>. -
text
public void text(Pcdata text, String fieldName) throws SAXException, IOException, XMLStreamException Thetext(String, String)method that takesPcdata. -
attribute
- Throws:
SAXException
-
attribute
- Throws:
IOExceptionXMLStreamException
-
getNamespaceContext
-
onID
-
onIDREF
- Throws:
SAXException
-
childAsRoot
public void childAsRoot(Object obj) throws javax.xml.bind.JAXBException, IOException, SAXException, XMLStreamException - Throws:
javax.xml.bind.JAXBExceptionIOExceptionSAXExceptionXMLStreamException
-
childAsSoleContent
public final void childAsSoleContent(Object child, String fieldName) throws SAXException, IOException, XMLStreamException The equivalent of:childAsURIs(child, fieldName); endNamespaceDecls(); childAsAttributes(child, fieldName); endAttributes(); childAsBody(child, fieldName);
This produces the given child object as the sole content of an element. Used to reduce the code size in the generated marshaller. -
childAsXsiType
public final void childAsXsiType(Object child, String fieldName, JaxBeanInfo expected, boolean nillable) throws SAXException, IOException, XMLStreamException This method is called when a type child object is found.This method produces events of the following form:
NSDECL* "endNamespaceDecls" ATTRIBUTE* "endAttributes" BODY
optionally including @xsi:type if necessary.- Parameters:
child- Object to be marshalled. TheJaxBeanInfofor this object must return a type name.expected- Expected type of the object.fieldName- property name of the parent objeect from which 'o' comes. Used as a part of the error message in case anything goes wrong with 'o'.- Throws:
SAXExceptionIOExceptionXMLStreamException
-
attWildcardAsURIs
-
attWildcardAsAttributes
public void attWildcardAsAttributes(Map<QName, String> attributes, String fieldName) throws SAXException- Throws:
SAXException
-
writeXsiNilTrue
Short for the following call sequence:getNamespaceContext().declareNamespace(WellKnownNamespace.XML_SCHEMA_INSTANCE,"xsi",true); endNamespaceDecls(); attribute(WellKnownNamespace.XML_SCHEMA_INSTANCE,"nil","true"); endAttributes(); -
writeDom
public <E> void writeDom(E element, javax.xml.bind.annotation.DomHandler<E, ?> domHandler, Object parentBean, String fieldName) throws SAXException- Throws:
SAXException
-
getIdentityTransformer
-
setPrefixMapper
-
startDocument
public void startDocument(XmlOutput out, boolean fragment, String schemaLocation, String noNsSchemaLocation) throws IOException, SAXException, XMLStreamException Reset this object to write to the specified output.- Parameters:
schemaLocation- if non-null, this value is printed on the root element as xsi:schemaLocationnoNsSchemaLocation- Similar to 'schemaLocation' but this one works for xsi:noNamespaceSchemaLocation- Throws:
IOExceptionSAXExceptionXMLStreamException
-
endDocument
-
close
public void close() -
addInscopeBinding
This method can be called afterstartDocument(com.sun.xml.bind.v2.runtime.output.XmlOutput, boolean, java.lang.String, java.lang.String)is called but before the marshalling begins, to set the currently in-scope namespace bindings.This method is useful to avoid redundant namespace declarations when the marshalling is producing a sub-document.
-
getXMIMEContentType
Gets the MIME type with which the binary content shall be printed.This method shall be used from those
RuntimeBuiltinLeafInfothat are bound to base64Binary. -
setExpectedMimeType
public javax.activation.MimeType setExpectedMimeType(javax.activation.MimeType expectedMimeType) This method is used byMimeTypedTransducerto set the expected MIME type for the encapsulatedTransducer. -
setInlineBinaryFlag
public boolean setInlineBinaryFlag(boolean value) -
getInlineBinaryFlag
public boolean getInlineBinaryFlag() -
setSchemaType
-
getSchemaType
-
setObjectIdentityCycleDetection
public void setObjectIdentityCycleDetection(boolean val) -
getObjectIdentityCycleDetection
public boolean getObjectIdentityCycleDetection() -
handleError
-
handleError
-
handleEvent
public boolean handleEvent(javax.xml.bind.ValidationEvent event) -
errorMissingId
Called when a referenced object doesn't have an ID.- Throws:
SAXException
-
getCurrentLocation
-
getLocation
protected javax.xml.bind.ValidationEventLocator getLocation()Description copied from class:CoordinatorGets the current location. Used for reporting the error source location.- Specified by:
getLocationin classCoordinator
-
getCurrentProperty
May return null when the property hasn't been set. Introduced based on Jersey requirements. -
clearCurrentProperty
public void clearCurrentProperty()Takes care of cleaning the currentProperty. Must be called from the same thread that created the XMLSerializer. -
getInstance
When called from within the realm of the marshaller, this method returns the currentXMLSerializerin charge.
-
Base64Datais no longer cached, so that XMLStreamWriterEx impl can retain the data, like JAX-WS does.