KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > wsif > schema > ComplexType


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 2002 The Apache Software Foundation. All rights
6  * reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Apache Software Foundation (http://www.apache.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "WSIF" and "Apache Software Foundation" must
28  * not be used to endorse or promote products derived from this
29  * software without prior written permission. For written
30  * permission, please contact apache@apache.org.
31  *
32  * 5. Products derived from this software may not be called "Apache",
33  * nor may "Apache" appear in their name, without prior written
34  * permission of the Apache Software Foundation.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  * ====================================================================
49  *
50  * This software consists of voluntary contributions made by many
51  * individuals on behalf of the Apache Software Foundation and was
52  * originally based on software copyright (c) 2001, 2002, International
53  * Business Machines, Inc., http://www.apache.org. For more
54  * information on the Apache Software Foundation, please see
55  * <http://www.apache.org/>.
56  */

57
58 package org.apache.wsif.schema;
59
60 import java.io.Serializable JavaDoc;
61 import java.util.ArrayList JavaDoc;
62 import java.util.Vector JavaDoc;
63
64 import javax.xml.namespace.QName JavaDoc;
65
66 import org.apache.wsif.WSIFConstants;
67 import org.w3c.dom.Element JavaDoc;
68 import org.w3c.dom.Node JavaDoc;
69 import org.w3c.dom.NodeList JavaDoc;
70
71 /**
72  * A class to represent a &lt;complexType&gt; element in a schema
73  *
74  * @author Owen Burroughs <owenb@apache.org>
75  */

76 public class ComplexType extends SchemaType implements Serializable JavaDoc {
77
78     static final long serialVersionUID = 1L;
79
80     private boolean isAnArray = false;
81     private String JavaDoc name = "";
82     private QName JavaDoc typeName = null;
83     private QName JavaDoc arrayType = null;
84     private int arrayDim = 0;
85     private ComplexContent complexContent = null;
86     private static final QName JavaDoc soapEncArray =
87         new QName JavaDoc(WSIFConstants.NS_URI_SOAP_ENC, "Array");
88     private static final QName JavaDoc soapEncArrayType =
89         new QName JavaDoc(WSIFConstants.NS_URI_SOAP_ENC, "arrayType");
90     private static final QName JavaDoc wsdlArrayType =
91         new QName JavaDoc(WSIFConstants.NS_URI_WSDL, "arrayType");
92     ArrayList JavaDoc sequenceElements = new ArrayList JavaDoc();
93
94     /**
95      * Constructor
96      * @param el The dom element for this complexType
97      */

98     ComplexType(Element JavaDoc el, String JavaDoc tns) {
99         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 JavaDoc 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 JavaDoc ref = a.getXMLAttribute("ref");
118                                     if (soapEncArrayType.equals(ref)) {
119                                         QName JavaDoc tempType =
120                                             a.getXMLAttribute(wsdlArrayType);
121                                         if (tempType != null) {
122                                             String JavaDoc ns =
123                                                 tempType.getNamespaceURI();
124                                             String JavaDoc lp = tempType.getLocalPart();
125                                             // Work out array dimension
126
int index = lp.lastIndexOf("[]");
127                                             while(index != -1) {
128                                                 lp = lp.substring(0, index);
129                                                 arrayDim++;
130                                                 index = lp.lastIndexOf("[]");
131                                             }
132                                             arrayType = new QName JavaDoc(ns, lp);
133                                         }
134                                         break;
135                                     }
136                                 }
137                             }
138                         } else {
139                             SequenceElement[] sels = res.getSequenceElements();
140                             if (sels != null && sels.length == 1) {
141                                 SequenceElement sel = sels[0];
142                                 QName JavaDoc tempType = sel.getXMLAttribute("type");
143                                 if (tempType != null) {
144                                     String JavaDoc ns = tempType.getNamespaceURI();
145                                     String JavaDoc lp = tempType.getLocalPart();
146                                     arrayType = new QName JavaDoc(ns, lp);
147                                 }
148                             }
149                         }
150                     }
151                 }
152                 isAnArray = true;
153             }
154         } else {
155         }
156     }
157
158     /**
159      * @see SchemaType#isComplex()
160      */

161     public boolean isComplex() {
162         return true;
163     }
164
165     /**
166      * @see SchemaType#isArray()
167      */

168     public boolean isArray() {
169         return isAnArray;
170     }
171
172     /**
173      * @see SchemaType#getArrayType()
174      */

175     public QName JavaDoc getArrayType() {
176         return arrayType;
177     }
178
179     /**
180      * @see SchemaType#getArrayDimension()
181      */

182     public int getArrayDimension() {
183         return arrayDim;
184     }
185
186     /**
187      * @see SchemaType#getTypeName()
188      */

189     public QName JavaDoc getTypeName() {
190         return typeName;
191     }
192
193     /**
194      * Get all the &lt;element&gt; elements within a sequence nested in this complexType
195      * @return The &lt;element&gt; elements within the sequnce
196      */

197     public SequenceElement[] getSequenceElements() {
198         return (SequenceElement[]) sequenceElements.toArray(
199             new SequenceElement[sequenceElements.size()]);
200     }
201
202     private void process(Element JavaDoc el, String JavaDoc tns) {
203         NodeList JavaDoc children = el.getChildNodes();
204         for (int i = 0; i < children.getLength(); i++) {
205             Node JavaDoc child = children.item(i);
206             if (child.getNodeType() == Node.ELEMENT_NODE) {
207                 Element JavaDoc subEl = (Element JavaDoc) child;
208                 String JavaDoc elType = subEl.getLocalName();
209                 if (elType.equals("complexContent")) {
210                     complexContent = new ComplexContent(subEl, tns);
211                 } else if (elType.equals("sequence")) {
212                     parseSequenceElements(subEl, tns);
213                 }
214             }
215         }
216     }
217     
218     private void parseSequenceElements(Element JavaDoc el, String JavaDoc tns) {
219         NodeList JavaDoc children = el.getChildNodes();
220         for (int i = 0; i < children.getLength(); i++) {
221             Node JavaDoc child = children.item(i);
222             if (child.getNodeType() == Node.ELEMENT_NODE) {
223                 Element JavaDoc subEl = (Element JavaDoc) child;
224                 String JavaDoc elType = subEl.getLocalName();
225                 if (elType.equals("element")) {
226                     sequenceElements.add(new SequenceElement(subEl, tns));
227                 }
228             }
229         }
230     }
231 }
232
Popular Tags