KickJava   Java API By Example, From Geeks To Geeks.

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


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.Map JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.Collections JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.Set JavaDoc;
29
30 import javax.xml.namespace.QName JavaDoc;
31
32 import org.jboss.xb.binding.Constants;
33 import org.jboss.xb.binding.JBossXBRuntimeException;
34 import org.jboss.xb.binding.sunday.xop.XOPUnmarshaller;
35 import org.jboss.xb.binding.sunday.xop.XOPMarshaller;
36 import org.jboss.xb.binding.metadata.PackageMetaData;
37
38 /**
39  * A SchemaBinding is a collection of binding objects (TypeBinding,
40  * ChoiceBinding, ElementBinding, ModelGroupBinding, SequenceBinding, WildcardBinding)
41  * for a single namespace keyed by the QNames of the schema components.
42  * @author <a HREF="mailto:alex@jboss.org">Alexey Loubyansky</a>
43  * @version <tt>$Revision: 2133 $</tt>
44  */

45 public class SchemaBinding
46 {
47    private static final ValueAdapter DATE_ADAPTER = new ValueAdapter()
48    {
49       public Object JavaDoc cast(Object JavaDoc o, Class JavaDoc c)
50       {
51          if (o != null && java.util.Date JavaDoc.class.isAssignableFrom(c))
52          {
53             o = ((java.util.Calendar JavaDoc) o).getTime();
54          }
55          return o;
56       }
57    };
58
59    /** The namespaces Set<String> */
60    private Set JavaDoc namespaces = Collections.EMPTY_SET;
61    /** Map<QName, TypeBinding> for simple/complex types */
62    private Map JavaDoc types = new HashMap JavaDoc();
63    /** Map<QName, ParticleBinding> for */
64    private Map JavaDoc elements = new HashMap JavaDoc();
65    /** The default package information */
66    private PackageMetaData packageMetaData;
67    /** Schema resolver to use for foreign namespaces */
68    private SchemaBindingResolver schemaResolver;
69    /** Must all content have a valid binding */
70    private boolean strictSchema = true;
71    /** Should child elements be ignored if they don't map to a parent field */
72    private boolean ignoreUnresolvedFieldOrClass = true;
73    /** Should '_' be considered as a word separator or part of Java identifier */
74    private boolean ignoreLowLine = true;
75    /** Should ${x} references be replaced with x system property */
76    private boolean replacePropertyRefs = true;
77    /** Should list xml types be unmarshalled as arrays */
78    private boolean unmarshalListsToArrays;
79    /** Should the default no-arg ctor be used to create the java instance */
80    private boolean useNoArgCtorIfFound;
81    /** The default property name to use for simple content bindings */
82    private String JavaDoc simpleContentProperty = "value";
83
84    /** default XOP unmarshaller */
85    private XOPUnmarshaller xopUnmarshaller;
86    /** default XOP marshaller */
87    private XOPMarshaller xopMarshaller;
88
89    public SchemaBinding()
90    {
91       addType(new SimpleTypeBinding(Constants.QNAME_ANYSIMPLETYPE));
92       addType(new SimpleTypeBinding(Constants.QNAME_STRING));
93       addType(new SimpleTypeBinding(Constants.QNAME_BOOLEAN));
94       addType(new SimpleTypeBinding(Constants.QNAME_DECIMAL));
95       addType(new SimpleTypeBinding(Constants.QNAME_FLOAT));
96       addType(new SimpleTypeBinding(Constants.QNAME_DOUBLE));
97       addType(new SimpleTypeBinding(Constants.QNAME_DURATION));
98       addType(new SimpleTypeBinding(Constants.QNAME_DATETIME, DATE_ADAPTER));
99       addType(new SimpleTypeBinding(Constants.QNAME_TIME, DATE_ADAPTER));
100       addType(new SimpleTypeBinding(Constants.QNAME_DATE, DATE_ADAPTER));
101       addType(new SimpleTypeBinding(Constants.QNAME_GYEARMONTH));
102       addType(new SimpleTypeBinding(Constants.QNAME_GYEAR));
103       addType(new SimpleTypeBinding(Constants.QNAME_GMONTHDAY));
104       addType(new SimpleTypeBinding(Constants.QNAME_GDAY));
105       addType(new SimpleTypeBinding(Constants.QNAME_GMONTH));
106       addType(new SimpleTypeBinding(Constants.QNAME_HEXBINARY));
107       addType(new SimpleTypeBinding(Constants.QNAME_BASE64BINARY));
108       addType(new SimpleTypeBinding(Constants.QNAME_ANYURI));
109       addType(new SimpleTypeBinding(Constants.QNAME_QNAME));
110       addType(new SimpleTypeBinding(Constants.QNAME_NOTATION));
111       addType(new SimpleTypeBinding(Constants.QNAME_NORMALIZEDSTRING));
112       addType(new SimpleTypeBinding(Constants.QNAME_TOKEN));
113       addType(new SimpleTypeBinding(Constants.QNAME_LANGUAGE));
114       addType(new SimpleTypeBinding(Constants.QNAME_NMTOKEN));
115       addType(new SimpleTypeBinding(Constants.QNAME_NMTOKENS));
116       addType(new SimpleTypeBinding(Constants.QNAME_NAME));
117       addType(new SimpleTypeBinding(Constants.QNAME_NCNAME));
118       addType(new SimpleTypeBinding(Constants.QNAME_ID));
119       addType(new SimpleTypeBinding(Constants.QNAME_IDREF));
120       addType(new SimpleTypeBinding(Constants.QNAME_IDREFS));
121       addType(new SimpleTypeBinding(Constants.QNAME_ENTITY));
122       addType(new SimpleTypeBinding(Constants.QNAME_ENTITIES));
123       addType(new SimpleTypeBinding(Constants.QNAME_INTEGER));
124       addType(new SimpleTypeBinding(Constants.QNAME_NONPOSITIVEINTEGER));
125       addType(new SimpleTypeBinding(Constants.QNAME_NEGATIVEINTEGER));
126       addType(new SimpleTypeBinding(Constants.QNAME_LONG));
127       addType(new SimpleTypeBinding(Constants.QNAME_INT));
128       addType(new SimpleTypeBinding(Constants.QNAME_SHORT));
129       addType(new SimpleTypeBinding(Constants.QNAME_BYTE));
130       addType(new SimpleTypeBinding(Constants.QNAME_NONNEGATIVEINTEGER));
131       addType(new SimpleTypeBinding(Constants.QNAME_UNSIGNEDLONG));
132       addType(new SimpleTypeBinding(Constants.QNAME_UNSIGNEDINT));
133       addType(new SimpleTypeBinding(Constants.QNAME_UNSIGNEDSHORT));
134       addType(new SimpleTypeBinding(Constants.QNAME_UNSIGNEDBYTE));
135       addType(new SimpleTypeBinding(Constants.QNAME_POSITIVEINTEGER));
136    }
137
138    /**
139     * Get the namespaces.
140     *
141     * @return the namespaces.
142     */

143    public Set JavaDoc getNamespaces()
144    {
145       return namespaces;
146    }
147
148    /**
149     * Set the namespaces.
150     *
151     * @param namespaces the namespaces.
152     * @throws IllegalArgumentException for null spaces
153     */

154    public void setNamespaces(Set JavaDoc namespaces)
155    {
156       if (namespaces == null)
157          throw new IllegalArgumentException JavaDoc("Null namespaces");
158       this.namespaces = namespaces;
159    }
160
161    public TypeBinding getType(QName JavaDoc qName)
162    {
163       return (TypeBinding)types.get(qName);
164    }
165
166    public void addType(TypeBinding type)
167    {
168       QName JavaDoc qName = type.getQName();
169       if(qName == null)
170       {
171          throw new JBossXBRuntimeException("Global type must have a name.");
172       }
173       types.put(qName, type);
174    }
175
176    public ElementBinding getElement(QName JavaDoc name)
177    {
178       ParticleBinding particle = (ParticleBinding)elements.get(name);
179       ElementBinding element = (ElementBinding)(particle == null ? null : particle.getTerm());
180       return element;
181    }
182
183    public ParticleBinding getElementParticle(QName JavaDoc name)
184    {
185       return (ParticleBinding)elements.get(name);
186    }
187
188    public void addElement(ElementBinding element)
189    {
190       ParticleBinding particle = new ParticleBinding(element);
191       elements.put(element.getQName(), particle);
192    }
193
194    public ElementBinding addElement(QName JavaDoc name, TypeBinding type)
195    {
196       ElementBinding element = new ElementBinding(this, name, type);
197       addElement(element);
198       return element;
199    }
200
201    public Iterator JavaDoc getElements()
202    {
203       return new Iterator JavaDoc()
204       {
205          private Iterator JavaDoc particleIterator = elements.values().iterator();
206
207          public boolean hasNext()
208          {
209             return particleIterator.hasNext();
210          }
211
212          public Object JavaDoc next()
213          {
214             ParticleBinding particle = (ParticleBinding)particleIterator.next();
215             return particle.getTerm();
216          }
217
218          public void remove()
219          {
220             throw new UnsupportedOperationException JavaDoc("remove is not implemented.");
221          }
222       };
223    }
224
225    public Iterator JavaDoc getElementParticles()
226    {
227       return elements.values().iterator();
228    }
229
230    public Iterator JavaDoc getTypes()
231    {
232       return Collections.unmodifiableCollection(types.values()).iterator();
233    }
234
235    public PackageMetaData getPackageMetaData()
236    {
237       return packageMetaData;
238    }
239
240    public void setPackageMetaData(PackageMetaData packageMetaData)
241    {
242       this.packageMetaData = packageMetaData;
243    }
244
245    public SchemaBindingResolver getSchemaResolver()
246    {
247       return schemaResolver;
248    }
249
250    public void setSchemaResolver(SchemaBindingResolver schemaResolver)
251    {
252       this.schemaResolver = schemaResolver;
253    }
254
255    public boolean isStrictSchema()
256    {
257       return strictSchema;
258    }
259
260    /**
261     * If strict-schema is true then all the elements and attributes in XML content being parsed must be bound
262     * in this instance of SchemaBinding (except attributes from xmlns and xsi namespaces),
263     * otherwise a runtime exception is thrown. The default value for this property is true.
264     */

265    public void setStrictSchema(boolean strictSchema)
266    {
267       this.strictSchema = strictSchema;
268    }
269
270    public boolean isIgnoreUnresolvedFieldOrClass()
271    {
272       return ignoreUnresolvedFieldOrClass;
273    }
274
275    /**
276     * If a field is not found in the parent class to set child value on or
277     * a class an element is bound to
278     * an exception will be thrown if this property is false. Otherwise,
279     * the process will just go on (the default for now).
280     */

281    public void setIgnoreUnresolvedFieldOrClass(boolean ignoreUnresolvedFieldOrClass)
282    {
283       this.ignoreUnresolvedFieldOrClass = ignoreUnresolvedFieldOrClass;
284    }
285
286    public boolean isReplacePropertyRefs()
287    {
288       return replacePropertyRefs;
289    }
290    /**
291     *
292     * @param flag
293     */

294    public void setReplacePropertyRefs(boolean flag)
295    {
296       this.replacePropertyRefs = flag;
297    }
298
299    public boolean isIgnoreLowLine()
300    {
301       return ignoreLowLine;
302    }
303
304    /**
305     * Where '_' should be considered as a word separator or a part of the Java identifier
306     * when mapping XML names to Java identifiers.
307     */

308    public void setIgnoreLowLine(boolean ignoreLowLine)
309    {
310       this.ignoreLowLine = ignoreLowLine;
311    }
312
313    public boolean isUnmarshalListsToArrays()
314    {
315       return unmarshalListsToArrays;
316    }
317
318    /**
319     * Should list xml types be unmarshalled as arrays
320     * @param unmarshalListsToArrays
321     */

322    public void setUnmarshalListsToArrays(boolean unmarshalListsToArrays)
323    {
324       this.unmarshalListsToArrays = unmarshalListsToArrays;
325    }
326
327    public boolean isUseNoArgCtorIfFound()
328    {
329       return useNoArgCtorIfFound;
330    }
331
332    /**
333     * Should the default no-arg ctor be used to create the java instance
334     * @param useNoArgCtorIfFound
335     */

336    public void setUseNoArgCtorIfFound(boolean useNoArgCtorIfFound)
337    {
338       this.useNoArgCtorIfFound = useNoArgCtorIfFound;
339    }
340
341    public String JavaDoc getSimpleContentProperty()
342    {
343       return simpleContentProperty;
344    }
345
346    /**
347     * Set the default property name to use for simple content bindings
348     * @param simpleContentProperty
349     */

350    public void setSimpleContentProperty(String JavaDoc simpleContentProperty)
351    {
352       this.simpleContentProperty = simpleContentProperty;
353    }
354
355    /**
356     * @return schema default XOP unmarshaller
357     */

358    public XOPUnmarshaller getXopUnmarshaller()
359    {
360       return xopUnmarshaller;
361    }
362
363    /**
364     * @param xopUnmarshaller schema default XOP unmarshaller
365     */

366    public void setXopUnmarshaller(XOPUnmarshaller xopUnmarshaller)
367    {
368       this.xopUnmarshaller = xopUnmarshaller;
369    }
370
371    /**
372     * @return schema default XOP marshaller
373     */

374    public XOPMarshaller getXopMarshaller()
375    {
376       return xopMarshaller;
377    }
378
379    /**
380     * @param xopMarshaller schema default XOP marshaller
381     */

382    public void setXopMarshaller(XOPMarshaller xopMarshaller)
383    {
384       this.xopMarshaller = xopMarshaller;
385    }
386
387    void addElementParticle(ParticleBinding particle)
388    {
389       ElementBinding element = (ElementBinding)particle.getTerm();
390       elements.put(element.getQName(), particle);
391    }
392 }
393
Popular Tags