KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.text.SimpleDateFormat JavaDoc;
10 import java.text.ParseException JavaDoc;
11 import java.util.TimeZone JavaDoc;
12 import java.util.Calendar JavaDoc;
13 import org.jboss.xb.binding.ObjectModelProvider;
14 import org.jboss.xb.binding.MarshallingContext;
15
16 /**
17  * org.jboss.xml.binding.ObjectModelProvider implementation that provides data to marshaller given
18  * specific Book instance.
19  *
20  * @version <tt>$Revision: 1.3.2.3 $</tt>
21  * @author <a HREF="mailto:alex@jboss.org">Alexey Loubyansky</a>
22  */

23 public class BookObjectProvider
24    implements ObjectModelProvider
25 {
26    public Object JavaDoc getRoot(Object JavaDoc o, MarshallingContext ctx, String JavaDoc namespaceURI, String JavaDoc localName)
27    {
28       return o;
29    }
30
31    public Object JavaDoc getChildren(Book book, String JavaDoc namespaceUri, String JavaDoc localName)
32    {
33       Object JavaDoc children = null;
34       if(localName.equals("book"))
35       {
36          children = book;
37       }
38       else if(localName.equals("character"))
39       {
40          children = book.getCharacters();
41       }
42       return children;
43    }
44
45    public Object JavaDoc getAttributeValue(Book book, String JavaDoc namespaceUri, String JavaDoc localName)
46    {
47       Object JavaDoc value;
48       if("isbn".equals(localName))
49       {
50          value = book.getIsbn();
51       }
52       else
53       {
54          value = null;
55       }
56       return value;
57    }
58
59    public Object JavaDoc getElementValue(Book book, String JavaDoc namespaceUri, String JavaDoc localName)
60    {
61       Object JavaDoc value;
62       if("title".equals(localName))
63       {
64          value = book.getTitle();
65       }
66       else if("author".equals(localName))
67       {
68          value = book.getAuthor();
69       }
70       else
71       {
72          value = null;
73       }
74       return value;
75    }
76
77    public Object JavaDoc getElementValue(BookCharacter character, String JavaDoc namespaceUri, String JavaDoc localName)
78    {
79       Object JavaDoc value = null;
80       if("name".equals(localName))
81       {
82          value = character.getName();
83       }
84       else if("friend-of".equals(localName))
85       {
86          value = character.getFriendOf();
87       }
88       else if("since".equals(localName))
89       {
90          String JavaDoc since = character.getSince();
91          if(since == null)
92          {
93             value = null;
94          }
95          else
96          {
97             SimpleDateFormat JavaDoc dateFormat = new SimpleDateFormat JavaDoc("yyyy-MM-dd");
98             dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
99             java.util.Date JavaDoc date = null;
100             try
101             {
102                date = dateFormat.parse(since);
103             }
104             catch(ParseException JavaDoc e)
105             {
106                throw new IllegalStateException JavaDoc("Failed to parse date " + since + ": " + e.getMessage());
107             }
108
109             Calendar JavaDoc cal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
110             cal.setTime(date);
111             value = cal;
112          }
113       }
114       else if("qualification".equals(localName))
115       {
116          value = character.getQualification();
117       }
118       return value;
119    }
120 }
121
Popular Tags