KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.util.HashMap JavaDoc;
25 import java.util.Map JavaDoc;
26 import javax.xml.namespace.QName JavaDoc;
27 import org.jboss.logging.Logger;
28 import org.jboss.util.Classes;
29 import org.jboss.xb.binding.introspection.FieldInfo;
30
31 /**
32  * @author <a HREF="mailto:alex@jboss.org">Alexey Loubyansky</a>
33  * @version <tt>$Revision: 1958 $</tt>
34  */

35 public class MappingObjectModelProvider
36    implements GenericObjectModelProvider
37 {
38    private static final Logger log = Logger.getLogger(MappingObjectModelProvider.class);
39
40    private final Map JavaDoc classMappings = new HashMap JavaDoc();
41    private final Map JavaDoc fieldMappings = new HashMap JavaDoc();
42    private boolean ignoreLowLine = true;
43    private boolean ignoreNotFoundField = true;
44
45    public boolean isIgnoreNotFoundField()
46    {
47       return ignoreNotFoundField;
48    }
49
50    public void setIgnoreNotFoundField(boolean ignoreNotFoundField)
51    {
52       this.ignoreNotFoundField = ignoreNotFoundField;
53    }
54
55    public void mapClassToElement(Class JavaDoc cls, String JavaDoc namespaceURI, String JavaDoc localName, ObjectModelProvider provider)
56    {
57       ClassToElementMapping mapping = new ClassToElementMapping(cls, namespaceURI, localName,
58          provider instanceof GenericObjectModelProvider ?
59          (GenericObjectModelProvider)provider : new DelegatingObjectModelProvider(provider)
60       );
61       classMappings.put(mapping.cls, mapping);
62    }
63
64    public void mapFieldToElement(Class JavaDoc cls,
65                                  String JavaDoc field,
66                                  String JavaDoc namespaceURI,
67                                  String JavaDoc localName,
68                                  TypeBinding converter)
69    {
70       FieldToElementMapping mapping = new FieldToElementMapping(cls, field, namespaceURI, localName, converter);
71       mapFieldToElement(mapping);
72    }
73
74    public boolean isIgnoreLowLine()
75    {
76       return ignoreLowLine;
77    }
78
79    public void setIgnoreLowLine(boolean ignoreLowLine)
80    {
81       this.ignoreLowLine = ignoreLowLine;
82    }
83
84    // GenericObjectModelProvider implementation
85

86    public Object JavaDoc getChildren(Object JavaDoc o, MarshallingContext ctx, String JavaDoc namespaceURI, String JavaDoc localName)
87    {
88       Object JavaDoc children = null;
89       if(!writeAsValue(o.getClass()))
90       {
91          children = getJavaValue(namespaceURI, localName, null, o, true, ignoreNotFoundField);
92       }
93       return children;
94    }
95
96    public Object JavaDoc getElementValue(Object JavaDoc o, MarshallingContext ctx, String JavaDoc namespaceURI, String JavaDoc localName)
97    {
98       Object JavaDoc value;
99       if(writeAsValue(o.getClass()))
100       {
101          value = o;
102       }
103       else
104       {
105          String JavaDoc fieldName = null;
106          if(ctx != null && ctx.isTypeComplex())
107          {
108             // this is how it should be
109
fieldName = ctx.getSimpleContentProperty();
110          }
111
112          // this is a hack for soap enc
113
try
114          {
115             value = getJavaValue(namespaceURI, localName, fieldName, o, false, false);
116          }
117          catch(JBossXBRuntimeException e)
118          {
119             value = getJavaValue(namespaceURI, localName, null, o, false, ignoreNotFoundField);
120          }
121       }
122       return value;
123    }
124
125    public Object JavaDoc getAttributeValue(Object JavaDoc o, MarshallingContext ctx, String JavaDoc namespaceURI, String JavaDoc localName)
126    {
127       boolean optional = ctx == null ? ignoreNotFoundField : !ctx.isAttributeRequired() || ignoreNotFoundField;
128       return getJavaValue(namespaceURI, localName, null, o, false, optional);
129    }
130
131    public Object JavaDoc getRoot(Object JavaDoc o, MarshallingContext ctx, String JavaDoc namespaceURI, String JavaDoc localName)
132    {
133       //String correspCls = Util.xmlNameToClassName(localName, true);
134
//String shortName = Classes.stripPackageName(o.getClass());
135
//return correspCls.equals(shortName) ? o : null;
136
return o;
137    }
138
139    // Private
140

141    private void mapFieldToElement(FieldToElementMapping mapping)
142    {
143       String JavaDoc mappingKey = mapping.cls.getName() + ":" + mapping.localName;
144       fieldMappings.put(mappingKey, mapping);
145    }
146
147    private Object JavaDoc getJavaValue(String JavaDoc namespaceURI, String JavaDoc localName, String JavaDoc fieldName, Object JavaDoc o, boolean forComplexType, boolean optional)
148    {
149       String JavaDoc mappingKey = o.getClass().getName() + ":" + localName;
150       FieldToElementMapping mapping = (FieldToElementMapping)fieldMappings.get(mappingKey);
151       if(mapping == null)
152       {
153          if(fieldName == null)
154          {
155             fieldName = Util.xmlNameToFieldName(localName, ignoreLowLine);
156          }
157
158          // cache the fieldmapping
159
try
160          {
161             mapping = new FieldToElementMapping(o.getClass(), fieldName, namespaceURI, localName, null);
162             mapFieldToElement(mapping);
163          }
164          catch(JBossXBRuntimeException e)
165          {
166             if(optional)
167             {
168                if(log.isTraceEnabled())
169                {
170                   log.trace(e.getMessage());
171                }
172             }
173             else
174             {
175                throw e;
176             }
177          }
178       }
179
180       FieldInfo fieldInfo = null;
181       if(mapping != null)
182       {
183          fieldInfo = mapping.fieldInfo;
184       }
185
186       Object JavaDoc value = null;
187       if(fieldInfo != null && (!forComplexType || forComplexType && !writeAsValue(fieldInfo.getType())))
188       {
189          value = fieldInfo.getValue(o);
190       }
191
192       if(value != null && mapping != null && mapping.converter != null)
193       {
194          value = mapping.converter.marshal(value);
195       }
196
197       return value;
198    }
199
200    private boolean writeAsValue(final Class JavaDoc type)
201    {
202       return Classes.isPrimitive(type) ||
203          type == String JavaDoc.class ||
204          type == java.util.Date JavaDoc.class ||
205          type == java.math.BigDecimal JavaDoc.class ||
206          type == java.math.BigInteger JavaDoc.class;
207    }
208
209    // Inner
210

211    private class ClassToElementMapping
212    {
213       public final Class JavaDoc cls;
214       public final String JavaDoc namespaceURI;
215       public final String JavaDoc localName;
216       public final GenericObjectModelProvider provider;
217
218       public ClassToElementMapping(Class JavaDoc cls,
219                                    String JavaDoc namespaceURI,
220                                    String JavaDoc localName,
221                                    GenericObjectModelProvider provider)
222       {
223          this.cls = cls;
224          this.namespaceURI = namespaceURI;
225          this.localName = localName;
226          this.provider = provider;
227
228          if(log.isTraceEnabled())
229          {
230             log.trace("new ClassToElementMapping: [cls=" +
231                cls.getName() +
232                ",qname=" +
233                new QName JavaDoc(namespaceURI, localName) +
234                "]"
235             );
236          }
237       }
238
239       public boolean equals(Object JavaDoc o)
240       {
241          if(this == o)
242          {
243             return true;
244          }
245          if(!(o instanceof ClassToElementMapping))
246          {
247             return false;
248          }
249
250          final ClassToElementMapping classToElementMapping = (ClassToElementMapping)o;
251
252          if(cls != null ? !cls.equals(classToElementMapping.cls) : classToElementMapping.cls != null)
253          {
254             return false;
255          }
256          if(localName != null ?
257             !localName.equals(classToElementMapping.localName) :
258             classToElementMapping.localName != null)
259          {
260             return false;
261          }
262          if(namespaceURI != null ?
263             !namespaceURI.equals(classToElementMapping.namespaceURI) :
264             classToElementMapping.namespaceURI != null)
265          {
266             return false;
267          }
268
269          return true;
270       }
271
272       public int hashCode()
273       {
274          int result;
275          result = (cls != null ? cls.hashCode() : 0);
276          result = 29 * result + (namespaceURI != null ? namespaceURI.hashCode() : 0);
277          result = 29 * result + (localName != null ? localName.hashCode() : 0);
278          return result;
279       }
280    }
281
282    private class FieldToElementMapping
283    {
284       public final Class JavaDoc cls;
285       public final String JavaDoc namespaceURI;
286       public final String JavaDoc localName;
287       public final TypeBinding converter;
288       public final FieldInfo fieldInfo;
289
290       public FieldToElementMapping(Class JavaDoc cls,
291                                    String JavaDoc field,
292                                    String JavaDoc namespaceURI,
293                                    String JavaDoc localName,
294                                    TypeBinding converter)
295       {
296          this.cls = cls;
297          this.namespaceURI = namespaceURI;
298          this.localName = localName;
299          this.converter = converter;
300
301          if(log.isTraceEnabled())
302          {
303             log.trace("new FieldToElementMapping: [cls=" +
304                cls.getName() +
305                ",field=" +
306                field +
307                ",qname=" +
308                new QName JavaDoc(namespaceURI, localName) +
309                "]"
310             );
311          }
312
313          fieldInfo = FieldInfo.getFieldInfo(cls, field, true);
314       }
315
316       public boolean equals(Object JavaDoc o)
317       {
318          if(this == o)
319          {
320             return true;
321          }
322          if(!(o instanceof FieldToElementMapping))
323          {
324             return false;
325          }
326
327          final FieldToElementMapping fieldToElementMapping = (FieldToElementMapping)o;
328
329          if(cls != null ? !cls.equals(fieldToElementMapping.cls) : fieldToElementMapping.cls != null)
330          {
331             return false;
332          }
333          if(!fieldInfo.getName().equals(fieldToElementMapping.fieldInfo.getName()))
334          {
335             return false;
336          }
337          if(localName != null ?
338             !localName.equals(fieldToElementMapping.localName) :
339             fieldToElementMapping.localName != null)
340          {
341             return false;
342          }
343          if(namespaceURI != null ?
344             !namespaceURI.equals(fieldToElementMapping.namespaceURI) :
345             fieldToElementMapping.namespaceURI != null)
346          {
347             return false;
348          }
349
350          return true;
351       }
352
353       public int hashCode()
354       {
355          int result;
356          result = (cls != null ? cls.hashCode() : 0);
357          result = 29 * result + fieldInfo.getName().hashCode();
358          result = 29 * result + (namespaceURI != null ? namespaceURI.hashCode() : 0);
359          result = 29 * result + (localName != null ? localName.hashCode() : 0);
360          return result;
361       }
362    }
363 }
364
Popular Tags