Skip to content

Commit b791962

Browse files
committed
Dynamic name test.
1 parent 4bf20ec commit b791962

File tree

6 files changed

+137
-0
lines changed

6 files changed

+137
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package org.hisrc.xml.bind.tests;
2+
3+
import javax.xml.bind.annotation.XmlTransient;
4+
import javax.xml.bind.annotation.XmlValue;
5+
6+
public class Characteristic {
7+
8+
public Characteristic() {
9+
}
10+
11+
private String characteristic;
12+
private String value;
13+
14+
@XmlTransient
15+
public String getCharacteristic() {
16+
return characteristic;
17+
}
18+
19+
public void setCharacteristic(String characteristic) {
20+
this.characteristic = characteristic;
21+
}
22+
23+
@XmlValue
24+
public String getValue() {
25+
return value;
26+
}
27+
28+
public void setValue(String value) {
29+
this.value = value;
30+
}
31+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package org.hisrc.xml.bind.tests;
2+
3+
import javax.xml.bind.JAXBElement;
4+
import javax.xml.namespace.QName;
5+
6+
public class CharacteristicElement extends JAXBElement<Characteristic> {
7+
8+
private static final long serialVersionUID = 6867156576690396968L;
9+
10+
public static final QName NAME = new QName("characteristic");
11+
12+
public CharacteristicElement(Characteristic value) {
13+
super(new QName("foo"), Characteristic.class, value);
14+
}
15+
16+
@Override
17+
public QName getName() {
18+
final Characteristic value = this.getValue();
19+
if (value != null) {
20+
final String characteristic = value.getCharacteristic();
21+
if (characteristic != null) {
22+
return new QName(characteristic);
23+
}
24+
}
25+
return NAME;
26+
}
27+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package org.hisrc.xml.bind.tests;
2+
3+
import java.util.LinkedList;
4+
import java.util.List;
5+
6+
import javax.xml.bind.JAXBElement;
7+
import javax.xml.bind.annotation.XmlElementRef;
8+
import javax.xml.bind.annotation.XmlRootElement;
9+
10+
@XmlRootElement(name = "characteristics")
11+
public class Characteristics {
12+
13+
private final List<JAXBElement<Characteristic>> characteristics = new LinkedList<JAXBElement<Characteristic>>();
14+
15+
public Characteristic createCharacteristic() {
16+
return new Characteristic();
17+
}
18+
19+
@XmlElementRef(name = "characteristic", type = JAXBElement.class)
20+
public List<JAXBElement<Characteristic>> getCharacteristics() {
21+
return characteristics;
22+
}
23+
24+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package org.hisrc.xml.bind.tests;
2+
3+
import javax.xml.bind.JAXBContext;
4+
import javax.xml.bind.JAXBException;
5+
6+
import org.junit.Test;
7+
8+
public class DynamicElementNameTest {
9+
10+
@Test
11+
public void marshallsDynamicElementName() throws JAXBException {
12+
JAXBContext context = JAXBContext.newInstance(ObjectFactory.class
13+
.getPackage().getName());
14+
final Characteristics characteristics = new Characteristics();
15+
final Characteristic characteristic = new Characteristic();
16+
characteristic.setCharacteristic("store_capacity");
17+
characteristic.setValue("40");
18+
characteristics.getCharacteristics().add(
19+
new CharacteristicElement(characteristic));
20+
context.createMarshaller().marshal(characteristics, System.out);
21+
}
22+
}

runtime/src/test/java/org/hisrc/xml/bind/tests/DynamicSchemaTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
package org.hisrc.xml.bind.tests;
23

34
import java.io.IOException;
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package org.hisrc.xml.bind.tests;
2+
3+
import javax.xml.bind.JAXBElement;
4+
import javax.xml.bind.annotation.XmlElementDecl;
5+
import javax.xml.bind.annotation.XmlRegistry;
6+
import javax.xml.namespace.QName;
7+
8+
@XmlRegistry
9+
public class ObjectFactory {
10+
11+
public Characteristic createCharacteristic() {
12+
return new Characteristic();
13+
}
14+
15+
public Characteristics createCharacteristics() {
16+
return new Characteristics();
17+
}
18+
19+
@XmlElementDecl(namespace = "", name = "characteristic")
20+
public JAXBElement<Characteristic> createCharacteristic(
21+
Characteristic characteristic) {
22+
return new CharacteristicElement(characteristic);
23+
}
24+
25+
@XmlElementDecl(namespace = "", name = "charactertistics")
26+
public JAXBElement<Characteristics> createCharacteristics(
27+
Characteristics value) {
28+
return new JAXBElement<Characteristics>(new QName("charactertistics"),
29+
Characteristics.class, value);
30+
}
31+
32+
}

0 commit comments

Comments
 (0)