View Javadoc
1   /**
2    * Redistribution and use of this software and associated documentation ("Software"), with or
3    * without modification, are permitted provided that the following conditions are met:
4    *
5    * 1. Redistributions of source code must retain copyright statements and notices. Redistributions
6    * must also contain a copy of this document.
7    *
8    * 2. Redistributions in binary form must reproduce the above copyright notice, this list of
9    * conditions and the following disclaimer in the documentation and/or other materials provided with
10   * the distribution.
11   *
12   * 3. The name "Exolab" must not be used to endorse or promote products derived from this Software
13   * without prior written permission of Intalio, Inc. For written permission, please contact
14   * info@exolab.org.
15   *
16   * 4. Products derived from this Software may not be called "Exolab" nor may "Exolab" appear in
17   * their names without prior written permission of Intalio, Inc. Exolab is a registered trademark of
18   * Intalio, Inc.
19   *
20   * 5. Due credit should be given to the Exolab Project (http://www.exolab.org/).
21   *
22   * THIS SOFTWARE IS PROVIDED BY INTALIO, INC. AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESSED OR
23   * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
24   * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL INTALIO, INC. OR ITS
25   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27   * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28   * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
29   * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30   *
31   * Copyright 2000 (C) Intalio, Inc. All Rights Reserved.
32   *
33   * $Id$
34   */
35  
36  
37  package org.exolab.castor.xml.schema;
38  
39  import java.util.Enumeration;
40  
41  /**
42   * An XML Schema Attribute Group Definition
43   * 
44   * @author <a href="mailto:kvisco@intalio.com">Keith Visco</a>
45   * @version $Revision$ $Date: 2006-04-14 04:14:43 -0600 (Fri, 14 Apr 2006) $
46   **/
47  public final class AttributeGroupReference extends AttributeGroup {
48    /** SerialVersionUID */
49    private static final long serialVersionUID = -6283626049554689747L;
50  
51    /**
52     * Error message for a null argument
53     **/
54    private static String NULL_ARGUMENT =
55        "A null argument was passed to the constructor of " + "AttributeGroupReference";
56  
57    /**
58     * The Schema to which this AttributeDecl belongs
59     **/
60    private Schema _schema = null;
61  
62    private String _reference = null;
63  
64    /**
65     * Creates a new AttributeGroup definition
66     * 
67     * @param schema the Schema that this AttributeGroup belongs to.
68     **/
69    public AttributeGroupReference(Schema schema, String reference) {
70      if (schema == null) {
71        String err = NULL_ARGUMENT + "; 'schema' must not be null.";
72        throw new IllegalArgumentException(err);
73      }
74      if (reference == null) {
75        String err = NULL_ARGUMENT + "; 'reference' must not be null.";
76        throw new IllegalArgumentException(err);
77      }
78      _schema = schema;
79      _reference = reference;
80    } // -- AttributeGroup
81  
82    /**
83     * Returns the anyAttribute set in this attribute group if any.
84     * 
85     * @return the anyAttribute set in this attribute group if any.
86     */
87    public Wildcard getAnyAttribute() {
88      return resolveReference().getAnyAttribute();
89    }
90  
91  
92    /**
93     * Gets the name of the attribute group this class refers to.
94     */
95    public String getReference() {
96      return _reference;
97    }
98  
99    /**
100    * Resolves the attribute group reference
101    * 
102    * @return the attribute group defined at the schema level that is refered to by this class.
103    */
104   public AttributeGroup resolveReference() {
105     AttributeGroup attrGroup = null;
106     // --check if there is no definition in the
107     // --master schema
108     if (_schema.getMasterSchema() != null) {
109       attrGroup = _schema.getMasterSchema().getAttributeGroup(_reference);
110     }
111 
112     if (attrGroup == null)
113       attrGroup = _schema.getAttributeGroup(_reference);
114 
115     if (attrGroup == null) {
116       throw new IllegalStateException("Invalid AttributeGroupReference");
117     }
118     return attrGroup;
119   }
120 
121   /**
122    * Returns the AttributeDecl associated with the given name
123    * 
124    * @return the AttributeDecl associated with the given name, or null if no AttributeDecl with the
125    *         given name was found.
126    **/
127   public AttributeDecl getAttribute(String name) {
128     return resolveReference().getAttribute(name);
129 
130   } // -- getAttribute
131 
132 
133   /**
134    * Returns an enumeration of the AttributeDecls and AttributeGroups of this AttributeGroup
135    *
136    * @return an Enumeration of the AttributeDecls and AttributeGroups of this AttributeGroup
137    **/
138   public Enumeration<AttributeDecl> getAttributes() {
139     return resolveReference().getAttributes();
140   } // -- getAttributes
141 
142   /**
143    * Returns true if this AttributeGroup does not contain any AttributeDecls or any non-empty
144    * AttributeGroups
145    *
146    * @return true if this AttributeGroup does not contain any AttributeDecls or any non-empty
147    *         AttributeGroups
148    **/
149   public boolean isEmpty() {
150     return resolveReference().isEmpty();
151 
152   } // -- isEmpty
153 
154 } // -- AttributeGroupReference