简体   繁体   English

JAXB @XmlMixed和@XmlAdapter

[英]JAXB @XmlMixed and @XmlAdapter

I am trying to create an @XmlAdapter to split words in a mixed List into objects of a Wrapper class called Word. 我试图创建一个@XmlAdapter,以将混合列表中的单词拆分为称为Word的Wrapper类的对象。 This field may also contain instances of an extension of Word called Group. 此字段也可能包含称为Group的Word扩展的实例。

The idea is that this: 这个想法是这样的:

<span>Some text and a <g>group of words</g> and more<span>

would be equivalent to: 等同于:

span.getContent().add(new Word("Some");
span.getContent().add(new Word("text");
span.getContent().add(new Word("and");
span.getContent().add(new Word("a");
span.getContent().add(new Group("group of words");
span.getContent().add(new Word("and");
span.getContent().add(new Word("more");

I am fighting with all the possible combinations ox XmlMixed and XmlAdapter I can think of but no luck! 我正在尝试使用所有可能想到的OX XmlMixed和XmlAdapter组合,但没有运气!

This is my latets adapter which doesn't work at all: 这是我的乳胶适配器,它根本不起作用:

public class SpanAdapter extends XmlAdapter<List<Word>, List> {

@Override
public List<Word> marshal(List v) throws Exception {
    List<Word> list = new ArrayList<>();
    for (Object o : v) {
        if (o instanceof String) {
            String s = (String) o;
            StringTokenizer st = new StringTokenizer(s, " ");
            while (st.hasMoreElements()) {
                Word word = new Word();
                word.setValue(st.nextToken());
                list.add(word);
            }
        } else if (o instanceof Group) {
            list.add((Group) o);
        }
    }
    return list;
}

@Override
public List unmarshal(List<Word> v) throws Exception {
    List list = new ArrayList<>();
    for (Word w : v) {
        if(w instanceof Group){
            list.add(w);
        } else{
            list.add(w.getValue()+ " ");
        }
    }
    return v;
}

}

And my span class: 我的班级:

@XmlRootElement(name = "span")
public class Span extends AudioComponent implements Item.Component {

private List<Word> value = new ArrayList();

@XmlMixed
@XmlElementRefs({
        @XmlElementRef(type = Group.class),
        @XmlElementRef(type = Word.class)
})
@XmlJavaTypeAdapter(SpanAdapter.class)
public List<Word> getValue() {
    return value;
}

public void setValue(List<Word> value) {
    this.value = value;
}

}

Word and Group are just wrappers of String. Word和Group只是String的包装。 Group extends Word. 组扩展Word。

I am using MOXY in case it helps. 我正在使用MOXY以防万一。

Thank you in advance! 先感谢您!

In case it helps anyone: 如果它可以帮助任何人:

I managed to do this by setting the Adapter in the List field that contains the Span element. 我设法通过在包含Span元素的List字段中设置Adapter来做到这一点。

Setting the adapter at package level or the Span class level would not work. 将适配器设置为程序包级别或Span类级别不起作用。

Also the adapter is not converting from List to List but from Span to Span, and modifying the list internally. 另外,适配器不是从列表转换为列表,而是从跨度转换为跨度,并在内部修改列表。

The final configuration is: 最终配置为:

The place where I define the adapter: 我定义适配器的地方:

@XmlElementRefs({
        @XmlElementRef(type = Span.class),
        @XmlElementRef(type = Other.class),
        @XmlElementRef(type = Another.class)

})
@XmlJavaTypeAdapter(value = SpanAdapter.class)
public List<Component> getComponents() {
    if (components == null) components = new ArrayList();
    return components;
}

The adapter (can't do it from span to span because I use it in a List field that may contain other classes): 适配器(无法跨接使用,因为我在可能包含其他类的List字段中使用了它):

public class SpanAdapter extends XmlAdapter<Object, Object> {

@Override
public Object unmarshal(Object o) throws Exception {
    if (o instanceof Span) {
        Span span = (Span) o;
        List<Span.Component> list = new ArrayList<>();
        for (Object child : span.getValue()) {
            if (child instanceof String) {
                String s = (String) child;
                for (String w : s.split(" ")) {
                    Word word = new Word();
                    word.setValue(w.trim());
                    list.add(word);
                }
            } else if (child instanceof Group) {
                list.add((Group) child);
            }
        }
        span.setValue(list);
        return span;
    }
    return o;
}

@Override
public Object marshal(Object o) throws Exception {
    if(o instanceof Span) {
        Span span = (Span) o;
        List list = new ArrayList<>();
        Iterator<Span.Component> iterator = span.getValue().iterator();
        while (iterator.hasNext()) {
            Span.Component w = iterator.next();
            if (w instanceof Group) {
                list.add(w);
            } else if (w instanceof Word) {
                String value = ((Word) w).getValue();
                list.add(iterator.hasNext() ? value + " " : value);
            }
        }
        span.setValue(list);
        return span;
    }else return o;
}
}

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

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