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