Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Difference between revisions of "EclipseLink/UserGuide/MOXy/Runtime/Bootstrapping/Single Project/Dynamic/From OXM"

(New page: == Bootstrapping from EclipseLink OXM Metadata == If you would like to have more control over how your <tt>DynamicEntities</tt> will be mapped to XML, you can instead bootstrap from an Ec...)
 
(Removing all content from page)
 
Line 1: Line 1:
== Bootstrapping from EclipseLink OXM Metadata ==
 
  
If you would like to have more control over how your <tt>DynamicEntities</tt> will be mapped to XML, you can instead bootstrap from an EclipseLink OXM Metadata file.  Using this approach, you can take advantage of EclipseLink's robust mappings framework and customize how each complex type in XML maps to its Java counterpart.  The following API on <tt>DynamicJAXBContextFactory</tt> can be used to bootstrap from an OXM file:
 
 
<div style="width:80%"><source lang="java">
 
 
/**
 
* Create a <tt>DynamicJAXBContext</tt>, using an EclipseLink OXM file as the metadata source.
 
*
 
* @param classLoader
 
*      The application's current class loader, which will be used to first lookup
 
*      classes to see if they exist before new <tt>DynamicTypes</tt> are generated.  Can be
 
*      <tt>null</tt>, in which case <tt>Thread.currentThread().getContextClassLoader()</tt> will be used.
 
* @param properties
 
*      Map of properties to use when creating a new <tt>DynamicJAXBContext</tt>.  This map must
 
*      contain a key of JAXBContext.ECLIPSELINK_OXM_XML_KEY, with a value of...
 
*
 
* @return
 
*      A new instance of <tt>DynamicJAXBContext</tt>.
 
*
 
* @throws JAXBException
 
*      if an error was encountered while creating the <tt>DynamicJAXBContext</tt>.
 
*/
 
public static DynamicJAXBContext createContextFromOXM(ClassLoader classLoader, Map<String, ?> properties) throws JAXBException {
 
 
</source></div>
 
 
Links to the actual OXM files are passed in via the <tt>properties</tt> parameter, using a special key, <tt>JAXBContextFactory.ECLIPSELINK_OXM_XML_KEY</tt>.  The value of this key will be a handle to the OXM metadata file, in the form of one of the following:
 
 
<tt>
 
* java.io.File
 
* java.io.InputStream
 
* java.io.Reader
 
* java.net.URL
 
* javax.xml.stream.XMLEventReader
 
* javax.xml.stream.XMLStreamReader
 
* javax.xml.transform.Source
 
* org.w3c.dom.Node
 
* org.xml.sax.InputSource
 
</tt>
 
 
<tt>Lists</tt> of the above inputs are acceptable as well, to bootstrap from multiple OXM files.  For more information, see the documentation on the <tt>DynamicJAXBContextFactory</tt> class.
 
 
In the following example, we will obtain our OXM file as a resource from our <tt>ClassLoader</tt>, and use the resulting <tt>InputStream</tt> to bootstrap a <tt>DynamicJAXBContext</tt>:
 
 
<div style="width:80%"><source lang="java">
 
InputStream iStream = myClassLoader.getResourceAsStream("mynamespace/resources/eclipselink/eclipselink-oxm.xml");
 
 
Map<String, Object> properties = new HashMap<String, Object>();
 
properties.put(JAXBContextFactory.ECLIPSELINK_OXM_XML_KEY, iStream);
 
 
DynamicJAXBContext jaxbContext = DynamicJAXBContextFactory.createContextFromOXM(myClassLoader, properties);
 
</source></div>
 
 
<br><br>
 
 
=== Example ===
 
 
Using the following example OXM, we will show an example of how to create and marshall a new object using Dynamic MOXy:
 
 
<div style="width:80%"><source lang="xml">
 
 
<?xml version="1.0" encoding="US-ASCII"?>
 
<xml-bindings xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 
    xmlns:xs="http://www.w3.org/2001/XMLSchema" package-name="mynamespace">
 
 
    <java-types>
 
        <java-type name="Customer">
 
            <xml-root-element name="customer"/>
 
            <java-attributes>
 
                <xml-element java-attribute="firstName" type="java.lang.String"/>
 
                <xml-element java-attribute="lastName" type="java.lang.String"/>
 
                <xml-element java-attribute="address" type="mynamespace.Address"/>
 
            </java-attributes>
 
        </java-type>
 
       
 
        <java-type name="Address">
 
            <java-attributes>
 
                <xml-element java-attribute="street" type="java.lang.String"/>
 
                <xml-element java-attribute="city" type="java.lang.String"/>
 
                <xml-element java-attribute="province" type="java.lang.String"/>
 
                <xml-element java-attribute="postalCode" type="java.lang.String"/>
 
            </java-attributes>
 
        </java-type>
 
    </java-types>
 
 
</xml-bindings>
 
 
</source></div>
 
 
The following code demonstrates:
 
* Passing the OXM file to <tt>DynamicJAXBContextFactory</tt> to create a <tt>DynamicJAXBContext</tt>
 
* Creating new <tt>DynamicEntities</tt> and setting their properties
 
* Creating a <tt>JAXBMarshaller</tt> and marshalling the Java objects to XML
 
 
<div style="width:80%"><source lang="java">
 
 
InputStream iStream = myClassLoader.getResourceAsStream("mynamespace/resources/eclipselink/eclipselink-oxm.xml");
 
 
Map<String, Object> properties = new HashMap<String, Object>();
 
properties.put(JAXBContextFactory.ECLIPSELINK_OXM_XML_KEY, iStream);
 
 
DynamicJAXBContext jaxbContext = DynamicJAXBContextFactory.createContextFromOXM(myClassLoader, properties);
 
 
DynamicEntity newCustomer = dContext.newDynamicEntity("mynamespace.Customer");
 
newCustomer.set("firstName", "George");
 
newCustomer.set("lastName", "Jones");
 
 
DynamicEntity newAddress = dContext.newDynamicEntity("mynamespace.Address");
 
newAddress.set("street", "227 Main St.");
 
newAddress.set("city", "Toronto");
 
newAddress.set("province", "Ontario");
 
newAddress.set("postalCode", "M5V1E6");
 
 
newCustomer.set("address", newAddress);
 
 
dContext.createMarshaller().marshal(newCustomer, System.out);
 
 
</source></div>
 

Latest revision as of 11:44, 29 November 2010

Back to the top