KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > xb > binding > sunday > unmarshalling > impl > runtime > RtCharactersHandler


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.sunday.unmarshalling.impl.runtime;
23
24 import java.lang.reflect.Method JavaDoc;
25 import javax.xml.namespace.NamespaceContext JavaDoc;
26 import javax.xml.namespace.QName JavaDoc;
27 import org.jboss.xb.binding.Constants;
28 import org.jboss.xb.binding.JBossXBRuntimeException;
29 import org.jboss.xb.binding.Util;
30 import org.jboss.xb.binding.metadata.CharactersMetaData;
31 import org.jboss.xb.binding.metadata.PropertyMetaData;
32 import org.jboss.xb.binding.metadata.ValueMetaData;
33 import org.jboss.xb.binding.sunday.unmarshalling.CharactersHandler;
34 import org.jboss.xb.binding.sunday.unmarshalling.ElementBinding;
35 import org.jboss.xb.binding.sunday.unmarshalling.TypeBinding;
36 import org.jboss.util.Classes;
37
38 /**
39  * @author <a HREF="mailto:alex@jboss.org">Alexey Loubyansky</a>
40  * @version <tt>$Revision: 2057 $</tt>
41  */

42 public class RtCharactersHandler
43    extends CharactersHandler
44 {
45    public static final RtCharactersHandler INSTANCE = new RtCharactersHandler();
46
47    public Object JavaDoc unmarshal(QName JavaDoc qName,
48                            TypeBinding typeBinding,
49                            NamespaceContext JavaDoc nsCtx,
50                            ValueMetaData valueMetaData,
51                            String JavaDoc value)
52    {
53       Object JavaDoc unmarshalled = null;
54       if(valueMetaData != null)
55       {
56          Method JavaDoc unmarshalMethod = RtUtil.getUnmarshalMethod(qName, valueMetaData);
57          Object JavaDoc args[] = unmarshalMethod.getParameterTypes().length == 1 ?
58             new Object JavaDoc[]{value} :
59             new Object JavaDoc[]{value, nsCtx};
60          unmarshalled = RtUtil.invokeUnmarshalMethod(unmarshalMethod, args, qName);
61       }
62       else
63       {
64          unmarshalled = super.unmarshal(qName, typeBinding, nsCtx, valueMetaData, value);
65
66          if(typeBinding.isSimple())
67          {
68             String JavaDoc clsName = null;
69             boolean failIfNotFound = false;
70             if(typeBinding.getClassMetaData() != null)
71             {
72                clsName = typeBinding.getClassMetaData().getImpl();
73                failIfNotFound = true;
74             }
75             else
76             {
77                QName JavaDoc typeName = typeBinding.getQName();
78                if(typeName != null && !Constants.NS_XML_SCHEMA.equals(typeName.getNamespaceURI()))
79                {
80                   boolean ignoreLowLine = typeBinding.getSchemaBinding() != null ?
81                      typeBinding.getSchemaBinding().isIgnoreLowLine() :
82                      true;
83                   clsName =
84                      Util.xmlNameToClassName(typeName.getNamespaceURI(), typeName.getLocalPart(), ignoreLowLine);
85                }
86             }
87
88             Class JavaDoc cls = clsName == null ? null : RtUtil.loadClass(clsName, failIfNotFound);
89             if(cls != null && !cls.isPrimitive())
90             {
91                // I assume if it doesn't have ctors, there should be static fromValue
92
// method like it is defined for enum types in JAXB2.0
93
// for java5 cls.isEnum() should be used instead
94
if(cls.getConstructors().length == 0)
95                {
96                   Class JavaDoc valueType = unmarshalled.getClass();
97                   // todo: this should be used in combination element.isNillable...
98
if(Classes.isPrimitiveWrapper(valueType))
99                   {
100                      valueType = Classes.getPrimitive(valueType);
101                   }
102
103                   // it should probably invoke fromValue even if unmarshalled is null
104
unmarshalled = unmarshalled == null ? null :
105                      RtUtil.invokeUnmarshalMethod(cls, "fromValue", unmarshalled, valueType, nsCtx, qName);
106                }
107                else
108                {
109                   throw new JBossXBRuntimeException("This case is not yet supported (create a feature request): " +
110                      "simple type (" +
111                      typeBinding.getQName() +
112                      ") is bound to a class (" +
113                      cls +
114                      ") with optional property metadata with " +
115                      "default value for the property name 'value'."
116                   );
117                }
118             }
119          }
120       }
121
122       return unmarshalled;
123    }
124
125    public void setValue(QName JavaDoc qName, ElementBinding element, Object JavaDoc owner, Object JavaDoc value)
126    {
127       //todo: assert if type is not null it must simple...
128

129       if(owner == null) // todo: owner should never be null
130
{
131          return;
132       }
133       
134       if (owner instanceof MapEntry)
135       {
136          TypeBinding type = element.getType();
137          CharactersMetaData characters = type.getCharactersMetaData();
138          if (characters != null)
139          {
140             if (characters.isMapEntryKey())
141             {
142                ((MapEntry) owner).setKey(value);
143             }
144             else if (characters.isMapEntryValue())
145             {
146                ((MapEntry) owner).setValue(value);
147             }
148             else
149             {
150                throw new JBossXBRuntimeException("Parent object is a map entry but characters of element " + qName
151                      + " of type " + type.getQName() + " were bound to niether key nor value in a map entry.");
152             }
153          }
154          else
155          {
156             throw new JBossXBRuntimeException("Parent object is a map entry but characters of element " + qName
157                   + " of type " + type.getQName() + " were bound to niether key nor value in a map entry.");
158          }
159       }
160       else
161       {
162          String JavaDoc propName = null;
163          String JavaDoc colType = null;
164          TypeBinding type = element.getType();
165          if (type != null && !type.isSimple()/* && type.hasSimpleContent()*/)
166          {
167             PropertyMetaData propertyMetaData = type.getPropertyMetaData();
168             if (propertyMetaData == null)
169             {
170                CharactersMetaData charactersMetaData = type.getCharactersMetaData();
171                propertyMetaData = charactersMetaData == null ? null : charactersMetaData.getProperty();
172             }
173
174             if (propertyMetaData != null)
175             {
176                propName = propertyMetaData.getName();
177                colType = propertyMetaData.getCollectionType();
178             }
179
180             if (propName == null)
181             {
182                propName = type.getSchemaBinding().getSimpleContentProperty();
183             }
184          }
185          else
186          {
187             PropertyMetaData PropertyMetaData = element.getPropertyMetaData();
188             if (PropertyMetaData != null)
189             {
190                propName = PropertyMetaData.getName();
191                colType = PropertyMetaData.getCollectionType();
192             }
193
194             if (propName == null)
195             {
196                propName = Util.xmlNameToFieldName(qName.getLocalPart(), element.getSchema().isIgnoreLowLine());
197             }
198          }
199
200          RtUtil.set(owner, value, propName, colType, element.getSchema().isIgnoreUnresolvedFieldOrClass(), element
201                .getValueAdapter());
202       }
203    }
204 }
205
Popular Tags