KickJava   Java API By Example, From Geeks To Geeks.

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


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.ArrayList JavaDoc;
25 import java.util.Collection JavaDoc;
26 import java.util.Collections JavaDoc;
27 import java.util.HashSet JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.Set JavaDoc;
30
31 import javax.xml.namespace.QName JavaDoc;
32
33 import org.jboss.logging.Logger;
34 import org.jboss.xb.binding.JBossXBRuntimeException;
35 import org.xml.sax.Attributes JavaDoc;
36
37 /**
38  * @author <a HREF="mailto:alex@jboss.org">Alexey Loubyansky</a>
39  * @version <tt>$Revision: 2139 $</tt>
40  */

41 public abstract class ModelGroupBinding
42    extends TermBinding
43    implements Cloneable JavaDoc
44 {
45    protected final Logger log = Logger.getLogger(getClass());
46
47    protected boolean requiredParticle;
48    protected ParticleHandler handler = DefaultHandlers.ELEMENT_HANDLER;
49
50    protected ModelGroupBinding(SchemaBinding schema)
51    {
52       super(schema);
53    }
54
55    public ParticleHandler getHandler()
56    {
57       return handler;
58    }
59
60    public void setHandler(ParticleHandler handler)
61    {
62       this.handler = handler;
63    }
64
65    public abstract ElementBinding getArrayItem();
66
67    /**
68     * Model group that is passed in as an argument to this method must be fully populated with
69     * element, wildcard and child model group bindings.
70     * @param particle
71     */

72    public void addParticle(ParticleBinding particle)
73    {
74       if(particle.isRequired())
75       {
76          requiredParticle = true;
77       }
78    }
79
80    public abstract Collection JavaDoc getParticles();
81
82    public boolean hasRequiredParticle()
83    {
84       return requiredParticle;
85    }
86
87    /**
88     * This method is not actually used during parsing. It's here only for internal tests.
89     *
90     * @param qName an element name
91     * @return true if the model group may start with the specified element
92     */

93    public boolean mayStartWith(QName JavaDoc qName)
94    {
95       return mayStartWith(qName, Collections.EMPTY_SET);
96    }
97
98    public abstract Cursor newCursor(ParticleBinding particle);
99
100    public Object JavaDoc clone() throws CloneNotSupportedException JavaDoc
101    {
102       return super.clone();
103    }
104
105    // Protected
106

107    protected abstract boolean mayStartWith(QName JavaDoc qName, Set JavaDoc set);
108
109    public boolean isSkip()
110    {
111       return skip == null ? true : skip.booleanValue();
112    }
113
114    public boolean isModelGroup()
115    {
116       return true;
117    }
118
119    public boolean isWildcard()
120    {
121       return false;
122    }
123
124    public boolean isElement()
125    {
126       return false;
127    }
128
129    // Inner
130

131    public abstract class Cursor
132    {
133       protected final ParticleBinding particle;
134       protected final boolean trace = log.isTraceEnabled();
135
136       protected Cursor(ParticleBinding particle)
137       {
138          this.particle = particle;
139       }
140
141       public ParticleBinding getParticle()
142       {
143          return particle;
144       }
145
146       public ModelGroupBinding getModelGroup()
147       {
148          return (ModelGroupBinding)particle.getTerm();
149       }
150
151       public abstract ParticleBinding getCurrentParticle();
152
153       public abstract ElementBinding getElement();
154
155       public abstract boolean isPositioned();
156
157       public List JavaDoc startElement(QName JavaDoc qName, Attributes JavaDoc attrs)
158       {
159          return startElement(qName, attrs, Collections.EMPTY_SET, Collections.EMPTY_LIST, true);
160       }
161
162       public ElementBinding getElement(QName JavaDoc qName, Attributes JavaDoc attrs, boolean ignoreWildcards)
163       {
164          return getElement(qName, attrs, Collections.EMPTY_SET, ignoreWildcards);
165       }
166
167       public abstract void endElement(QName JavaDoc qName);
168
169       public abstract int getOccurence();
170
171       // Protected
172

173       protected abstract List JavaDoc startElement(QName JavaDoc qName,
174                                            Attributes JavaDoc atts,
175                                            Set JavaDoc passedGroups,
176                                            List JavaDoc groupStack,
177                                            boolean required);
178
179       protected abstract ElementBinding getElement(QName JavaDoc qName, Attributes JavaDoc atts, Set JavaDoc passedGroups, boolean ignoreWildcards);
180
181       protected ElementBinding getElement(List JavaDoc group, QName JavaDoc qName, Attributes JavaDoc atts, Set JavaDoc passedGroups, boolean ignoreWildcards)
182       {
183          ElementBinding element = null;
184          for(int i = 0; i < group.size(); ++i)
185          {
186             ParticleBinding nextParticle = (ParticleBinding)group.get(i);
187             TermBinding item = nextParticle.getTerm();
188             if(item.isElement())
189             {
190                ElementBinding choice = (ElementBinding)item;
191                if(qName.equals(choice.getQName()))
192                {
193                   element = choice;
194                   break;
195                }
196             }
197             else if(item.isModelGroup())
198             {
199                ModelGroupBinding modelGroup = (ModelGroupBinding)item;
200                if(!passedGroups.contains(modelGroup))
201                {
202                   switch(passedGroups.size())
203                   {
204                      case 0:
205                         passedGroups = Collections.singleton(this);
206                         break;
207                      case 1:
208                         passedGroups = new HashSet JavaDoc(passedGroups);
209                      default:
210                         passedGroups.add(this);
211                   }
212
213                   ElementBinding e = modelGroup.newCursor(nextParticle).getElement(qName, atts, passedGroups, ignoreWildcards);
214                   if(e != null)
215                   {
216                      element = e;
217                      if(!qName.equals(e.getQName()))
218                      {
219                         throw new JBossXBRuntimeException(
220                            "There is a bug in ModelGroupBinding.Cursor.getElement(QName,Attributes) impl"
221                         );
222                      }
223                      break;
224                   }
225                }
226             }
227             else if(!ignoreWildcards)
228             {
229                WildcardBinding wildcard = (WildcardBinding)item;
230                ElementBinding e = wildcard.getElement(qName, atts);
231                if(e != null)
232                {
233                   element = e;
234                   if(!qName.equals(e.getQName()))
235                   {
236                      throw new JBossXBRuntimeException(
237                         "There is a bug in ModelGroupBinding.Cursor.getElement(QName,Attributes) impl"
238                      );
239                   }
240                   break;
241                }
242             }
243          }
244          return element;
245       }
246
247       protected List JavaDoc addItem(List JavaDoc list, Object JavaDoc o)
248       {
249          switch(list.size())
250          {
251             case 0:
252                list = Collections.singletonList(o);
253                break;
254             case 1:
255                list = new ArrayList JavaDoc(list);
256             default:
257                list.add(o);
258          }
259          return list;
260       }
261    }
262 }
263
Popular Tags