KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.xb.binding;
23
24
25 import org.jboss.logging.Logger;
26 import org.jboss.util.Classes;
27 import org.xml.sax.Attributes JavaDoc;
28
29 import java.lang.reflect.Method JavaDoc;
30 import java.util.Collection JavaDoc;
31 import java.util.List JavaDoc;
32 import java.util.ArrayList JavaDoc;
33 import java.util.Set JavaDoc;
34 import java.util.HashSet JavaDoc;
35 import java.util.Locale JavaDoc;
36 import java.text.SimpleDateFormat JavaDoc;
37
38 /**
39  * Sandbox. Very testcase specific impl.
40  *
41  * @author <a HREF="mailto:alex@jboss.org">Alexey Loubyansky</a>
42  * @version <tt>$Revision: 1958 $</tt>
43  */

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