Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » MOXy - mapping enums
MOXy - mapping enums [message #486396] Thu, 17 September 2009 13:54 Go to next message
Matti Hansson is currently offline Matti Hansson
Messages: 68
Registered: July 2009
Member
Hi!
I'm trying to map an enumeration with MOXy using a transformation mapping but I can't get it to work. Here's what I got:

Java:
public enum Detail { GENERAL, DETAILED }

public class MyClass {
    Detail detail;
    /* getters & setters */
}

XML:
<app:MyClass detail="GENERAL" xmlns:app="http://example.com"/>

Configuration:
private XMLDescriptor createDescriptor() {
    XMLDescriptor descriptor = super.buildDescriptor(MyClass.class);
    descriptor.setDefaultRootElement("app:MyClass");
    descriptor.add(createDetailMapping());
    return descriptor;
}

private XMLTransformationMapping createDetailMapping() {
    XMLTransformationMapping mapping = new XMLTransformationMapping();
    mapping.setAttributeName("detail");
    EnumTransformer transformer = new EnumTransformer();
    mapping.setAttributeTransformer(transformer);
    mapping.addFieldTransformer("@detail", transformer);
    return mapping;
}

Transformer:
public class EnumTransformer implements AttributeTransformer, FieldTransformer {
  private AbstractTransformationMapping mapping;
  private XMLField detailField;

  public void initialize(AbstractTransformationMapping mapping) {
    this.mapping = mapping;
    NamespaceResolver namespaceResolver = ((XMLDescriptor)mapping.getDescriptor()).getNamespaceResolver();
    detailField = new XMLField("@detail");
    detailField.setNamespaceResolver(namespaceResolver);
  }

  public Object buildAttributeValue(Record record, Object object, Session session) {
    String detail = (String) record.get(detailField);
    return Detail.valueOf(detail);
  }
  
  public Object buildFieldValue(Object instance, String fieldName, Session session) {
    return mapping.getAttributeValueFromObject(instance);
  }
}


When I try to run it I get this stack trace:
java.lang.NullPointerException
	at org.eclipse.persistence.oxm.record.UnmarshalRecord.startElement(UnmarshalRecord.java:551)
	at org.eclipse.persistence.internal.oxm.record.SAXUnmarshallerHandler.startElement(SAXUnmarshallerHandler.java:273)
	at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.startElement(AbstractSAXParser.java:501)
	at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement(XMLNSDocumentScannerImpl.java:400)
	at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl$NSContentDriver.scanRootElementHook(XMLNSDocumentScannerImpl.java:626)
	at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:3095)
	at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(XMLDocumentScannerImpl.java:921)
	at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:648)
	at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:140)
	at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:510)
	at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:807)
	at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:737)
	at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:107)
	at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1205)
	at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:522)
	at org.eclipse.persistence.internal.oxm.record.XMLReader.parse(XMLReader.java:99)
	at org.eclipse.persistence.internal.oxm.record.SAXUnmarshaller.unmarshal(SAXUnmarshaller.java:215)
	at org.eclipse.persistence.internal.oxm.record.SAXUnmarshaller.unmarshal(SAXUnmarshaller.java:192)
	at org.eclipse.persistence.oxm.XMLUnmarshaller.unmarshal(XMLUnmarshaller.java:274)
	...


Anyone knows what's going on?
Thanks!
Re: MOXy - mapping enums [message #486577 is a reply to message #486396] Fri, 18 September 2009 09:47 Go to previous message
Matti Hansson is currently offline Matti Hansson
Messages: 68
Registered: July 2009
Member
Sorry. I have a much trickier transformation mapping which uses a similar
structure that I'm getting the same error with, and I was hoping that
getting help with this simpler mapping would lead me in the right
direction on the other issue. However, it turns out that the error came
from the tricky mapping all along and had nothing to do with the enum
mapping.

I'll probably be back with questions about the other mapping soon. :P
/Matti
Previous Topic:ddl-generation
Next Topic:Inheritance problem with Id in subclass
Goto Forum:
  


Current Time: Sat Oct 25 12:59:14 GMT 2014

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

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