KickJava   Java API By Example, From Geeks To Geeks.

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


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.lang.reflect.Array JavaDoc;
25 import java.lang.reflect.Constructor JavaDoc;
26 import java.util.ArrayList JavaDoc;
27 import java.util.List JavaDoc;
28 import javax.xml.namespace.QName JavaDoc;
29 import org.jboss.xb.binding.sunday.unmarshalling.impl.runtime.RtUtil;
30
31 /**
32  * @author <a HREF="mailto:alex@jboss.org">Alexey Loubyansky</a>
33  * @version <tt>$Revision: 2045 $</tt>
34  */

35 public interface GenericValueContainer
36 {
37    class FACTORY
38    {
39       public static GenericValueContainer array(final Class JavaDoc itemClass)
40       {
41          return array(null, null, itemClass);
42       }
43       
44       public static GenericValueContainer array(final Class JavaDoc wrapperClass,
45                                                 final String JavaDoc itemProperty,
46                                                 final Class JavaDoc itemClass)
47       {
48          return new GenericValueContainer()
49          {
50             private final Class JavaDoc wrapperType = wrapperClass;
51             private final String JavaDoc itemProp = itemProperty;
52             private final Class JavaDoc itemType = itemClass;
53             private final List JavaDoc items = new ArrayList JavaDoc();
54
55             public void addChild(QName JavaDoc name, Object JavaDoc value)
56             {
57                items.add(value);
58             }
59
60             public Object JavaDoc instantiate()
61             {
62 /* for collected repeatable particles
63                Object arr;
64                if(items.isEmpty())
65                {
66                   arr = Array.newInstance(itemType, 0);
67                }
68                else
69                {
70                   java.util.Collection col = (java.util.Collection)items.get(0);
71                   arr = Array.newInstance(itemType, col.size());
72                   if(itemType.isPrimitive())
73                   {
74                      int i = 0;
75                      for(java.util.Iterator iter = col.iterator(); iter.hasNext();)
76                      {
77                         Object item = iter.next();
78                         try
79                         {
80                            Array.set(arr, i++, item);
81                         }
82                         catch(IllegalArgumentException e)
83                         {
84                            throw new JBossXBRuntimeException("Failed to set " +
85                               item +
86                               " as an item of array " + arr
87                            );
88                         }
89                      }
90                   }
91                   else
92                   {
93                      col.toArray((Object[])arr);
94                   }
95                }
96 */

97                Object JavaDoc arr = Array.newInstance(itemType, items.size());
98                for(int i = 0; i < items.size(); ++i)
99                {
100                   try
101                   {
102                      Array.set(arr, i, items.get(i));
103                   }
104                   catch(IllegalArgumentException JavaDoc e)
105                   {
106                      throw new JBossXBRuntimeException("Failed to set " +
107                         items.get(i) +
108                         " as an item of array " + arr
109                      );
110                   }
111                }
112
113                Object JavaDoc result = arr;
114                // wrap
115
if(wrapperType != null)
116                {
117                   Constructor JavaDoc ctor = null;
118                   try
119                   {
120                      try
121                      {
122                         ctor = wrapperType.getConstructor(null);
123                         result = ctor.newInstance(null);
124                         RtUtil.set(result, arr, itemProp, null, false, null);
125                      }
126                      catch(NoSuchMethodException JavaDoc e)
127                      {
128                         Constructor JavaDoc[] ctors = wrapperType.getConstructors();
129                         for(int i = 0; i < ctors.length; ++i)
130                         {
131                            Class JavaDoc[] types = ctors[i].getParameterTypes();
132                            if(types.length == 1 && types[0].isAssignableFrom(arr.getClass()))
133                            {
134                               ctor = ctors[i];
135                               break;
136                            }
137                         }
138
139                         if(ctor == null)
140                         {
141                            throw new JBossXBRuntimeException("Failed to find an appropriate ctor in " +
142                               wrapperType +
143                               " to wrap " + arr
144                            );
145                         }
146
147                         result = ctor.newInstance(new Object JavaDoc[]{arr});
148                      }
149                   }
150                   catch(JBossXBRuntimeException e)
151                   {
152                      throw e;
153                   }
154                   catch(Exception JavaDoc e)
155                   {
156                      throw new JBossXBRuntimeException(
157                         "Failed to initialize array wrapper " + wrapperType + " for " + arr, e
158                      );
159                   }
160                }
161                return result;
162             }
163
164             public Class JavaDoc getTargetClass()
165             {
166                // this method should only be called for multidimansional arrays
167
// todo: what's the best way to get a class for array having the item class?
168
return Array.newInstance(itemType, 0).getClass();
169             }
170             
171             public String JavaDoc toString()
172             {
173                return super.toString() + "array";
174             }
175          };
176       }
177    }
178
179    void addChild(QName JavaDoc name, Object JavaDoc value);
180
181    Object JavaDoc instantiate();
182
183    Class JavaDoc getTargetClass();
184 }
185
Popular Tags