KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 1999-2003 The Apache Software Foundation. All rights
6  * reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Apache Software Foundation (http://www.apache.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "Xerces" and "Apache Software Foundation" must
28  * not be used to endorse or promote products derived from this
29  * software without prior written permission. For written
30  * permission, please contact apache@apache.org.
31  *
32  * 5. Products derived from this software may not be called "Apache",
33  * nor may "Apache" appear in their name, without prior written
34  * permission of the Apache Software Foundation.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  * ====================================================================
49  *
50  * This software consists of voluntary contributions made by many
51  * individuals on behalf of the Apache Software Foundation and was
52  * originally based on software copyright (c) 1999, International
53  * Business Machines, Inc., http://www.apache.org. For more
54  * information on the Apache Software Foundation, please see
55  * <http://www.apache.org/>.
56  */

57
58 package com.sun.org.apache.xerces.internal.impl.xs.models;
59
60 import com.sun.org.apache.xerces.internal.xni.QName;
61 import com.sun.org.apache.xerces.internal.impl.xs.XSElementDecl;
62 import com.sun.org.apache.xerces.internal.impl.xs.SubstitutionGroupHandler;
63 import com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaException;
64 import com.sun.org.apache.xerces.internal.impl.xs.XSConstraints;
65
66 import java.util.Vector JavaDoc;
67
68 /**
69  * XSAllCM implements XSCMValidator and handles <all>
70  *
71  * @author Pavani Mukthipudi, Sun Microsystems Inc.
72  * @version $Id: XSAllCM.java,v 1.8 2003/03/11 15:48:33 sandygao Exp $
73  */

74 public class XSAllCM implements XSCMValidator {
75
76     //
77
// Constants
78
//
79

80     // start the content model: did not see any children
81
private static final short STATE_START = 0;
82     private static final short STATE_VALID = 1;
83     private static final short STATE_CHILD = 1;
84
85
86     //
87
// Data
88
//
89

90     private XSElementDecl fAllElements[];
91     private boolean fIsOptionalElement[];
92     private boolean fHasOptionalContent = false;
93     private int fNumElements = 0;
94
95     //
96
// Constructors
97
//
98

99     public XSAllCM (boolean hasOptionalContent, int size) {
100         fHasOptionalContent = hasOptionalContent;
101         fAllElements = new XSElementDecl[size];
102         fIsOptionalElement = new boolean[size];
103     }
104
105     public void addElement (XSElementDecl element, boolean isOptional) {
106         fAllElements[fNumElements] = element;
107         fIsOptionalElement[fNumElements] = isOptional;
108         fNumElements++;
109     }
110
111
112     //
113
// XSCMValidator methods
114
//
115

116     /**
117      * This methods to be called on entering a first element whose type
118      * has this content model. It will return the initial state of the
119      * content model
120      *
121      * @return Start state of the content model
122      */

123     public int[] startContentModel() {
124
125         int[] state = new int[fNumElements + 1];
126
127         for (int i = 0; i <= fNumElements; i++) {
128             state[i] = STATE_START;
129         }
130         return state;
131     }
132
133     // convinient method: when error occurs, to find a matching decl
134
// from the candidate elements.
135
Object JavaDoc findMatchingDecl(QName elementName, SubstitutionGroupHandler subGroupHandler) {
136         Object JavaDoc matchingDecl = null;
137         for (int i = 0; i < fNumElements; i++) {
138             matchingDecl = subGroupHandler.getMatchingElemDecl(elementName, fAllElements[i]);
139             if (matchingDecl != null)
140                 break;
141         }
142         return matchingDecl;
143     }
144
145     /**
146      * The method corresponds to one transition in the content model.
147      *
148      * @param elementName
149      * @param state Current state
150      * @return an element decl object
151      */

152     public Object JavaDoc oneTransition (QName elementName, int[] currentState, SubstitutionGroupHandler subGroupHandler) {
153
154         // error state
155
if (currentState[0] < 0) {
156             currentState[0] = XSCMValidator.SUBSEQUENT_ERROR;
157             return findMatchingDecl(elementName, subGroupHandler);
158         }
159
160         // seen child
161
currentState[0] = STATE_CHILD;
162         
163         Object JavaDoc matchingDecl = null;
164
165         for (int i = 0; i < fNumElements; i++) {
166             // we only try to look for a matching decl if we have not seen
167
// this element yet.
168
if (currentState[i+1] != STATE_START)
169                 continue;
170             matchingDecl = subGroupHandler.getMatchingElemDecl(elementName, fAllElements[i]);
171             if (matchingDecl != null) {
172                 // found the decl, mark this element as "seen".
173
currentState[i+1] = STATE_VALID;
174                 return matchingDecl;
175             }
176         }
177
178         // couldn't find the decl, change to error state.
179
currentState[0] = XSCMValidator.FIRST_ERROR;
180         return findMatchingDecl(elementName, subGroupHandler);
181     }
182
183
184     /**
185      * The method indicates the end of list of children
186      *
187      * @param state Current state of the content model
188      * @return true if the last state was a valid final state
189      */

190     public boolean endContentModel (int[] currentState) {
191
192         int state = currentState[0];
193
194         if (state == XSCMValidator.FIRST_ERROR || state == XSCMValidator.SUBSEQUENT_ERROR) {
195             return false;
196         }
197
198         // If <all> has minOccurs of zero and there are
199
// no children to validate, it is trivially valid
200
if (fHasOptionalContent && state == STATE_START) {
201             return true;
202         }
203
204         for (int i = 0; i < fNumElements; i++) {
205             // if one element is required, but not present, then error
206
if (!fIsOptionalElement[i] && currentState[i+1] == STATE_START)
207                 return false;
208         }
209
210         return true;
211     }
212
213     /**
214      * check whether this content violates UPA constraint.
215      *
216      * @param errors to hold the UPA errors
217      * @return true if this content model contains other or list wildcard
218      */

219     public boolean checkUniqueParticleAttribution(SubstitutionGroupHandler subGroupHandler) throws XMLSchemaException {
220         // check whether there is conflict between any two leaves
221
for (int i = 0; i < fNumElements; i++) {
222             for (int j = i+1; j < fNumElements; j++) {
223                 if (XSConstraints.overlapUPA(fAllElements[i], fAllElements[j], subGroupHandler)) {
224                     // REVISIT: do we want to report all errors? or just one?
225
throw new XMLSchemaException("cos-nonambig", new Object JavaDoc[]{fAllElements[i].toString(),
226                                                                               fAllElements[j].toString()});
227                 }
228             }
229         }
230
231         return false;
232     }
233
234     /**
235      * Check which elements are valid to appear at this point. This method also
236      * works if the state is in error, in which case it returns what should
237      * have been seen.
238      *
239      * @param state the current state
240      * @return a Vector whose entries are instances of
241      * either XSWildcardDecl or XSElementDecl.
242      */

243     public Vector JavaDoc whatCanGoHere(int[] state) {
244         Vector JavaDoc ret = new Vector JavaDoc();
245         for (int i = 0; i < fNumElements; i++) {
246             // we only try to look for a matching decl if we have not seen
247
// this element yet.
248
if (state[i+1] == STATE_START)
249                 ret.addElement(fAllElements[i]);
250         }
251         return ret;
252     }
253
254 } // class XSAllCM
255

256
Popular Tags