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.List;
062: import java.util.Map;
063:
064: import javax.xml.namespace.QName;
065:
066: import org.w3c.dom.Element;
067: import org.w3c.dom.NamedNodeMap;
068: import org.w3c.dom.Node;
069:
070: import com.ibm.wsdl.util.xml.DOMUtils;
071:
072: /**
073: * Super class of both ComplexType and SimpleType
074: *
075: * @author Owen Burroughs <owenb@apache.org>
076: */
077: public abstract class SchemaType implements Serializable {
078:
079: /**
080: * Get a flag to indicate if this type is a complexType
081: * @return The boolean flag
082: */
083: public boolean isComplex() {
084: return false;
085: }
086:
087: /**
088: * Get a flag to indicate if this type is a simpleType
089: * @return The boolean flag
090: */
091: public boolean isSimple() {
092: return false;
093: }
094:
095: /**
096: * Get a flag to indicate if this type is an element type
097: * @return The boolean flag
098: */
099: 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 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 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 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 getAttributeQName(Element element,
150: String attr) {
151:
152: if (element == null || attr == null)
153: throw new IllegalArgumentException(
154: "Argument to 'getAttrQName' " + "cannot be null.");
155:
156: String name = DOMUtils.getAttribute(element, attr);
157:
158: if (name == null)
159: return null;
160:
161: int index = name.lastIndexOf(":");
162: String prefix = null;
163:
164: if (index != -1) {
165: prefix = name.substring(0, index);
166: name = name.substring(index + 1);
167: }
168: String uri = DOMUtils
169: .getNamespaceURIFromPrefix(element, prefix);
170:
171: return new QName(uri, name);
172: }
173:
174: /**
175: * Get a specified attribute from a given dom element
176: * @param element The dom element
177: * @param attr The name of the attribute to retrieve
178: * @param tns The targetNamespace used in resolving the attribute value
179: * @return The attribute value or null is the attriute does not exist
180: */
181: protected static QName getAttributeQName(Element element,
182: String attr, String tns) {
183:
184: if (element == null || attr == null)
185: throw new IllegalArgumentException(
186: "Argument to 'getAttrQName' " + "cannot be null.");
187:
188: String name = DOMUtils.getAttribute(element, attr);
189:
190: if (name == null)
191: return null;
192:
193: int index = name.lastIndexOf(":");
194: String prefix = null;
195:
196: if (index != -1) {
197: prefix = name.substring(0, index);
198: name = name.substring(index + 1);
199: }
200:
201: String uri = null;
202: if (prefix != null) {
203: uri = DOMUtils.getNamespaceURIFromPrefix(element, prefix);
204: } else {
205: uri = tns;
206: }
207:
208: return new QName(uri, name);
209: }
210:
211: /**
212: * Get a specified attribute from a given dom element when the attribute name is a QName
213: * @param element The dom element
214: * @param attr The name of the attribute to retrieve
215: * @return The attribute value or null is the attriute does not exist
216: */
217: protected static QName getAttributeQName(Element element, QName attr) {
218:
219: if (element == null || attr == null)
220: throw new IllegalArgumentException(
221: "Argument to 'getAttrQName' " + "cannot be null.");
222:
223: String ns = attr.getNamespaceURI();
224: String lp = attr.getLocalPart();
225: String name = DOMUtils.getAttributeNS(element, ns, lp);
226:
227: if (name == null)
228: return null;
229:
230: int index = name.lastIndexOf(":");
231: String prefix = null;
232:
233: if (index != -1) {
234: prefix = name.substring(0, index);
235: name = name.substring(index + 1);
236: }
237: String uri = DOMUtils
238: .getNamespaceURIFromPrefix(element, prefix);
239:
240: return new QName(uri, name);
241:
242: }
243:
244: /**
245: * Get all the attributes from a given dom element
246: * @param element The dom element
247: * @param tns The targetNamespace used in resolving the attribute value
248: * @param attributes A map to populate with the attributes
249: * @return The map of QName pairs (attribute name -> attribute value) for all the element's attributes
250: */
251: protected static void getAllAttributes(Element el, String tns,
252: Map attributes) {
253: NamedNodeMap atts = el.getAttributes();
254: if (atts != null) {
255: for (int a = 0; a < atts.getLength(); a++) {
256: Node attribute = atts.item(a);
257: String ln = attribute.getLocalName();
258: String ns = attribute.getNamespaceURI();
259:
260: String name = "";
261: if (ns != null) {
262: name = DOMUtils.getAttributeNS(el, ns, ln);
263: } else {
264: name = DOMUtils.getAttribute(el, ln);
265: }
266:
267: int index = name.lastIndexOf(":");
268: String prefix = null;
269:
270: if (index != -1) {
271: prefix = name.substring(0, index);
272: name = name.substring(index + 1);
273: }
274:
275: String uri = null;
276: if (prefix != null || tns == null) {
277: uri = DOMUtils
278: .getNamespaceURIFromPrefix(el, prefix);
279: } else {
280: uri = tns;
281: }
282: attributes.put(new QName(ns, ln), new QName(uri, name));
283: }
284: }
285: }
286: }
|