Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » MOXy: Override rules of binding files
MOXy: Override rules of binding files [message #1076962] Thu, 01 August 2013 04:18 Go to next message
Tim Braun is currently offline Tim BraunFriend
Messages: 8
Registered: July 2013
Junior Member
I would, in the scenario below, like the binding of java-type name="SubClass" to be applied to set the text field on SuperClass. However it is not. Is there a problem with overriding the bindingsA.xml? According to the Overriding rules:
Quote:
If the same java-type occurs in multiple files, any values that are set in the later file, will override values from the previous file

What do I need to do to make it work?


Input:
<?xml version="1.0" encoding="UTF-8"?>
<a text="A text">B text</a>

Bindings A:
<?xml version="1.0"?>
<xml-bindings
 xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
 package-name="test">

 <java-types>        
   <java-type name="SuperClass">
    <xml-root-element name="a"/>
    <java-attributes>
        <xml-element java-attribute="text" xml-path="@text" />
    </java-attributes>
  </java-type>
 </java-types>

</xml-bindings>

Bindings B:
<?xmlversion="1.0"?>
<xml-bindings
 xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
 package-name="test">

<java-types>        
  <java-type name="SuperClass" xml-transient="true"></java-type>
  <java-type name="SubClass">
    <xml-root-element name="a"/>
    <java-attributes>
        <xml-element java-attribute="text" xml-path="text()" />
    </java-attributes>
  </java-type>
</java-types>

</xml-bindings>

Classes:
public class SuperClass {

 private String text;

 public String getText() {
    return text;
 }

 public void setText(String text) {
    this.text = text;
 }
}

public class SubClass extends SuperClass { }

Demo:
Map<String, Object> jaxbContextProperties = new HashMap<String, Object>(1);
List<String> bindings = new LinkedList<String>();
bindings.add("bindingsA.xml");
bindings.add("bindingsB.xml");
jaxbContextProperties.put(JAXBContextProperties.OXM_METADATA_SOURCE, bindings);
JAXBContext jaxbContext = JAXBContextFactory.createContext(new Class[] {SuperClass.class}, jaxbContextProperties);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
SuperClass superClass = (SuperClass)unmarshaller.unmarshal(new File("input.xml"));
System.out.println(superClass.getText()); 

Output:
[EL Warning]: 2013-07-31 16:08:07.771--Ignoring attribute [text] on class [SubClass] as no Property was generated for it.
A text
Re: MOXy: Override rules of binding files [message #1077369 is a reply to message #1076962] Thu, 01 August 2013 15:51 Go to previous message
Blaise Doughan is currently offline Blaise DoughanFriend
Messages: 163
Registered: July 2009
Senior Member

It's a bit odd to map the text property differently on the super and sub classes. If this is something you really want to do, then below is a way you could accomplish this.

Java Model

SuperClass
package forum17982654;

public class SuperClass {

    private String text;

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

}

SubClass

We will override the accessor methods from the super class. This will help us trick MOXy into thinking that SubClass has its own property text.
package forum17982654;

public class SubClass extends SuperClass {

    @Override
    public String getText() {
        return super.getText();
    }

    @Override
    public void setText(String text) {
        super.setText(text);
    } 

}

Metadata

bindings.xml

In the mapping document we will tell MOXy that the real super class of SubClass is java.lang.Object.
<?xml version="1.0"?>
<xml-bindings
    xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
    package-name="forum17982654">
    <java-types>
        <java-type name="SuperClass">
            <xml-root-element/>
            <java-attributes>
                <xml-attribute java-attribute="text"/>
            </java-attributes>
        </java-type>
        <java-type name="SubClass" super-type="java.lang.Object">
            <xml-root-element/>
            <java-attributes>
                <xml-value java-attribute="text"/>
            </java-attributes>
        </java-type>
    </java-types>
</xml-bindings>

Demo Code

Below is some demo code you can run to prove that everything works:

Demo
package forum17982654;

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

public class Demo {

    public static void main(String[] args) throws Exception {
        Map<String, Object> jaxbContextProperties = new HashMap<String, Object>(1);
        jaxbContextProperties.put(JAXBContextProperties.OXM_METADATA_SOURCE, "bindings.xml");
        JAXBContext jaxbContext = JAXBContextFactory.createContext(new Class[] {SuperClass.class}, jaxbContextProperties);
        Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();

        StringReader superClassXML = new StringReader("<superClass text='Hello Super Class'/>");
        SuperClass superClass = (SuperClass) unmarshaller.unmarshal(superClassXML);
        System.out.println(superClass.getText());

        StringReader subClassXML = new StringReader("<subClass>Hello Sub Class</subClass>");
        SubClass subClass = (SubClass) unmarshaller.unmarshal(subClassXML);
        System.out.println(subClass.getText());
    }

}

Output
Hello Super Class
Hello Sub Class

Previous Topic:EclipseLink MOXy: Logical operators in XmlPath annotation
Next Topic:MongoDB queries throwing NPE
Goto Forum:
  


Current Time: Tue Apr 16 20:29:49 GMT 2024

Powered by FUDForum. Page generated in 0.75058 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top