KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xquark > schema > validation > ChoiceContentIterator


1 /*
2  * This file belongs to the XQuark distribution.
3  * Copyright (C) 2003 Universite de Versailles Saint-Quentin.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307.
18  * You can also get it at http://www.gnu.org/licenses/lgpl.html
19  *
20  * For more information on this software, see http://www.xquark.org.
21  */

22
23 package org.xquark.schema.validation;
24
25 import java.util.ArrayList JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.List JavaDoc;
28
29 import org.xquark.schema.*;
30
31 public class ChoiceContentIterator extends ContentIterator {
32 private static final String JavaDoc RCSRevision = "$Revision: 1.1 $";
33 private static final String JavaDoc RCSName = "$Name: $";
34
35   private ChoiceModelGroup group;
36   private int count = 0;
37   private ContentIterator iterator = null;
38   
39   public ChoiceContentIterator(Particle particle)
40   {
41     super(particle);
42     group = (ChoiceModelGroup)particle.getTerm();
43   }
44
45   public ElementDeclaration getMatchedDeclaration() {
46     if (iterator != null)
47       return iterator.getMatchedDeclaration();
48     else return null;
49   }
50
51   public ElementDeclaration getModelDeclaration() {
52     if (iterator != null)
53       return iterator.getModelDeclaration();
54     else return null;
55   }
56
57   public int getProcessContents() {
58     if (iterator != null)
59       return iterator.getProcessContents();
60     else return LAX;
61   }
62   
63   private ContentIterator selectContentIterator(String JavaDoc namespace, String JavaDoc localName)
64   {
65     Iterator JavaDoc it = group.iterator();
66     ContentIterator result = null;
67     
68     if ( reporter != null && count < maxOccurs && parentParticle.hasNextElement(namespace, localName) )
69       reporter.startGroup(parentParticle, ModelGroup.CHOICE, count);
70     
71     while (it.hasNext()) {
72       Particle particle = (Particle)it.next();
73       if (particle.hasNextElement(namespace, localName)) {
74     result = ContentIteratorFactory.createIterator(particle, reporter);
75       }
76       // it's necessary to test if the parentParticle has this element
77
// if not, too many particle will be skiped
78
// noted Dec 07 2001
79
else if (reporter != null && parentParticle.hasNextElement(namespace, localName) ) {
80     reporter.alternates(particle);
81       }
82     }
83     return result;
84   }
85   
86   protected int nextElement(String JavaDoc namespace, String JavaDoc localName)
87   {
88     // Iterator already in use
89
ArrayList JavaDoc previousExceptions = null;
90     if (iterator != null) {
91       switch (iterator.nextElement(namespace, localName)) {
92       case MATCHED:
93     return MATCHED;
94       case UNKNOWN:
95         return UNKNOWN;
96       case INVALID:
97     if (iterator.getMatchedDeclaration() != null)
98       return invalidElement(iterator.getExceptions());
99     else
100       return invalidElement("cvc-particle.3.3", parentParticle, iterator.getExceptions());
101       case TOO_MANY:
102     previousExceptions = iterator.getExceptions();
103     // try next particle
104
case DONE:
105     if (reporter != null)
106       reporter.endGroup(parentParticle, ModelGroup.CHOICE);
107     count++;
108     break;
109     // try next particle
110
}
111     }
112
113     iterator = selectContentIterator(namespace, localName);
114     if (iterator == null) {
115       if (previousExceptions != null) {
116     // Previous iterator had too many occurrences, and no new iterator matched
117
// Report previous error
118
return invalidElement("cvc-particle.3.3", parentParticle, previousExceptions);
119       } else if (count == 0 && parentParticle.getMinExtent() > 0) {
120     SchemaException se = SchemaException.computeMinExtentException(parentParticle);
121     return invalidElement(se);
122       } else {
123     // Iterator is finished
124
return stopProcessing();
125       }
126     }
127     if (count >= maxOccurs) {
128       if (previousExceptions != null)
129     invalidElement("cvc-particle.3.2", parentParticle, previousExceptions);
130       else
131     invalidElement("cvc-particle.3.2", parentParticle);
132       return TOO_MANY;
133     }
134     switch (iterator.nextElement(namespace, localName)) {
135     case MATCHED:
136       return MATCHED;
137     case UNKNOWN:
138       return UNKNOWN;
139     case INVALID:
140     case TOO_MANY:
141     case DONE:
142     default:
143       if (iterator.getMatchedDeclaration() != null)
144     return invalidElement(iterator.getExceptions());
145       else
146     return invalidElement("cvc-particle.3.3", parentParticle, iterator.getExceptions());
147     }
148   }
149
150   public int stopProcessing() {
151     int c;
152     // Stop current iterator
153
if (iterator != null) {
154       switch (iterator.stopProcessing()) {
155       case INVALID:
156     return invalidElement("cvc-particle.3.3", parentParticle, iterator.getExceptions());
157       case DONE:
158     // Current iterator can be stopped. Continue
159
}
160       if (reporter != null)
161     reporter.endGroup(parentParticle, ModelGroup.CHOICE);
162       c = count+1;
163     } else {
164       c = count;
165     }
166     if (c < minOccurs && parentParticle.getMinExtent() != 0) {
167       return invalidElement("cvc-particle.3.1", parentParticle);
168     } else if (c < maxOccurs && reporter != null) {
169       reporter.skipped(parentParticle);
170     }
171     
172     return DONE;
173   }
174
175   protected boolean nextValidElements(List JavaDoc elements) {
176     int c;
177     // Iterator in use
178
if (iterator != null) {
179       boolean canStop = iterator.nextValidElements(elements);
180       // If current iterator cannot be terminated immediately
181
// return false
182
if (!canStop) return false;
183       c = count+1;
184     } else {
185       c = count;
186     }
187     // Add elements of the group
188
if (c < maxOccurs && elements != null) {
189       Iterator JavaDoc it = group.iterator();
190       while (it.hasNext()) {
191     Particle particle = (Particle)it.next();
192     elements.addAll(particle.firstValidElements());
193       }
194     }
195     // If all elements are optional or minOccurs is reached
196
// add null to the elements list
197
if (c >= minOccurs || parentParticle.getMinExtent() == 0) return true;
198     else return false;
199   }
200   
201 }
202
203
204
Popular Tags