KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > xb > binding > sunday > unmarshalling > CharactersHandler


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;
23
24 import java.util.List JavaDoc;
25 import java.lang.reflect.Array JavaDoc;
26 import javax.xml.namespace.QName JavaDoc;
27 import javax.xml.namespace.NamespaceContext JavaDoc;
28
29 import org.jboss.xb.binding.Constants;
30 import org.jboss.xb.binding.SimpleTypeBindings;
31 import org.jboss.xb.binding.JBossXBRuntimeException;
32 import org.jboss.xb.binding.metadata.ValueMetaData;
33
34 /**
35  * @author <a HREF="mailto:alex@jboss.org">Alexey Loubyansky</a>
36  * @version <tt>$Revision: 1958 $</tt>
37  */

38 public abstract class CharactersHandler
39 {
40    public static CharactersHandler NOOP = new CharactersHandler()
41    {
42       public Object JavaDoc unmarshal(QName JavaDoc qName,
43                               TypeBinding typeBinding,
44                               NamespaceContext JavaDoc nsCtx,
45                               ValueMetaData valueMetaData,
46                               String JavaDoc value)
47       {
48          return value;
49       }
50    };
51
52    public static CharactersHandler DEFAULT = new CharactersHandler()
53    {
54    };
55
56    public Object JavaDoc unmarshalEmpty(QName JavaDoc qName, TypeBinding typeBinding, NamespaceContext JavaDoc nsCtx, ValueMetaData valueMetaData)
57    {
58       Object JavaDoc result = null;
59       QName JavaDoc typeQName = typeBinding.getQName();
60       if(Constants.QNAME_STRING.equals(typeQName))
61       {
62          result = "";
63       }
64       else if(Constants.QNAME_BASE64BINARY.equals(typeQName))
65       {
66          result = new byte[0];
67       }
68       return result;
69    }
70
71    public Object JavaDoc unmarshal(QName JavaDoc qName, TypeBinding typeBinding, NamespaceContext JavaDoc nsCtx, ValueMetaData valueMetaData, String JavaDoc value)
72    {
73       Object JavaDoc o;
74       QName JavaDoc typeQName = typeBinding.getQName();
75       TypeBinding itemType = typeBinding.getItemType();
76       if(itemType != null)
77       {
78          QName JavaDoc itemTypeQName = itemType.getQName();
79          if(itemTypeQName != null && Constants.NS_XML_SCHEMA.equals(itemTypeQName.getNamespaceURI()))
80          {
81             List JavaDoc list = SimpleTypeBindings.unmarshalList(itemTypeQName.getLocalPart(), value, nsCtx);
82             if(typeBinding.getSchemaBinding().isUnmarshalListsToArrays())
83             {
84                if(list.isEmpty())
85                {
86                   Class JavaDoc compType = SimpleTypeBindings.classForType(itemTypeQName.getLocalPart(), true);
87                   o = Array.newInstance(compType, 0);
88                }
89                else
90                {
91                   Class JavaDoc compType = list.get(0).getClass();
92                   o = list.toArray((Object JavaDoc[])Array.newInstance(compType, list.size()));
93                }
94             }
95             else
96             {
97                o = list;
98             }
99          }
100          else
101          {
102             // todo
103
throw new JBossXBRuntimeException(
104                "Only list types with item type from " + Constants.NS_XML_SCHEMA +
105                " namespace are supported currently."
106             );
107          }
108       }
109       else if(typeQName != null && Constants.NS_XML_SCHEMA.equals(typeQName.getNamespaceURI()))
110       {
111          o = SimpleTypeBindings.unmarshal(typeQName.getLocalPart(), value, nsCtx);
112       }
113       else
114       {
115          TypeBinding baseType = typeBinding.getBaseType();
116          o = (baseType == null ? value : unmarshal(qName, baseType, nsCtx, valueMetaData, value));
117       }
118       return o;
119    }
120
121    public void setValue(QName JavaDoc qName, ElementBinding element, Object JavaDoc owner, Object JavaDoc value)
122    {
123    }
124 }
125
Popular Tags