KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xerces > impl > xs > XSAttributeGroupDecl


1 /*
2  * Copyright 2001-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;
18
19 import org.apache.xerces.impl.dv.ValidatedInfo;
20 import org.apache.xerces.xs.XSAnnotation;
21 import org.apache.xerces.xs.XSAttributeGroupDefinition;
22 import org.apache.xerces.xs.XSAttributeUse;
23 import org.apache.xerces.xs.XSConstants;
24 import org.apache.xerces.xs.XSNamespaceItem;
25 import org.apache.xerces.xs.XSObjectList;
26 import org.apache.xerces.xs.XSWildcard;
27 import org.apache.xerces.impl.xs.util.XSObjectListImpl;
28
29 /**
30  * The XML representation for an attribute group declaration
31  * schema component is a global <attributeGroup> element information item
32  *
33  * @xerces.internal
34  *
35  * @author Sandy Gao, IBM
36  * @author Rahul Srivastava, Sun Microsystems Inc.
37  *
38  * @version $Id: XSAttributeGroupDecl.java,v 1.18 2004/10/06 15:14:55 mrglavas Exp $
39  */

40 public class XSAttributeGroupDecl implements XSAttributeGroupDefinition {
41
42     // name of the attribute group
43
public String JavaDoc fName = null;
44     // target namespace of the attribute group
45
public String JavaDoc fTargetNamespace = null;
46     // number of attribute uses included by this attribute group
47
int fAttrUseNum = 0;
48     // attribute uses included by this attribute group
49
private static final int INITIAL_SIZE = 5;
50     XSAttributeUseImpl[] fAttributeUses = new XSAttributeUseImpl[INITIAL_SIZE];
51     // attribute wildcard included by this attribute group
52
public XSWildcardDecl fAttributeWC = null;
53     // whether there is an attribute use whose type is or is derived from ID.
54
public String JavaDoc fIDAttrName = null;
55
56     // optional annotation
57
public XSAnnotationImpl fAnnotation;
58     
59     protected XSObjectListImpl fAttrUses = null;
60
61     // add an attribute use
62
// if the type is derived from ID, but there is already another attribute
63
// use of type ID, then return the name of the other attribute use;
64
// otherwise, return null
65
public String JavaDoc addAttributeUse(XSAttributeUseImpl attrUse) {
66
67         if (fAttrUseNum == fAttributeUses.length) {
68             fAttributeUses = resize(fAttributeUses, fAttrUseNum*2);
69         }
70         fAttributeUses[fAttrUseNum++] = attrUse;
71         // if this attribute use is prohibited, then don't check whether it's
72
// of type ID
73
if (attrUse.fUse == SchemaSymbols.USE_PROHIBITED)
74             return null;
75
76         if (attrUse.fAttrDecl.fType.isIDType()) {
77             // if there is already an attribute use of type ID, return it' sname
78
if (fIDAttrName == null)
79                 fIDAttrName = attrUse.fAttrDecl.fName;
80             else
81                 return fIDAttrName;
82         }
83
84         return null;
85     }
86
87     public XSAttributeUse getAttributeUse(String JavaDoc namespace, String JavaDoc name) {
88         for (int i=0; i<fAttrUseNum; i++) {
89             if ( (fAttributeUses[i].fAttrDecl.fTargetNamespace == namespace) &&
90                  (fAttributeUses[i].fAttrDecl.fName == name) )
91                 return fAttributeUses[i];
92         }
93
94         return null;
95     }
96
97     public void removeProhibitedAttrs() {
98         if (fAttrUseNum == 0) return;
99         int pCount = 0;
100         XSAttributeUseImpl[] pUses = new XSAttributeUseImpl[fAttrUseNum];
101         for (int i = 0; i < fAttrUseNum; i++) {
102             if (fAttributeUses[i].fUse == SchemaSymbols.USE_PROHIBITED) {
103                 pCount++;
104                 // we use the entries at the end, so that we can use the
105
// first entries to store non-prohibited attribute uses,
106
// hence avoid creating a new array.
107
pUses[fAttrUseNum-pCount] = fAttributeUses[i];
108             }
109         }
110
111         int newCount = 0;
112         if (pCount > 0) {
113             OUTER: for (int i = 0; i < fAttrUseNum; i++) {
114                 if (fAttributeUses[i].fUse == SchemaSymbols.USE_PROHIBITED)
115                     continue;
116                 for (int j = 1; j <= pCount; j++) {
117                     if (fAttributeUses[i].fAttrDecl.fName == pUses[fAttrUseNum-pCount].fAttrDecl.fName &&
118                         fAttributeUses[i].fAttrDecl.fTargetNamespace == pUses[fAttrUseNum-pCount].fAttrDecl.fTargetNamespace) {
119                         continue OUTER;
120                     }
121                 }
122                 pUses[newCount++] = fAttributeUses[i];
123             }
124             fAttributeUses = pUses;
125             fAttrUseNum = newCount;
126         }
127     }
128
129     /**
130      * Check that the attributes in this group validly restrict those from a base group.
131      * If an error is found, an Object[] is returned. This contains the arguments for the error message
132      * describing the error. The last element in the array (at index arr.length - 1) is the the error code.
133      * Returns null if there is no error.
134      *
135      * REVISIT: is there a better way of returning the appropriate information for the error?
136      *
137      * @param typeName the name of the type containing this attribute group, used for error reporting purposes
138      * @param baseGroup the XSAttributeGroupDecl that is the base we are checking against
139      */

140     public Object JavaDoc[] validRestrictionOf(String JavaDoc typeName, XSAttributeGroupDecl baseGroup) {
141
142         Object JavaDoc[] errorArgs = null;
143         XSAttributeUseImpl attrUse = null;
144         XSAttributeDecl attrDecl = null;
145         XSAttributeUseImpl baseAttrUse = null;
146         XSAttributeDecl baseAttrDecl = null;
147
148         for (int i=0; i<fAttrUseNum; i++) {
149
150             attrUse = fAttributeUses[i];
151             attrDecl = attrUse.fAttrDecl;
152
153             // Look for a match in the base
154
baseAttrUse = (XSAttributeUseImpl)baseGroup.getAttributeUse(attrDecl.fTargetNamespace,attrDecl.fName);
155             if (baseAttrUse != null) {
156                 //
157
// derivation-ok-restriction. Constraint 2.1.1
158
//
159

160                 if (baseAttrUse.getRequired() && !attrUse.getRequired()) {
161                     errorArgs = new Object JavaDoc[]{typeName, attrDecl.fName,
162                                              attrUse.fUse == SchemaSymbols.USE_OPTIONAL ? SchemaSymbols.ATTVAL_OPTIONAL : SchemaSymbols.ATTVAL_PROHIBITED,
163                                              "derivation-ok-restriction.2.1.1"};
164                     return errorArgs;
165                 }
166
167                 // if this attribute is prohibited in the derived type, don't
168
// need to check any of the following constraints.
169
if (attrUse.fUse == SchemaSymbols.USE_PROHIBITED) {
170                     continue;
171                 }
172
173                 baseAttrDecl = baseAttrUse.fAttrDecl;
174                 //
175
// derivation-ok-restriction. Constraint 2.1.1
176
//
177
if (! XSConstraints.checkSimpleDerivationOk(attrDecl.fType,
178                                                             baseAttrDecl.fType,
179                                                             baseAttrDecl.fType.getFinal()) ) {
180                     errorArgs = new Object JavaDoc[]{typeName, attrDecl.fName, attrDecl.fType.getName(),
181                                              baseAttrDecl.fType.getName(), "derivation-ok-restriction.2.1.2"};
182                     return errorArgs;
183                 }
184
185
186                 //
187
// derivation-ok-restriction. Constraint 2.1.3
188
//
189
int baseConsType=baseAttrUse.fConstraintType!=XSConstants.VC_NONE?
190                                  baseAttrUse.fConstraintType:baseAttrDecl.getConstraintType();
191                 int thisConstType = attrUse.fConstraintType!=XSConstants.VC_NONE?
192                                     attrUse.fConstraintType:attrDecl.getConstraintType();
193
194                 if (baseConsType == XSConstants.VC_FIXED) {
195
196                     if (thisConstType != XSConstants.VC_FIXED) {
197                         errorArgs = new Object JavaDoc[]{typeName, attrDecl.fName,
198                                                  "derivation-ok-restriction.2.1.3.a"};
199                         return errorArgs;
200                     } else {
201                         // check the values are the same.
202
ValidatedInfo baseFixedValue=(baseAttrUse.fDefault!=null ?
203                                                       baseAttrUse.fDefault: baseAttrDecl.fDefault);
204                         ValidatedInfo thisFixedValue=(attrUse.fDefault!=null ?
205                                                       attrUse.fDefault: attrDecl.fDefault);
206                         if (!baseFixedValue.actualValue.equals(thisFixedValue.actualValue)) {
207                             errorArgs = new Object JavaDoc[]{typeName, attrDecl.fName, thisFixedValue.stringValue(),
208                                                      baseFixedValue.stringValue(), "derivation-ok-restriction.2.1.3.b"};
209                             return errorArgs;
210                         }
211
212                     }
213
214                 }
215             } else {
216                 // No matching attribute in base - there should be a matching wildcard
217

218                 //
219
// derivation-ok-restriction. Constraint 2.2
220
//
221
if (baseGroup.fAttributeWC == null) {
222                     errorArgs = new Object JavaDoc[]{typeName, attrDecl.fName,
223                                              "derivation-ok-restriction.2.2.a"};
224                     return errorArgs;
225                 }
226                 else if (!baseGroup.fAttributeWC.allowNamespace(attrDecl.fTargetNamespace)) {
227                     errorArgs = new Object JavaDoc[]{typeName, attrDecl.fName,
228                                              attrDecl.fTargetNamespace==null?"":attrDecl.fTargetNamespace,
229                                              "derivation-ok-restriction.2.2.b"};
230                     return errorArgs;
231                 }
232             }
233         }
234
235         //
236
// Check that any REQUIRED attributes in the base have matching attributes
237
// in this group
238
// derivation-ok-restriction. Constraint 3
239
//
240
for (int i=0; i<baseGroup.fAttrUseNum; i++) {
241
242             baseAttrUse = baseGroup.fAttributeUses[i];
243
244             if (baseAttrUse.fUse == SchemaSymbols.USE_REQUIRED) {
245
246                 baseAttrDecl = baseAttrUse.fAttrDecl;
247                 // Look for a match in this group
248
if (getAttributeUse(baseAttrDecl.fTargetNamespace,baseAttrDecl.fName) == null) {
249                     errorArgs = new Object JavaDoc[]{typeName, baseAttrUse.fAttrDecl.fName,
250                                              "derivation-ok-restriction.3"};
251                     return errorArgs;
252                 }
253             }
254         }
255
256
257         // Now, check wildcards
258
//
259
// derivation-ok-restriction. Constraint 4
260
//
261
if (fAttributeWC != null) {
262             if (baseGroup.fAttributeWC == null) {
263                 errorArgs = new Object JavaDoc[]{typeName, "derivation-ok-restriction.4.1"};
264                 return errorArgs;
265             }
266             if (! fAttributeWC.isSubsetOf(baseGroup.fAttributeWC)) {
267                 errorArgs = new Object JavaDoc[]{typeName, "derivation-ok-restriction.4.2"};
268                 return errorArgs;
269             }
270             if (fAttributeWC.weakerProcessContents(baseGroup.fAttributeWC)) {
271                 errorArgs = new Object JavaDoc[]{typeName,
272                                          fAttributeWC.getProcessContentsAsString(),
273                                          baseGroup.fAttributeWC.getProcessContentsAsString(),
274                                          "derivation-ok-restriction.4.3"};
275                 return errorArgs;
276             }
277         }
278
279         return null;
280
281     }
282
283     static final XSAttributeUseImpl[] resize(XSAttributeUseImpl[] oldArray, int newSize) {
284         XSAttributeUseImpl[] newArray = new XSAttributeUseImpl[newSize];
285         System.arraycopy(oldArray, 0, newArray, 0, Math.min(oldArray.length, newSize));
286         return newArray;
287     }
288
289     // reset the attribute group declaration
290
public void reset(){
291         fName = null;
292         fTargetNamespace = null;
293         // reset attribute uses
294
for (int i=0;i<fAttrUseNum;i++) {
295             fAttributeUses[i] = null;
296         }
297         fAttrUseNum = 0;
298         fAttributeWC = null;
299         fAnnotation = null;
300         fIDAttrName = null;
301
302     }
303
304     /**
305      * Get the type of the object, i.e ELEMENT_DECLARATION.
306      */

307     public short getType() {
308         return XSConstants.ATTRIBUTE_GROUP;
309     }
310
311     /**
312      * The <code>name</code> of this <code>XSObject</code> depending on the
313      * <code>XSObject</code> type.
314      */

315     public String JavaDoc getName() {
316         return fName;
317     }
318
319     /**
320      * The namespace URI of this node, or <code>null</code> if it is
321      * unspecified. defines how a namespace URI is attached to schema
322      * components.
323      */

324     public String JavaDoc getNamespace() {
325         return fTargetNamespace;
326     }
327
328     /**
329      * {attribute uses} A set of attribute uses.
330      */

331     public XSObjectList getAttributeUses() {
332         if (fAttrUses == null){
333             fAttrUses = new XSObjectListImpl(fAttributeUses, fAttrUseNum);
334         }
335         return fAttrUses;
336     }
337
338     /**
339      * {attribute wildcard} Optional. A wildcard.
340      */

341     public XSWildcard getAttributeWildcard() {
342         return fAttributeWC;
343     }
344
345     /**
346      * Optional. Annotation.
347      */

348     public XSAnnotation getAnnotation() {
349         return fAnnotation;
350     }
351     
352     /**
353      * @see org.apache.xerces.xs.XSObject#getNamespaceItem()
354      */

355     public XSNamespaceItem getNamespaceItem() {
356         //REVISIT: implement
357
return null;
358     }
359
360 } // class XSAttributeGroupDecl
361
Popular Tags