001: /*
002: * BEGIN_HEADER - DO NOT EDIT
003: *
004: * The contents of this file are subject to the terms
005: * of the Common Development and Distribution License
006: * (the "License"). You may not use this file except
007: * in compliance with the License.
008: *
009: * You can obtain a copy of the license at
010: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
011: * See the License for the specific language governing
012: * permissions and limitations under the License.
013: *
014: * When distributing Covered Code, include this CDDL
015: * HEADER in each file and include the License file at
016: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
017: * If applicable add the following below this CDDL HEADER,
018: * with the fields enclosed by brackets "[]" replaced with
019: * your own identifying information: Portions Copyright
020: * [year] [name of copyright owner]
021: */
022:
023: /*
024: * @(#)DOMUtilities.java
025: * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
026: *
027: * END_HEADER - DO NOT EDIT
028: */
029: package com.sun.jbi.wsdl2.impl;
030:
031: import com.sun.jbi.wsdl2.WsdlException;
032:
033: import javax.xml.namespace.QName;
034: import javax.xml.parsers.DocumentBuilder;
035: import javax.xml.parsers.DocumentBuilderFactory;
036:
037: import org.w3c.dom.Attr;
038: import org.w3c.dom.Document;
039: import org.w3c.dom.Element;
040: import org.w3c.dom.Node;
041:
042: import org.xml.sax.InputSource;
043:
044: /**
045: * This class contains a set of utilities for dealing with DOM trees.
046: *
047: * @author Sun Microsystems, Inc.
048: */
049: class DOMUtilities {
050: /**
051: * Resolve the given attribute value, which is assumed to contain a prefixed
052: * name value into a proper QName.
053: *
054: * @param attr Attribute containing the prefixed name value
055: * (e.g., "foo:bar") to be resolved.
056: * @param documentURI The URI of the containing document.
057: * @return The qualified name for the attribute value; null if it cannot be
058: * resolved.
059: * @exception WsdlException ILLEGAL_QNAME_ATTR_VALUE when the QName value in
060: * the attribute is illegal.
061: */
062: public static QName resolveNamespace(Attr attr, String documentURI)
063: throws WsdlException {
064: QName result = null;
065:
066: if (attr != null) {
067: Element element = attr.getOwnerElement();
068: String value = attr.getValue();
069:
070: try {
071: result = resolveNamespace(value, element, documentURI);
072: } catch (WsdlException ex) {
073: throw new WsdlException(
074: "Illegal qualified name {0} in value of attribute {2} in "
075: + "document {1}", new Object[] { value,
076: documentURI, attr.getName() });
077: }
078: }
079:
080: return result;
081: }
082:
083: /**
084: * Resolve the given attribute value, which is assumed to contain a prefixed
085: * name value into a proper QName. The context for the namespace is the
086: * parent element given.
087: *
088: * @param value The attribute value to resolve to a QName.
089: * @param element The parent element to the attribute value given.
090: * @param documentURI The URI of the containing document.
091: * @return The qualified name for the attribute value; null if it cannot be
092: * resolved.
093: * @exception WsdlException ILLEGAL_QNAME_VALUE when the QName value in
094: * the attribute is illegal.
095: */
096: public static QName resolveNamespace(String value, Element element,
097: String documentURI) throws WsdlException {
098: QName result = null;
099:
100: if (value != null) {
101: int valueLength = value.length();
102: int colonIndex = value.indexOf(':');
103: String targetNsDecl;
104:
105: if (colonIndex > 0 && colonIndex < valueLength - 1) {
106: targetNsDecl = "xmlns:"
107: + value.substring(0, colonIndex);
108: } else if (colonIndex < 0 && valueLength > 0) {
109: targetNsDecl = "xmlns";
110: } else {
111: throw new WsdlException(
112: "Illegal qualified name {0} in element {2} in document {1}",
113: new Object[] { value, documentURI,
114: element.getNodeName() });
115: }
116:
117: Attr nsDeclAttr = findAttribute(element, targetNsDecl);
118:
119: if (nsDeclAttr != null) {
120: result = new QName(nsDeclAttr.getValue(), value
121: .substring(colonIndex + 1));
122: }
123: }
124:
125: return result;
126: }
127:
128: /**
129: * Starting at the given element, search for the named attribute. The
130: * attribute is searched for from child to parent, up to (and including the
131: * root document element. <p>This search order conforms to the XML
132: * name space scoping rules.<p>Note: this function uses recursion to search
133: * up the parent edges of the node graph.
134: *
135: * @param element The element to search from.
136: * @param targetName The name of the attribute to search for.
137: * @return The target attribute; null if not found.
138: */
139: private static Attr findAttribute(Element element, String targetName) {
140: Attr result = null;
141:
142: if (element != null) {
143: result = element.getAttributeNode(targetName);
144:
145: if (result == null) {
146: Node parent = element.getParentNode();
147:
148: if (parent != null
149: && parent.getNodeType() == Node.ELEMENT_NODE) {
150: result = findAttribute((Element) parent, targetName);
151: }
152: }
153: }
154:
155: return result;
156: }
157:
158: /**
159: * Get the document from the given input source.
160: *
161: * @param documentBaseURI The base URI for the document to be read.
162: * @param inputSource The input source for the document.
163: * @return A DOM document representation of the well-formed XML document
164: * from the input stream.
165: * @exception WsdlException if an XML parsing error occurs.
166: */
167: public static Document getDocument(String documentBaseURI,
168: InputSource inputSource) throws WsdlException {
169: Document result;
170: DocumentBuilderFactory factory = DocumentBuilderFactory
171: .newInstance();
172:
173: factory.setNamespaceAware(true);
174: factory.setValidating(false);
175:
176: try {
177: DocumentBuilder builder = factory.newDocumentBuilder();
178: result = builder.parse(inputSource);
179: } catch (Exception ex) {
180: throw new WsdlException("Problem parsing '{0}.\n"
181: + "\tReason = {0}\n", new Object[] {
182: documentBaseURI, ex.getMessage() });
183: }
184:
185: return result;
186: }
187: }
|