KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > xb > binding > group > ValueList


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.group;
23
24 import java.util.ArrayList JavaDoc;
25 import java.util.Collection JavaDoc;
26 import java.util.Collections JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.Map JavaDoc;
29 import javax.xml.namespace.QName JavaDoc;
30
31 import org.jboss.xb.binding.JBossXBRuntimeException;
32 import org.jboss.xb.binding.metadata.PropertyMetaData;
33 import org.jboss.xb.binding.sunday.unmarshalling.AttributeBinding;
34 import org.jboss.xb.binding.sunday.unmarshalling.ParticleBinding;
35 import org.jboss.xb.binding.sunday.unmarshalling.CharactersHandler;
36 import org.jboss.xb.binding.sunday.unmarshalling.impl.runtime.RtUtil;
37
38 /**
39  * @author <a HREF="mailto:alex@jboss.org">Alexey Loubyansky</a>
40  * @version <tt>$Revision: 2045 $</tt>
41  */

42 public class ValueList
43 {
44    private final ValueListInitializer initializer;
45    private final ValueListHandler handler;
46    private final Class JavaDoc targetClass;
47    //private final int requiredTotal;
48

49    //private final Object[] requiredValues;
50
//private int state;
51
//private Map nonRequiredValues = Collections.EMPTY_MAP;
52
//private List nonRequiredBindings;
53
private List JavaDoc nonRequiredValues = Collections.EMPTY_LIST;
54
55    ValueList(ValueListInitializer initializer, ValueListHandler handler, Class JavaDoc targetClass)
56    {
57       this.initializer = initializer;
58       this.handler = handler;
59       this.targetClass = targetClass;
60       //this.requiredTotal = initializer.getRequiredBindings().size();
61
//requiredValues = new Object[requiredTotal];
62
}
63
64    void setRequiredValue(int index, int stateIncrement, Object JavaDoc value)
65    {
66       throw new UnsupportedOperationException JavaDoc();
67 /*
68       if(index >= requiredTotal)
69       {
70          throw new JBossXBRuntimeException(
71             "Maximum argument index for this value list is " + requiredTotal + " but got " + index
72          );
73       }
74       requiredValues[index] = value;
75       state += stateIncrement;
76 */

77    }
78
79    Object JavaDoc getRequiredValue(int index)
80    {
81 /*
82       if(index >= requiredTotal)
83       {
84          throw new JBossXBRuntimeException(
85             "Maximum argument index for this value list is " + requiredTotal + " but got " + index
86          );
87       }
88       return requiredValues[index];
89 */

90       throw new UnsupportedOperationException JavaDoc();
91    }
92
93    int getState()
94    {
95 // return state;
96
throw new UnsupportedOperationException JavaDoc();
97    }
98
99    void setAttributeValue(QName JavaDoc qName, AttributeBinding binding, Object JavaDoc value)
100    {
101       setNonRequiredValue(qName, binding, null, value, null);
102    }
103
104    void addTextValue(QName JavaDoc qName, ParticleBinding particle, CharactersHandler handler, Object JavaDoc value)
105    {
106       setNonRequiredValue(qName, particle, handler, value, null);
107    }
108
109    void addTermValue(QName JavaDoc qName, ParticleBinding binding, Object JavaDoc handler, Object JavaDoc value, ParticleBinding parentParticle)
110    {
111       setNonRequiredValue(qName, binding, handler, value, parentParticle);
112    }
113
114    void addRepeatableTermValue(QName JavaDoc qName, ParticleBinding binding, Object JavaDoc handler, Object JavaDoc value, ParticleBinding parentParticle)
115    {
116       NonRequiredValue last = (NonRequiredValue) (nonRequiredValues.isEmpty() ? null : nonRequiredValues.get(nonRequiredValues.size() - 1));
117       if (last == null || last.binding != binding)
118       {
119          Collection JavaDoc col;
120          PropertyMetaData propMetaData = binding.getTerm().getPropertyMetaData();
121          if(propMetaData != null && propMetaData.getCollectionType() != null)
122          {
123             Class JavaDoc colCls = RtUtil.loadClass(propMetaData.getCollectionType(), true);
124             try
125             {
126                col = (Collection JavaDoc) colCls.newInstance();
127             }
128             catch (Exception JavaDoc e)
129             {
130                throw new JBossXBRuntimeException("Failed to create an instance of " + colCls.getName() + " for property " + propMetaData.getName());
131             }
132          }
133          else
134          {
135             col = new ArrayList JavaDoc();
136          }
137          
138          col.add(value);
139          setNonRequiredValue(qName, binding, handler, col, parentParticle);
140       }
141       else
142       {
143          Collection JavaDoc col = (Collection JavaDoc) last.value;
144          col.add(value);
145       }
146    }
147
148    void setNonRequiredValue(QName JavaDoc qName, Object JavaDoc binding, Object JavaDoc handler, Object JavaDoc value, ParticleBinding parentParticle)
149    {
150       NonRequiredValue val = new NonRequiredValue(qName, binding, handler, value, parentParticle);
151       switch(nonRequiredValues.size())
152       {
153          case 0:
154             nonRequiredValues = Collections.singletonList(val);
155             break;
156          case 1:
157             nonRequiredValues = new ArrayList JavaDoc(nonRequiredValues);
158          default:
159             nonRequiredValues.add(val);
160       }
161    }
162
163    Object JavaDoc getNonRequiredValue(QName JavaDoc qName)
164    {
165 // return nonRequiredValues.get(qName);
166
throw new UnsupportedOperationException JavaDoc();
167    }
168
169    public ValueListInitializer getInitializer()
170    {
171       return initializer;
172    }
173
174    public List JavaDoc getRequiredValues()
175    {
176 // return Arrays.asList(requiredValues);
177
throw new UnsupportedOperationException JavaDoc();
178    }
179
180    public Map JavaDoc getNonRequiredValues()
181    {
182 // return nonRequiredValues;
183
throw new UnsupportedOperationException JavaDoc();
184    }
185
186    public List JavaDoc getNonRequiredBindings()
187    {
188 // return nonRequiredBindings;
189
throw new UnsupportedOperationException JavaDoc();
190    }
191
192    public ValueListHandler getHandler()
193    {
194       return handler;
195    }
196
197    public Class JavaDoc getTargetClass()
198    {
199       return targetClass;
200    }
201
202    public NonRequiredValue getValue(int i)
203    {
204       return (NonRequiredValue)nonRequiredValues.get(i);
205    }
206
207    public int size()
208    {
209       return nonRequiredValues.size();
210    }
211
212    public static final class NonRequiredValue
213    {
214       public final QName JavaDoc qName;
215       public final Object JavaDoc binding;
216       public final Object JavaDoc handler;
217       public Object JavaDoc value;
218       public ParticleBinding parentParticle;
219
220       public NonRequiredValue(QName JavaDoc qName, Object JavaDoc binding, Object JavaDoc handler, Object JavaDoc value, ParticleBinding parentParticle)
221       {
222          this.qName = qName;
223          this.binding = binding;
224          this.handler = handler;
225          this.value = value;
226          this.parentParticle = parentParticle;
227       }
228    }
229 }
230
Popular Tags