KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 2001-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) 2001, 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;
59
60 import com.sun.org.apache.xerces.internal.impl.dv.ValidatedInfo;
61 import com.sun.org.apache.xerces.internal.xs.XSAnnotation;
62 import com.sun.org.apache.xerces.internal.xs.XSAttributeGroupDefinition;
63 import com.sun.org.apache.xerces.internal.xs.XSAttributeUse;
64 import com.sun.org.apache.xerces.internal.xs.XSConstants;
65 import com.sun.org.apache.xerces.internal.xs.XSNamespaceItem;
66 import com.sun.org.apache.xerces.internal.xs.XSObjectList;
67 import com.sun.org.apache.xerces.internal.xs.XSWildcard;
68 import com.sun.org.apache.xerces.internal.impl.xs.util.XSObjectListImpl;
69
70 /**
71  * The XML representation for an attribute group declaration
72  * schema component is a global <attributeGroup> element information item
73  *
74  * @author Sandy Gao, IBM
75  * @author Rahul Srivastava, Sun Microsystems Inc.
76  *
77  * @version $Id: XSAttributeGroupDecl.java,v 1.16 2003/11/11 20:14:58 sandygao Exp $
78  */

79 public class XSAttributeGroupDecl implements XSAttributeGroupDefinition {
80
81     // name of the attribute group
82
public String JavaDoc fName = null;
83     // target namespace of the attribute group
84
public String JavaDoc fTargetNamespace = null;
85     // number of attribute uses included by this attribute group
86
int fAttrUseNum = 0;
87     // attribute uses included by this attribute group
88
private static final int INITIAL_SIZE = 5;
89     XSAttributeUseImpl[] fAttributeUses = new XSAttributeUseImpl[INITIAL_SIZE];
90     // attribute wildcard included by this attribute group
91
public XSWildcardDecl fAttributeWC = null;
92     // whether there is an attribute use whose type is or is derived from ID.
93
public String JavaDoc fIDAttrName = null;
94
95     // optional annotation
96
public XSAnnotationImpl fAnnotation;
97     
98     protected XSObjectListImpl fAttrUses = null;
99
100     // add an attribute use
101
// if the type is derived from ID, but there is already another attribute
102
// use of type ID, then return the name of the other attribute use;
103
// otherwise, return null
104
public String JavaDoc addAttributeUse(XSAttributeUseImpl attrUse) {
105
106         if (fAttrUseNum == fAttributeUses.length) {
107             fAttributeUses = resize(fAttributeUses, fAttrUseNum*2);
108         }
109         fAttributeUses[fAttrUseNum++] = attrUse;
110         // if this attribute use is prohibited, then don't check whether it's
111
// of type ID
112
if (attrUse.fUse == SchemaSymbols.USE_PROHIBITED)
113             return null;
114
115         if (attrUse.fAttrDecl.fType.isIDType()) {
116             // if there is already an attribute use of type ID, return it' sname
117
if (fIDAttrName == null)
118                 fIDAttrName = attrUse.fAttrDecl.fName;
119             else
120                 return fIDAttrName;
121         }
122
123         return null;
124     }
125
126     public XSAttributeUse getAttributeUse(String JavaDoc namespace, String JavaDoc name) {
127         for (int i=0; i<fAttrUseNum; i++) {
128             if ( (fAttributeUses[i].fAttrDecl.fTargetNamespace == namespace) &&
129                  (fAttributeUses[i].fAttrDecl.fName == name) )
130                 return fAttributeUses[i];
131         }
132
133         return null;
134     }
135
136     public void removeProhibitedAttrs() {
137         if (fAttrUseNum == 0) return;
138         int pCount = 0;
139         XSAttributeUseImpl[] pUses = new XSAttributeUseImpl[fAttrUseNum];
140         for (int i = 0; i < fAttrUseNum; i++) {
141             if (fAttributeUses[i].fUse == SchemaSymbols.USE_PROHIBITED) {
142                 pCount++;
143                 // we use the entries at the end, so that we can use the
144
// first entries to store non-prohibited attribute uses,
145
// hence avoid creating a new array.
146
pUses[fAttrUseNum-pCount] = fAttributeUses[i];
147             }
148         }
149
150         int newCount = 0;
151         if (pCount > 0) {
152             OUTER: for (int i = 0; i < fAttrUseNum; i++) {
153                 if (fAttributeUses[i].fUse == SchemaSymbols.USE_PROHIBITED)
154                     continue;
155                 for (int j = 1; j <= pCount; j++) {
156                     if (fAttributeUses[i].fAttrDecl.fName == pUses[fAttrUseNum-pCount].fAttrDecl.fName &&
157                         fAttributeUses[i].fAttrDecl.fTargetNamespace == pUses[fAttrUseNum-pCount].fAttrDecl.fTargetNamespace) {
158                         continue OUTER;
159                     }
160                 }
161                 pUses[newCount++] = fAttributeUses[i];
162             }
163             fAttributeUses = pUses;
164             fAttrUseNum = newCount;
165         }
166     }
167
168     /**
169      * Check that the attributes in this group validly restrict those from a base group.
170      * If an error is found, an Object[] is returned. This contains the arguments for the error message
171      * describing the error. The last element in the array (at index arr.length - 1) is the the error code.
172      * Returns null if there is no error.
173      *
174      * REVISIT: is there a better way of returning the appropriate information for the error?
175      *
176      * @param typeName the name of the type containing this attribute group, used for error reporting purposes
177      * @param baseGroup the XSAttributeGroupDecl that is the base we are checking against
178      */

179     public Object JavaDoc[] validRestrictionOf(String JavaDoc typeName, XSAttributeGroupDecl baseGroup) {
180
181         Object JavaDoc[] errorArgs = null;
182         XSAttributeUseImpl attrUse = null;
183         XSAttributeDecl attrDecl = null;
184         XSAttributeUseImpl baseAttrUse = null;
185         XSAttributeDecl baseAttrDecl = null;
186
187         for (int i=0; i<fAttrUseNum; i++) {
188
189             attrUse = fAttributeUses[i];
190             attrDecl = attrUse.fAttrDecl;
191
192             // Look for a match in the base
193
baseAttrUse = (XSAttributeUseImpl)baseGroup.getAttributeUse(attrDecl.fTargetNamespace,attrDecl.fName);
194             if (baseAttrUse != null) {
195                 //
196
// derivation-ok-restriction. Constraint 2.1.1
197
//
198

199                 if (baseAttrUse.getRequired() && !attrUse.getRequired()) {
200                     errorArgs = new Object JavaDoc[]{typeName, attrDecl.fName,
201                                              attrUse.fUse == SchemaSymbols.USE_OPTIONAL ? SchemaSymbols.ATTVAL_OPTIONAL : SchemaSymbols.ATTVAL_PROHIBITED,
202                                              "derivation-ok-restriction.2.1.1"};
203                     return errorArgs;
204                 }
205
206                 // if this attribute is prohibited in the derived type, don't
207
// need to check any of the following constraints.
208
if (attrUse.fUse == SchemaSymbols.USE_PROHIBITED) {
209                     continue;
210                 }
211
212                 baseAttrDecl = baseAttrUse.fAttrDecl;
213                 //
214
// derivation-ok-restriction. Constraint 2.1.1
215
//
216
if (! XSConstraints.checkSimpleDerivationOk(attrDecl.fType,
217                                                             baseAttrDecl.fType,
218                                                             baseAttrDecl.fType.getFinal()) ) {
219                     errorArgs = new Object JavaDoc[]{typeName, attrDecl.fName, attrDecl.fType.getName(),
220                                              baseAttrDecl.fType.getName(), "derivation-ok-restriction.2.1.2"};
221                     return errorArgs;
222                 }
223
224
225                 //
226
// derivation-ok-restriction. Constraint 2.1.3
227
//
228
int baseConsType=baseAttrUse.fConstraintType!=XSConstants.VC_NONE?
229                                  baseAttrUse.fConstraintType:baseAttrDecl.getConstraintType();
230                 int thisConstType = attrUse.fConstraintType!=XSConstants.VC_NONE?
231                                     attrUse.fConstraintType:attrDecl.getConstraintType();
232
233                 if (baseConsType == XSConstants.VC_FIXED) {
234
235                     if (thisConstType != XSConstants.VC_FIXED) {
236                         errorArgs = new Object JavaDoc[]{typeName, attrDecl.fName,
237                                                  "derivation-ok-restriction.2.1.3.a"};
238                         return errorArgs;
239                     } else {
240                         // check the values are the same.
241
ValidatedInfo baseFixedValue=(baseAttrUse.fDefault!=null ?
242                                                       baseAttrUse.fDefault: baseAttrDecl.fDefault);
243                         ValidatedInfo thisFixedValue=(attrUse.fDefault!=null ?
244                                                       attrUse.fDefault: attrDecl.fDefault);
245                         if (!baseFixedValue.actualValue.equals(thisFixedValue.actualValue)) {
246                             errorArgs = new Object JavaDoc[]{typeName, attrDecl.fName, thisFixedValue.stringValue(),
247                                                      baseFixedValue.stringValue(), "derivation-ok-restriction.2.1.3.b"};
248                             return errorArgs;
249                         }
250
251                     }
252
253                 }
254             } else {
255                 // No matching attribute in base - there should be a matching wildcard
256

257                 //
258
// derivation-ok-restriction. Constraint 2.2
259
//
260
if (baseGroup.fAttributeWC == null) {
261                     errorArgs = new Object JavaDoc[]{typeName, attrDecl.fName,
262                                              "derivation-ok-restriction.2.2.a"};
263                     return errorArgs;
264                 }
265                 else if (!baseGroup.fAttributeWC.allowNamespace(attrDecl.fTargetNamespace)) {
266                     errorArgs = new Object JavaDoc[]{typeName, attrDecl.fName,
267                                              attrDecl.fTargetNamespace==null?"":attrDecl.fTargetNamespace,
268                                              "derivation-ok-restriction.2.2.b"};
269                     return errorArgs;
270                 }
271             }
272         }
273
274         //
275
// Check that any REQUIRED attributes in the base have matching attributes
276
// in this group
277
// derivation-ok-restriction. Constraint 3
278
//
279
for (int i=0; i<baseGroup.fAttrUseNum; i++) {
280
281             baseAttrUse = baseGroup.fAttributeUses[i];
282
283             if (baseAttrUse.fUse == SchemaSymbols.USE_REQUIRED) {
284
285                 baseAttrDecl = baseAttrUse.fAttrDecl;
286                 // Look for a match in this group
287
if (getAttributeUse(baseAttrDecl.fTargetNamespace,baseAttrDecl.fName) == null) {
288                     errorArgs = new Object JavaDoc[]{typeName, baseAttrUse.fAttrDecl.fName,
289                                              "derivation-ok-restriction.3"};
290                     return errorArgs;
291                 }
292             }
293         }
294
295
296         // Now, check wildcards
297
//
298
// derivation-ok-restriction. Constraint 4
299
//
300
if (fAttributeWC != null) {
301             if (baseGroup.fAttributeWC == null) {
302                 errorArgs = new Object JavaDoc[]{typeName, "derivation-ok-restriction.4.1"};
303                 return errorArgs;
304             }
305             if (! fAttributeWC.isSubsetOf(baseGroup.fAttributeWC)) {
306                 errorArgs = new Object JavaDoc[]{typeName, "derivation-ok-restriction.4.2"};
307                 return errorArgs;
308             }
309             if (fAttributeWC.weakerProcessContents(baseGroup.fAttributeWC)) {
310                 errorArgs = new Object JavaDoc[]{typeName,
311                                          fAttributeWC.getProcessContentsAsString(),
312                                          baseGroup.fAttributeWC.getProcessContentsAsString(),
313                                          "derivation-ok-restriction.4.3"};
314                 return errorArgs;
315             }
316         }
317
318         return null;
319
320     }
321
322     static final XSAttributeUseImpl[] resize(XSAttributeUseImpl[] oldArray, int newSize) {
323         XSAttributeUseImpl[] newArray = new XSAttributeUseImpl[newSize];
324         System.arraycopy(oldArray, 0, newArray, 0, Math.min(oldArray.length, newSize));
325         return newArray;
326     }
327
328     // reset the attribute group declaration
329
public void reset(){
330         fName = null;
331         fTargetNamespace = null;
332         // reset attribute uses
333
for (int i=0;i<fAttrUseNum;i++) {
334             fAttributeUses[i] = null;
335         }
336         fAttrUseNum = 0;
337         fAttributeWC = null;
338         fAnnotation = null;
339         fIDAttrName = null;
340
341     }
342
343     /**
344      * Get the type of the object, i.e ELEMENT_DECLARATION.
345      */

346     public short getType() {
347         return XSConstants.ATTRIBUTE_GROUP;
348     }
349
350     /**
351      * The <code>name</code> of this <code>XSObject</code> depending on the
352      * <code>XSObject</code> type.
353      */

354     public String JavaDoc getName() {
355         return fName;
356     }
357
358     /**
359      * The namespace URI of this node, or <code>null</code> if it is
360      * unspecified. defines how a namespace URI is attached to schema
361      * components.
362      */

363     public String JavaDoc getNamespace() {
364         return fTargetNamespace;
365     }
366
367     /**
368      * {attribute uses} A set of attribute uses.
369      */

370     public XSObjectList getAttributeUses() {
371         if (fAttrUses == null){
372             fAttrUses = new XSObjectListImpl(fAttributeUses, fAttrUseNum);
373         }
374         return fAttrUses;
375     }
376
377     /**
378      * {attribute wildcard} Optional. A wildcard.
379      */

380     public XSWildcard getAttributeWildcard() {
381         return fAttributeWC;
382     }
383
384     /**
385      * Optional. Annotation.
386      */

387     public XSAnnotation getAnnotation() {
388         return fAnnotation;
389     }
390     
391     /**
392      * @see com.sun.org.apache.xerces.internal.xs.XSObject#getNamespaceItem()
393      */

394     public XSNamespaceItem getNamespaceItem() {
395         //REVISIT: implement
396
return null;
397     }
398
399 } // class XSAttributeGroupDecl
400
Popular Tags