KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xerces > impl > xs > models > XSAllCM


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.xerces.impl.xs.models;
18
19 import org.apache.xerces.xni.QName;
20 import org.apache.xerces.impl.xs.XSElementDecl;
21 import org.apache.xerces.impl.xs.SubstitutionGroupHandler;
22 import org.apache.xerces.impl.xs.XMLSchemaException;
23 import org.apache.xerces.impl.xs.XSConstraints;
24
25 import java.util.Vector JavaDoc;
26
27 /**
28  * XSAllCM implements XSCMValidator and handles <all>
29  *
30  * @xerces.internal
31  *
32  * @author Pavani Mukthipudi, Sun Microsystems Inc.
33  * @version $Id: XSAllCM.java,v 1.12 2004/10/06 15:14:53 mrglavas Exp $
34  */

35 public class XSAllCM implements XSCMValidator {
36
37     //
38
// Constants
39
//
40

41     // start the content model: did not see any children
42
private static final short STATE_START = 0;
43     private static final short STATE_VALID = 1;
44     private static final short STATE_CHILD = 1;
45
46
47     //
48
// Data
49
//
50

51     private XSElementDecl fAllElements[];
52     private boolean fIsOptionalElement[];
53     private boolean fHasOptionalContent = false;
54     private int fNumElements = 0;
55
56     //
57
// Constructors
58
//
59

60     public XSAllCM (boolean hasOptionalContent, int size) {
61         fHasOptionalContent = hasOptionalContent;
62         fAllElements = new XSElementDecl[size];
63         fIsOptionalElement = new boolean[size];
64     }
65
66     public void addElement (XSElementDecl element, boolean isOptional) {
67         fAllElements[fNumElements] = element;
68         fIsOptionalElement[fNumElements] = isOptional;
69         fNumElements++;
70     }
71
72
73     //
74
// XSCMValidator methods
75
//
76

77     /**
78      * This methods to be called on entering a first element whose type
79      * has this content model. It will return the initial state of the
80      * content model
81      *
82      * @return Start state of the content model
83      */

84     public int[] startContentModel() {
85
86         int[] state = new int[fNumElements + 1];
87
88         for (int i = 0; i <= fNumElements; i++) {
89             state[i] = STATE_START;
90         }
91         return state;
92     }
93
94     // convinient method: when error occurs, to find a matching decl
95
// from the candidate elements.
96
Object JavaDoc findMatchingDecl(QName elementName, SubstitutionGroupHandler subGroupHandler) {
97         Object JavaDoc matchingDecl = null;
98         for (int i = 0; i < fNumElements; i++) {
99             matchingDecl = subGroupHandler.getMatchingElemDecl(elementName, fAllElements[i]);
100             if (matchingDecl != null)
101                 break;
102         }
103         return matchingDecl;
104     }
105
106     /**
107      * The method corresponds to one transition in the content model.
108      *
109      * @param elementName
110      * @param currentState Current state
111      * @return an element decl object
112      */

113     public Object JavaDoc oneTransition (QName elementName, int[] currentState, SubstitutionGroupHandler subGroupHandler) {
114
115         // error state
116
if (currentState[0] < 0) {
117             currentState[0] = XSCMValidator.SUBSEQUENT_ERROR;
118             return findMatchingDecl(elementName, subGroupHandler);
119         }
120
121         // seen child
122
currentState[0] = STATE_CHILD;
123         
124         Object JavaDoc matchingDecl = null;
125
126         for (int i = 0; i < fNumElements; i++) {
127             // we only try to look for a matching decl if we have not seen
128
// this element yet.
129
if (currentState[i+1] != STATE_START)
130                 continue;
131             matchingDecl = subGroupHandler.getMatchingElemDecl(elementName, fAllElements[i]);
132             if (matchingDecl != null) {
133                 // found the decl, mark this element as "seen".
134
currentState[i+1] = STATE_VALID;
135                 return matchingDecl;
136             }
137         }
138
139         // couldn't find the decl, change to error state.
140
currentState[0] = XSCMValidator.FIRST_ERROR;
141         return findMatchingDecl(elementName, subGroupHandler);
142     }
143
144
145     /**
146      * The method indicates the end of list of children
147      *
148      * @param currentState Current state of the content model
149      * @return true if the last state was a valid final state
150      */

151     public boolean endContentModel (int[] currentState) {
152
153         int state = currentState[0];
154
155         if (state == XSCMValidator.FIRST_ERROR || state == XSCMValidator.SUBSEQUENT_ERROR) {
156             return false;
157         }
158
159         // If <all> has minOccurs of zero and there are
160
// no children to validate, it is trivially valid
161
if (fHasOptionalContent && state == STATE_START) {
162             return true;
163         }
164
165         for (int i = 0; i < fNumElements; i++) {
166             // if one element is required, but not present, then error
167
if (!fIsOptionalElement[i] && currentState[i+1] == STATE_START)
168                 return false;
169         }
170
171         return true;
172     }
173
174     /**
175      * check whether this content violates UPA constraint.
176      *
177      * @param subGroupHandler the substitution group handler
178      * @return true if this content model contains other or list wildcard
179      */

180     public boolean checkUniqueParticleAttribution(SubstitutionGroupHandler subGroupHandler) throws XMLSchemaException {
181         // check whether there is conflict between any two leaves
182
for (int i = 0; i < fNumElements; i++) {
183             for (int j = i+1; j < fNumElements; j++) {
184                 if (XSConstraints.overlapUPA(fAllElements[i], fAllElements[j], subGroupHandler)) {
185                     // REVISIT: do we want to report all errors? or just one?
186
throw new XMLSchemaException("cos-nonambig", new Object JavaDoc[]{fAllElements[i].toString(),
187                                                                               fAllElements[j].toString()});
188                 }
189             }
190         }
191
192         return false;
193     }
194
195     /**
196      * Check which elements are valid to appear at this point. This method also
197      * works if the state is in error, in which case it returns what should
198      * have been seen.
199      *
200      * @param state the current state
201      * @return a Vector whose entries are instances of
202      * either XSWildcardDecl or XSElementDecl.
203      */

204     public Vector JavaDoc whatCanGoHere(int[] state) {
205         Vector JavaDoc ret = new Vector JavaDoc();
206         for (int i = 0; i < fNumElements; i++) {
207             // we only try to look for a matching decl if we have not seen
208
// this element yet.
209
if (state[i+1] == STATE_START)
210                 ret.addElement(fAllElements[i]);
211         }
212         return ret;
213     }
214
215 } // class XSAllCM
216

217
Popular Tags