KickJava   Java API By Example, From Geeks To Geeks.

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


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.List JavaDoc;
62 import java.util.Map JavaDoc;
63
64 import javax.xml.namespace.QName JavaDoc;
65
66 import org.w3c.dom.Element JavaDoc;
67 import org.w3c.dom.NamedNodeMap JavaDoc;
68 import org.w3c.dom.Node JavaDoc;
69
70 import com.ibm.wsdl.util.xml.DOMUtils;
71
72 /**
73  * Super class of both ComplexType and SimpleType
74  *
75  * @author Owen Burroughs <owenb@apache.org>
76  */

77 public abstract class SchemaType implements Serializable JavaDoc {
78     
79     /**
80      * Get a flag to indicate if this type is a complexType
81      * @return The boolean flag
82      */

83     public boolean isComplex() {
84         return false;
85     }
86
87     /**
88      * Get a flag to indicate if this type is a simpleType
89      * @return The boolean flag
90      */

91     public boolean isSimple() {
92         return false;
93     }
94
95     /**
96      * Get a flag to indicate if this type is an element type
97      * @return The boolean flag
98      */

99     public boolean isElement() {
100         return false;
101     }
102
103     /**
104      * Get a flag to indicate if this type represents an array
105      * @return The boolean flag
106      */

107     public boolean isArray() {
108         return false;
109     }
110
111     /**
112      * Get the type of the elements in the array represented by this type (if applicable)
113      * @return The type
114      */

115     public QName JavaDoc getArrayType() {
116         return null;
117     }
118
119     /**
120      * Get the dimension of the array represented by this type (if applicable)
121      * @return The dimension
122      */

123     public int getArrayDimension() {
124         return 0;
125     }
126
127     /**
128      * Get the "name" attribute of this type
129      * @return The type's name
130      */

131     public QName JavaDoc getTypeName() {
132         return null;
133     }
134
135     /**
136      * Get a the direct children (SimpleType or ComplexType only) for this element
137      * @return The children
138      */

139     public List JavaDoc getChildren() {
140         return null;
141     }
142
143     /**
144      * Get a specified attribute from a given dom element
145      * @param element The dom element
146      * @param attr The name of the attribute to retrieve
147      * @return The attribute value or null is the attriute does not exist
148      */

149     protected static QName JavaDoc getAttributeQName(Element JavaDoc element, String JavaDoc attr) {
150
151         if (element == null || attr == null)
152             throw new IllegalArgumentException JavaDoc(
153                 "Argument to 'getAttrQName' " + "cannot be null.");
154
155         String JavaDoc name = DOMUtils.getAttribute(element, attr);
156
157         if (name == null)
158             return null;
159
160         int index = name.lastIndexOf(":");
161         String JavaDoc prefix = null;
162
163         if (index != -1) {
164             prefix = name.substring(0, index);
165             name = name.substring(index + 1);
166         }
167         String JavaDoc uri = DOMUtils.getNamespaceURIFromPrefix(element, prefix);
168
169         return new QName JavaDoc(uri, name);
170     }
171
172     /**
173      * Get a specified attribute from a given dom element
174      * @param element The dom element
175      * @param attr The name of the attribute to retrieve
176      * @param tns The targetNamespace used in resolving the attribute value
177      * @return The attribute value or null is the attriute does not exist
178      */

179     protected static QName JavaDoc getAttributeQName(Element JavaDoc element, String JavaDoc attr, String JavaDoc tns) {
180
181         if (element == null || attr == null)
182             throw new IllegalArgumentException JavaDoc(
183                 "Argument to 'getAttrQName' " + "cannot be null.");
184
185         String JavaDoc name = DOMUtils.getAttribute(element, attr);
186
187         if (name == null)
188             return null;
189
190         int index = name.lastIndexOf(":");
191         String JavaDoc prefix = null;
192
193         if (index != -1) {
194             prefix = name.substring(0, index);
195             name = name.substring(index + 1);
196         }
197         
198         String JavaDoc uri = null;
199         if (prefix != null) {
200             uri = DOMUtils.getNamespaceURIFromPrefix(element, prefix);
201         } else {
202             uri = tns;
203         }
204
205         return new QName JavaDoc(uri, name);
206     }
207
208     /**
209      * Get a specified attribute from a given dom element when the attribute name is a QName
210      * @param element The dom element
211      * @param attr The name of the attribute to retrieve
212      * @return The attribute value or null is the attriute does not exist
213      */

214     protected static QName JavaDoc getAttributeQName(Element JavaDoc element, QName JavaDoc attr) {
215
216         if (element == null || attr == null)
217             throw new IllegalArgumentException JavaDoc(
218                 "Argument to 'getAttrQName' " + "cannot be null.");
219                 
220         String JavaDoc ns = attr.getNamespaceURI();
221         String JavaDoc lp = attr.getLocalPart();
222         String JavaDoc name = DOMUtils.getAttributeNS(element, ns, lp);
223
224         if (name == null)
225             return null;
226
227         int index = name.lastIndexOf(":");
228         String JavaDoc prefix = null;
229
230         if (index != -1) {
231             prefix = name.substring(0, index);
232             name = name.substring(index + 1);
233         }
234         String JavaDoc uri = DOMUtils.getNamespaceURIFromPrefix(element, prefix);
235
236         return new QName JavaDoc(uri, name);
237
238     }
239
240     /**
241      * Get all the attributes from a given dom element
242      * @param element The dom element
243      * @param tns The targetNamespace used in resolving the attribute value
244      * @param attributes A map to populate with the attributes
245      * @return The map of QName pairs (attribute name -> attribute value) for all the element's attributes
246      */

247     protected static void getAllAttributes(Element JavaDoc el, String JavaDoc tns, Map JavaDoc attributes) {
248         NamedNodeMap JavaDoc atts = el.getAttributes();
249         if (atts != null) {
250             for (int a = 0; a < atts.getLength(); a++) {
251                 Node JavaDoc attribute = atts.item(a);
252                 String JavaDoc ln = attribute.getLocalName();
253                 String JavaDoc ns = attribute.getNamespaceURI();
254                 
255                 String JavaDoc name = "";
256                 if (ns != null) {
257                     name = DOMUtils.getAttributeNS(el, ns, ln);
258                 } else {
259                     name = DOMUtils.getAttribute(el, ln);
260                 }
261                 
262                 int index = name.lastIndexOf(":");
263                 String JavaDoc prefix = null;
264
265                 if (index != -1) {
266                     prefix = name.substring(0, index);
267                     name = name.substring(index + 1);
268                 }
269
270                 String JavaDoc uri = null;
271                 if (prefix != null || tns == null) {
272                     uri = DOMUtils.getNamespaceURIFromPrefix(el, prefix);
273                 } else {
274                     uri = tns;
275                 }
276                 attributes.put(new QName JavaDoc(ns, ln) ,new QName JavaDoc(uri, name));
277             }
278         }
279     }
280 }
281
Popular Tags