You are viewing a plain text version of this content. The canonical link for it is here.
Posted to woden-dev@ws.apache.org by jk...@apache.org on 2007/08/23 13:01:35 UTC
svn commit: r568932 [13/36] - in
/incubator/woden/trunk/java/src/org/apache/woden: ./ ant/ internal/
internal/resolver/ internal/schema/ internal/util/ internal/util/dom/
internal/util/om/ internal/wsdl20/ internal/wsdl20/extensions/
internal/wsdl20/ex...
Modified: incubator/woden/trunk/java/src/org/apache/woden/internal/util/dom/DOMUtils.java
URL: http://svn.apache.org/viewvc/incubator/woden/trunk/java/src/org/apache/woden/internal/util/dom/DOMUtils.java?rev=568932&r1=568931&r2=568932&view=diff
==============================================================================
--- incubator/woden/trunk/java/src/org/apache/woden/internal/util/dom/DOMUtils.java (original)
+++ incubator/woden/trunk/java/src/org/apache/woden/internal/util/dom/DOMUtils.java Thu Aug 23 04:01:23 2007
@@ -1,725 +1,725 @@
-/**
+/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.woden.internal.util.dom;
-
-import java.net.URI;
-import java.net.URISyntaxException;
-import java.util.List;
-import java.util.ListIterator;
-import java.util.Vector;
-
-import javax.xml.namespace.QName;
-
-import org.apache.woden.ErrorReporter;
-import org.apache.woden.WSDLException;
-import org.apache.woden.internal.ErrorLocatorImpl;
-import org.apache.woden.internal.ErrorReporterImpl;
-import org.apache.woden.wsdl20.xml.DescriptionElement;
-import org.w3c.dom.Attr;
-import org.w3c.dom.CharacterData;
-import org.w3c.dom.Element;
-import org.w3c.dom.NamedNodeMap;
-import org.w3c.dom.Node;
-
-/**
- * This class originated from WSDL4J.
- *
- * @author jkaputin@apache.org (Woden changes)
- */
-public class DOMUtils {
- /**
- * The namespaceURI represented by the prefix <code>xmlns</code>.
- */
- private static String NS_URI_XMLNS = "http://www.w3.org/2000/xmlns/";
-
- private static final String ATTR_XMLNS = "xmlns";
- private static final String emptyString = "";
-
- /**
- * Returns a list of attributes of an element. Returns an
- * empty list if the element has no attributes. Does not
- * include namespace declarations.
- *
- * @param el Element whose attributes are returned
- * @return the List of Attr
- */
- static public List getAttributes (Element el) {
- String nodename, prefix = null;
- List attrs = new Vector();
- NamedNodeMap attrMap = el.getAttributes();
- for(int i = 0; i < attrMap.getLength(); i++)
- {
- nodename = attrMap.item(i).getNodeName();
- prefix = attrMap.item(i).getPrefix();
-
- if (ATTR_XMLNS.equals(nodename) || ATTR_XMLNS.equals(prefix))
- {
- //ignore namespace declarations
- continue;
- }
- else
- {
- attrs.add(attrMap.item(i));
- }
- }
-
- return attrs;
- }
-
- /**
- * Returns the value of an attribute of an element. Returns null
- * if the attribute is not found (whereas Element.getAttribute
- * returns "" if an attrib is not found). This method should be
- * used for elements that support extension attributes because it
- * does not track unexpected attributes.
- *
- * @param el Element whose attrib is looked for
- * @param attrName name of attribute to look for
- * @return the attribute value including prefix if present
- */
- static public String getAttribute (Element el, String attrName) {
- String sRet = null;
- Attr attr = el.getAttributeNode(attrName);
-
- if (attr != null) {
- sRet = attr.getValue();
- }
- return sRet;
- }
-
- /**
- * Returns the value of an attribute of an element. Returns null
- * if the attribute is not found (whereas Element.getAttribute
- * returns "" if an attrib is not found). This method should be
- * used for elements that do not support extension attributes
- * because it tracks the element's remaining attributes so that
- * eventually any unexpected attributes can be identified.
- *
- * @param el Element whose attrib is looked for
- * @param attrName name of attribute to look for
- * @param remainingAttrs List of remaining attributes
- * @return the attribute value
- */
- static public String getAttribute (Element el, String attrName, List remainingAttrs) {
- String sRet = null;
- Attr attr = el.getAttributeNode(attrName);
-
- if (attr != null) {
- sRet = attr.getValue();
- remainingAttrs.remove(attr);
- }
- return sRet;
- }
-
- /**
- * Returns the value of an attribute of an element. Returns null
- * if the attribute is not found (whereas Element.getAttributeNS
- * returns "" if an attrib is not found).
- *
- * @param el Element whose attrib is looked for
- * @param namespaceURI namespace URI of attribute to look for
- * @param localPart local part of attribute to look for
- * @return the attribute value
- */
- static public String getAttributeNS (Element el,
- String namespaceURI,
- String localPart) {
- String sRet = null;
- Attr attr = el.getAttributeNodeNS (namespaceURI, localPart);
-
- if (attr != null) {
- sRet = attr.getValue ();
- }
-
- return sRet;
- }
-
- /**
- * Concat all the text and cdata node children of this elem and return
- * the resulting text.
- *
- * @param parentEl the element whose cdata/text node values are to
- * be combined.
- * @return the concatanated string.
- */
- static public String getChildCharacterData (Element parentEl) {
- if (parentEl == null) {
- return null;
- }
- Node tempNode = parentEl.getFirstChild();
- StringBuffer strBuf = new StringBuffer();
- CharacterData charData;
-
- while (tempNode != null) {
- switch (tempNode.getNodeType()) {
- case Node.TEXT_NODE :
- case Node.CDATA_SECTION_NODE : charData = (CharacterData)tempNode;
- strBuf.append(charData.getData());
- break;
- }
- tempNode = tempNode.getNextSibling();
- }
- return strBuf.toString();
- }
-
- /**
- * Return the first child element of the given element. Null if no
- * children are found.
- *
- * @param elem Element whose child is to be returned
- * @return the first child element.
- */
- public static Element getFirstChildElement (Element elem) {
- for (Node n = elem.getFirstChild (); n != null; n = n.getNextSibling ()) {
- if (n.getNodeType () == Node.ELEMENT_NODE) {
- return (Element) n;
- }
- }
- return null;
- }
-
- /**
- * Return the next sibling element of the given element. Null if no
- * more sibling elements are found.
- *
- * @param elem Element whose sibling element is to be returned
- * @return the next sibling element.
- */
- public static Element getNextSiblingElement (Element elem) {
- for (Node n = elem.getNextSibling (); n != null; n = n.getNextSibling ()) {
- if (n.getNodeType () == Node.ELEMENT_NODE) {
- return (Element) n;
- }
- }
- return null;
- }
-
- /**
- * Return the first child element of the given element which has the
- * given attribute with the given value.
- *
- * @param elem the element whose children are to be searched
- * @param attrName the attrib that must be present
- * @param attrValue the desired value of the attribute
- *
- * @return the first matching child element.
- */
- public static Element findChildElementWithAttribute (Element elem,
- String attrName,
- String attrValue) {
- for (Node n = elem.getFirstChild (); n != null; n = n.getNextSibling ()) {
- if (n.getNodeType () == Node.ELEMENT_NODE) {
- if (attrValue.equals (DOMUtils.getAttribute ((Element) n, attrName))) {
- return (Element) n;
- }
- }
- }
- return null;
- }
-
- /**
- * Count number of children of a certain type of the given element.
- *
- * @param elem the element whose kids are to be counted
- *
- * @return the number of matching kids.
- */
- public static int countKids (Element elem, short nodeType) {
- int nkids = 0;
- for (Node n = elem.getFirstChild (); n != null; n = n.getNextSibling ()) {
- if (n.getNodeType () == nodeType) {
- nkids++;
- }
- }
- return nkids;
- }
-
- /**
- * Given a prefix and a node, return the namespace URI that the prefix
- * has been associated with. This method is useful in resolving the
- * namespace URI of attribute values which are being interpreted as
- * QNames. If prefix is null, this method will return the default
- * namespace.
- *
- * @param context the starting node (looks up recursively from here)
- * @param prefix the prefix to find an xmlns:prefix=uri for
- *
- * @return the namespace URI or null if not found
- */
- public static String getNamespaceURIFromPrefix (Node context,
- String prefix) {
- short nodeType = context.getNodeType ();
- Node tempNode = null;
-
- switch (nodeType)
- {
- case Node.ATTRIBUTE_NODE :
- {
- tempNode = ((Attr) context).getOwnerElement ();
- break;
- }
- case Node.ELEMENT_NODE :
- {
- tempNode = context;
- break;
- }
- default :
- {
- tempNode = context.getParentNode ();
- break;
- }
- }
-
- while (tempNode != null && tempNode.getNodeType () == Node.ELEMENT_NODE)
- {
- Element tempEl = (Element) tempNode;
- String namespaceURI = (prefix == null)
- ? getAttribute (tempEl, ATTR_XMLNS)
- : getAttributeNS (tempEl, NS_URI_XMLNS, prefix);
-
- if (namespaceURI != null)
- {
- return namespaceURI;
- }
- else
- {
- tempNode = tempEl.getParentNode ();
- }
- }
-
- return null;
- }
-
- /*
- * this is a new version of the original getQName method from WSDL4J, with the invocation
- * of registerUniquePrefix commented out and the 'desc' arg removed from the arg list.
- * TODO pending modification to original method post-M2 (see todo comments below).
- */
- public static QName getQName(String prefixedValue,
- Element contextEl)
- throws WSDLException
- {
- int index = prefixedValue.indexOf(':');
- String prefix = (index != -1)
- ? prefixedValue.substring(0, index)
- : null;
- String localPart = prefixedValue.substring(index + 1);
- String namespaceURI = getNamespaceURIFromPrefix(contextEl, prefix);
-
- if (namespaceURI != null)
- {
- //TODO investigate changing the registration of namespaces and prefixes (i.e. namespace decls)
- //so it can happen within any WSDL element, not just Description (current behaviour is copied from WSDL4J)
- //registerUniquePrefix(prefix, namespaceURI, desc);
-
- //TODO when passing prefix to QName ctor, what if it was modified by
- //registerUniquePrefix because of a name clash (pass original or modification)?
- return new QName(namespaceURI,
- localPart,
- prefix != null ? prefix : emptyString);
- }
- else
- {
- //TODO use ErrorReporter here or in callers to report the problem
-
- String faultCode = (prefix == null)
- ? WSDLException.NO_PREFIX_SPECIFIED
- : WSDLException.UNBOUND_PREFIX;
-
- WSDLException wsdlExc = new WSDLException(faultCode,
- "Unable to determine " +
- "namespace of '" +
- prefixedValue + "'.");
-
- wsdlExc.setLocation(XPathUtils.getXPathExprFromNode(contextEl));
-
- throw wsdlExc;
- }
- }
-
- /*
- * this is the original version of the getQName method, per WSDL4J. See to do comment below
- * about changing register unique prefix behaviour. TODO TBD post-M2 (JK)
- */
- public static QName getQName(String prefixedValue,
- Element contextEl,
- DescriptionElement desc)
- throws WSDLException
- {
- int index = prefixedValue.indexOf(':');
- String prefix = (index != -1)
- ? prefixedValue.substring(0, index)
- : null;
- String localPart = prefixedValue.substring(index + 1);
- String namespaceURI = getNamespaceURIFromPrefix(contextEl, prefix);
-
- if (namespaceURI != null)
- {
- //TODO investigate changing the registration of namespaces and prefixes (i.e. namespace decls)
- //so it can happen within any WSDL element, not just Description (current behaviour is copied from WSDL4J)
- registerUniquePrefix(prefix, namespaceURI, desc);
-
- //TODO when passing prefix to QName ctor, what if it was modified by
- //registerUniquePrefix because of a name clash (pass original or modification)?
- return new QName(namespaceURI,
- localPart,
- prefix != null ? prefix : emptyString);
- }
- else
- {
- //TODO use ErrorReporter here or in callers to report the problem
-
- String faultCode = (prefix == null)
- ? WSDLException.NO_PREFIX_SPECIFIED
- : WSDLException.UNBOUND_PREFIX;
-
- WSDLException wsdlExc = new WSDLException(faultCode,
- "Unable to determine " +
- "namespace of '" +
- prefixedValue + "'.");
-
- wsdlExc.setLocation(XPathUtils.getXPathExprFromNode(contextEl));
-
- throw wsdlExc;
- }
- }
-
- public static void registerUniquePrefix(String prefix,
- String namespaceURI,
- DescriptionElement desc)
- throws WSDLException
- {
- URI nsUri = desc.getNamespace(prefix);
- String tempNSUri = nsUri != null ? nsUri.toString() : null;
-
- if (tempNSUri != null && tempNSUri.equals(namespaceURI))
- {
- return;
- }
-
- while (tempNSUri != null && !tempNSUri.equals(namespaceURI))
- {
- prefix += "_";
- nsUri = desc.getNamespace(prefix);
- tempNSUri = nsUri != null ? nsUri.toString() : null;
- }
-
- URI uri = null;
- try {
- uri = new URI(namespaceURI);
- } catch (URISyntaxException e) {
- //TODO make sure custom err handler is used, if configured
- new ErrorReporterImpl().reportError(
- new ErrorLocatorImpl(), //TODO line&col nos.
- "WSDL506",
- new Object[] {namespaceURI},
- ErrorReporter.SEVERITY_ERROR,
- e);
- }
-
- desc.addNamespace(prefix, uri);
- }
-
- /**
- * This method should be used for elements that support extension attributes
- * because it does not track the remaining attributes to test for unexpected
- * attributes.
- */
- /*
- public static QName getQualifiedAttributeValue(Element el,
- String attrName,
- String elDesc,
- boolean isRequired,
- Definition def)
- throws WSDLException
- {
- String attrValue = DOMUtils.getAttribute(el, attrName);
-
- if (attrValue != null)
- {
- return getQName(attrValue, el, def);
- }
- else if (isRequired)
- {
- WSDLException wsdlExc = new WSDLException(WSDLException.INVALID_WSDL,
- "The '" + attrName +
- "' attribute must be " +
- "specified for every " +
- elDesc + " element.");
-
- wsdlExc.setLocation(XPathUtils.getXPathExprFromNode(el));
-
- throw wsdlExc;
- }
- else
- {
- return null;
- }
- }
- /*
-
- /**
- * This method should be used for elements that do not support extension attributes
- * because it tracks the remaining attributes so that eventually any
- * unexpected attributes can be identified.
- */
- /*
- public static QName getQualifiedAttributeValue(Element el,
- String attrName,
- String elDesc,
- boolean isRequired,
- Definition def,
- List remainingAttrs)
- throws WSDLException
- {
- String attrValue = null;
-
- attrValue = DOMUtils.getAttribute(el, attrName, remainingAttrs);
-
- if (attrValue != null)
- {
- return getQName(attrValue, el, def);
- }
- else if (isRequired)
- {
- WSDLException wsdlExc = new WSDLException(WSDLException.INVALID_WSDL,
- "The '" + attrName +
- "' attribute must be " +
- "specified for every " +
- elDesc + " element.");
-
- wsdlExc.setLocation(XPathUtils.getXPathExprFromNode(el));
-
- throw wsdlExc;
- }
- else
- {
- return null;
- }
- }
- */
-
- public static void throwWSDLException(Element location) throws WSDLException
- {
- String elName = DOMQNameUtils.newQName(location).toString();
-
- WSDLException wsdlExc = new WSDLException(WSDLException.INVALID_WSDL,
- "Encountered unexpected element '" +
- elName + "'.");
-
- wsdlExc.setLocation(XPathUtils.getXPathExprFromNode(location));
-
- throw wsdlExc;
- }
-
- public static void throwWSDLException(Element location, List remainingAttrs) throws WSDLException
- {
- String elName = DOMQNameUtils.newQName(location).toString();
-
- StringBuffer sb = new StringBuffer();
- ListIterator i = remainingAttrs.listIterator();
- while (i.hasNext())
- {
- String attrName = DOMQNameUtils.newQName((Attr)i.next()).toString();
- sb.append(attrName);
- sb.append( i.hasNext() ? " " : "");
- }
-
- WSDLException wsdlExc = new WSDLException(WSDLException.INVALID_WSDL,
- "Element '" +
- elName +
- "' contained unexpected attributes: '" +
- sb.toString() +
- "'");
-
- wsdlExc.setLocation(XPathUtils.getXPathExprFromNode(location));
-
- throw wsdlExc;
- }
-
- /*
- public static void printAttribute(String name,
- String value,
- PrintWriter pw)
- {
- if (value != null)
- {
- pw.print(' ' + name + "=\"" + cleanString(value) + '\"');
- }
- }
- */
-
- /**
- * Prints attributes with qualified names.
- */
- /*
- public static void printQualifiedAttribute(QName name,
- String value,
- Definition def,
- PrintWriter pw)
- throws WSDLException
- {
- if (name != null)
- {
- printAttribute(getQualifiedValue(name.getNamespaceURI(),
- name.getLocalPart(),
- def),
- value,
- pw);
- }
- }
-
- public static void printQualifiedAttribute(QName name,
- QName value,
- Definition def,
- PrintWriter pw)
- throws WSDLException
- {
- if (value != null)
- {
- printAttribute(getQualifiedValue(name.getNamespaceURI(),
- name.getLocalPart(),
- def),
- getQualifiedValue(value.getNamespaceURI(),
- value.getLocalPart(),
- def),
- pw);
- }
- }
-
- public static void printQualifiedAttribute(String name,
- QName value,
- Definition def,
- PrintWriter pw)
- throws WSDLException
- {
- if (value != null)
- {
- printAttribute(name,
- getQualifiedValue(value.getNamespaceURI(),
- value.getLocalPart(),
- def),
- pw);
- }
- }
-
- public static String getQualifiedValue(String namespaceURI,
- String localPart,
- Definition def)
- throws WSDLException
- {
- String prefix = null;
-
- if (namespaceURI != null && !namespaceURI.equals(""))
- {
- prefix = getPrefix(namespaceURI, def);
- }
-
- return ((prefix != null && !prefix.equals(""))
- ? prefix + ":"
- : "") + localPart;
- }
-
- public static String getPrefix(String namespaceURI,
- Definition def)
- throws WSDLException
- {
- String prefix = def.getPrefix(namespaceURI);
-
- if (prefix == null)
- {
- throw new WSDLException(WSDLException.OTHER_ERROR,
- "Can't find prefix for '" + namespaceURI +
- "'. Namespace prefixes must be set on the" +
- " Definition object using the " +
- "addNamespace(...) method.");
- }
-
- return prefix;
- }
-
- public static String cleanString(String orig)
- {
- if (orig == null)
- {
- return "";
- }
-
- StringBuffer strBuf = new StringBuffer();
- char[] chars = orig.toCharArray();
- boolean inCDATA = false;
-
- for (int i = 0; i < chars.length; i++)
- {
- if (!inCDATA)
- {
- switch (chars[i])
- {
- case '&' : strBuf.append("&");
- break;
- case '\"' : strBuf.append(""");
- break;
- case '\'' : strBuf.append("'");
- break;
- case '<' :
- {
- if (chars.length >= i + 9)
- {
- String tempStr = new String(chars, i, 9);
-
- if (tempStr.equals("<![CDATA["))
- {
- strBuf.append(tempStr);
- i += 8;
- inCDATA = true;
- }
- else
- {
- strBuf.append("<");
- }
- }
- else
- {
- strBuf.append("<");
- }
- }
- break;
- case '>' : strBuf.append(">");
- break;
- default : strBuf.append(chars[i]);
- break;
- }
- }
- else
- {
- strBuf.append(chars[i]);
-
- if (chars[i] == '>'
- && chars[i - 1] == ']'
- && chars[i - 2] == ']')
- {
- inCDATA = false;
- }
- }
- }
-
- return strBuf.toString();
- }
- */
-
-}
-
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.woden.internal.util.dom;
+
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.util.List;
+import java.util.ListIterator;
+import java.util.Vector;
+
+import javax.xml.namespace.QName;
+
+import org.apache.woden.ErrorReporter;
+import org.apache.woden.WSDLException;
+import org.apache.woden.internal.ErrorLocatorImpl;
+import org.apache.woden.internal.ErrorReporterImpl;
+import org.apache.woden.wsdl20.xml.DescriptionElement;
+import org.w3c.dom.Attr;
+import org.w3c.dom.CharacterData;
+import org.w3c.dom.Element;
+import org.w3c.dom.NamedNodeMap;
+import org.w3c.dom.Node;
+
+/**
+ * This class originated from WSDL4J.
+ *
+ * @author jkaputin@apache.org (Woden changes)
+ */
+public class DOMUtils {
+ /**
+ * The namespaceURI represented by the prefix <code>xmlns</code>.
+ */
+ private static String NS_URI_XMLNS = "http://www.w3.org/2000/xmlns/";
+
+ private static final String ATTR_XMLNS = "xmlns";
+ private static final String emptyString = "";
+
+ /**
+ * Returns a list of attributes of an element. Returns an
+ * empty list if the element has no attributes. Does not
+ * include namespace declarations.
+ *
+ * @param el Element whose attributes are returned
+ * @return the List of Attr
+ */
+ static public List getAttributes (Element el) {
+ String nodename, prefix = null;
+ List attrs = new Vector();
+ NamedNodeMap attrMap = el.getAttributes();
+ for(int i = 0; i < attrMap.getLength(); i++)
+ {
+ nodename = attrMap.item(i).getNodeName();
+ prefix = attrMap.item(i).getPrefix();
+
+ if (ATTR_XMLNS.equals(nodename) || ATTR_XMLNS.equals(prefix))
+ {
+ //ignore namespace declarations
+ continue;
+ }
+ else
+ {
+ attrs.add(attrMap.item(i));
+ }
+ }
+
+ return attrs;
+ }
+
+ /**
+ * Returns the value of an attribute of an element. Returns null
+ * if the attribute is not found (whereas Element.getAttribute
+ * returns "" if an attrib is not found). This method should be
+ * used for elements that support extension attributes because it
+ * does not track unexpected attributes.
+ *
+ * @param el Element whose attrib is looked for
+ * @param attrName name of attribute to look for
+ * @return the attribute value including prefix if present
+ */
+ static public String getAttribute (Element el, String attrName) {
+ String sRet = null;
+ Attr attr = el.getAttributeNode(attrName);
+
+ if (attr != null) {
+ sRet = attr.getValue();
+ }
+ return sRet;
+ }
+
+ /**
+ * Returns the value of an attribute of an element. Returns null
+ * if the attribute is not found (whereas Element.getAttribute
+ * returns "" if an attrib is not found). This method should be
+ * used for elements that do not support extension attributes
+ * because it tracks the element's remaining attributes so that
+ * eventually any unexpected attributes can be identified.
+ *
+ * @param el Element whose attrib is looked for
+ * @param attrName name of attribute to look for
+ * @param remainingAttrs List of remaining attributes
+ * @return the attribute value
+ */
+ static public String getAttribute (Element el, String attrName, List remainingAttrs) {
+ String sRet = null;
+ Attr attr = el.getAttributeNode(attrName);
+
+ if (attr != null) {
+ sRet = attr.getValue();
+ remainingAttrs.remove(attr);
+ }
+ return sRet;
+ }
+
+ /**
+ * Returns the value of an attribute of an element. Returns null
+ * if the attribute is not found (whereas Element.getAttributeNS
+ * returns "" if an attrib is not found).
+ *
+ * @param el Element whose attrib is looked for
+ * @param namespaceURI namespace URI of attribute to look for
+ * @param localPart local part of attribute to look for
+ * @return the attribute value
+ */
+ static public String getAttributeNS (Element el,
+ String namespaceURI,
+ String localPart) {
+ String sRet = null;
+ Attr attr = el.getAttributeNodeNS (namespaceURI, localPart);
+
+ if (attr != null) {
+ sRet = attr.getValue ();
+ }
+
+ return sRet;
+ }
+
+ /**
+ * Concat all the text and cdata node children of this elem and return
+ * the resulting text.
+ *
+ * @param parentEl the element whose cdata/text node values are to
+ * be combined.
+ * @return the concatanated string.
+ */
+ static public String getChildCharacterData (Element parentEl) {
+ if (parentEl == null) {
+ return null;
+ }
+ Node tempNode = parentEl.getFirstChild();
+ StringBuffer strBuf = new StringBuffer();
+ CharacterData charData;
+
+ while (tempNode != null) {
+ switch (tempNode.getNodeType()) {
+ case Node.TEXT_NODE :
+ case Node.CDATA_SECTION_NODE : charData = (CharacterData)tempNode;
+ strBuf.append(charData.getData());
+ break;
+ }
+ tempNode = tempNode.getNextSibling();
+ }
+ return strBuf.toString();
+ }
+
+ /**
+ * Return the first child element of the given element. Null if no
+ * children are found.
+ *
+ * @param elem Element whose child is to be returned
+ * @return the first child element.
+ */
+ public static Element getFirstChildElement (Element elem) {
+ for (Node n = elem.getFirstChild (); n != null; n = n.getNextSibling ()) {
+ if (n.getNodeType () == Node.ELEMENT_NODE) {
+ return (Element) n;
+ }
+ }
+ return null;
+ }
+
+ /**
+ * Return the next sibling element of the given element. Null if no
+ * more sibling elements are found.
+ *
+ * @param elem Element whose sibling element is to be returned
+ * @return the next sibling element.
+ */
+ public static Element getNextSiblingElement (Element elem) {
+ for (Node n = elem.getNextSibling (); n != null; n = n.getNextSibling ()) {
+ if (n.getNodeType () == Node.ELEMENT_NODE) {
+ return (Element) n;
+ }
+ }
+ return null;
+ }
+
+ /**
+ * Return the first child element of the given element which has the
+ * given attribute with the given value.
+ *
+ * @param elem the element whose children are to be searched
+ * @param attrName the attrib that must be present
+ * @param attrValue the desired value of the attribute
+ *
+ * @return the first matching child element.
+ */
+ public static Element findChildElementWithAttribute (Element elem,
+ String attrName,
+ String attrValue) {
+ for (Node n = elem.getFirstChild (); n != null; n = n.getNextSibling ()) {
+ if (n.getNodeType () == Node.ELEMENT_NODE) {
+ if (attrValue.equals (DOMUtils.getAttribute ((Element) n, attrName))) {
+ return (Element) n;
+ }
+ }
+ }
+ return null;
+ }
+
+ /**
+ * Count number of children of a certain type of the given element.
+ *
+ * @param elem the element whose kids are to be counted
+ *
+ * @return the number of matching kids.
+ */
+ public static int countKids (Element elem, short nodeType) {
+ int nkids = 0;
+ for (Node n = elem.getFirstChild (); n != null; n = n.getNextSibling ()) {
+ if (n.getNodeType () == nodeType) {
+ nkids++;
+ }
+ }
+ return nkids;
+ }
+
+ /**
+ * Given a prefix and a node, return the namespace URI that the prefix
+ * has been associated with. This method is useful in resolving the
+ * namespace URI of attribute values which are being interpreted as
+ * QNames. If prefix is null, this method will return the default
+ * namespace.
+ *
+ * @param context the starting node (looks up recursively from here)
+ * @param prefix the prefix to find an xmlns:prefix=uri for
+ *
+ * @return the namespace URI or null if not found
+ */
+ public static String getNamespaceURIFromPrefix (Node context,
+ String prefix) {
+ short nodeType = context.getNodeType ();
+ Node tempNode = null;
+
+ switch (nodeType)
+ {
+ case Node.ATTRIBUTE_NODE :
+ {
+ tempNode = ((Attr) context).getOwnerElement ();
+ break;
+ }
+ case Node.ELEMENT_NODE :
+ {
+ tempNode = context;
+ break;
+ }
+ default :
+ {
+ tempNode = context.getParentNode ();
+ break;
+ }
+ }
+
+ while (tempNode != null && tempNode.getNodeType () == Node.ELEMENT_NODE)
+ {
+ Element tempEl = (Element) tempNode;
+ String namespaceURI = (prefix == null)
+ ? getAttribute (tempEl, ATTR_XMLNS)
+ : getAttributeNS (tempEl, NS_URI_XMLNS, prefix);
+
+ if (namespaceURI != null)
+ {
+ return namespaceURI;
+ }
+ else
+ {
+ tempNode = tempEl.getParentNode ();
+ }
+ }
+
+ return null;
+ }
+
+ /*
+ * this is a new version of the original getQName method from WSDL4J, with the invocation
+ * of registerUniquePrefix commented out and the 'desc' arg removed from the arg list.
+ * TODO pending modification to original method post-M2 (see todo comments below).
+ */
+ public static QName getQName(String prefixedValue,
+ Element contextEl)
+ throws WSDLException
+ {
+ int index = prefixedValue.indexOf(':');
+ String prefix = (index != -1)
+ ? prefixedValue.substring(0, index)
+ : null;
+ String localPart = prefixedValue.substring(index + 1);
+ String namespaceURI = getNamespaceURIFromPrefix(contextEl, prefix);
+
+ if (namespaceURI != null)
+ {
+ //TODO investigate changing the registration of namespaces and prefixes (i.e. namespace decls)
+ //so it can happen within any WSDL element, not just Description (current behaviour is copied from WSDL4J)
+ //registerUniquePrefix(prefix, namespaceURI, desc);
+
+ //TODO when passing prefix to QName ctor, what if it was modified by
+ //registerUniquePrefix because of a name clash (pass original or modification)?
+ return new QName(namespaceURI,
+ localPart,
+ prefix != null ? prefix : emptyString);
+ }
+ else
+ {
+ //TODO use ErrorReporter here or in callers to report the problem
+
+ String faultCode = (prefix == null)
+ ? WSDLException.NO_PREFIX_SPECIFIED
+ : WSDLException.UNBOUND_PREFIX;
+
+ WSDLException wsdlExc = new WSDLException(faultCode,
+ "Unable to determine " +
+ "namespace of '" +
+ prefixedValue + "'.");
+
+ wsdlExc.setLocation(XPathUtils.getXPathExprFromNode(contextEl));
+
+ throw wsdlExc;
+ }
+ }
+
+ /*
+ * this is the original version of the getQName method, per WSDL4J. See to do comment below
+ * about changing register unique prefix behaviour. TODO TBD post-M2 (JK)
+ */
+ public static QName getQName(String prefixedValue,
+ Element contextEl,
+ DescriptionElement desc)
+ throws WSDLException
+ {
+ int index = prefixedValue.indexOf(':');
+ String prefix = (index != -1)
+ ? prefixedValue.substring(0, index)
+ : null;
+ String localPart = prefixedValue.substring(index + 1);
+ String namespaceURI = getNamespaceURIFromPrefix(contextEl, prefix);
+
+ if (namespaceURI != null)
+ {
+ //TODO investigate changing the registration of namespaces and prefixes (i.e. namespace decls)
+ //so it can happen within any WSDL element, not just Description (current behaviour is copied from WSDL4J)
+ registerUniquePrefix(prefix, namespaceURI, desc);
+
+ //TODO when passing prefix to QName ctor, what if it was modified by
+ //registerUniquePrefix because of a name clash (pass original or modification)?
+ return new QName(namespaceURI,
+ localPart,
+ prefix != null ? prefix : emptyString);
+ }
+ else
+ {
+ //TODO use ErrorReporter here or in callers to report the problem
+
+ String faultCode = (prefix == null)
+ ? WSDLException.NO_PREFIX_SPECIFIED
+ : WSDLException.UNBOUND_PREFIX;
+
+ WSDLException wsdlExc = new WSDLException(faultCode,
+ "Unable to determine " +
+ "namespace of '" +
+ prefixedValue + "'.");
+
+ wsdlExc.setLocation(XPathUtils.getXPathExprFromNode(contextEl));
+
+ throw wsdlExc;
+ }
+ }
+
+ public static void registerUniquePrefix(String prefix,
+ String namespaceURI,
+ DescriptionElement desc)
+ throws WSDLException
+ {
+ URI nsUri = desc.getNamespace(prefix);
+ String tempNSUri = nsUri != null ? nsUri.toString() : null;
+
+ if (tempNSUri != null && tempNSUri.equals(namespaceURI))
+ {
+ return;
+ }
+
+ while (tempNSUri != null && !tempNSUri.equals(namespaceURI))
+ {
+ prefix += "_";
+ nsUri = desc.getNamespace(prefix);
+ tempNSUri = nsUri != null ? nsUri.toString() : null;
+ }
+
+ URI uri = null;
+ try {
+ uri = new URI(namespaceURI);
+ } catch (URISyntaxException e) {
+ //TODO make sure custom err handler is used, if configured
+ new ErrorReporterImpl().reportError(
+ new ErrorLocatorImpl(), //TODO line&col nos.
+ "WSDL506",
+ new Object[] {namespaceURI},
+ ErrorReporter.SEVERITY_ERROR,
+ e);
+ }
+
+ desc.addNamespace(prefix, uri);
+ }
+
+ /**
+ * This method should be used for elements that support extension attributes
+ * because it does not track the remaining attributes to test for unexpected
+ * attributes.
+ */
+ /*
+ public static QName getQualifiedAttributeValue(Element el,
+ String attrName,
+ String elDesc,
+ boolean isRequired,
+ Definition def)
+ throws WSDLException
+ {
+ String attrValue = DOMUtils.getAttribute(el, attrName);
+
+ if (attrValue != null)
+ {
+ return getQName(attrValue, el, def);
+ }
+ else if (isRequired)
+ {
+ WSDLException wsdlExc = new WSDLException(WSDLException.INVALID_WSDL,
+ "The '" + attrName +
+ "' attribute must be " +
+ "specified for every " +
+ elDesc + " element.");
+
+ wsdlExc.setLocation(XPathUtils.getXPathExprFromNode(el));
+
+ throw wsdlExc;
+ }
+ else
+ {
+ return null;
+ }
+ }
+ /*
+
+ /**
+ * This method should be used for elements that do not support extension attributes
+ * because it tracks the remaining attributes so that eventually any
+ * unexpected attributes can be identified.
+ */
+ /*
+ public static QName getQualifiedAttributeValue(Element el,
+ String attrName,
+ String elDesc,
+ boolean isRequired,
+ Definition def,
+ List remainingAttrs)
+ throws WSDLException
+ {
+ String attrValue = null;
+
+ attrValue = DOMUtils.getAttribute(el, attrName, remainingAttrs);
+
+ if (attrValue != null)
+ {
+ return getQName(attrValue, el, def);
+ }
+ else if (isRequired)
+ {
+ WSDLException wsdlExc = new WSDLException(WSDLException.INVALID_WSDL,
+ "The '" + attrName +
+ "' attribute must be " +
+ "specified for every " +
+ elDesc + " element.");
+
+ wsdlExc.setLocation(XPathUtils.getXPathExprFromNode(el));
+
+ throw wsdlExc;
+ }
+ else
+ {
+ return null;
+ }
+ }
+ */
+
+ public static void throwWSDLException(Element location) throws WSDLException
+ {
+ String elName = DOMQNameUtils.newQName(location).toString();
+
+ WSDLException wsdlExc = new WSDLException(WSDLException.INVALID_WSDL,
+ "Encountered unexpected element '" +
+ elName + "'.");
+
+ wsdlExc.setLocation(XPathUtils.getXPathExprFromNode(location));
+
+ throw wsdlExc;
+ }
+
+ public static void throwWSDLException(Element location, List remainingAttrs) throws WSDLException
+ {
+ String elName = DOMQNameUtils.newQName(location).toString();
+
+ StringBuffer sb = new StringBuffer();
+ ListIterator i = remainingAttrs.listIterator();
+ while (i.hasNext())
+ {
+ String attrName = DOMQNameUtils.newQName((Attr)i.next()).toString();
+ sb.append(attrName);
+ sb.append( i.hasNext() ? " " : "");
+ }
+
+ WSDLException wsdlExc = new WSDLException(WSDLException.INVALID_WSDL,
+ "Element '" +
+ elName +
+ "' contained unexpected attributes: '" +
+ sb.toString() +
+ "'");
+
+ wsdlExc.setLocation(XPathUtils.getXPathExprFromNode(location));
+
+ throw wsdlExc;
+ }
+
+ /*
+ public static void printAttribute(String name,
+ String value,
+ PrintWriter pw)
+ {
+ if (value != null)
+ {
+ pw.print(' ' + name + "=\"" + cleanString(value) + '\"');
+ }
+ }
+ */
+
+ /**
+ * Prints attributes with qualified names.
+ */
+ /*
+ public static void printQualifiedAttribute(QName name,
+ String value,
+ Definition def,
+ PrintWriter pw)
+ throws WSDLException
+ {
+ if (name != null)
+ {
+ printAttribute(getQualifiedValue(name.getNamespaceURI(),
+ name.getLocalPart(),
+ def),
+ value,
+ pw);
+ }
+ }
+
+ public static void printQualifiedAttribute(QName name,
+ QName value,
+ Definition def,
+ PrintWriter pw)
+ throws WSDLException
+ {
+ if (value != null)
+ {
+ printAttribute(getQualifiedValue(name.getNamespaceURI(),
+ name.getLocalPart(),
+ def),
+ getQualifiedValue(value.getNamespaceURI(),
+ value.getLocalPart(),
+ def),
+ pw);
+ }
+ }
+
+ public static void printQualifiedAttribute(String name,
+ QName value,
+ Definition def,
+ PrintWriter pw)
+ throws WSDLException
+ {
+ if (value != null)
+ {
+ printAttribute(name,
+ getQualifiedValue(value.getNamespaceURI(),
+ value.getLocalPart(),
+ def),
+ pw);
+ }
+ }
+
+ public static String getQualifiedValue(String namespaceURI,
+ String localPart,
+ Definition def)
+ throws WSDLException
+ {
+ String prefix = null;
+
+ if (namespaceURI != null && !namespaceURI.equals(""))
+ {
+ prefix = getPrefix(namespaceURI, def);
+ }
+
+ return ((prefix != null && !prefix.equals(""))
+ ? prefix + ":"
+ : "") + localPart;
+ }
+
+ public static String getPrefix(String namespaceURI,
+ Definition def)
+ throws WSDLException
+ {
+ String prefix = def.getPrefix(namespaceURI);
+
+ if (prefix == null)
+ {
+ throw new WSDLException(WSDLException.OTHER_ERROR,
+ "Can't find prefix for '" + namespaceURI +
+ "'. Namespace prefixes must be set on the" +
+ " Definition object using the " +
+ "addNamespace(...) method.");
+ }
+
+ return prefix;
+ }
+
+ public static String cleanString(String orig)
+ {
+ if (orig == null)
+ {
+ return "";
+ }
+
+ StringBuffer strBuf = new StringBuffer();
+ char[] chars = orig.toCharArray();
+ boolean inCDATA = false;
+
+ for (int i = 0; i < chars.length; i++)
+ {
+ if (!inCDATA)
+ {
+ switch (chars[i])
+ {
+ case '&' : strBuf.append("&");
+ break;
+ case '\"' : strBuf.append(""");
+ break;
+ case '\'' : strBuf.append("'");
+ break;
+ case '<' :
+ {
+ if (chars.length >= i + 9)
+ {
+ String tempStr = new String(chars, i, 9);
+
+ if (tempStr.equals("<![CDATA["))
+ {
+ strBuf.append(tempStr);
+ i += 8;
+ inCDATA = true;
+ }
+ else
+ {
+ strBuf.append("<");
+ }
+ }
+ else
+ {
+ strBuf.append("<");
+ }
+ }
+ break;
+ case '>' : strBuf.append(">");
+ break;
+ default : strBuf.append(chars[i]);
+ break;
+ }
+ }
+ else
+ {
+ strBuf.append(chars[i]);
+
+ if (chars[i] == '>'
+ && chars[i - 1] == ']'
+ && chars[i - 2] == ']')
+ {
+ inCDATA = false;
+ }
+ }
+ }
+
+ return strBuf.toString();
+ }
+ */
+
+}
+
Propchange: incubator/woden/trunk/java/src/org/apache/woden/internal/util/dom/DOMUtils.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified: incubator/woden/trunk/java/src/org/apache/woden/internal/util/dom/XPathUtils.java
URL: http://svn.apache.org/viewvc/incubator/woden/trunk/java/src/org/apache/woden/internal/util/dom/XPathUtils.java?rev=568932&r1=568931&r2=568932&view=diff
==============================================================================
--- incubator/woden/trunk/java/src/org/apache/woden/internal/util/dom/XPathUtils.java (original)
+++ incubator/woden/trunk/java/src/org/apache/woden/internal/util/dom/XPathUtils.java Thu Aug 23 04:01:23 2007
@@ -1,215 +1,215 @@
-/**
+/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.woden.internal.util.dom;
-
-import java.util.Vector;
-
-import org.w3c.dom.Element;
-import org.w3c.dom.Node;
-import org.w3c.dom.ProcessingInstruction;
-import org.w3c.dom.Text;
-
-/**
- * This class copied from WSDL4J.
- *
- * A <code>XPathUtils</code> ...
- *
- * @author Matthew J. Duftler (duftler@us.ibm.com)
- * @author Sanjiva Weerawarana (sanjiva@watson.ibm.com)
- */
-public class XPathUtils
-{
- private static Node getPreviousTypedNode(Node node, short nodeType)
- {
- node = node.getPreviousSibling();
-
- while (node != null && node.getNodeType() != nodeType)
- {
- node = node.getPreviousSibling();
- }
-
- return node;
- }
-
- private static Node getNextTypedNode(Node node, short nodeType)
- {
- node = node.getNextSibling();
-
- while (node != null && node.getNodeType() != nodeType)
- {
- node = node.getNextSibling();
- }
-
- return node;
- }
-
- private static String getValue(Node node, short nodeType)
- {
- switch (nodeType)
- {
- case Node.ELEMENT_NODE :
- return ((Element)node).getTagName();
-
- case Node.TEXT_NODE :
- return ((Text)node).getData();
-
- case Node.PROCESSING_INSTRUCTION_NODE :
- return ((ProcessingInstruction)node).getData();
-
- default :
- return "";
- }
- }
-
- private static short getNodeType(Node node)
- {
- return (node != null ? node.getNodeType() : -1);
- }
-
- private static String getXPathFromVector(Vector path)
- {
- StringBuffer strBuf = new StringBuffer();
- int length = path.size();
-
- for (int i = 0; i < length; i++)
- {
- Node tempNode = (Node)path.elementAt(i);
- short nodeType = getNodeType(tempNode);
- String targetValue = getValue(tempNode, nodeType);
- int position = 1;
-
- tempNode = getPreviousTypedNode(tempNode, nodeType);
-
- while (tempNode != null)
- {
- if (nodeType == Node.ELEMENT_NODE)
- {
- if (getValue(tempNode, nodeType).equals(targetValue))
- {
- position++;
- }
- }
- else
- {
- position++;
- }
-
- tempNode = getPreviousTypedNode(tempNode, nodeType);
- }
-
- boolean hasMatchingSiblings = (position > 1);
-
- if (!hasMatchingSiblings)
- {
- tempNode = (Node)path.elementAt(i);
- tempNode = getNextTypedNode(tempNode, nodeType);
-
- while (!hasMatchingSiblings && tempNode != null)
- {
- if (nodeType == Node.ELEMENT_NODE)
- {
- if (getValue(tempNode, nodeType).equals(targetValue))
- {
- hasMatchingSiblings = true;
- }
- else
- {
- tempNode = getNextTypedNode(tempNode, nodeType);
- }
- }
- else
- {
- hasMatchingSiblings = true;
- }
- }
- }
-
- String step;
-
- switch (nodeType)
- {
- case Node.TEXT_NODE :
- step = "text()";
- break;
- case Node.PROCESSING_INSTRUCTION_NODE :
- step = "processing-instruction()";
- break;
- default :
- step = targetValue;
- break;
- }
-
- if (step != null && step.length() > 0)
- {
- strBuf.append('/' + step);
- }
-
- if (hasMatchingSiblings)
- {
- strBuf.append("[" + position + "]");
- }
- }
-
- return strBuf.toString();
- }
-
- private static Vector getVectorPathFromNode(Node node)
- {
- Vector path = new Vector();
-
- while (node != null)
- {
- path.insertElementAt(node, 0);
- node = node.getParentNode();
- }
-
- return path;
- }
-
- /**
- * Generates an XPath expression that will return only the given node as its
- * result. This method only works for element, text, document and PI nodes.
- *
- * @param node the node to generate an XPath expression for. This node must
- * be an element node, a text node, a document node, or a processing
- * instruction node.
- * @return an XPath expression that will return only the given node as its
- * result.
- * @exception IllegalArgumentException if the given node is not an element,
- * text, document or PI node.
- */
- public static String getXPathExprFromNode(Node node)
- throws IllegalArgumentException
- {
- short nodeType = getNodeType(node);
-
- switch (nodeType)
- {
- case Node.ELEMENT_NODE :
- case Node.TEXT_NODE :
- case Node.PROCESSING_INSTRUCTION_NODE :
- return getXPathFromVector(getVectorPathFromNode(node));
-
- case Node.DOCUMENT_NODE :
- return "/";
-
- default :
- throw new IllegalArgumentException("Only works for element, text, " +
- "document, and PI nodes.");
- }
- }
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.woden.internal.util.dom;
+
+import java.util.Vector;
+
+import org.w3c.dom.Element;
+import org.w3c.dom.Node;
+import org.w3c.dom.ProcessingInstruction;
+import org.w3c.dom.Text;
+
+/**
+ * This class copied from WSDL4J.
+ *
+ * A <code>XPathUtils</code> ...
+ *
+ * @author Matthew J. Duftler (duftler@us.ibm.com)
+ * @author Sanjiva Weerawarana (sanjiva@watson.ibm.com)
+ */
+public class XPathUtils
+{
+ private static Node getPreviousTypedNode(Node node, short nodeType)
+ {
+ node = node.getPreviousSibling();
+
+ while (node != null && node.getNodeType() != nodeType)
+ {
+ node = node.getPreviousSibling();
+ }
+
+ return node;
+ }
+
+ private static Node getNextTypedNode(Node node, short nodeType)
+ {
+ node = node.getNextSibling();
+
+ while (node != null && node.getNodeType() != nodeType)
+ {
+ node = node.getNextSibling();
+ }
+
+ return node;
+ }
+
+ private static String getValue(Node node, short nodeType)
+ {
+ switch (nodeType)
+ {
+ case Node.ELEMENT_NODE :
+ return ((Element)node).getTagName();
+
+ case Node.TEXT_NODE :
+ return ((Text)node).getData();
+
+ case Node.PROCESSING_INSTRUCTION_NODE :
+ return ((ProcessingInstruction)node).getData();
+
+ default :
+ return "";
+ }
+ }
+
+ private static short getNodeType(Node node)
+ {
+ return (node != null ? node.getNodeType() : -1);
+ }
+
+ private static String getXPathFromVector(Vector path)
+ {
+ StringBuffer strBuf = new StringBuffer();
+ int length = path.size();
+
+ for (int i = 0; i < length; i++)
+ {
+ Node tempNode = (Node)path.elementAt(i);
+ short nodeType = getNodeType(tempNode);
+ String targetValue = getValue(tempNode, nodeType);
+ int position = 1;
+
+ tempNode = getPreviousTypedNode(tempNode, nodeType);
+
+ while (tempNode != null)
+ {
+ if (nodeType == Node.ELEMENT_NODE)
+ {
+ if (getValue(tempNode, nodeType).equals(targetValue))
+ {
+ position++;
+ }
+ }
+ else
+ {
+ position++;
+ }
+
+ tempNode = getPreviousTypedNode(tempNode, nodeType);
+ }
+
+ boolean hasMatchingSiblings = (position > 1);
+
+ if (!hasMatchingSiblings)
+ {
+ tempNode = (Node)path.elementAt(i);
+ tempNode = getNextTypedNode(tempNode, nodeType);
+
+ while (!hasMatchingSiblings && tempNode != null)
+ {
+ if (nodeType == Node.ELEMENT_NODE)
+ {
+ if (getValue(tempNode, nodeType).equals(targetValue))
+ {
+ hasMatchingSiblings = true;
+ }
+ else
+ {
+ tempNode = getNextTypedNode(tempNode, nodeType);
+ }
+ }
+ else
+ {
+ hasMatchingSiblings = true;
+ }
+ }
+ }
+
+ String step;
+
+ switch (nodeType)
+ {
+ case Node.TEXT_NODE :
+ step = "text()";
+ break;
+ case Node.PROCESSING_INSTRUCTION_NODE :
+ step = "processing-instruction()";
+ break;
+ default :
+ step = targetValue;
+ break;
+ }
+
+ if (step != null && step.length() > 0)
+ {
+ strBuf.append('/' + step);
+ }
+
+ if (hasMatchingSiblings)
+ {
+ strBuf.append("[" + position + "]");
+ }
+ }
+
+ return strBuf.toString();
+ }
+
+ private static Vector getVectorPathFromNode(Node node)
+ {
+ Vector path = new Vector();
+
+ while (node != null)
+ {
+ path.insertElementAt(node, 0);
+ node = node.getParentNode();
+ }
+
+ return path;
+ }
+
+ /**
+ * Generates an XPath expression that will return only the given node as its
+ * result. This method only works for element, text, document and PI nodes.
+ *
+ * @param node the node to generate an XPath expression for. This node must
+ * be an element node, a text node, a document node, or a processing
+ * instruction node.
+ * @return an XPath expression that will return only the given node as its
+ * result.
+ * @exception IllegalArgumentException if the given node is not an element,
+ * text, document or PI node.
+ */
+ public static String getXPathExprFromNode(Node node)
+ throws IllegalArgumentException
+ {
+ short nodeType = getNodeType(node);
+
+ switch (nodeType)
+ {
+ case Node.ELEMENT_NODE :
+ case Node.TEXT_NODE :
+ case Node.PROCESSING_INSTRUCTION_NODE :
+ return getXPathFromVector(getVectorPathFromNode(node));
+
+ case Node.DOCUMENT_NODE :
+ return "/";
+
+ default :
+ throw new IllegalArgumentException("Only works for element, text, " +
+ "document, and PI nodes.");
+ }
+ }
}
Propchange: incubator/woden/trunk/java/src/org/apache/woden/internal/util/dom/XPathUtils.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified: incubator/woden/trunk/java/src/org/apache/woden/internal/util/om/OMQNameUtils.java
URL: http://svn.apache.org/viewvc/incubator/woden/trunk/java/src/org/apache/woden/internal/util/om/OMQNameUtils.java?rev=568932&r1=568931&r2=568932&view=diff
==============================================================================
--- incubator/woden/trunk/java/src/org/apache/woden/internal/util/om/OMQNameUtils.java (original)
+++ incubator/woden/trunk/java/src/org/apache/woden/internal/util/om/OMQNameUtils.java Thu Aug 23 04:01:23 2007
@@ -1,51 +1,51 @@
-/**
+/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.woden.internal.util.om;
-
-import org.apache.axiom.om.OMElement;
-import org.apache.axiom.om.OMNamespace;
-
-import javax.xml.namespace.QName;
-
-/**
- * Class to handle some trivial functionality related to QNames.
- */
-public class OMQNameUtils {
-
- /**
- * @param qname The relevant QName to be matched
- * @param element
- * @return true if the given QName matches the element's and false otherwise
- */
- public static boolean matches(QName qname, OMElement element){
- return (element != null && qname.equals(newQName(element)));
- }
-
- /* TODO: this method could be unnecessary!
- * @param element
- * @return a QName based on the OMElement
- */
- public static QName newQName(OMElement element){
- if (element != null){
- OMNamespace namespace = element.getNamespace();
- return new QName(namespace.getNamespaceURI(), element.getLocalName());
- }
- else{
- return null;
- }
- }
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.woden.internal.util.om;
+
+import org.apache.axiom.om.OMElement;
+import org.apache.axiom.om.OMNamespace;
+
+import javax.xml.namespace.QName;
+
+/**
+ * Class to handle some trivial functionality related to QNames.
+ */
+public class OMQNameUtils {
+
+ /**
+ * @param qname The relevant QName to be matched
+ * @param element
+ * @return true if the given QName matches the element's and false otherwise
+ */
+ public static boolean matches(QName qname, OMElement element){
+ return (element != null && qname.equals(newQName(element)));
+ }
+
+ /* TODO: this method could be unnecessary!
+ * @param element
+ * @return a QName based on the OMElement
+ */
+ public static QName newQName(OMElement element){
+ if (element != null){
+ OMNamespace namespace = element.getNamespace();
+ return new QName(namespace.getNamespaceURI(), element.getLocalName());
+ }
+ else{
+ return null;
+ }
+ }
}
Propchange: incubator/woden/trunk/java/src/org/apache/woden/internal/util/om/OMQNameUtils.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified: incubator/woden/trunk/java/src/org/apache/woden/internal/util/om/OMUtils.java
URL: http://svn.apache.org/viewvc/incubator/woden/trunk/java/src/org/apache/woden/internal/util/om/OMUtils.java?rev=568932&r1=568931&r2=568932&view=diff
==============================================================================
--- incubator/woden/trunk/java/src/org/apache/woden/internal/util/om/OMUtils.java (original)
+++ incubator/woden/trunk/java/src/org/apache/woden/internal/util/om/OMUtils.java Thu Aug 23 04:01:23 2007
@@ -1,211 +1,211 @@
-/**
+/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.woden.internal.util.om;
-
-import java.io.ByteArrayInputStream;
-import java.io.IOException;
-import java.io.InputStream;
-import java.net.URI;
-import java.net.URISyntaxException;
-import java.net.URL;
-import java.util.Iterator;
-
-import javax.xml.namespace.QName;
-import javax.xml.stream.XMLStreamException;
-
-import org.apache.axiom.om.OMAttribute;
-import org.apache.axiom.om.OMElement;
-import org.apache.axiom.om.impl.builder.StAXOMBuilder;
-import org.apache.woden.ErrorReporter;
-import org.apache.woden.WSDLException;
-import org.apache.woden.internal.ErrorLocatorImpl;
-import org.apache.woden.internal.ErrorReporterImpl;
-import org.apache.woden.wsdl20.xml.DescriptionElement;
-import org.xml.sax.InputSource;
-
-/**
- * This class contains utility methods required for parsing elements
- * in a WSDL using AXIOM.
- */
-public class OMUtils {
-
- /**
- * @param strUri The URI where the WSDL can be found
- * @return A StAXOMBuilder which could be used in obtaining the document object
- * @throws IOException
- */
- public static StAXOMBuilder getOMBuilder(String strUri) throws IOException {
- StAXOMBuilder builder = null;
- try {
- URI uri = new URI(strUri);
- URL url = uri.toURL();
-
- InputStream in = url.openStream();
- builder = new StAXOMBuilder(in);
-// } catch (IOException e) {
-// e.printStackTrace();
- } catch (XMLStreamException e) {
- e.printStackTrace();
- } catch (URISyntaxException e) {
- e.printStackTrace();
- }
- return builder;
- }
-
-
- /**
- * todo add validation
- * @param uri of the OMDocument
- * @return an OMElement representing the document just read
- * @throws IOException
- */
- public static OMElement getElement(String uri) throws IOException {
- StAXOMBuilder builder = OMUtils.getOMBuilder(uri);
- return builder == null ? null : builder.getDocumentElement();
- }
-
- /**
- * Returns the value of an attribute of an element. Returns null
- * if the attribute is not found
- * @param omElement Element whose attrib is looked for
- * @param attrName name of attribute to look for
- * @return the attribute value
- */
- public static String getAttribute(OMElement omElement, String attrName) {
- String val = null;
- Iterator allAttr = omElement.getAllAttributes();
- while(allAttr.hasNext()){
- OMAttribute attr = (OMAttribute)allAttr.next();
- if (attr.getLocalName().equals(attrName)){
- val = attr.getAttributeValue();
- }
- }
- return val;
- }
-
- /**
- * @param element OMElement which would most probably contain <xs:schema>
- * @return a SAX inputsource from the OMElement
- */
- public static InputSource getInputSource(OMElement element){
-
- String elementString;
- byte[] bytes = new byte[0];
-
- //Obtain the String value of the OMElement after building the OM tree
- try {
- elementString = element.toStringWithConsume();
- bytes= elementString.getBytes();
- } catch (XMLStreamException e) {
- e.printStackTrace();
- }
-
- //Deserialize from a byte array
- InputStream inputStream = new ByteArrayInputStream(bytes);
-
- return new InputSource(inputStream);
- }
-
- /**
- * @param prefixedValue to which the QName is prefixed
- * @param contextEl Element in which the QName is sought for
- * @return The relevant QName for the prefix
- * @throws WSDLException
- */
- public static QName getQName(String prefixedValue,
- OMElement contextEl)
- throws WSDLException{
- int index = prefixedValue.indexOf(':');
- String prefix = (index != -1)
- ? prefixedValue.substring(0, index)
- : null;
- String localPart = prefixedValue.substring(index + 1);
- String namespaceURI;
-
- if (prefix != null){
- namespaceURI = contextEl.findNamespaceURI(prefix).getNamespaceURI();
- //TODO investigate changing the registration of namespaces and prefixes (i.e. namespace decls)
- //so it can happen within any WSDL element, not just Description (current behaviour is copied from WSDL4J)
- //registerUniquePrefix(prefix, namespaceURI, desc);
-
- //TODO when passing prefix to QName ctor, what if it was modified by
- //registerUniquePrefix because of a name clash (pass original or modification)?
- return new QName(namespaceURI, localPart, prefix);
- }
- else{
- //TODO use ErrorReporter here or in callers to report the problem
-
- String faultCode = WSDLException.NO_PREFIX_SPECIFIED;
-
- throw new WSDLException(faultCode,
- "Unable to determine " +
- "namespace of '" +
- prefixedValue + "'.");
- }
- }
-
- /**
- * @param el Element whose attrib is looked for
- * @param namespaceURI namespace URI of attribute to look for
- * @param localPart local part of attribute to look for
- * @return the attribute value
- */
- static public String getAttributeNS (OMElement el,
- String namespaceURI,
- String localPart) {
- String sRet = null;
- OMAttribute attr = el.getAttribute(new QName(namespaceURI, localPart));
- if (attr != null) {
- sRet = attr.getAttributeValue();
- }
- return sRet;
- }
-
-
- /*This is the same method taken from DOMUtils*/
- public static void registerUniquePrefix(String prefix, String namespaceURI, DescriptionElement desc)
- throws WSDLException {
- URI nsUri = desc.getNamespace(prefix);
- String tempNSUri = nsUri != null ? nsUri.toString() : null;
-
- if (tempNSUri != null && tempNSUri.equals(namespaceURI)){
- return;
- }
-
- while (tempNSUri != null && !tempNSUri.equals(namespaceURI)){
- prefix += "_";
- nsUri = desc.getNamespace(prefix);
- tempNSUri = nsUri != null ? nsUri.toString() : null;
- }
-
- URI uri = null;
- try {
- uri = new URI(namespaceURI);
- } catch (URISyntaxException e) {
- //TODO make sure custom err handler is used, if configured
- new ErrorReporterImpl().reportError(
- new ErrorLocatorImpl(), //TODO line&col nos.
- "WSDL506",
- new Object[] {namespaceURI},
- ErrorReporter.SEVERITY_ERROR,
- e);
- }
- desc.addNamespace(prefix, uri);
- }
-}
-
-
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.woden.internal.util.om;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.net.URL;
+import java.util.Iterator;
+
+import javax.xml.namespace.QName;
+import javax.xml.stream.XMLStreamException;
+
+import org.apache.axiom.om.OMAttribute;
+import org.apache.axiom.om.OMElement;
+import org.apache.axiom.om.impl.builder.StAXOMBuilder;
+import org.apache.woden.ErrorReporter;
+import org.apache.woden.WSDLException;
+import org.apache.woden.internal.ErrorLocatorImpl;
+import org.apache.woden.internal.ErrorReporterImpl;
+import org.apache.woden.wsdl20.xml.DescriptionElement;
+import org.xml.sax.InputSource;
+
+/**
+ * This class contains utility methods required for parsing elements
+ * in a WSDL using AXIOM.
+ */
+public class OMUtils {
+
+ /**
+ * @param strUri The URI where the WSDL can be found
+ * @return A StAXOMBuilder which could be used in obtaining the document object
+ * @throws IOException
+ */
+ public static StAXOMBuilder getOMBuilder(String strUri) throws IOException {
+ StAXOMBuilder builder = null;
+ try {
+ URI uri = new URI(strUri);
+ URL url = uri.toURL();
+
+ InputStream in = url.openStream();
+ builder = new StAXOMBuilder(in);
+// } catch (IOException e) {
+// e.printStackTrace();
+ } catch (XMLStreamException e) {
+ e.printStackTrace();
+ } catch (URISyntaxException e) {
+ e.printStackTrace();
+ }
+ return builder;
+ }
+
+
+ /**
+ * todo add validation
+ * @param uri of the OMDocument
+ * @return an OMElement representing the document just read
+ * @throws IOException
+ */
+ public static OMElement getElement(String uri) throws IOException {
+ StAXOMBuilder builder = OMUtils.getOMBuilder(uri);
+ return builder == null ? null : builder.getDocumentElement();
+ }
+
+ /**
+ * Returns the value of an attribute of an element. Returns null
+ * if the attribute is not found
+ * @param omElement Element whose attrib is looked for
+ * @param attrName name of attribute to look for
+ * @return the attribute value
+ */
+ public static String getAttribute(OMElement omElement, String attrName) {
+ String val = null;
+ Iterator allAttr = omElement.getAllAttributes();
+ while(allAttr.hasNext()){
+ OMAttribute attr = (OMAttribute)allAttr.next();
+ if (attr.getLocalName().equals(attrName)){
+ val = attr.getAttributeValue();
+ }
+ }
+ return val;
+ }
+
+ /**
+ * @param element OMElement which would most probably contain <xs:schema>
+ * @return a SAX inputsource from the OMElement
+ */
+ public static InputSource getInputSource(OMElement element){
+
+ String elementString;
+ byte[] bytes = new byte[0];
+
+ //Obtain the String value of the OMElement after building the OM tree
+ try {
+ elementString = element.toStringWithConsume();
+ bytes= elementString.getBytes();
+ } catch (XMLStreamException e) {
+ e.printStackTrace();
+ }
+
+ //Deserialize from a byte array
+ InputStream inputStream = new ByteArrayInputStream(bytes);
+
+ return new InputSource(inputStream);
+ }
+
+ /**
+ * @param prefixedValue to which the QName is prefixed
+ * @param contextEl Element in which the QName is sought for
+ * @return The relevant QName for the prefix
+ * @throws WSDLException
+ */
+ public static QName getQName(String prefixedValue,
+ OMElement contextEl)
+ throws WSDLException{
+ int index = prefixedValue.indexOf(':');
+ String prefix = (index != -1)
+ ? prefixedValue.substring(0, index)
+ : null;
+ String localPart = prefixedValue.substring(index + 1);
+ String namespaceURI;
+
+ if (prefix != null){
+ namespaceURI = contextEl.findNamespaceURI(prefix).getNamespaceURI();
+ //TODO investigate changing the registration of namespaces and prefixes (i.e. namespace decls)
+ //so it can happen within any WSDL element, not just Description (current behaviour is copied from WSDL4J)
+ //registerUniquePrefix(prefix, namespaceURI, desc);
+
+ //TODO when passing prefix to QName ctor, what if it was modified by
+ //registerUniquePrefix because of a name clash (pass original or modification)?
+ return new QName(namespaceURI, localPart, prefix);
+ }
+ else{
+ //TODO use ErrorReporter here or in callers to report the problem
+
+ String faultCode = WSDLException.NO_PREFIX_SPECIFIED;
+
+ throw new WSDLException(faultCode,
+ "Unable to determine " +
+ "namespace of '" +
+ prefixedValue + "'.");
+ }
+ }
+
+ /**
+ * @param el Element whose attrib is looked for
+ * @param namespaceURI namespace URI of attribute to look for
+ * @param localPart local part of attribute to look for
+ * @return the attribute value
+ */
+ static public String getAttributeNS (OMElement el,
+ String namespaceURI,
+ String localPart) {
+ String sRet = null;
+ OMAttribute attr = el.getAttribute(new QName(namespaceURI, localPart));
+ if (attr != null) {
+ sRet = attr.getAttributeValue();
+ }
+ return sRet;
+ }
+
+
+ /*This is the same method taken from DOMUtils*/
+ public static void registerUniquePrefix(String prefix, String namespaceURI, DescriptionElement desc)
+ throws WSDLException {
+ URI nsUri = desc.getNamespace(prefix);
+ String tempNSUri = nsUri != null ? nsUri.toString() : null;
+
+ if (tempNSUri != null && tempNSUri.equals(namespaceURI)){
+ return;
+ }
+
+ while (tempNSUri != null && !tempNSUri.equals(namespaceURI)){
+ prefix += "_";
+ nsUri = desc.getNamespace(prefix);
+ tempNSUri = nsUri != null ? nsUri.toString() : null;
+ }
+
+ URI uri = null;
+ try {
+ uri = new URI(namespaceURI);
+ } catch (URISyntaxException e) {
+ //TODO make sure custom err handler is used, if configured
+ new ErrorReporterImpl().reportError(
+ new ErrorLocatorImpl(), //TODO line&col nos.
+ "WSDL506",
+ new Object[] {namespaceURI},
+ ErrorReporter.SEVERITY_ERROR,
+ e);
+ }
+ desc.addNamespace(prefix, uri);
+ }
+}
+
+
Propchange: incubator/woden/trunk/java/src/org/apache/woden/internal/util/om/OMUtils.java
------------------------------------------------------------------------------
svn:eol-style = native
---------------------------------------------------------------------
To unsubscribe, e-mail: woden-dev-unsubscribe@ws.apache.org
For additional commands, e-mail: woden-dev-help@ws.apache.org