001    //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/branches/2.3_testing/src/org/deegree/framework/xml/schema/ComplexTypeDeclaration.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 java.util.LinkedHashSet;
039    
040    import org.deegree.datatypes.QualifiedName;
041    
042    /**
043     * Represents an XML complex type declaration in an {@link XMLSchema}.
044     * <p>
045     * The following limitations apply:
046     * <ul>
047     * <li>the type may be defined using 'extension', but must not use 'restriction'</li>
048     * <li>the content model must be a sequence</li>
049     * </ul>
050     * </p>
051     *
052     * @author <a href="mailto:schneider@lat-lon.de">Markus Schneider </a>
053     * @author last edited by: $Author: mschneider $
054     *
055     * @version $Revision: 18195 $, $Date: 2009-06-18 17:55:39 +0200 (Do, 18. Jun 2009) $
056     */
057    public class ComplexTypeDeclaration implements TypeDeclaration {
058    
059        private QualifiedName name;
060    
061        private TypeReference extensionBaseType;
062    
063        private ElementDeclaration[] subElements;
064    
065        /**
066         * Creates a new <code>ComplexTypeDeclaration</code> instance from the given parameters.
067         *
068         * @param name
069         * @param extensionBaseType
070         * @param subElements
071         */
072        public ComplexTypeDeclaration( QualifiedName name, TypeReference extensionBaseType,
073                                      ElementDeclaration[] subElements ) {
074            this.name = name;
075            this.extensionBaseType = extensionBaseType;
076            this.subElements = subElements;
077        }
078    
079        /**
080         * Returns the qualified name of the declared XML type.
081         *
082         * @return the qualified name of the declared XML type
083         */
084        public QualifiedName getName() {
085            return this.name;
086        }
087    
088        /**
089         * Returns a {@link TypeReference} to the XML type that this complex type extends.
090         *
091         * @return a TypeReference to the XML type that this complex type extends
092         */
093        public TypeReference getExtensionBaseType() {
094            return this.extensionBaseType;
095        }
096    
097        /**
098         * Returns the {@link ElementDeclaration}s that this {@link ComplexTypeDeclaration}
099         * contains, but not the ones that are inherited (from the extended type).
100         *
101         * @return the explicit ElementDeclarations in this ComplexTypeDeclaration
102         */
103        public ElementDeclaration[] getExplicitElements() {
104            return this.subElements;
105        }
106    
107        /**
108         * Returns the {@link ElementDeclaration}s in this {@link ComplexTypeDeclaration}
109         * contains, this includes the ones that are inherited (from the extended type).
110         *
111         * @return the explicit+implicit ElementDeclarations in this ComplexTypeDeclaration
112         */
113        public ElementDeclaration[] getElements() {
114            LinkedHashSet<ElementDeclaration> allElementSet = new LinkedHashSet<ElementDeclaration>();
115            addElements( allElementSet );
116            return allElementSet.toArray( new ElementDeclaration[allElementSet.size()] );
117        }
118    
119        /**
120         * Returns a string representation of the object.
121         *
122         * @return a string representation of the object
123         */
124        @Override
125        public String toString() {
126            return toString( "" );
127        }
128    
129        /**
130         * Returns a string representation of the object (indented for better readablity,
131         * as this is a hierarchical structure).
132         *
133         * @param indent
134         *             current indentation (as a whitespace string)
135         * @return an indented string representation of the object
136         */
137        public String toString( String indent ) {
138            StringBuffer sb = new StringBuffer();
139            sb.append( indent );
140            sb.append( "- complexType" );
141            if ( name != null ) {
142                sb.append( " name=\"" );
143                sb.append( this.name );
144                sb.append( "\"" );
145            }
146            if ( this.extensionBaseType != null ) {
147                sb.append( ", extension base=\"" );
148                sb.append( this.extensionBaseType.getName() );
149                sb.append( "\"" );
150            }
151            sb.append( "\n" );
152            for (int i = 0; i < subElements.length; i++) {
153                sb.append( subElements[i].toString( indent
154                    + "  " ) );
155            }
156            return sb.toString();
157        }
158    
159        /**
160         * Recursively collects all <code>ElementDeclaration</code>s that this
161         * <code>ComplexType</code> has.
162         * <p>
163         * Respects order and scope (overwriting) of <code>ElementDeclaration</code>s.
164         *
165         * @param elementSet
166         *            the inherited (and own) elements are added to this LinkedHashSet
167         */
168        private void addElements( LinkedHashSet<ElementDeclaration> elementSet ) {
169            if ( this.extensionBaseType != null
170                && this.extensionBaseType.getTypeDeclaration() != null ) {
171                ( (ComplexTypeDeclaration) this.extensionBaseType.getTypeDeclaration() )
172                    .addElements( elementSet );
173            }
174            for (int i = 0; i < subElements.length; i++) {
175                elementSet.add( this.subElements[i] );
176            }
177        }
178    }