简体   繁体   English

可以通过JAXB getter进行验证吗?

[英]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: 该XML如下所示:

<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. 我想做的是在我的getter方法中添加一个清洁器,以删除空格和其他东西。 So I thought about adding my ElementHelper to the getter (see code snippet). 因此,我考虑将我的ElementHelper添加到该吸气剂中(请参见代码段)。 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. 如果我在代码中的某个地方调用Elements.getElement()并将返回的列表与ElementHelper一起使用,则它确实可以工作。 So it seems to me, that the getter method is not called as it should. 因此在我看来,没有按应有的方式调用getter方法。

Would it help to move the XmlElement to the getter method? XmlElement移至getter方法是否有帮助? 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). 我想在代码的这一部分(集中式)上执行此操作,因为我不想每次在代码的各个位置都需要ElementHelper时就调用它,而在其他一些地方却忘记了它(这是旧代码) 。 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. 我刚刚开始了解JAXB,但对我来说仍然不清楚。 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. 如果您可以更详细地回答有关JAXB在该问题上如何工作的问题,或者指向一个很好且易于理解的资源,我将不胜感激。

EDIT: 编辑:

Here the code for the ElementHelper: 下面是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. 如果您使用@XmlAccessorType(XmlAccessType.FIELD) jaxb将直接访问字段,请使用PROPERTYNONE并将注释移至getter。
  2. The List will be empty for the first call to getElement , so nothing to normalize. 第一次调用getElementList将为空,因此无需进行任何标准化。 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. 一旦完全设置类void afterUnmarshal(Unmarshaller u, Object parent)我将使用void afterUnmarshal(Unmarshaller u, Object parent)方法对类中的所有内容进行规范化。

Something like that should do: 这样的事情应该做:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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