2
* The Apache Software License, Version 1.1
5
* Copyright (c) 2001-2003 The Apache Software Foundation. All rights
8
* Redistribution and use in source and binary forms, with or without
9
* modification, are permitted provided that the following conditions
12
* 1. Redistributions of source code must retain the above copyright
13
* notice, this list of conditions and the following disclaimer.
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
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.
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.
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.
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
48
* ====================================================================
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/>.
58
package org.apache.xerces.impl.xs;
60
import org.apache.xerces.impl.dv.ValidatedInfo;
61
import org.apache.xerces.xs.XSAnnotation;
62
import org.apache.xerces.xs.XSAttributeGroupDefinition;
63
import org.apache.xerces.xs.XSAttributeUse;
64
import org.apache.xerces.xs.XSConstants;
65
import org.apache.xerces.xs.XSNamespaceItem;
66
import org.apache.xerces.xs.XSObjectList;
67
import org.apache.xerces.xs.XSWildcard;
68
import org.apache.xerces.impl.xs.util.XSObjectListImpl;
71
* The XML representation for an attribute group declaration
72
* schema component is a global <attributeGroup> element information item
74
* @author Sandy Gao, IBM
75
* @author Rahul Srivastava, Sun Microsystems Inc.
77
* @version $Id: XSAttributeGroupDecl.java,v 1.16 2003/11/11 20:14:58 sandygao Exp $
79
public class XSAttributeGroupDecl implements XSAttributeGroupDefinition {
81
// name of the attribute group
82
public String fName = null;
83
// target namespace of the attribute group
84
public String fTargetNamespace = null;
85
// number of attribute uses included by this attribute group
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 fIDAttrName = null;
95
// optional annotation
96
public XSAnnotationImpl fAnnotation;
98
protected XSObjectListImpl fAttrUses = null;
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 addAttributeUse(XSAttributeUseImpl attrUse) {
106
if (fAttrUseNum == fAttributeUses.length) {
107
fAttributeUses = resize(fAttributeUses, fAttrUseNum*2);
109
fAttributeUses[fAttrUseNum++] = attrUse;
110
// if this attribute use is prohibited, then don't check whether it's
112
if (attrUse.fUse == SchemaSymbols.USE_PROHIBITED)
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;
126
public XSAttributeUse getAttributeUse(String namespace, String 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];
136
public void removeProhibitedAttrs() {
137
if (fAttrUseNum == 0) return;
139
XSAttributeUseImpl[] pUses = new XSAttributeUseImpl[fAttrUseNum];
140
for (int i = 0; i < fAttrUseNum; i++) {
141
if (fAttributeUses[i].fUse == SchemaSymbols.USE_PROHIBITED) {
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];
152
OUTER: for (int i = 0; i < fAttrUseNum; i++) {
153
if (fAttributeUses[i].fUse == SchemaSymbols.USE_PROHIBITED)
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) {
161
pUses[newCount++] = fAttributeUses[i];
163
fAttributeUses = pUses;
164
fAttrUseNum = newCount;
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.
174
* REVISIT: is there a better way of returning the appropriate information for the error?
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
179
public Object[] validRestrictionOf(String typeName, XSAttributeGroupDecl baseGroup) {
181
Object[] errorArgs = null;
182
XSAttributeUseImpl attrUse = null;
183
XSAttributeDecl attrDecl = null;
184
XSAttributeUseImpl baseAttrUse = null;
185
XSAttributeDecl baseAttrDecl = null;
187
for (int i=0; i<fAttrUseNum; i++) {
189
attrUse = fAttributeUses[i];
190
attrDecl = attrUse.fAttrDecl;
192
// Look for a match in the base
193
baseAttrUse = (XSAttributeUseImpl)baseGroup.getAttributeUse(attrDecl.fTargetNamespace,attrDecl.fName);
194
if (baseAttrUse != null) {
196
// derivation-ok-restriction. Constraint 2.1.1
199
if (baseAttrUse.getRequired() && !attrUse.getRequired()) {
200
errorArgs = new Object[]{typeName, attrDecl.fName,
201
attrUse.fUse == SchemaSymbols.USE_OPTIONAL ? SchemaSymbols.ATTVAL_OPTIONAL : SchemaSymbols.ATTVAL_PROHIBITED,
202
"derivation-ok-restriction.2.1.1"};
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) {
212
baseAttrDecl = baseAttrUse.fAttrDecl;
214
// derivation-ok-restriction. Constraint 2.1.1
216
if (! XSConstraints.checkSimpleDerivationOk(attrDecl.fType,
218
baseAttrDecl.fType.getFinal()) ) {
219
errorArgs = new Object[]{typeName, attrDecl.fName, attrDecl.fType.getName(),
220
baseAttrDecl.fType.getName(), "derivation-ok-restriction.2.1.2"};
226
// derivation-ok-restriction. Constraint 2.1.3
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();
233
if (baseConsType == XSConstants.VC_FIXED) {
235
if (thisConstType != XSConstants.VC_FIXED) {
236
errorArgs = new Object[]{typeName, attrDecl.fName,
237
"derivation-ok-restriction.2.1.3.a"};
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[]{typeName, attrDecl.fName, thisFixedValue.stringValue(),
247
baseFixedValue.stringValue(), "derivation-ok-restriction.2.1.3.b"};
255
// No matching attribute in base - there should be a matching wildcard
258
// derivation-ok-restriction. Constraint 2.2
260
if (baseGroup.fAttributeWC == null) {
261
errorArgs = new Object[]{typeName, attrDecl.fName,
262
"derivation-ok-restriction.2.2.a"};
265
else if (!baseGroup.fAttributeWC.allowNamespace(attrDecl.fTargetNamespace)) {
266
errorArgs = new Object[]{typeName, attrDecl.fName,
267
attrDecl.fTargetNamespace==null?"":attrDecl.fTargetNamespace,
268
"derivation-ok-restriction.2.2.b"};
275
// Check that any REQUIRED attributes in the base have matching attributes
277
// derivation-ok-restriction. Constraint 3
279
for (int i=0; i<baseGroup.fAttrUseNum; i++) {
281
baseAttrUse = baseGroup.fAttributeUses[i];
283
if (baseAttrUse.fUse == SchemaSymbols.USE_REQUIRED) {
285
baseAttrDecl = baseAttrUse.fAttrDecl;
286
// Look for a match in this group
287
if (getAttributeUse(baseAttrDecl.fTargetNamespace,baseAttrDecl.fName) == null) {
288
errorArgs = new Object[]{typeName, baseAttrUse.fAttrDecl.fName,
289
"derivation-ok-restriction.3"};
296
// Now, check wildcards
298
// derivation-ok-restriction. Constraint 4
300
if (fAttributeWC != null) {
301
if (baseGroup.fAttributeWC == null) {
302
errorArgs = new Object[]{typeName, "derivation-ok-restriction.4.1"};
305
if (! fAttributeWC.isSubsetOf(baseGroup.fAttributeWC)) {
306
errorArgs = new Object[]{typeName, "derivation-ok-restriction.4.2"};
309
if (fAttributeWC.weakerProcessContents(baseGroup.fAttributeWC)) {
310
errorArgs = new Object[]{typeName,
311
fAttributeWC.getProcessContentsAsString(),
312
baseGroup.fAttributeWC.getProcessContentsAsString(),
313
"derivation-ok-restriction.4.3"};
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));
328
// reset the attribute group declaration
331
fTargetNamespace = null;
332
// reset attribute uses
333
for (int i=0;i<fAttrUseNum;i++) {
334
fAttributeUses[i] = null;
344
* Get the type of the object, i.e ELEMENT_DECLARATION.
346
public short getType() {
347
return XSConstants.ATTRIBUTE_GROUP;
351
* The <code>name</code> of this <code>XSObject</code> depending on the
352
* <code>XSObject</code> type.
354
public String getName() {
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
363
public String getNamespace() {
364
return fTargetNamespace;
368
* {attribute uses} A set of attribute uses.
370
public XSObjectList getAttributeUses() {
371
if (fAttrUses == null){
372
fAttrUses = new XSObjectListImpl(fAttributeUses, fAttrUseNum);
378
* {attribute wildcard} Optional. A wildcard.
380
public XSWildcard getAttributeWildcard() {
385
* Optional. Annotation.
387
public XSAnnotation getAnnotation() {
392
* @see org.apache.xerces.xs.XSObject#getNamespaceItem()
394
public XSNamespaceItem getNamespaceItem() {
399
} // class XSAttributeGroupDecl