views:

295

answers:

1

Assuming I have a schema that describes a root element class Root that contains a List<Entry> where the Entry class has a required field name.

Here is how it looks in code:

@XmlRootElement 
class Root{
  @XmlElement(name="entry")
  public List<Entry> entries = Lists.newArrayList();
}

@XmlRootElement 
class Entry{
  @XmlElement(name="name",required=true)
  public String name;
}

If I supply the following XML for unmarshalling:

<root>
  <entry>
    <name>ekeren</name>
  </entry>
  <entry>
  </entry>
</root>

I have a problem because the second entry does not contain a name. So unmarshall produces null.

Is there a way to customize JAXB to unmarshall a Root object that will only contain the "good" entry?

+3  A: 

You could add the magic afterUnmarshal method to take care of empty entries:

@XmlRootElement 
class Root{
  @XmlElement(name="entry")
  public List<Entry> entries = Lists.newArrayList();

  void afterUnmarshal(final Unmarshaller unmarshaller, final Object parent) {
    Iterator<Entry> iter = entries.iterator();
    while (iter.hasNext()) {
      if (iter.next().name == null) iter.remove();
    }
  }
}

EDIT:

Not sure if this is better suited for you, but maybe it's of help. You could also use a Pacher, e.g. if not all objects you need to fix/validate your result are available in afterUnmarshal(..)

Runs by UnmarshallingContext after all the parsing is done. Primarily used to resolve forward IDREFs, but it can run any action. (javadoc)

Here's an example:

@XmlRootElement 
class Entry{
  @XmlElement(name="name",required=true)
  public String name;

  private boolean isValidEntry() {
    return name != null;
  }

  void afterUnmarshal(final Unmarshaller unmarshaller, final Object parent) {
    if (!isValidEntry()) {
      // entry not yet added to parent - use a patcher
      UnmarshallingContext.getInstance().addPatcher(new Patcher() {
        public void run() throws SAXException {
          ((Root)parent).removeEntry(this);
        }
      });
    }
  }
}

I wouldn't abuse it too much though, not only as it is Sun-only API.

But if you're really looking into something configurable that isn't part of the code of the marshalled objects itself. It might be best to look at something after unmarshalling. I wonder if Bean Validation (JSR 303) wouldn't be a perfect fit for you, e.g. using Hibernate Validator (don't get intimidated by the name, you don't need Hibernate ORM to use it). I haven't used it myself, but using a (new) standard for validation sounds reasonable, doesn't it?

sfussenegger
it is a good idea, Thanks. Of-course I will need to set the eventHandler to be tolerant for the "field is required" error but it should not be a problem. I wonder if there is a different solution, I am trying to make the user of my system (I am writing a lib for my group that deal with configuration) to be able to easily set this feature. I can do it with annotation and some class introspection to see all fields that need to be tested for the name required issue... inside afterUnmarshal(...) . I'll keep this unanswered maybe there is someone with an a more suiting answer for what I need.
ekeren
@ekeren see my edit, hopefully that helps. cheers
sfussenegger