KickJava   Java API By Example, From Geeks To Geeks.

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


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 javax.xml.namespace.QName JavaDoc;
25 import org.jboss.logging.Logger;
26 import org.jboss.xb.binding.JBossXBRuntimeException;
27 import org.jboss.xb.binding.Util;
28 import org.xml.sax.Attributes JavaDoc;
29
30 /**
31  * @author <a HREF="mailto:alex@jboss.org">Alexey Loubyansky</a>
32  * @version <tt>$Revision: 2139 $</tt>
33  */

34 public class WildcardBinding
35    extends TermBinding
36 {
37    private static final Logger log = Logger.getLogger(WildcardBinding.class);
38
39    private static final short PC_LAX = 3;
40    private static final short PC_SKIP = 2;
41    private static final short PC_STRICT = 1;
42
43    private QName JavaDoc qName;
44    private SchemaBindingResolver schemaResolver;
45    private short pc;
46
47    private ParticleHandler unresolvedElementHandler;
48    private CharactersHandler unresolvedCharactersHandler;
49    private ParticleHandler wildcardHandler;
50
51    public WildcardBinding(SchemaBinding schema)
52    {
53       super(schema);
54    }
55
56    public QName JavaDoc getQName()
57    {
58       return qName;
59    }
60
61    public void setQName(QName JavaDoc qName)
62    {
63       this.qName = qName;
64    }
65
66    public SchemaBindingResolver getSchemaResolver()
67    {
68       return schemaResolver;
69    }
70
71    public void setSchemaResolver(SchemaBindingResolver schemaResolver)
72    {
73       this.schemaResolver = schemaResolver;
74    }
75
76    public short getProcessContents()
77    {
78       return pc;
79    }
80
81    public void setProcessContents(short pc)
82    {
83       this.pc = pc;
84       if(pc != PC_LAX && pc != PC_SKIP && pc != PC_STRICT)
85       {
86          throw new JBossXBRuntimeException("Unexpected value for process contents: " + pc);
87       }
88    }
89
90    public boolean isProcessContentsLax()
91    {
92       return pc == PC_LAX;
93    }
94
95    public boolean isProcessContentsSkip()
96    {
97       return pc == PC_SKIP;
98    }
99
100    public boolean isProcessContentsStrict()
101    {
102       return pc == PC_STRICT;
103    }
104
105    public void setWildcardHandler(ParticleHandler wildcardHandler)
106    {
107       this.wildcardHandler = wildcardHandler;
108    }
109
110    public ParticleHandler getWildcardHandler()
111    {
112       return wildcardHandler;
113    }
114
115    public ParticleHandler getUnresolvedElementHandler()
116    {
117       return unresolvedElementHandler;
118    }
119
120    public void setUnresolvedElementHandler(ParticleHandler unresolvedElementHandler)
121    {
122       this.unresolvedElementHandler = unresolvedElementHandler;
123    }
124
125    public CharactersHandler getUnresolvedCharactersHandler()
126    {
127       return unresolvedCharactersHandler;
128    }
129
130    public void setUnresolvedCharactersHandler(CharactersHandler unresolvedCharactersHandler)
131    {
132       this.unresolvedCharactersHandler = unresolvedCharactersHandler;
133    }
134
135    public ElementBinding getElement(QName JavaDoc qName, Attributes JavaDoc attrs)
136    {
137       if(pc == PC_SKIP)
138       {
139          return getUnresolvedElement(qName, false);
140       }
141
142       ElementBinding element = null;
143       // first, look into the own schema
144
if(schema != null)
145       {
146          element = schema.getElement(qName);
147       }
148
149       if(element == null)
150       {
151          SchemaBindingResolver resolver = schemaResolver;
152          if(resolver == null && schema != null)
153          {
154             resolver = schema.getSchemaResolver();
155          }
156
157          if(resolver != null)
158          {
159             // this is wildcard handling
160
String JavaDoc schemaLocation = attrs == null ? null : Util.getSchemaLocation(attrs, qName.getNamespaceURI());
161             SchemaBinding schema = resolver.resolve(qName.getNamespaceURI(), null, schemaLocation);
162             if(schema != null)
163             {
164                element = schema.getElement(qName);
165             }
166          }
167       }
168
169       if(element == null && pc == PC_LAX)
170       {
171          element = getUnresolvedElement(qName, false);
172       }
173
174       return element;
175    }
176
177    /**
178     * todo: this method is called for each unresolved element TWICE currently
179     * because getElement() is called TWICE. Look into fixing this!
180     *
181     * @param qName
182     * @return
183     */

184    private ElementBinding getUnresolvedElement(QName JavaDoc qName, boolean required)
185    {
186       if(log.isTraceEnabled())
187       {
188          log.trace(
189             "getUnresolvedElement for " + qName + ", required=" + required
190             + ", unresolvedElementHandler=" + unresolvedElementHandler
191          );
192       }
193
194       if(unresolvedElementHandler == null)
195       {
196          if(required)
197          {
198             throw new JBossXBRuntimeException("Schema could not be resolved for wildcard content element " +
199                qName +
200                " and particle handler for unresolved wildcard content elements is not initialized."
201             );
202          }
203          else
204          {
205             // todo this stuff could be cached
206
ParticleBinding particle = new ParticleBinding(this);
207             SequenceBinding sequence = new SequenceBinding(schema);
208             sequence.addParticle(particle);
209
210             TypeBinding type = new TypeBinding();
211             type.setParticle(new ParticleBinding(sequence));
212
213             ElementBinding element = new ElementBinding(schema, qName, type);
214             // this is unresolved element we don't care about
215
element.setSkip(Boolean.TRUE);
216             return element;
217          }
218       }
219
220       // todo this stuff could be cached
221
ParticleBinding particle = new ParticleBinding(this);
222       SequenceBinding sequence = new SequenceBinding(schema);
223       sequence.addParticle(particle);
224
225       TypeBinding type = new TypeBinding();
226       type.setHandler(unresolvedElementHandler);
227       type.setSimpleType(unresolvedCharactersHandler);
228       type.setParticle(new ParticleBinding(sequence));
229
230       return new ElementBinding(schema, qName, type);
231    }
232
233    public boolean isSkip()
234    {
235       return skip == null ? false : skip.booleanValue();
236    }
237
238    public boolean isModelGroup()
239    {
240       return false;
241    }
242
243    public boolean isWildcard()
244    {
245       return true;
246    }
247
248    public boolean isElement()
249    {
250       return false;
251    }
252 }
253
Popular Tags