Jump to: navigation, search

Difference between revisions of "EclipseLink/Examples/MOXy/GettingStarted/MOXyExtensions"

(New page: ==Overview== This example will build upon the lessons learned in the previous example (JAXBCustomizations), and demonstrate ...)
 
Line 1: Line 1:
 
==Overview==
 
==Overview==
This example will build upon the lessons learned in the previous example ([[EclipseLink/Examples/MOXy/GettingStarted/JAXBCustomizations|JAXBCustomizations]]), and demonstrate how MOXy extensions can be used to further customize the XML output.
+
 
 +
This example will build upon the lessons learned in the previous example ([[EclipseLink/Examples/MOXy/GettingStarted/JAXBCustomizations|JAXB Customizations]]), and demonstrate how MOXy extensions can be used to further customize the XML output.
 +
 
 +
==Specifying MOXy Extensions==
 +
 
 +
<source lang="java">
 +
package example.gettingstarted;
 +
 
 +
import java.util.ArrayList;
 +
import java.util.List;
 +
 
 +
import javax.xml.bind.annotation.XmlRootElement;
 +
import javax.xml.bind.annotation.XmlType;
 +
 
 +
import org.eclipse.persistence.oxm.annotations.XmlCustomizer;
 +
 
 +
@XmlRootElement
 +
@XmlType(propOrder={"name", "address", "phoneNumbers"})
 +
@XmlCustomizer(CustomerCustomizer.class)
 +
public class Customer {
 +
 
 +
    private String name;
 +
    private Address address;
 +
    private List<PhoneNumber> phoneNumbers;
 +
 
 +
    public Customer() {
 +
        phoneNumbers = new ArrayList<PhoneNumber>();
 +
    }
 +
 
 +
    public String getName() {
 +
        return name;
 +
    }
 +
 
 +
    public void setName(String name) {
 +
        this.name = name;
 +
    }
 +
 
 +
    public Address getAddress() {
 +
        return address;
 +
    }
 +
 
 +
    public void setAddress(Address address) {
 +
        this.address = address;
 +
    }
 +
 
 +
    public List<PhoneNumber> getPhoneNumbers() {
 +
        return phoneNumbers;
 +
    }
 +
 
 +
    public void setPhoneNumbers(List<PhoneNumber> phoneNumbers) {
 +
        this.phoneNumbers = phoneNumbers;
 +
    }
 +
 
 +
}
 +
</source>
 +
 
 +
==Using the MOXy Extensions==
 +
 
 +
<source lang="java">
 +
package example.gettingstarted;
 +
 
 +
import org.eclipse.persistence.config.DescriptorCustomizer;
 +
import org.eclipse.persistence.descriptors.ClassDescriptor;
 +
import org.eclipse.persistence.oxm.mappings.XMLCompositeCollectionMapping;
 +
import org.eclipse.persistence.oxm.mappings.XMLCompositeObjectMapping;
 +
import org.eclipse.persistence.oxm.mappings.XMLDirectMapping;
 +
 
 +
public class CustomerCustomizer implements DescriptorCustomizer {
 +
 
 +
    public void customize(ClassDescriptor descriptor) throws Exception {
 +
        XMLDirectMapping nameMapping = (XMLDirectMapping) descriptor.getMappingForAttributeName("name");
 +
        nameMapping.setXPath("personal-info/name/text()");
 +
 
 +
        XMLCompositeObjectMapping addressMapping = (XMLCompositeObjectMapping) descriptor.getMappingForAttributeName("address");
 +
        addressMapping.setXPath("contact-info/address");
 +
 
 +
        XMLCompositeCollectionMapping phoneNumbersMapping = (XMLCompositeCollectionMapping) descriptor.getMappingForAttributeName("phoneNumbers");
 +
        phoneNumbersMapping.setXPath("contact-info/phone-number");
 +
    }
 +
 
 +
}
 +
</source>
 +
 
 +
==XML Output==
 +
 
 +
The following is the resulting XML. 
 +
 
 +
<source lang="xml">
 +
<?xml version="1.0" encoding="UTF-8"?>
 +
<customer>
 +
  <personal-info>
 +
      <name>Jane Doe</name>
 +
  </personal-info>
 +
  <contact-info>
 +
      <address>
 +
        <city>My Town</city>
 +
        <street>123 Any Street</street>
 +
      </address>
 +
      <phone-number type="work">613-555-1111</phone-number>
 +
      <phone-number type="cell">613-555-2222</phone-number>
 +
  </contact-info>
 +
</customer>
 +
</source>

Revision as of 15:02, 8 January 2010

Overview

This example will build upon the lessons learned in the previous example (JAXB Customizations), and demonstrate how MOXy extensions can be used to further customize the XML output.

Specifying MOXy Extensions

package example.gettingstarted;
 
import java.util.ArrayList;
import java.util.List;
 
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
 
import org.eclipse.persistence.oxm.annotations.XmlCustomizer;
 
@XmlRootElement
@XmlType(propOrder={"name", "address", "phoneNumbers"})
@XmlCustomizer(CustomerCustomizer.class)
public class Customer {
 
    private String name;
    private Address address;
    private List<PhoneNumber> phoneNumbers;
 
    public Customer() {
        phoneNumbers = new ArrayList<PhoneNumber>();
    }
 
    public String getName() {
        return name;
    }
 
    public void setName(String name) {
        this.name = name;
    }
 
    public Address getAddress() {
        return address;
    }
 
    public void setAddress(Address address) {
        this.address = address;
    }
 
    public List<PhoneNumber> getPhoneNumbers() {
        return phoneNumbers;
    }
 
    public void setPhoneNumbers(List<PhoneNumber> phoneNumbers) {
        this.phoneNumbers = phoneNumbers;
    }
 
}

Using the MOXy Extensions

package example.gettingstarted;
 
import org.eclipse.persistence.config.DescriptorCustomizer;
import org.eclipse.persistence.descriptors.ClassDescriptor;
import org.eclipse.persistence.oxm.mappings.XMLCompositeCollectionMapping;
import org.eclipse.persistence.oxm.mappings.XMLCompositeObjectMapping;
import org.eclipse.persistence.oxm.mappings.XMLDirectMapping;
 
public class CustomerCustomizer implements DescriptorCustomizer {
 
    public void customize(ClassDescriptor descriptor) throws Exception {
        XMLDirectMapping nameMapping = (XMLDirectMapping) descriptor.getMappingForAttributeName("name");
        nameMapping.setXPath("personal-info/name/text()");
 
        XMLCompositeObjectMapping addressMapping = (XMLCompositeObjectMapping) descriptor.getMappingForAttributeName("address");
        addressMapping.setXPath("contact-info/address");
 
        XMLCompositeCollectionMapping phoneNumbersMapping = (XMLCompositeCollectionMapping) descriptor.getMappingForAttributeName("phoneNumbers");
        phoneNumbersMapping.setXPath("contact-info/phone-number");
    }
 
}

XML Output

The following is the resulting XML.

<?xml version="1.0" encoding="UTF-8"?>
<customer>
   <personal-info>
      <name>Jane Doe</name>
   </personal-info>
   <contact-info>
      <address>
         <city>My Town</city>
         <street>123 Any Street</street>
      </address>
      <phone-number type="work">613-555-1111</phone-number>
      <phone-number type="cell">613-555-2222</phone-number>
   </contact-info>
</customer>