org.apache.axiom.om.ds.jaxb

## Class JAXBOMDataSource

- java.lang.Object
- - org.apache.axiom.om.ds.AbstractOMDataSource
  - - org.apache.axiom.om.ds.AbstractPushOMDataSource
    - - org.apache.axiom.om.ds.jaxb.JAXBOMDataSource

- All Implemented Interfaces:
    OMDataSource, OMDataSourceExt, QNameAwareOMDataSource

      public class JAXBOMDataSource
      extends AbstractPushOMDataSource
      implements QNameAwareOMDataSource

  `OMDataSource` backed by a JAXB object. This class can be used both for plain JAXB objects and for `JAXBElement` instances. It implements `QNameAwareOMDataSource` so that it can be used with `OMFactory.createOMElement(OMDataSource)`, i.e. it is not necessary to supply the QName during construction of the `OMSourcedElement`. It also has full support for XOP/MTOM. It is implemented as a push-style `OMDataSource` so that an `OMSourcedElement` backed by an instance of this class can be expanded in an efficient way (including the case where the JAXB object contains base64 binary data represented as `DataHandler` instances or byte arrays).

  The JAXB object encapsulated by an instance of this class can be retrieved using `OMDataSourceExt.getObject()`. Note that modifying the JAXB object after passing it to the constructor may result in unexpected behavior and should be avoided.

  Instances of this class are non destructive, in the sense defined by `OMDataSourceExt.isDestructiveWrite()`.

- - ### Field Summary

    - ### Fields inherited from interface org.apache.axiom.om.OMDataSourceExt

      `LOSSY_PREFIX`

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `JAXBOMDataSource(javax.xml.bind.JAXBContext context, Object object)` Constructor.

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `OMDataSourceExt` | `copy()` Create a copy of the data source.
    `String` | `getLocalName()` Get the local name of the element represented by this data source.
    `String` | `getNamespaceURI()` Get the namespace URI of the element represented by this data source.
    `Object` | `getObject()` Get the object that backs this data source.
    `String` | `getPrefix()` Get the namespace prefix of the element represented by this data source.
    `boolean` | `isDestructiveWrite()` Returns true if writing the backing object is destructive.
    `void` | `serialize(javax.xml.stream.XMLStreamWriter writer)` Serializes element data directly to StAX writer.

    - ### Methods inherited from class org.apache.axiom.om.ds.AbstractPushOMDataSource

      `getReader, isDestructiveRead`

    - ### Methods inherited from class org.apache.axiom.om.ds.AbstractOMDataSource

      `close, getProperty, getXMLBytes, getXMLInputStream, hasProperty, serialize, serialize, setProperty`

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

    - ### Methods inherited from interface org.apache.axiom.om.OMDataSource

      `getReader, serialize, serialize`

- - ### Constructor Detail

    - #### JAXBOMDataSource

          public JAXBOMDataSource(javax.xml.bind.JAXBContext context,
                                  Object object)

      Constructor.
      Parameters:
        `context` - the JAXB context to which the object is known
        `object` - the JAXB object; this may be a plain Java bean or a `JAXBElement`

  - ### Method Detail

    - #### isDestructiveWrite

          public boolean isDestructiveWrite()

      Description copied from interface: `OMDataSourceExt`
      Returns true if writing the backing object is destructive. An example of an object with a destructive write is an InputStream. The owning OMSourcedElement uses this information to detemine if OM tree expansion is needed when writing the OMDataSourceExt.
      Specified by:
        `isDestructiveWrite` in interface `OMDataSourceExt`
      Returns:
        boolean

    - #### serialize

          public void serialize(javax.xml.stream.XMLStreamWriter writer)
                         throws javax.xml.stream.XMLStreamException

      Description copied from interface: `OMDataSource`
      Serializes element data directly to StAX writer.

      The implementation of this method must satisfy the following requirements:

      - The implementation MUST NOT not write any start document or end document event, i.e. it MUST NOT use `XMLStreamWriter.writeStartDocument()`, `XMLStreamWriter.writeStartDocument(String)`, `XMLStreamWriter.writeStartDocument(String, String)` or `XMLStreamWriter.writeEndElement()`.
      - The implementation MUST output a single element (hereafter called the root element). It MUST NOT output any other content before or after that element.
      - The implementation MUST specify the namespace URI when writing an element, i.e. it MUST NOT use the namespace unaware methods `XMLStreamWriter.writeStartElement(String)` or `XMLStreamWriter.writeEmptyElement(String)`. On the other hand, it MAY use the namespace unaware `XMLStreamWriter.writeAttribute(String, String)` method, provided that the specified name is an `NCName`.
      - The implementation MUST ensure that the produced XML is well formed with respect to namespaces, i.e. it MUST generate the required namespace declarations using `XMLStreamWriter.writeNamespace(String, String)` and `XMLStreamWriter.writeDefaultNamespace(String)`. It MUST NOT assume that the `XMLStreamWriter` performs any kind of namespace repairing (although that may be the case).
      - In addition the implementation MAY use `XMLStreamWriter.setPrefix(String, String)` and `XMLStreamWriter.setDefaultNamespace(String)` to track the namespace declarations it generates, so that the namespace context maintained by the `XMLStreamWriter` accurately reflects the namespace context in the output document. However, it MUST NOT call these methods before the start of the root element or after the end of the root element.
      - Since the element may be serialized as part of a larger document, the implementation MUST take into account the pre-existing namespace context (which can be queried using `XMLStreamWriter.getPrefix(String)` and `XMLStreamWriter.getNamespaceContext()`). This means that the implementation MUST NOT assume that the empty prefix is bound to the empty namespace URI. Therefore if the implementation outputs elements that have no namespace, it MUST generate namespace declarations of the form `xmlns=""` in the appropriate locations. In addition it MAY use the namespace context information to minimize the number of generated namespace declarations (by reusing already bound prefixes).
      - If the implementation produces base64 binary data (that could be optimized using XOP/MTOM), then it SHOULD use `XMLStreamWriterUtils.writeDataHandler(XMLStreamWriter, DataHandler, String, boolean)` or `XMLStreamWriterUtils.writeDataHandler(XMLStreamWriter, DataHandlerProvider, String, boolean)` to write the data to the stream. If this is not possible (e.g. because the content is produced by a third party library that is not aware of these APIs), then the implementation MUST use the following approach:
        - If the `XMLStreamWriter` is an `MTOMXMLStreamWriter`, then the implementation MAY use `MTOMXMLStreamWriter.prepareDataHandler(DataHandler)` and generate the necessary `xop:Include` elements itself. In this case, the implementation MAY use `MTOMXMLStreamWriter.isOptimized()` to check if XOP/MTOM is enabled at all. Alternatively, instead of handling `MTOMXMLStreamWriter` in a special way, the implementation MAY use the approach described in the next item. This works because `MTOMXMLStreamWriter` exposes the `DataHandlerWriter` extension. However, this causes a slight overhead because the stream is first XOP decoded and then reencoded again.
        - If the `XMLStreamWriter` exposes the `DataHandlerWriter` extension, but is not an `MTOMXMLStreamWriter` (or is an `MTOMXMLStreamWriter`, but the implementation doesn't implement the approach described in the previous item), then the implementation MUST wrap the `XMLStreamWriter` in an `XOPDecodingStreamWriter` and write `xop:Include` elements to that wrapper, so that they can be translated into appropriate calls to the `DataHandlerWriter`. This requirement is important for two reasons:
          - It allows Axiom to respect the contract of the `OMSerializable.serialize(XMLStreamWriter, boolean)` method.
          - If the `OMDataSource` is push-only (see `AbstractPushOMDataSource`), then it enables `OMSourcedElement` to create `OMText` nodes for the binary content in an efficient way.
        - In all other cases, the implementation MUST use `XMLStreamWriter.writeCharacters(String)` or `XMLStreamWriter.writeCharacters(char[], int, int)` to write the base64 encoded data to the stream.
      - The implementation MUST NOT call `XMLStreamWriter.close()`.

      On the other hand, the caller of this method (typically an `OMSourcedElement` instance) must ensure that the following requirements are satisfied:

      - The namespace context information provided by `XMLStreamWriter.getPrefix(String)` and `XMLStreamWriter.getNamespaceContext()` MUST accurately reflect the actual namespace context at the location in the output document where the root element is serialized. Note that this requirement may be relaxed if the caller implements some form of namespace repairing.

      It is assumed that this method consumes the content (i.e. destroys the backing object) unless the data source also implements `OMDataSourceExt` and `OMDataSourceExt.isDestructiveWrite()` returns `false`.

      Specified by:
        `serialize` in interface `OMDataSource`
      Parameters:
        `writer` - destination writer
      Throws:
        `javax.xml.stream.XMLStreamException`

    - #### getLocalName

          public String getLocalName()

      Description copied from interface: `QNameAwareOMDataSource`
      Get the local name of the element represented by this data source.
      Specified by:
        `getLocalName` in interface `QNameAwareOMDataSource`
      Returns:
        the local name of the element or `null` if the local name is not known

    - #### getNamespaceURI

          public String getNamespaceURI()

      Description copied from interface: `QNameAwareOMDataSource`
      Get the namespace URI of the element represented by this data source.
      Specified by:
        `getNamespaceURI` in interface `QNameAwareOMDataSource`
      Returns:
        the namespace URI of the element, the empty string if the element has no namespace or `null` if the namespace URI is not known

    - #### getPrefix

          public String getPrefix()

      Description copied from interface: `QNameAwareOMDataSource`
      Get the namespace prefix of the element represented by this data source.
      Specified by:
        `getPrefix` in interface `QNameAwareOMDataSource`
      Returns:
        the prefix of the element, the empty string if the element has no prefix or `null` if the prefix is not known; the implementation is expected to return an empty string if `QNameAwareOMDataSource.getNamespaceURI()` returns an empty string (because an element without namespace must not have a prefix)

    - #### getObject

          public Object getObject()

      Description copied from interface: `OMDataSourceExt`
      Get the object that backs this data source. Application code should in general not call this method directly, but use `OMSourcedElement.getObject(Class)` instead.

      Data sources that support non destructive read/write should return the object from which the XML is produced. Data sources with destructive read/write should return a non null value only if the backing object has not been consumed yet (even partially).

      Specified by:
        `getObject` in interface `OMDataSourceExt`
      Overrides:
        `getObject` in class `AbstractOMDataSource`
      Returns:
        the backing object, or `null` if the data source has no backing object or if the backing object can't be accessed in a safe way

    - #### copy

          public OMDataSourceExt copy()

      Description copied from interface: `OMDataSourceExt`
      Create a copy of the data source. This method is used by `OMInformationItem.clone(OMCloneOptions)` when the `OMCloneOptions.isCopyOMDataSources()` option is enabled. If the data source is immutable and stateless, then it may return a reference to itself instead of creating a new data source instance.
      Specified by:
        `copy` in interface `OMDataSourceExt`
      Overrides:
        `copy` in class `AbstractOMDataSource`
      Returns:
        the copy of the data source, or `null` if the data source can't be copied (e.g. because it is destructive)

Copyright © The Apache Software Foundation. All Rights Reserved.