File tree Expand file tree Collapse file tree 6 files changed +137
-0
lines changed
runtime/src/test/java/org/hisrc/xml/bind/tests Expand file tree Collapse file tree 6 files changed +137
-0
lines changed Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 1+
12package org .hisrc .xml .bind .tests ;
23
34import java .io .IOException ;
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments