开发者

Customizing error handling of JAXB unmarshall process

开发者 https://www.devze.com 2022-12-29 17:11 出处:网络
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.

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 开发者_JS百科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?


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?

0

精彩评论

暂无评论...
验证码 换一张
取 消