001 //$HeadURL: svn+ssh://rbezema@svn.wald.intevation.org/deegree/base/branches/2.2_testing/src/org/deegree/framework/xml/schema/ElementDeclaration.java $
002 /*---------------- FILE HEADER ------------------------------------------
003
004 This file is part of deegree.
005 Copyright (C) 2001-2008 by:
006 EXSE, Department of Geography, University of Bonn
007 http://www.giub.uni-bonn.de/deegree/
008 lat/lon GmbH
009 http://www.lat-lon.de
010
011 This library is free software; you can redistribute it and/or
012 modify it under the terms of the GNU Lesser General Public
013 License as published by the Free Software Foundation; either
014 version 2.1 of the License, or (at your option) any later version.
015
016 This library is distributed in the hope that it will be useful,
017 but WITHOUT ANY WARRANTY; without even the implied warranty of
018 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
019 Lesser General Public License for more details.
020
021 You should have received a copy of the GNU Lesser General Public
022 License along with this library; if not, write to the Free Software
023 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
024
025 Contact:
026
027 Andreas Poth
028 lat/lon GmbH
029 Aennchenstraße 19
030 53177 Bonn
031 Germany
032 E-Mail: poth@lat-lon.de
033
034 Prof. Dr. Klaus Greve
035 Department of Geography
036 University of Bonn
037 Meckenheimer Allee 166
038 53115 Bonn
039 Germany
040 E-Mail: greve@giub.uni-bonn.de
041
042 ---------------------------------------------------------------------------*/
043 package org.deegree.framework.xml.schema;
044
045 import org.deegree.datatypes.QualifiedName;
046
047 /**
048 * Represents an XML element declaration in an {@link XMLSchema}.
049 *
050 * @author <a href="mailto:schneider@lat-lon.de">Markus Schneider </a>
051 * @author last edited by: $Author: apoth $
052 *
053 * @version $Revision: 9339 $, $Date: 2007-12-27 13:31:52 +0100 (Do, 27 Dez 2007) $
054 */
055 public class ElementDeclaration {
056
057 private QualifiedName name;
058
059 private boolean isAbstract;
060
061 private TypeReference type;
062
063 private int minOccurs;
064
065 private int maxOccurs;
066
067 private ElementReference substitutionGroup;
068
069 /**
070 * Creates a new <code>ElementDeclaration</code> instance from the given parameters.
071 *
072 * @param name
073 * @param isAbstract
074 * @param type
075 * @param minOccurs
076 * @param maxOccurs
077 * @param substitutionGroup
078 */
079 public ElementDeclaration( QualifiedName name, boolean isAbstract, TypeReference type, int minOccurs, int maxOccurs,
080 QualifiedName substitutionGroup ) {
081 this.name = name;
082 this.isAbstract = isAbstract;
083 this.type = type;
084 this.minOccurs = minOccurs;
085 this.maxOccurs = maxOccurs;
086 if ( substitutionGroup != null ) {
087 this.substitutionGroup = new ElementReference( substitutionGroup );
088 }
089 }
090
091 /**
092 * Returns the qualified name of the declared XML element.
093 *
094 * @return the qualified name of the declared XML element
095 */
096 public QualifiedName getName() {
097 return this.name;
098 }
099
100 /**
101 * Returns whether the element is declared abstract.
102 *
103 * @return true, if the element is abstract, false otherwise
104 */
105 public boolean isAbstract() {
106 return this.isAbstract;
107 }
108
109 /**
110 * Returns a {@link TypeReference} that describes the content of the element.
111 *
112 * @return a TypeReference that describes the content of the element
113 */
114 public TypeReference getType() {
115 return this.type;
116 }
117
118 /**
119 * Returns the minimum number of occurences of the element.
120 *
121 * @return the minimum number of occurences of the element, -1 if it is unconstrained
122 */
123 public int getMinOccurs() {
124 return this.minOccurs;
125 }
126
127 /**
128 * Returns the maximum number of occurences of the element.
129 *
130 * @return the maximum number of occurences of the element, -1 if it is unconstrained
131 */
132 public int getMaxOccurs() {
133 return this.maxOccurs;
134 }
135
136 /**
137 * Returns an {@link ElementReference} which the element may be substituted for.
138 *
139 * @return an ElementReference which the element may be substituted for
140 */
141 public ElementReference getSubstitutionGroup() {
142 return this.substitutionGroup;
143 }
144
145 /**
146 * Returns whether this element is substitutable for the given element name.
147 * <p>
148 * This is true if the substitutionGroup equals the given element name, or if an element that
149 * this element is substitutable for may be substituted for the given element name.
150 *
151 * @param substitutionName
152 * @return true, if this element declaration is a valid substiution for elements with the
153 * given name
154 */
155 public boolean isSubstitutionFor( QualifiedName substitutionName ) {
156 if ( this.name.equals( substitutionName ) ) {
157 return true;
158 }
159 if ( this.substitutionGroup == null ) {
160 return false;
161 }
162 if ( this.substitutionGroup.getElementDeclaration() == null ) {
163 return this.substitutionGroup.getName().equals( substitutionName );
164 }
165 return this.substitutionGroup.getElementDeclaration().isSubstitutionFor( substitutionName );
166 }
167
168 /**
169 * Returns a string representation of the object (indented for better readablity,
170 * as this is part of a hierarchical structure).
171 *
172 * @param indent
173 * current indentation (as a whitespace string)
174 * @return an indented string representation of the object
175 */
176 public String toString( String indent ) {
177 StringBuffer sb = new StringBuffer();
178 sb.append( indent );
179 sb.append( "- element" );
180 if ( this.name != null ) {
181 sb.append( " name=\"" );
182 sb.append( this.name.getLocalName() );
183 sb.append( "\"" );
184 }
185 if ( !this.type.isAnonymous() ) {
186 sb.append( " type=\"" );
187 sb.append( this.type.getName() );
188 sb.append( "\"" );
189 } else {
190 sb.append( " anonymous type" );
191 }
192 if ( this.substitutionGroup != null ) {
193 sb.append( " substitutionGroup=\"" );
194 sb.append( this.substitutionGroup.getName() );
195 sb.append( "\"" );
196 }
197 sb.append( "\n" );
198 if ( this.type.isAnonymous() ) {
199 sb.append( this.type.getTypeDeclaration().toString( indent
200 + " " ) );
201 }
202 return sb.toString();
203 }
204 }