KickJava   Java API By Example, From Geeks To Geeks.

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


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.jboss.xb.binding.GenericObjectModelFactory;
10 import org.jboss.xb.binding.UnmarshallingContext;
11 import org.xml.sax.Attributes JavaDoc;
12
13 /**
14  * @author <a HREF="mailto:alex@jboss.org">Alexey Loubyansky</a>
15  * @version <tt>$Revision: 1.1.2.3 $</tt>
16  */

17 public class BookGenericObjectModelFactory
18    implements GenericObjectModelFactory
19 {
20    public Object JavaDoc completeRoot(Object JavaDoc root, UnmarshallingContext ctx,
21          String JavaDoc uri, String JavaDoc name)
22    {
23       return root;
24    }
25
26    public Object JavaDoc newRoot(Object JavaDoc root,
27                          UnmarshallingContext navigator,
28                          String JavaDoc namespaceURI,
29                          String JavaDoc localName,
30                          Attributes JavaDoc attrs)
31    {
32       final Book book;
33       if(root == null)
34       {
35          root = book = new Book();
36       }
37       else
38       {
39          book = (Book) root;
40       }
41
42       if(attrs.getLength() > 0)
43       {
44          for(int i = 0; i < attrs.getLength(); ++i)
45          {
46             if(attrs.getLocalName(i).equals("isbn"))
47             {
48                book.setIsbn(attrs.getValue(i));
49             }
50          }
51       }
52
53       return root;
54    }
55
56    public Object JavaDoc newChild(Object JavaDoc parent,
57                           UnmarshallingContext navigator,
58                           String JavaDoc namespaceURI,
59                           String JavaDoc localName,
60                           Attributes JavaDoc attrs)
61    {
62       Object JavaDoc child = null;
63       if(parent instanceof Book)
64       {
65          if("character".equals(localName))
66          {
67             child = new BookCharacter();
68          }
69       }
70       return child;
71    }
72
73    public void addChild(Object JavaDoc parent,
74                         Object JavaDoc child,
75                         UnmarshallingContext navigator,
76                         String JavaDoc namespaceURI,
77                         String JavaDoc localName)
78    {
79       if(parent instanceof Book)
80       {
81          final Book book = (Book)parent;
82          if(child instanceof BookCharacter)
83          {
84             book.addCharacter((BookCharacter)child);
85          }
86       }
87    }
88
89    public void setValue(Object JavaDoc o, UnmarshallingContext navigator, String JavaDoc namespaceURI, String JavaDoc localName, String JavaDoc value)
90    {
91       if(o instanceof Book)
92       {
93          final Book book = (Book)o;
94          if("title".equals(localName))
95          {
96             book.setTitle(value);
97          }
98          else if("author".equals(localName))
99          {
100             book.setAuthor(value);
101          }
102       }
103       else if(o instanceof BookCharacter)
104       {
105          BookCharacter character = (BookCharacter)o;
106          if("name".equals(localName))
107          {
108             character.setName(value);
109          }
110          else if("friend-of".equals(localName))
111          {
112             character.setFriendOf(value);
113          }
114          else if("since".equals(localName))
115          {
116             character.setSince(value);
117          }
118          else if("qualification".equals(localName))
119          {
120             character.setQualification(value);
121          }
122       }
123    }
124
125    public Object JavaDoc completedRoot(Object JavaDoc root, UnmarshallingContext navigator, String JavaDoc namespaceURI, String JavaDoc localName)
126    {
127       return root;
128    }
129 }
130
Popular Tags