KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > xml > binding > SchemalessObjectModelFactory


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.xml.binding;
8
9
10 import org.jboss.logging.Logger;
11 import org.jboss.util.Classes;
12 import org.xml.sax.Attributes JavaDoc;
13
14 import java.lang.reflect.Method JavaDoc;
15 import java.util.Collection JavaDoc;
16 import java.util.List JavaDoc;
17 import java.util.ArrayList JavaDoc;
18 import java.util.Set JavaDoc;
19 import java.util.HashSet JavaDoc;
20 import java.util.Locale JavaDoc;
21 import java.text.SimpleDateFormat JavaDoc;
22
23 /**
24  * Sandbox. Very testcase specific impl.
25  *
26  * @author <a HREF="mailto:alex@jboss.org">Alexey Loubyansky</a>
27  * @version <tt>$Revision: 1.1.2.6 $</tt>
28  */

29 public class SchemalessObjectModelFactory
30    implements GenericObjectModelFactory
31 {
32    private static final Logger log = Logger.getLogger(SchemalessObjectModelFactory.class);
33
34    public Object JavaDoc newChild(Object JavaDoc parent,
35                           ContentNavigator navigator,
36                           String JavaDoc namespaceURI,
37                           String JavaDoc localName,
38                           Attributes JavaDoc attrs)
39    {
40       Object JavaDoc child = null;
41       try
42       {
43          if(parent instanceof Collection JavaDoc)
44          {
45             if(!localName.equals(java.lang.String JavaDoc.class.getName()))
46             {
47                Class JavaDoc itemClass = Thread.currentThread().getContextClassLoader().loadClass(localName);
48                child = itemClass.newInstance();
49                ((Collection JavaDoc)parent).add(child);
50             }
51          }
52          else
53          {
54             Method JavaDoc getter = parent.getClass().getMethod("get" + localName, null);
55             if(!SchemalessMarshaller.isAttributeType(getter.getReturnType()))
56             {
57                if(List JavaDoc.class.isAssignableFrom(getter.getReturnType()))
58                {
59                   child = new ArrayList JavaDoc();
60                }
61                else if(Set JavaDoc.class.isAssignableFrom(getter.getReturnType()))
62                {
63                   child = new HashSet JavaDoc();
64                }
65                else if(Collection JavaDoc.class.isAssignableFrom(getter.getReturnType()))
66                {
67                   child = new ArrayList JavaDoc();
68                }
69                else
70                {
71                   child = getter.getReturnType().newInstance();
72                }
73             }
74
75             if(child != null)
76             {
77                Method JavaDoc setter = Classes.getAttributeSetter(parent.getClass(), localName, getter.getReturnType());
78                setter.invoke(parent, new Object JavaDoc[]{child});
79             }
80          }
81       }
82       catch(NoSuchMethodException JavaDoc e)
83       {
84          log.error("Failed to get getter/setter method for " + localName + " from " + parent.getClass(), e);
85          throw new IllegalStateException JavaDoc("Failed to get getter/setter method for " +
86             localName +
87             " from " +
88             parent.getClass() +
89             ": " +
90             e.getMessage()
91          );
92       }
93       catch(Exception JavaDoc e)
94       {
95          log.error("Failed to instantiate child", e);
96          throw new IllegalStateException JavaDoc("Failed to instantiate child: " + e.getMessage());
97       }
98       return child;
99    }
100
101    public void addChild(Object JavaDoc parent,
102                         Object JavaDoc child,
103                         ContentNavigator navigator,
104                         String JavaDoc namespaceURI,
105                         String JavaDoc localName)
106    {
107    }
108
109    public void setValue(Object JavaDoc o, ContentNavigator navigator, String JavaDoc namespaceURI, String JavaDoc localName, String JavaDoc value)
110    {
111       try
112       {
113          if(o instanceof Collection JavaDoc)
114          {
115             if(localName.equals(java.lang.String JavaDoc.class.getName()))
116             {
117                ((Collection JavaDoc)o).add(value);
118             }
119          }
120          else
121          {
122             Method JavaDoc getter = Classes.getAttributeGetter(o.getClass(), localName);
123             Method JavaDoc setter = Classes.getAttributeSetter(o.getClass(), localName, getter.getReturnType());
124
125             Object JavaDoc fieldValue;
126             if(java.util.Date JavaDoc.class.isAssignableFrom(getter.getReturnType()))
127             {
128                SimpleDateFormat JavaDoc formatter = new SimpleDateFormat JavaDoc("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US);
129                fieldValue = formatter.parse(value);
130             }
131             else
132             {
133                fieldValue = value;
134             }
135
136             setter.invoke(o, new Object JavaDoc[]{fieldValue});
137          }
138       }
139       catch(NoSuchMethodException JavaDoc e)
140       {
141          throw new IllegalStateException JavaDoc("Failed to discover getter/setter for " + localName + " in " + o);
142       }
143       catch(Exception JavaDoc e)
144       {
145          throw new IllegalStateException JavaDoc("Failed to set value for " + localName + " in " + o);
146       }
147    }
148
149    public Object JavaDoc newRoot(Object JavaDoc root,
150                          ContentNavigator navigator,
151                          String JavaDoc namespaceURI,
152                          String JavaDoc localName,
153                          Attributes JavaDoc attrs)
154    {
155       Class JavaDoc rootClass;
156       try
157       {
158          rootClass = Thread.currentThread().getContextClassLoader().loadClass(localName);
159       }
160       catch(ClassNotFoundException JavaDoc e)
161       {
162          log.error("Faile to load root class " + localName, e);
163          throw new IllegalStateException JavaDoc("Failed to load root class: " + localName + ": " + e.getMessage());
164       }
165
166       try
167       {
168          root = rootClass.newInstance();
169       }
170       catch(Exception JavaDoc e)
171       {
172          log.error("Failed to create an instance of root " + localName, e);
173          throw new IllegalStateException JavaDoc("Failed to create an instance of root " + localName + ": " + e.getMessage());
174       }
175
176       return root;
177    }
178
179    public Object JavaDoc completedRoot(Object JavaDoc root, ContentNavigator navigator, String JavaDoc namespaceURI, String JavaDoc localName)
180    {
181       return root;
182    }
183 }
184
Popular Tags