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