KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.xquark.schema.*;
26
27 public class AllContentIterator extends ContentIterator {
28 private static final String JavaDoc RCSRevision = "$Revision: 1.1 $";
29 private static final String JavaDoc RCSName = "$Name: $";
30
31   private AllModelGroup group;
32   private AllModelGroup current;
33   private ContentIterator iterator;
34   private boolean inUse = false;
35   
36   public AllContentIterator(Particle particle)
37   {
38     super(particle);
39     group = (AllModelGroup)particle.getTerm();
40     this.current = (AllModelGroup)group.clone();
41   }
42
43   public ElementDeclaration getMatchedDeclaration() {
44     if (iterator != null)
45       return iterator.getMatchedDeclaration();
46     else return null;
47   }
48
49   public ElementDeclaration getModelDeclaration() {
50     if (iterator != null)
51       return iterator.getModelDeclaration();
52     else return null;
53   }
54
55   public int getProcessContents() {
56     if (iterator != null)
57       return iterator.getProcessContents();
58     else return LAX;
59   }
60   
61   private ContentIterator selectContentIterator(String JavaDoc namespace, String JavaDoc localName)
62   {
63     java.util.Iterator JavaDoc it = current.iterator();
64     while (it.hasNext()) {
65       Particle particle = (Particle)it.next();
66       if (particle.hasNextElement(namespace, localName)) {
67         inUse = true;
68     it.remove();
69     return ContentIteratorFactory.createIterator(particle, reporter);
70       }
71     }
72     return null;
73   }
74   
75   protected int nextElement(String JavaDoc namespace, String JavaDoc localName)
76   {
77     iterator = selectContentIterator(namespace, localName);
78     if (iterator == null)
79       return stopProcessing();
80     switch (iterator.nextElement(namespace, localName)) {
81     case MATCHED:
82       return MATCHED;
83     case UNKNOWN:
84       return UNKNOWN;
85     case INVALID:
86     case TOO_MANY:
87     case DONE:
88     default:
89       if (iterator.getMatchedDeclaration() != null)
90     return invalidElement(iterator.getExceptions());
91       else
92     return invalidElement("cvc-particle.3.3", parentParticle, iterator.getExceptions());
93     }
94   }
95
96   public int stopProcessing() {
97     if (!inUse) {
98       if ( minOccurs != 0 && groupMinExtent() !=0 ) {
99         return invalidElement("cvc-particle.3.1", parentParticle);
100       } else if (reporter != null) {
101         reporter.skipped(parentParticle);
102       }
103       return DONE;
104     }
105     java.util.Iterator JavaDoc it = current.iterator();
106     while (it.hasNext()) {
107       Particle particle = (Particle)it.next();
108       if (particle.getMinExtent() > 0) {
109     SchemaException se = SchemaException.computeMinExtentException(particle);
110     return invalidElement("cvc-particle.3.3", parentParticle, se);
111       } else if (reporter != null) {
112     reporter.skipped(particle);
113       }
114     }
115     return DONE;
116   }
117
118   protected boolean nextValidElements(java.util.List JavaDoc elements) {
119     // for All model group, it's better to add the particle whose minExtent is 0
120
// in the top of the elements list, and the particle whose minExtent is 1
121
// int the end of the elements list. If not, ElementGenrator will generate more elements
122
// than necessary
123
// noted Nov. 27, 2001
124
java.util.Iterator JavaDoc it = current.iterator();
125     boolean allOptional = true;
126     while (it.hasNext()) {
127       Particle particle = (Particle)it.next();
128       if (elements != null) {
129         if (particle.getMinExtent() > 0)
130           elements.addAll(particle.firstValidElements());
131         else
132           elements.addAll(0, particle.firstValidElements());
133       }
134       if (particle.getMinExtent() > 0) allOptional = false;
135     }
136     return allOptional;
137   }
138   
139   private int groupMinExtent() {
140     if ( group == null || group.size() == 0 ) return 0;
141     java.util.Iterator JavaDoc it = group.iterator();
142     int minExtent = 0;
143     while ( it.hasNext() ) {
144       Particle p = (Particle)it.next();
145       minExtent += p.getMinExtent();
146     }
147     return minExtent;
148   }
149     
150 }
151
152
153
154
155
Popular Tags