Question

I have a quite simple question :

Say I have a model class defined like this :

public class Test{

   private String testAttribute;

   public Test(){

   }

   public String getFormattedTestAttribute(){
      return testAttribute + "A nice formatted thingy"; //right, this is just an example
   }

   public void setTestAttribute(String value){
      testAttribute = value;
   }

}

You can see that I have a standard setter for testProperty but the getter has a different name : getFormattedTestProperty().

Is it possible into Jaxb/Moxy to specify which getter to use for a specific property ?

I'm using MOXy implementation with external metadata bindings file. The project which I'm working on used tu use Castor. Into Castor's mapping files, you could specify which getter/setter to use like that :

   <field name="testAttribute"
      get-method="getFormattedTestAttribute">
      <bind-xml name="test-attribute" node="attribute"/>
   </field>

Is the same kind of thing possible with moxy's external metadata ?

If that kind of customization isn't supported, is it possible to mark a field as read-only and another as write-only ? so I could declare a read-only property named "formattedTestAttribute" and a write-only property named "testAttribute" into the metadata bindings file ?

<!-- read only property -->
<xml-element java-attribute="formattedTestAttribute" xml-path="@test-attribute" />

<!-- write only property -->
<xml-element java-attribute="testAttribute" xml-path="@test-attribute" /> 

Please note that I have very limited control over the model classes.

Thanks in advance for your answers.

Was it helpful?

Solution

You could represent this in EclipseLink JAXB (MOXy)'s external mapping document as follows:

<?xml version="1.0"?>
<xml-bindings
    xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
    package-name="forum8834871">
    <java-types>
        <java-type name="Test" xml-accessor-type="PUBLIC_MEMBER">
            <xml-root-element/>
            <java-attributes>
                <xml-element 
                    java-attribute="testAttribute" 
                    name="test-attribute">
                    <xml-access-methods 
                        get-method="getFormattedTestAttribute" 
                        set-method="setTestAttribute"/>
                </xml-element>
                <xml-transient java-attribute="formattedTestAttribute"/>
            </java-attributes>
        </java-type>
    </java-types>
</xml-bindings>

Test

I have modified your Test class, to put some logic in the get/set methods.

package forum8834871;

public class Test{

    private String testAttribute;

    public Test(){
    }

    public String getFormattedTestAttribute(){
       return "APPENDED_ON_GET " + testAttribute;
    }

    public void setTestAttribute(String value){
       testAttribute = "APPENDED_ON_SET " + value;
    }

}

Demo

package forum8834871;

import java.io.File;
import java.util.*;
import javax.xml.bind.*;
import org.eclipse.persistence.jaxb.JAXBContextFactory;

public class Demo {

    public static void main(String[] args) throws Exception {
        Map<String, Object> properties = new HashMap<String, Object>(1);
        properties.put(JAXBContextFactory.ECLIPSELINK_OXM_XML_KEY, "forum8834871/oxm.xml");
        JAXBContext jc = JAXBContext.newInstance(new Class[] {Test.class}, properties);

        File xml = new File("src/forum8834871/input.xml");
        Unmarshaller unmarshaller = jc.createUnmarshaller();
        Test test = (Test) unmarshaller.unmarshal(xml);

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(test, System.out);
    }

}

input.xml

<?xml version="1.0" encoding="UTF-8"?>
<test>
    <test-attribute>ORIGINAL</test-attribute>
</test>

Output

<?xml version="1.0" encoding="UTF-8"?>
<test>
   <test-attribute>APPENDED_ON_GET APPENDED_ON_SET ORIGINAL</test-attribute>
</test>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top