KickJava   Java API By Example, From Geeks To Geeks.

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


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.List JavaDoc;
27
28 import org.xquark.schema.*;
29
30 public abstract class ContentIterator implements SchemaConstants {
31     private static final String JavaDoc RCSRevision = "$Revision: 1.2 $";
32     private static final String JavaDoc RCSName = "$Name: $";
33
34     public static final int MATCHED = 0;
35     public static final int INVALID = 1;
36     public static final int DONE = 2;
37     public static final int TOO_MANY = 3;
38     public static final int UNKNOWN = 4;
39
40     protected boolean endRequired = false;
41     protected boolean done = false;
42     protected Particle parentParticle = null;
43     protected int minOccurs = 1;
44     protected int maxOccurs = 1;
45     protected ContentReporter reporter = null;
46     protected ArrayList JavaDoc exceptions = null;
47     protected Type currentType = null;
48
49     public ContentIterator() {}
50
51     public ContentIterator(int minOccurs, int maxOccurs) {
52         this.minOccurs = minOccurs;
53         this.maxOccurs = maxOccurs;
54     }
55
56     public ContentIterator(Particle particle) {
57         this(particle.getMinOccurs(), particle.getMaxOccurs());
58         parentParticle = particle;
59     }
60
61     public boolean isValid() {
62         return exceptions == null;
63     }
64
65     public ArrayList JavaDoc getExceptions() {
66         return exceptions;
67     }
68
69     void resetExceptions() {
70         exceptions = null;
71     }
72
73     public ContentReporter getContentReporter() {
74         return reporter;
75     }
76
77     public void setContentReporter(ContentReporter reporter) {
78         this.reporter = reporter;
79     }
80
81     public void startElement(String JavaDoc namespace, String JavaDoc localName, Type localType) throws SchemaException {
82         startElement(namespace, localName, localType, localType != null);
83     }
84
85     public void startElement(String JavaDoc namespace, String JavaDoc localName, Type localType, boolean localTypePresent)
86         throws SchemaException {
87         if (done)
88             illegalState("Iterator terminated for element {" + namespace + "}" + localName);
89         else if (endRequired)
90             illegalState("Missing endElement statement before element {" + namespace + "}" + localName);
91
92         switch (nextElement(namespace, localName)) {
93             case MATCHED :
94                 endRequired = true;
95                 done = false;
96                 currentType = checkDeclarationAndType(localType, localTypePresent);
97                 break;
98             case DONE :
99             case INVALID :
100             case TOO_MANY :
101                 endRequired = false;
102                 done = true;
103                 break;
104         }
105         if (!isValid())
106             throw new SchemaException(exceptions);
107     }
108
109     public final void startElement(String JavaDoc namespace, String JavaDoc localName) throws SchemaException {
110         startElement(namespace, localName, null, false);
111     }
112
113     public final void startElement(org.w3c.dom.Element JavaDoc node, Type localType) throws SchemaException {
114         startElement(node.getNamespaceURI(), node.getLocalName(), localType, true);
115     }
116
117     public final void startElement(org.w3c.dom.Element JavaDoc node) throws SchemaException {
118         startElement(node.getNamespaceURI(), node.getLocalName(), null, false);
119     }
120
121     public void endElement(String JavaDoc namespace, String JavaDoc localName) throws SchemaException {
122         if (done)
123             illegalState("Iterator terminated for element {" + namespace + "}" + localName);
124         else if (!endRequired)
125             illegalState("Unexpected endElement statement for element {" + namespace + "}" + localName);
126         endRequired = false;
127         done = false;
128     }
129
130     public final void endElement(org.w3c.dom.Element JavaDoc node) throws SchemaException {
131         endElement(node.getNamespaceURI(), node.getLocalName());
132     }
133
134     public final List JavaDoc nextValidElements() {
135         if (endRequired || done)
136             return null;
137         ArrayList JavaDoc result = new ArrayList JavaDoc();
138         boolean canStop = nextValidElements(result);
139         if (canStop)
140             result.add(null);
141         return result;
142     }
143
144     public final boolean canStopProcessing() {
145         if (endRequired || done)
146             return true;
147         return nextValidElements(null);
148     }
149
150     protected final Type checkDeclarationAndType(Type localType, boolean localTypePresent) throws SchemaException {
151         ElementDeclaration matchedDecl = getMatchedDeclaration();
152         ElementDeclaration origDecl = getModelDeclaration();
153         Type result = null;
154         if (matchedDecl == null) {
155             result = localType;
156         } else {
157             Type declType = matchedDecl.getType();
158             if (matchedDecl.isAbstract())
159                 invalidElement("cvc-elt.2", matchedDecl);
160             if (localType == null) {
161                 if (localTypePresent)
162                     result = null;
163                 else
164                     result = declType;
165             } else {
166                 int exclusions = declType.getBlock() | matchedDecl.getBlock();
167                 if (origDecl != null)
168                     exclusions |= origDecl.getBlock();
169                 String JavaDoc errCode = localType.checkTypeDerivationOK(declType, exclusions, false);
170                 if (errCode != null)
171                     invalidElement("cvc-elt.4.3", matchedDecl, new SchemaException(errCode, localType));
172                 result = localType;
173             }
174         }
175         if (result != null && result.isAbstract()) {
176             invalidElement("cvc-complex-type.1", result);
177         }
178         return result;
179     }
180
181     protected final int invalidElement(SchemaException se) {
182         if (exceptions == null)
183             exceptions = new ArrayList JavaDoc();
184         exceptions.add(se);
185         return INVALID;
186     }
187
188     protected final int invalidElement(List JavaDoc exList) {
189         if (exceptions == null)
190             exceptions = new ArrayList JavaDoc();
191         exceptions.addAll(exList);
192         return INVALID;
193     }
194
195     protected final int invalidElement(String JavaDoc errCode, String JavaDoc errMsg) {
196         return invalidElement(new SchemaException(errCode, errMsg));
197     }
198
199     protected final int invalidElement(String JavaDoc errCode, Object JavaDoc obj) {
200         return invalidElement(new SchemaException(errCode, obj));
201     }
202
203     protected final int invalidElement(String JavaDoc errCode, Object JavaDoc obj, SchemaException se) {
204         return invalidElement(new SchemaException(errCode, obj, se));
205     }
206
207     protected final int invalidElement(String JavaDoc errCode, Object JavaDoc obj, List JavaDoc exList) {
208         return invalidElement(new SchemaException(errCode, obj, exList));
209     }
210
211     protected final void illegalState(String JavaDoc message) {
212         throw new IllegalStateException JavaDoc(message);
213     }
214
215     public int getMinOccurs() {
216         return minOccurs;
217     }
218
219     public int getMaxOccurs() {
220         return maxOccurs;
221     }
222
223     public Type getType() {
224         return currentType;
225     }
226
227     public Particle getParticle() {
228         return parentParticle;
229     }
230
231     void setType(Type type) {
232         currentType = type;
233     }
234
235     public abstract ElementDeclaration getMatchedDeclaration();
236
237     public ElementDeclaration getModelDeclaration() {
238         return null;
239     }
240
241     public abstract int getProcessContents();
242
243     protected abstract int nextElement(String JavaDoc namespace, String JavaDoc localName);
244
245     public abstract int stopProcessing();
246
247     protected abstract boolean nextValidElements(List JavaDoc elements);
248
249 }
250
Popular Tags