001: /**
002: * Redistribution and use of this software and associated documentation
003: * ("Software"), with or without modification, are permitted provided
004: * that the following conditions are met:
005: *
006: * 1. Redistributions of source code must retain copyright
007: * statements and notices. Redistributions must also contain a
008: * copy of this document.
009: *
010: * 2. Redistributions in binary form must reproduce the
011: * above copyright notice, this list of conditions and the
012: * following disclaimer in the documentation and/or other
013: * materials provided with the distribution.
014: *
015: * 3. The name "Exolab" must not be used to endorse or promote
016: * products derived from this Software without prior written
017: * permission of Intalio, Inc. For written permission,
018: * please contact info@exolab.org.
019: *
020: * 4. Products derived from this Software may not be called "Exolab"
021: * nor may "Exolab" appear in their names without prior written
022: * permission of Intalio, Inc. Exolab is a registered
023: * trademark of Intalio, Inc.
024: *
025: * 5. Due credit should be given to the Exolab Project
026: * (http://www.exolab.org/).
027: *
028: * THIS SOFTWARE IS PROVIDED BY INTALIO, INC. AND CONTRIBUTORS
029: * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
030: * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
031: * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
032: * INTALIO, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
033: * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
034: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
035: * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
036: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
037: * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
038: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
039: * OF THE POSSIBILITY OF SUCH DAMAGE.
040: *
041: * Copyright 2000-2003 (C) Intalio, Inc. All Rights Reserved.
042: *
043: * $Id: AttributeGroupDecl.java 6230 2006-09-19 07:56:07Z wguttmn $
044: */package org.exolab.castor.xml.schema;
045:
046: import org.exolab.castor.xml.ValidationException;
047:
048: import java.util.Vector;
049: import java.util.Enumeration;
050:
051: /**
052: * An XML Schema Attribute Group Definition
053: * @author <a href="mailto:kvisco@intalio.com">Keith Visco</a>
054: * @version $Revision: 6230 $ $Date: 2006-04-25 15:08:23 -0600 (Tue, 25 Apr 2006) $
055: **/
056: public final class AttributeGroupDecl extends AttributeGroup {
057: /** SerialVersionUID */
058: private static final long serialVersionUID = -5401452412514803353L;
059:
060: /**
061: * Error message for a null argument
062: **/
063: private static String NULL_ARGUMENT = "A null argument was passed to the constructor of "
064: + AttributeDecl.class.getName();
065:
066: /**
067: * The name of this AttributeGroup
068: **/
069: private String _name = null;
070:
071: /**
072: * The Schema to which this AttributeDecl belongs
073: **/
074: private Schema _schema = null;
075:
076: /**
077: * The collection of attributes for this AttributeGroup
078: **/
079: private Vector _attributes = null;
080:
081: /**
082: * The collection of AttributesGroupReferences for this
083: * AttributeGroup
084: **/
085: private Vector _references = null;
086:
087: /**
088: * the anyattribute wilcard, if any
089: */
090: private Wildcard _anyAttribute = null;
091:
092: /**
093: * An attribute that indicates if this AttributeGroupDecl is
094: * a redefinition
095: */
096: private boolean _redefinition = false;
097:
098: /**
099: * Creates a new AttributeGroup definition
100: * @param schema the Schema that this AttributeGroup
101: * belongs to.
102: **/
103: public AttributeGroupDecl(Schema schema) {
104: if (schema == null) {
105: String err = NULL_ARGUMENT + "; 'schema' must not be null.";
106: throw new IllegalArgumentException(err);
107: }
108: _schema = schema;
109: _attributes = new Vector();
110: _references = new Vector();
111: } //-- AttributeGroupDecl
112:
113: /**
114: * Adds the given attribute definition to this AttributeGroup
115: *
116: * @param attrDecl the AttributeDecl to add
117: **/
118: public void addAttribute(AttributeDecl attrDecl) {
119:
120: if (attrDecl == null)
121: return;
122:
123: //-- add validation code
124:
125: //-- add to internal collection
126: _attributes.addElement(attrDecl);
127: //--set the parent
128: attrDecl.setParent(this );
129:
130: } //-- addAttribute
131:
132: /**
133: * Adds the given AttributeGroupReference to this AttributeGroup
134: *
135: * @param attrGroup the AttributeGroupReference to add
136: **/
137: public void addReference(AttributeGroupReference attrGroup) {
138:
139: if (attrGroup == null)
140: return;
141:
142: //-- add validation code
143:
144: //-- add to internal collection
145: _references.addElement(attrGroup);
146:
147: } //-- addReference
148:
149: /**
150: * Returns the attributes of THIS attribute group.
151: * (not those of the nested groups)
152: */
153: public Enumeration getLocalAttributes() {
154: return _attributes.elements();
155: }
156:
157: /**
158: * Returns the AttributeGroupReference of THIS attribute group.
159: * (not those of the nested groups)
160: */
161: public Enumeration getLocalAttributeGroupReferences() {
162: return _references.elements();
163: }
164:
165: /**
166: * Returns the wilcard used in this complexType (can be null)
167: * @return the wilcard used in this complexType (can be null)
168: */
169: public Wildcard getAnyAttribute() {
170: return _anyAttribute;
171: }
172:
173: /**
174: * Returns the AttributeDecl associated with the given name
175: * @return the AttributeDecl associated with the given name, or
176: * null if no AttributeDecl with the given name was found.
177: **/
178: public AttributeDecl getAttribute(String name) {
179:
180: if (name == null)
181: return null;
182:
183: for (int i = 0; i < _attributes.size(); i++) {
184: AttributeDecl attr = (AttributeDecl) _attributes
185: .elementAt(i);
186: if (name.equals(attr.getName()))
187: return attr;
188: }
189:
190: for (int i = 0; i < _references.size(); i++) {
191: AttributeGroupReference ref = (AttributeGroupReference) _references
192: .elementAt(i);
193:
194: AttributeDecl attr = ref.getAttribute(name);
195: if (attr != null)
196: return attr;
197: }
198:
199: return null;
200: } //-- getAttribute
201:
202: /**
203: * Returns an Enumeration of all the attributes of this
204: * attribute group. The enumeration includes attributes from
205: * all AttributeGroupReferences contained in this AttributeGroup.
206: *
207: * @return an Enumeration of all the attributes of this
208: * attribute group.
209: **/
210: public Enumeration getAttributes() {
211: return new AttributeGroupEnumeration(_attributes, _references);
212: } //-- getAttributes
213:
214: /**
215: * Returns the name of this AttributeGroup
216: * @return the name of this AttributeGroup, or null, if
217: * no name was defined.
218: **/
219: public String getName() {
220: return _name;
221: } //-- getName
222:
223: /**
224: * Returns the Schema that this AttributeGroupDecl belongs to.
225: *
226: * @return the Schema that this AttributeGroupDecl belongs to
227: **/
228: public Schema getSchema() {
229: return _schema;
230: } //-- getSchema
231:
232: /**
233: * Returns true if this AttributeGroup does not contain any
234: * AttributeDecls or any non-empty AttributeGroups
235: *
236: * @return true if this AttributeGroup does not contain any
237: * AttributeDecls or any non-empty AttributeGroups
238: **/
239: public boolean isEmpty() {
240:
241: if (_attributes.size() > 0)
242: return false;
243:
244: if (_references.size() == 0)
245: return true;
246:
247: for (int i = 0; i < _references.size(); i++) {
248: if (!((AttributeGroup) _references.elementAt(i)).isEmpty())
249: return false;
250: }
251: return true;
252:
253: } //-- isEmpty
254:
255: /**
256: * Returns true if this attributeGroupDecl is a redefinition.
257: *
258: * @return true if this attributeGroupDecl is a redefinition.
259: */
260: public boolean isRedefined() {
261: return _redefinition;
262: }
263:
264: /**
265: * Removes the given AttributeDecl from this AttributeGroup.
266: * @param attr the attribute to remove.
267: **/
268: public boolean removeAttribute(AttributeDecl attr) {
269: if (attr == null)
270: return false;
271: if (_attributes.contains(attr)) {
272: _attributes.removeElement(attr);
273: return true;
274: }
275: return false;
276: } //-- removeAttribute
277:
278: /**
279: * Removes the given AttributeGroupReference from this AttributeGroup.
280: * @param attrGroupReference the AttributeGroupReference to remove.
281: **/
282: public boolean removeReference(
283: AttributeGroupReference attrGroupReference) {
284: if (attrGroupReference == null)
285: return false;
286: if (_references.contains(attrGroupReference)) {
287: _references.removeElement(attrGroupReference);
288: return true;
289: }
290: return false;
291: } //-- removeReference
292:
293: /**
294: * Sets the wildcard (anyAttribute) of this attribute Group
295: * @exception SchemaException thrown when a wildcard as already be set
296: * or when the wildCard is not an {@literal <anyAttribute>}.
297: */
298: public void setAnyAttribute(Wildcard wildcard)
299: throws SchemaException {
300: if (wildcard != null) {
301: if (_anyAttribute != null) {
302: String err = "<anyAttribute> already set in this AttributeGroup: "
303: + this .getName();
304: throw new SchemaException(err);
305: }
306:
307: if (!wildcard.isAttributeWildcard()) {
308: String err = "In AttributeGroup, " + this .getName()
309: + "the wildcard must be an <anyAttribute>";
310: throw new SchemaException(err);
311: }
312: }
313: _anyAttribute = wildcard;
314:
315: }
316:
317: /**
318: * Sets the name of this AttributeGroup
319: * @param name the name of this AttributeGroup
320: **/
321: public void setName(String name) {
322:
323: if (name == null)
324: throw new IllegalArgumentException("name must not be null");
325:
326: //-- strip namespace prefix if necessary
327: int idx = name.indexOf(':');
328: if (idx >= 0)
329: this ._name = name.substring(idx + 1);
330: else
331: this ._name = name;
332:
333: } //-- setName
334:
335: /**
336: * Sets this Group has redefined.
337: */
338: public void setRedefined() {
339: _redefinition = true;
340: }
341:
342: //-------------------------------/
343: //- Implementation of Structure -/
344: //-------------------------------/
345:
346: /**
347: * Returns the type of this Schema Structure
348: * @return the type of this Schema Structure
349: **/
350: public short getStructureType() {
351: return Structure.ATTRIBUTE_GROUP;
352: } //-- getStructureType
353:
354: /**
355: * Checks the validity of this Attribute declaration
356: * @exception ValidationException when this Attribute declaration
357: * is invalid
358: **/
359: public void validate() throws ValidationException {
360: //-- no validation currently needed
361:
362: } //-- validate
363:
364: } //-- AttributeGroup
365:
366: /**
367: * A simple enumerator for the AttributeGroup class
368: **/
369: class AttributeGroupEnumeration implements Enumeration {
370:
371: private Vector references = null;
372: int index = 0;
373:
374: private Enumeration enumeration = null;
375:
376: AttributeGroupEnumeration(Vector definitions, Vector references) {
377: enumeration = definitions.elements();
378: if (!enumeration.hasMoreElements())
379: enumeration = null;
380: this .references = references;
381: } //-- AttributeGroupEnumeration
382:
383: public boolean hasMoreElements() {
384: if (enumeration != null)
385: return true;
386:
387: int i = index;
388: while (i < references.size()) {
389: AttributeGroupReference ref = (AttributeGroupReference) references
390: .elementAt(i);
391: ++i;
392: if (!ref.isEmpty())
393: return true;
394: }
395: return false;
396:
397: } //-- hasMoreElements
398:
399: public Object nextElement() {
400:
401: if (enumeration != null) {
402: Object obj = enumeration.nextElement();
403: if (!enumeration.hasMoreElements())
404: enumeration = null;
405: return obj;
406: }
407:
408: while (index < references.size()) {
409: AttributeGroupReference ref = (AttributeGroupReference) references
410: .elementAt(index);
411:
412: ++index;
413:
414: enumeration = ref.getAttributes();
415: if (enumeration.hasMoreElements()) {
416: Object obj = enumeration.nextElement();
417: if (!enumeration.hasMoreElements())
418: enumeration = null;
419: return obj;
420: }
421: }
422:
423: return null;
424: }
425: } //-- AttributeGroupEnumeration
|