简体   繁体   中英

JAXB getter with validation possible?

My code looks like this:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Elements", namespace = MyNamespaceContext.NS_SOME, 
propOrder = { "element" })
public class Elements implements Serializable {

private static final long serialVersionUID = 1L;

@XmlElement(required = true)
protected List<Element> element;

public List<element> getElement() {
    if (element == null) {
        element = new ArrayList<Element>();
    }
    //Added by me afterwards
    element = ElementHelper.normalizeElement(element);
    return this.element;
}

}

The XML for that looks like that:

<elements>
    <element>
        <one>something</one>
        <two>something</two>
    </element>
    <element>
        <one>another </one>
        <two>another</two>
    </element>
</elements>

What I would like to do is to add a cleaner to my getter method to remove whitespaces and stuff. So I thought about adding my ElementHelper to the getter (see code snippet). But it does not work (still whitespaces in my output). If I call Elements.getElement() somewhere in my code and use the returned list with the ElementHelper it does work. So it seems to me, that the getter method is not called as it should.

Would it help to move the XmlElement to the getter method? Or is there some other simple trick to do this kind of stuff?

I would like to do this at this part of the code (centralized) because I don't want to call the ElementHelper every time it is needed in all kinds of places in the code and forget it at some other places (it's legacy code). Or do one of you have a suggestion where to do this instead? Because I don't like doing this in a getter.

I just started learning about JAXB and a lot is still not clear to me. If you could answer in more detail about how JAXB works in that matter or point to a good and understandable source, I would appreciate it.

EDIT:

Here the code for the ElementHelper:

public static List<Element> normalizeElement(List<Element> elements){
    for(int i = 0; i < elements.size(); i++){
        normalizeValues(elements.get(i));
    }
    return elements;
}

private static void normalizeValues(Element element){
    String one;
    String two;
    if( (one = element.getOne()) != null){
        element.setOne(one.trim());
        element.setOne(one.replaceAll("\\s+", ""));
    }

    if( (two = element.getTwo()) != null){
        element.setTwo(two.trim());
        element.setTwo(two.replaceAll("\\s+", ""));     
    }

}

The desired output is, that the strings for One and Two will have no whitespaces and other non-visible characters.

  1. If you use @XmlAccessorType(XmlAccessType.FIELD) jaxb will access the fields directly, use PROPERTY or NONE and move the annotations to the getter.
  2. The List will be empty for the first call to getElement , so nothing to normalize. i'm not sure if it will be called a second time.

I would use the void afterUnmarshal(Unmarshaller u, Object parent) method to normalize everything in the class once it is completely setup.

Something like that should do:

void afterUnmarshal(Unmarshaller u, Object parent) {
    this.element = ElementHelper.normalizeElement(element);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM