Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » XmlReadTransformer only for a read-only mapping
XmlReadTransformer only for a read-only mapping [message #955136] Tue, 23 October 2012 14:44 Go to next message
Michal Politowski is currently offline Michal PolitowskiFriend
Messages: 2
Registered: October 2012
Junior Member
I was under the impression that this should be possible.
XmlWriteTransformer javadoc seems to say that it is not needed for read-only mappings.
And there are even some tests for it in org/eclipse/persistence/testing/oxm/readonly/TransformationMappingTestCases.java
although these seem to be targeted at a different API level.

But with the following basic class:

package mpol.moxy;

import javax.xml.bind.annotation.XmlRootElement;

import org.eclipse.persistence.oxm.annotations.XmlReadTransformer;

@XmlRootElement
public class Transformed {

@XmlReadTransformer(transformerClass=StringTransformer.class)
public String propA;
}

and the following simple transformer:

package mpol.moxy;

import org.eclipse.persistence.mappings.foundation.AbstractTransformationMapping;
import org.eclipse.persistence.mappings.transformers.AttributeTransformer;
import org.eclipse.persistence.sessions.Record;
import org.eclipse.persistence.sessions.Session;

@SuppressWarnings("serial")
public class StringTransformer implements AttributeTransformer {

@Override
public void initialize(AbstractTransformationMapping mapping) {}

@Override
public Object buildAttributeValue(Record record, Object object, Session session) {
String value = (String) record.get("A_PROP");

return value!=null?value.toUpperCase():null;
}
}

the following code fails with NPE, because record is null in buildAttributeValue:

unmarshaller.unmarshal(new StringReader("<transformed><A_PROP>a</A_PROP></transformed>"));

Is it a MOXy bug, or do I need to do something more to make this work?

--
Michał Politowski
Re: XmlReadTransformer only for a read-only mapping [message #955443 is a reply to message #955136] Tue, 23 October 2012 20:08 Go to previous messageGo to next message
Blaise Doughan is currently offline Blaise DoughanFriend
Messages: 163
Registered: July 2009
Senior Member

Hi Michal,

There appears to be a bug here, would you mind entering one using the following URL:

- https://bugs.eclipse.org/bugs/enter_bug.cgi?product=EclipseLink

Below is an example of what you will need to do today to get your example to run:

Transformed

The way that MOXy currently works today, the XPath statements specified in the @XmlWriterTransformers are used to create a mini record that is passed to the AttributeTransformer. This is why currently your use case isn't working.

package mpol.moxy;

import javax.xml.bind.annotation.XmlRootElement;
import org.eclipse.persistence.oxm.annotations.XmlReadTransformer;
import org.eclipse.persistence.oxm.annotations.XmlWriteTransformer;

@XmlRootElement
public class Transformed {

    @XmlReadTransformer(transformerClass = StringTransformer.class)
    @XmlWriteTransformer(transformerClass = StringTransformer.class, xmlPath="A_PROP/text()")
    public String propA;

}


StringTransformer

In the current implementation of MOXy you will need an implementation of FieldTransformer. You can have a new class or extend your StringTransformer to implement the buildFieldValue method.

package mpol.moxy;

import org.eclipse.persistence.mappings.foundation.AbstractTransformationMapping;
import org.eclipse.persistence.mappings.transformers.AttributeTransformer;
import org.eclipse.persistence.mappings.transformers.FieldTransformer;
import org.eclipse.persistence.sessions.Record;
import org.eclipse.persistence.sessions.Session;

@SuppressWarnings("serial")
public class StringTransformer implements AttributeTransformer, FieldTransformer {

    public void initialize(AbstractTransformationMapping mapping) {
    }

    public Object buildAttributeValue(Record record, Object object, Session session) {
        String value = (String) record.get("A_PROP/text()");

        return value != null ? value.toUpperCase() : null;
    }

    public Object buildFieldValue(Object object, String name, Session session) {
        Transformed transformed = (Transformed) object;
        return transformed.propA;
    }

}


jaxb.properties

To use MOXy as your JAXB provider you need to include a file called jaxb.properties in the same directory as your domain classes with the following entry.

javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory


Demo

I have expanded your unmarshal code into a running example:

package mpol.moxy;

import java.io.StringReader;
import javax.xml.bind.*;

public class Demo {

    public static void main(String[] args) throws Exception  {
        JAXBContext jc = JAXBContext.newInstance(Transformed.class);
        
        Unmarshaller unmarshaller = jc.createUnmarshaller();
        Transformed transformed = (Transformed) unmarshaller.unmarshal(new StringReader("<transformed><A_PROP>a</A_PROP></transformed>"));

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

}


Output

Below is the output from running the demo code:

A
<?xml version="1.0" encoding="UTF-8"?>
<transformed>
   <A_PROP>A</A_PROP>
</transformed>


-Blaise
Re: XmlReadTransformer only for a read-only mapping [message #956115 is a reply to message #955443] Wed, 24 October 2012 08:47 Go to previous message
Michal Politowski is currently offline Michal PolitowskiFriend
Messages: 2
Registered: October 2012
Junior Member
Hi Blaise,

Thank you for confirming that this is a bug.
I entered https://bugs.eclipse.org/bugs/show_bug.cgi?id=392721

--
Michał Politowski
Previous Topic:JPA mapping problem. Multiple values
Next Topic:PersistenceException upon flush() - constraint violation
Goto Forum:
  


Current Time: Sat Dec 20 17:57:45 GMT 2014

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

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