KickJava   Java API By Example, From Geeks To Geeks.

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


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

40 public class AllBinding
41    extends ModelGroupBinding
42 {
43    private static final Logger log = Logger.getLogger(AllBinding.class);
44
45    private Map JavaDoc elements = Collections.EMPTY_MAP;
46
47    public AllBinding(SchemaBinding schema)
48    {
49       super(schema);
50    }
51
52    public ElementBinding getArrayItem()
53    {
54       return null;
55    }
56
57    public void addParticle(ParticleBinding particle)
58    {
59       if(!particle.getTerm().isElement())
60       {
61          throw new JBossXBRuntimeException("Model group all may contain only elements!");
62       }
63
64       ElementBinding element = (ElementBinding)particle.getTerm();
65       switch(elements.size())
66       {
67          case 0:
68             elements = Collections.singletonMap(element.getQName(), particle);
69             break;
70          case 1:
71             elements = new HashMap JavaDoc(elements);
72          default:
73             elements.put(element.getQName(), particle);
74       }
75       super.addParticle(particle);
76    }
77
78    public Collection JavaDoc getParticles()
79    {
80       return Collections.unmodifiableCollection(elements.values());
81    }
82
83    public Cursor newCursor(ParticleBinding particle)
84    {
85       return new Cursor(particle)
86       {
87          private ParticleBinding curParticle;
88          private int occurence;
89
90          public ParticleBinding getCurrentParticle()
91          {
92             if(curParticle == null)
93             {
94                throw new JBossXBRuntimeException("The cursor in all group has not been positioned yet!");
95             }
96             return curParticle;
97          }
98
99          public ElementBinding getElement()
100          {
101             return (ElementBinding)getCurrentParticle().getTerm();
102          }
103
104          public boolean isPositioned()
105          {
106             return curParticle != null;
107          }
108
109          public void endElement(QName JavaDoc qName)
110          {
111             if(curParticle == null || !getElement().getQName().equals(qName))
112             {
113                throw new JBossXBRuntimeException("Failed to process endElement for " + qName +
114                   " since the current element is " + (curParticle == null ? null : getElement().getQName())
115                );
116             }
117          }
118
119          public int getOccurence()
120          {
121             return occurence;
122          }
123
124          protected List JavaDoc startElement(QName JavaDoc qName, Attributes JavaDoc atts, Set JavaDoc passedGroups, List JavaDoc groupStack, boolean required)
125          {
126             ParticleBinding particle = (ParticleBinding)elements.get(qName);
127             if(particle != null)
128             {
129                if(curParticle == particle)
130                {
131                   ++occurence;
132                }
133                else
134                {
135                   curParticle = particle;
136                   occurence = 1;
137                }
138                groupStack = addItem(groupStack, this);
139             }
140             else
141             {
142                log.warn("Element " + qName + " not found in " + elements.keySet());
143             }
144             return groupStack;
145          }
146
147          protected ElementBinding getElement(QName JavaDoc qName, Attributes JavaDoc atts, Set JavaDoc passedGroups, boolean ignoreWildcards)
148          {
149             ParticleBinding particle = (ParticleBinding)elements.get(qName);
150             return particle == null ? null : (ElementBinding)particle.getTerm();
151          }
152       };
153    }
154
155    protected boolean mayStartWith(QName JavaDoc qName, Set JavaDoc set)
156    {
157       return elements.containsKey(qName);
158    }
159 }
160
Popular Tags