File tree Expand file tree Collapse file tree 7 files changed +154
-1
lines changed
java/org/hisrc/xml/bind/tests
resources/org/hisrc/xml/bind/tests/dogs Expand file tree Collapse file tree 7 files changed +154
-1
lines changed Original file line number Diff line number Diff line change 1+ package org .hisrc .xml .bind .tests .dogs ;
2+
3+ import javax .xml .bind .JAXBElement ;
4+ import javax .xml .namespace .QName ;
5+
6+ public class Dog extends JAXBElement <DogType > {
7+
8+ public static final QName NAME = new QName ("dog" );
9+
10+ private static final long serialVersionUID = 1L ;
11+
12+ public Dog (DogType value ) {
13+ super (NAME , DogType .class , value );
14+ }
15+
16+ public Dog (String dogName , DogType value ) {
17+ super (NAME , DogType .class , value );
18+ // if (value != null) {
19+ // value.setName(dogName);
20+ // }
21+ }
22+
23+ @ Override
24+ public QName getName () {
25+ final DogType value = getValue ();
26+ if (value != null && value .getName () != null ) {
27+ return new QName (value .getName ());
28+ } else {
29+ return super .getName ();
30+ }
31+ }
32+ }
Original file line number Diff line number Diff line change 1+ package org .hisrc .xml .bind .tests .dogs ;
2+
3+ import javax .xml .bind .JAXBContext ;
4+ import javax .xml .bind .JAXBException ;
5+
6+ import org .junit .Assert ;
7+ import org .junit .Test ;
8+
9+ public class DogTest {
10+
11+ @ Test
12+ public void unmarshallsDogs () throws JAXBException {
13+ final JAXBContext context = JAXBContext
14+ .newInstance (ObjectFactory .class );
15+ final Dogs dogs = (Dogs ) context .createUnmarshaller ().unmarshal (
16+ getClass ().getResource ("dogs.xml" ));
17+ Assert .assertEquals (3 , dogs .getDogs ().size ());
18+ // Does not work
19+ // Assert.assertEquals("henry", dogs.getDogs().get(0).getValue()
20+ // .getName());
21+ Assert .assertEquals ("bark" , dogs .getDogs ().get (0 ).getValue ().getSound ());
22+ // Does not work
23+ // Assert.assertEquals("fido", dogs.getDogs().get(1).getValue()
24+ // .getName());
25+ Assert .assertEquals ("woof" , dogs .getDogs ().get (1 ).getValue ().getSound ());
26+ // Does not work
27+ // Assert.assertEquals("barks", dogs.getDogs().get(2).getValue()
28+ // .getName());
29+ Assert .assertEquals ("miau" , dogs .getDogs ().get (2 ).getValue ().getSound ());
30+ }
31+ }
Original file line number Diff line number Diff line change 1+ package org .hisrc .xml .bind .tests .dogs ;
2+
3+ public class DogType {
4+
5+ private String name ;
6+ private String sound ;
7+
8+ public String getName () {
9+ return name ;
10+ }
11+
12+ public void setName (String dogName ) {
13+ this .name = dogName ;
14+ }
15+
16+ public String getSound () {
17+ return sound ;
18+ }
19+
20+ public void setSound (String sound ) {
21+ this .sound = sound ;
22+ }
23+ }
Original file line number Diff line number Diff line change 1+ package org .hisrc .xml .bind .tests .dogs ;
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 .XmlElementWrapper ;
9+ import javax .xml .bind .annotation .XmlRootElement ;
10+
11+ @ XmlRootElement (name = "listOfDogs" )
12+ public class Dogs {
13+
14+ private List <JAXBElement <DogType >> dogs = new LinkedList <JAXBElement <DogType >>();
15+
16+ @ XmlElementWrapper (name = "dogs" )
17+ @ XmlElementRef (name = "dog" )
18+ public List <JAXBElement <DogType >> getDogs () {
19+ return this .dogs ;
20+ }
21+
22+ @ Override
23+ public String toString () {
24+ return "Dogs [dogs=" + dogs + "]" ;
25+ }
26+ }
Original file line number Diff line number Diff line change 1+ package org .hisrc .xml .bind .tests .dogs ;
2+
3+ import javax .xml .bind .annotation .XmlElementDecl ;
4+ import javax .xml .bind .annotation .XmlRegistry ;
5+
6+ @ XmlRegistry
7+ public class ObjectFactory {
8+
9+ public Dogs createDogs () {
10+ return new Dogs ();
11+ }
12+
13+ @ XmlElementDecl (name = "dog" )
14+ public Dog createDog (DogType value ) {
15+ return new Dog (value );
16+ }
17+
18+ @ XmlElementDecl (name = "fido" , substitutionHeadName = "dog" , substitutionHeadNamespace = "" )
19+ public Dog createFido (DogType value ) {
20+ return new Dog ("fido" , value );
21+ }
22+
23+ @ XmlElementDecl (name = "barks" , substitutionHeadName = "dog" , substitutionHeadNamespace = "" )
24+ public Dog createBarks (DogType value ) {
25+ return new Dog ("barks" , value );
26+ }
27+ }
Original file line number Diff line number Diff line change @@ -73,7 +73,7 @@ public QName getName() {
7373 if (characteristic != null ) {
7474 return new QName (characteristic );
7575 }
76- return NAME ;
76+ return super . getName () ;
7777 }
7878
7979 private String characteristic ;
Original file line number Diff line number Diff line change 1+ <listOfDogs >
2+ <dogs >
3+ <dog >
4+ <name >henry</name >
5+ <sound >bark</sound >
6+ </dog >
7+ <fido >
8+ <sound >woof</sound >
9+ </fido >
10+ <barks >
11+ <sound >miau</sound >
12+ </barks >
13+ </dogs >
14+ </listOfDogs >
You can’t perform that action at this time.
0 commit comments