|
| 1 | +package org.hisrc.xml.bind.tests.dynamicelementname; |
| 2 | + |
| 3 | +import java.util.LinkedList; |
| 4 | +import java.util.List; |
| 5 | + |
| 6 | +import javax.xml.bind.JAXBContext; |
| 7 | +import javax.xml.bind.JAXBElement; |
| 8 | +import javax.xml.bind.JAXBException; |
| 9 | +import javax.xml.bind.annotation.XmlElementDecl; |
| 10 | +import javax.xml.bind.annotation.XmlElementRef; |
| 11 | +import javax.xml.bind.annotation.XmlRegistry; |
| 12 | +import javax.xml.bind.annotation.XmlRootElement; |
| 13 | +import javax.xml.bind.annotation.XmlTransient; |
| 14 | +import javax.xml.namespace.QName; |
| 15 | + |
| 16 | +import org.junit.Test; |
| 17 | + |
| 18 | +public class DynamicElementNameTest { |
| 19 | + |
| 20 | + @Test |
| 21 | + public void marshallsDynamicElementName() throws JAXBException { |
| 22 | + JAXBContext context = JAXBContext.newInstance(ObjectFactory.class); |
| 23 | + final Characteristics characteristics = new Characteristics(); |
| 24 | + final Characteristic characteristic = new Characteristic( |
| 25 | + "store_capacity", "40"); |
| 26 | + characteristics.getCharacteristics().add(characteristic); |
| 27 | + context.createMarshaller().marshal(characteristics, System.out); |
| 28 | + } |
| 29 | + |
| 30 | + @XmlRegistry |
| 31 | + public static class ObjectFactory { |
| 32 | + |
| 33 | + public Characteristics createCharacteristics() { |
| 34 | + return new Characteristics(); |
| 35 | + } |
| 36 | + |
| 37 | + @XmlElementDecl(name = "characteristic") |
| 38 | + public JAXBElement<String> createCharacteristic(String value) { |
| 39 | + return new Characteristic(value); |
| 40 | + } |
| 41 | + |
| 42 | + } |
| 43 | + |
| 44 | + @XmlRootElement(name = "characteristics") |
| 45 | + public static class Characteristics { |
| 46 | + |
| 47 | + private final List<Characteristic> characteristics = new LinkedList<Characteristic>(); |
| 48 | + |
| 49 | + @XmlElementRef(name = "characteristic") |
| 50 | + public List<Characteristic> getCharacteristics() { |
| 51 | + return characteristics; |
| 52 | + } |
| 53 | + |
| 54 | + } |
| 55 | + |
| 56 | + public static class Characteristic extends JAXBElement<String> { |
| 57 | + |
| 58 | + private static final long serialVersionUID = 1L; |
| 59 | + public static final QName NAME = new QName("characteristic"); |
| 60 | + |
| 61 | + public Characteristic(String value) { |
| 62 | + super(NAME, String.class, value); |
| 63 | + } |
| 64 | + |
| 65 | + public Characteristic(String characteristic, String value) { |
| 66 | + super(NAME, String.class, value); |
| 67 | + this.characteristic = characteristic; |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + public QName getName() { |
| 72 | + final String characteristic = getCharacteristic(); |
| 73 | + if (characteristic != null) { |
| 74 | + return new QName(characteristic); |
| 75 | + } |
| 76 | + return NAME; |
| 77 | + } |
| 78 | + |
| 79 | + private String characteristic; |
| 80 | + |
| 81 | + @XmlTransient |
| 82 | + public String getCharacteristic() { |
| 83 | + return characteristic; |
| 84 | + } |
| 85 | + |
| 86 | + public void setCharacteristic(String characteristic) { |
| 87 | + this.characteristic = characteristic; |
| 88 | + } |
| 89 | + } |
| 90 | +} |
0 commit comments