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