KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > xml > book > BookObjectFactory


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.test.xml.book;
8
9 import org.xml.sax.Attributes JavaDoc;
10 import org.jboss.xb.binding.UnmarshallingContext;
11 import org.jboss.xb.binding.ObjectModelFactory;
12
13 /**
14  * org.jboss.xml.binding.ObjectModelFactory implementation that accepts data chuncks from unmarshaller
15  * and assembles them into an instance Book.
16  *
17  * @author <a HREF="mailto:alex@jboss.org">Alexey Loubyansky</a>
18  * @version <tt>$Revision: 1.3.2.3 $</tt>
19  */

20 public class BookObjectFactory
21    implements ObjectModelFactory
22 {
23    // ObjectModelFactory implementation
24

25    public Object JavaDoc completeRoot(Object JavaDoc root, UnmarshallingContext ctx,
26          String JavaDoc uri, String JavaDoc name)
27    {
28       return root;
29    }
30
31    /**
32     * Return the root.
33     */

34    public Object JavaDoc newRoot(Object JavaDoc root,
35                          UnmarshallingContext navigator,
36                          String JavaDoc namespaceURI,
37                          String JavaDoc localName,
38                          Attributes JavaDoc attrs)
39    {
40       final Book book;
41       if(root == null)
42       {
43          root = book = new Book();
44       }
45       else
46       {
47          book = (Book) root;
48       }
49
50       if(attrs.getLength() > 0)
51       {
52          for(int i = 0; i < attrs.getLength(); ++i)
53          {
54             if(attrs.getLocalName(i).equals("isbn"))
55             {
56                book.setIsbn(attrs.getValue(i));
57             }
58          }
59       }
60
61       return root;
62    }
63
64    // Methods discovered by introspection
65

66    /**
67     * Called when a child element with simple content is read for book.
68     */

69    public void setValue(Book book,
70                         UnmarshallingContext navigator,
71                         String JavaDoc namespaceURI,
72                         String JavaDoc localName,
73                         String JavaDoc value)
74    {
75       if("title".equals(localName))
76       {
77          book.setTitle(value);
78       }
79       else if("author".equals(localName))
80       {
81          book.setAuthor(value);
82       }
83    }
84
85    /**
86     * Called when parsing of a new element started.
87     */

88    public Object JavaDoc newChild(Book book,
89                           UnmarshallingContext navigator,
90                           String JavaDoc namespaceURI,
91                           String JavaDoc localName,
92                           Attributes JavaDoc attrs)
93    {
94       Object JavaDoc child = null;
95       if("character".equals(localName))
96       {
97          child = new BookCharacter();
98       }
99       return child;
100    }
101
102    /**
103     * Called when a child element with simple content is read for character.
104     */

105    public void setValue(BookCharacter character,
106                         UnmarshallingContext navigator,
107                         String JavaDoc namespaceURI,
108                         String JavaDoc localName,
109                         String JavaDoc value)
110    {
111       if("name".equals(localName))
112       {
113          character.setName(value);
114       }
115       else if("friend-of".equals(localName))
116       {
117          character.setFriendOf(value);
118       }
119       else if("since".equals(localName))
120       {
121          if(value.endsWith("Z"))
122          {
123             value = value.substring(0, value.length() - 1);
124          }
125          character.setSince(value);
126       }
127       else if("qualification".equals(localName))
128       {
129          character.setQualification(value);
130       }
131    }
132
133    /**
134     * Called when parsing character is complete.
135     */

136    public void addChild(Book book,
137                         BookCharacter character,
138                         UnmarshallingContext navigator,
139                         String JavaDoc namespaceURI,
140                         String JavaDoc localName)
141    {
142       book.addCharacter(character);
143    }
144 }
145
Popular Tags