001: /*
002: * The Apache Software License, Version 1.1
003: *
004: *
005: * Copyright (c) 2002 The Apache Software Foundation. All rights
006: * reserved.
007: *
008: * Redistribution and use in source and binary forms, with or without
009: * modification, are permitted provided that the following conditions
010: * are met:
011: *
012: * 1. Redistributions of source code must retain the above copyright
013: * notice, this list of conditions and the following disclaimer.
014: *
015: * 2. Redistributions in binary form must reproduce the above copyright
016: * notice, this list of conditions and the following disclaimer in
017: * the documentation and/or other materials provided with the
018: * distribution.
019: *
020: * 3. The end-user documentation included with the redistribution,
021: * if any, must include the following acknowledgment:
022: * "This product includes software developed by the
023: * Apache Software Foundation (http://www.apache.org/)."
024: * Alternately, this acknowledgment may appear in the software itself,
025: * if and wherever such third-party acknowledgments normally appear.
026: *
027: * 4. The names "WSIF" and "Apache Software Foundation" must
028: * not be used to endorse or promote products derived from this
029: * software without prior written permission. For written
030: * permission, please contact apache@apache.org.
031: *
032: * 5. Products derived from this software may not be called "Apache",
033: * nor may "Apache" appear in their name, without prior written
034: * permission of the Apache Software Foundation.
035: *
036: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
037: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
038: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
039: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
040: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
041: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
042: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
043: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
044: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
045: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
046: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
047: * SUCH DAMAGE.
048: * ====================================================================
049: *
050: * This software consists of voluntary contributions made by many
051: * individuals on behalf of the Apache Software Foundation and was
052: * originally based on software copyright (c) 2001, 2002, International
053: * Business Machines, Inc., http://www.apache.org. For more
054: * information on the Apache Software Foundation, please see
055: * <http://www.apache.org/>.
056: */
057:
058: package org.apache.wsif.schema;
059:
060: import java.io.Serializable;
061: import java.util.ArrayList;
062: import java.util.Vector;
063:
064: import javax.xml.namespace.QName;
065:
066: import org.apache.wsif.WSIFConstants;
067: import org.w3c.dom.Element;
068: import org.w3c.dom.Node;
069: import org.w3c.dom.NodeList;
070:
071: /**
072: * A class to represent a <complexType> element in a schema
073: *
074: * @author Owen Burroughs <owenb@apache.org>
075: */
076: public class ComplexType extends SchemaType implements Serializable {
077:
078: static final long serialVersionUID = 1L;
079:
080: private boolean isAnArray = false;
081: private String name = "";
082: private QName typeName = null;
083: private QName arrayType = null;
084: private int arrayDim = 0;
085: private ComplexContent complexContent = null;
086: private static final QName soapEncArray = new QName(
087: WSIFConstants.NS_URI_SOAP_ENC, "Array");
088: private static final QName soapEncArrayType = new QName(
089: WSIFConstants.NS_URI_SOAP_ENC, "arrayType");
090: private static final QName wsdlArrayType = new QName(
091: WSIFConstants.NS_URI_WSDL, "arrayType");
092: ArrayList sequenceElements = new ArrayList();
093:
094: /**
095: * Constructor
096: * @param el The dom element for this complexType
097: */
098: ComplexType(Element el, String tns) {
099: typeName = getAttributeQName(el, "name", tns);
100: if (typeName != null) {
101: name = typeName.getLocalPart();
102: }
103:
104: process(el, tns);
105:
106: if (name.startsWith("ArrayOf")) {
107: if (complexContent != null) {
108: Restriction res = complexContent.getRestriction();
109: if (res != null) {
110: QName base = res.getBase();
111: if (soapEncArray.equals(base)) {
112: Attribute[] atts = res.getAttributes();
113: if (atts != null && atts.length > 0) {
114: for (int i = 0; i < atts.length; i++) {
115: Attribute a = atts[i];
116: if (a != null) {
117: QName ref = a
118: .getXMLAttribute("ref");
119: if (soapEncArrayType.equals(ref)) {
120: QName tempType = a
121: .getXMLAttribute(wsdlArrayType);
122: if (tempType != null) {
123: String ns = tempType
124: .getNamespaceURI();
125: String lp = tempType
126: .getLocalPart();
127: // Work out array dimension
128: int index = lp
129: .lastIndexOf("[]");
130: while (index != -1) {
131: lp = lp.substring(0,
132: index);
133: arrayDim++;
134: index = lp
135: .lastIndexOf("[]");
136: }
137: arrayType = new QName(ns,
138: lp);
139: }
140: break;
141: }
142: }
143: }
144: } else {
145: SequenceElement[] sels = res
146: .getSequenceElements();
147: if (sels != null && sels.length == 1) {
148: SequenceElement sel = sels[0];
149: QName tempType = sel
150: .getXMLAttribute("type");
151: if (tempType != null) {
152: String ns = tempType
153: .getNamespaceURI();
154: String lp = tempType.getLocalPart();
155: arrayType = new QName(ns, lp);
156: }
157: }
158: }
159: }
160: }
161: isAnArray = true;
162: }
163: } else {
164: }
165: }
166:
167: /**
168: * @see SchemaType#isComplex()
169: */
170: public boolean isComplex() {
171: return true;
172: }
173:
174: /**
175: * @see SchemaType#isArray()
176: */
177: public boolean isArray() {
178: return isAnArray;
179: }
180:
181: /**
182: * @see SchemaType#getArrayType()
183: */
184: public QName getArrayType() {
185: return arrayType;
186: }
187:
188: /**
189: * @see SchemaType#getArrayDimension()
190: */
191: public int getArrayDimension() {
192: return arrayDim;
193: }
194:
195: /**
196: * @see SchemaType#getTypeName()
197: */
198: public QName getTypeName() {
199: return typeName;
200: }
201:
202: /**
203: * Get all the <element> elements within a sequence nested in this complexType
204: * @return The <element> elements within the sequnce
205: */
206: public SequenceElement[] getSequenceElements() {
207: return (SequenceElement[]) sequenceElements
208: .toArray(new SequenceElement[sequenceElements.size()]);
209: }
210:
211: private void process(Element el, String tns) {
212: NodeList children = el.getChildNodes();
213: for (int i = 0; i < children.getLength(); i++) {
214: Node child = children.item(i);
215: if (child.getNodeType() == Node.ELEMENT_NODE) {
216: Element subEl = (Element) child;
217: String elType = subEl.getLocalName();
218: if (elType.equals("complexContent")) {
219: complexContent = new ComplexContent(subEl, tns);
220: } else if (elType.equals("sequence")) {
221: parseSequenceElements(subEl, tns);
222: }
223: }
224: }
225: }
226:
227: private void parseSequenceElements(Element el, String tns) {
228: NodeList children = el.getChildNodes();
229: for (int i = 0; i < children.getLength(); i++) {
230: Node child = children.item(i);
231: if (child.getNodeType() == Node.ELEMENT_NODE) {
232: Element subEl = (Element) child;
233: String elType = subEl.getLocalName();
234: if (elType.equals("element")) {
235: sequenceElements
236: .add(new SequenceElement(subEl, tns));
237: }
238: }
239: }
240: }
241: }
|