Added: webservices/woden/trunk/java/woden-dom/src/main/java/org/apache/woden/internal/util/dom/DOMUtils.java URL: http://svn.apache.org/viewvc/webservices/woden/trunk/java/woden-dom/src/main/java/org/apache/woden/internal/util/dom/DOMUtils.java?rev=809836&view=auto ============================================================================== --- webservices/woden/trunk/java/woden-dom/src/main/java/org/apache/woden/internal/util/dom/DOMUtils.java (added) +++ webservices/woden/trunk/java/woden-dom/src/main/java/org/apache/woden/internal/util/dom/DOMUtils.java Tue Sep 1 05:55:10 2009 @@ -0,0 +1,452 @@ +/** + * 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.List; +import java.util.ListIterator; +import java.util.Vector; + +import org.apache.woden.WSDLException; +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 [email protected] (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; + } + + 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(); + } + */ + +} +
Added: webservices/woden/trunk/java/woden-dom/src/main/java/org/apache/woden/internal/util/dom/XPathUtils.java URL: http://svn.apache.org/viewvc/webservices/woden/trunk/java/woden-dom/src/main/java/org/apache/woden/internal/util/dom/XPathUtils.java?rev=809836&view=auto ============================================================================== --- webservices/woden/trunk/java/woden-dom/src/main/java/org/apache/woden/internal/util/dom/XPathUtils.java (added) +++ webservices/woden/trunk/java/woden-dom/src/main/java/org/apache/woden/internal/util/dom/XPathUtils.java Tue Sep 1 05:55:10 2009 @@ -0,0 +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 ([email protected]) + * @author Sanjiva Weerawarana ([email protected]) + */ +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."); + } + } +} \ No newline at end of file Added: webservices/woden/trunk/java/woden-dom/src/main/java/org/apache/woden/internal/xpointer/DOMXMLElementEvaluator.java URL: http://svn.apache.org/viewvc/webservices/woden/trunk/java/woden-dom/src/main/java/org/apache/woden/internal/xpointer/DOMXMLElementEvaluator.java?rev=809836&view=auto ============================================================================== --- webservices/woden/trunk/java/woden-dom/src/main/java/org/apache/woden/internal/xpointer/DOMXMLElementEvaluator.java (added) +++ webservices/woden/trunk/java/woden-dom/src/main/java/org/apache/woden/internal/xpointer/DOMXMLElementEvaluator.java Tue Sep 1 05:55:10 2009 @@ -0,0 +1,77 @@ +/** + * 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.xpointer; + +//Woden +import org.apache.woden.XMLElement; +import org.apache.woden.internal.DOMXMLElement; +import org.apache.woden.ErrorReporter; + +//XPointer +import org.apache.woden.xpointer.XPointer; + +//DOM +import org.w3c.dom.Element; + +/** + * This class extends the XMLElementEvaluator to support the DOM implementation in XMLElement. + * + * @author Dan Harvey <[email protected]> + * + */ +public class DOMXMLElementEvaluator extends XMLElementEvaluator { + + /** + * Constructs a new DOMXMLElementEvaluator to evaluate a XPointer on a DOM Element. + * + * @param xpointer an XPointer to evaluate. + * @param element an DOM Element to be evaluated. + * @param errorReporter the Woden Error Reporter context object. + */ + public DOMXMLElementEvaluator(XPointer xpointer, Element element, ErrorReporter errorReporter) { + super(xpointer, createXMLElement(element, errorReporter)); + } + + /* + * (non-Javadoc) + * @see org.apache.woden.internal.xpointer.XMLElementEvaluator#testElementShorthand(org.apache.woden.XMLElement, java.lang.String) + */ + public boolean testElementShorthand(XMLElement element, String shorthand) { + //Simple http://www.w3.org/TR/xml-id/ support for now until we support full scheme based ID's. + Element domElement = (Element)element.getSource(); + String attr = domElement.getAttributeNS("http://www.w3.org/XML/1998/namespace", "id"); + return attr != null && attr.equals(shorthand); + } + + /** + * Evaluates the XPointer on the root Element and returns the resulting Element or null. + * + * @return an Element from the resultant evaluation of the root Element or null if evaluation fails. + */ + public Element evaluateElement(){ + XMLElement element = evaluate(); + if (element != null) return (Element)element.getSource(); + return null; + } + + //Private methods + private static XMLElement createXMLElement(Element element, ErrorReporter errorReporter) { + DOMXMLElement domXMLElement = new DOMXMLElement(errorReporter); + domXMLElement.setSource(element); + return domXMLElement; + } +} Added: webservices/woden/trunk/java/woden-dom/src/test/java/org/apache/woden/DOMXMLElementTest.java URL: http://svn.apache.org/viewvc/webservices/woden/trunk/java/woden-dom/src/test/java/org/apache/woden/DOMXMLElementTest.java?rev=809836&view=auto ============================================================================== --- webservices/woden/trunk/java/woden-dom/src/test/java/org/apache/woden/DOMXMLElementTest.java (added) +++ webservices/woden/trunk/java/woden-dom/src/test/java/org/apache/woden/DOMXMLElementTest.java Tue Sep 1 05:55:10 2009 @@ -0,0 +1,133 @@ +/** + * 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; + +import java.io.IOException; +import java.net.URL; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; + +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.xml.sax.SAXException; +import org.xml.sax.InputSource; +import org.apache.xerces.parsers.DOMParser; +import org.apache.woden.internal.DOMXMLElement; +import org.apache.woden.internal.ErrorReporterImpl; +import org.apache.woden.internal.wsdl20.Constants; +import org.apache.woden.internal.util.dom.DOMQNameUtils; +import org.apache.woden.internal.util.dom.DOMUtils; + +public class DOMXMLElementTest extends TestCase { + + private URL wsdlURL = getClass().getClassLoader().getResource("org/apache/woden/primer-hotelReservationService.wsdl"); + private InputSource inputSource = new InputSource(wsdlURL.toString()); + private ErrorReporter errorReporter; + private Element elem = null; + + public static Test suite(){ + return new TestSuite(DOMXMLElementTest.class); + } + + protected void setUp() throws Exception{ + try { + DOMParser parser = new DOMParser(); + parser.parse(inputSource); + Document doc = parser.getDocument(); + elem = doc.getDocumentElement(); + errorReporter = new ErrorReporterImpl(); + + } catch (IOException e) { + e.printStackTrace(); + } catch (SAXException e) { + e.printStackTrace(); + } + } + + protected void tearDown() throws Exception{ + elem = null; + } + + public void testGetFirstChildElement() throws WSDLException { + + DOMXMLElement domXMLElement = new DOMXMLElement(errorReporter); + domXMLElement.setSource(elem); + assertNotNull(domXMLElement.getFirstChildElement()); + } + + /* TODO implement this method only if getAttributes() is added to XMLElement. + * + public void testGetAttributes() throws WSDLException { + + //The <binding> element in the hotelReservation WSDL has many attributes + //So, let's test with that element + DOMXMLElement domXMLElement = new DOMXMLElement(errorReporter); + domXMLElement.setSource(elem); + Object obj; + Element tempEl; + if ((obj = domXMLElement.getFirstChildElement().getSource()) instanceof Element){ + tempEl = (Element)obj; + while (tempEl != null){ + if (DOMQNameUtils.matches(Constants.Q_ELEM_BINDING, tempEl)){ + domXMLElement.setSource(tempEl); + assertNotNull(domXMLElement.getAttributes()); + } + tempEl = DOMUtils.getNextSiblingElement(tempEl); + } + } + } + */ + + public void testGetAttributeValue() throws WSDLException { + //The <binding> element in the hotelReservation WSDL has many attributes + //So, let's test with that element + DOMXMLElement domXMLElement = new DOMXMLElement(errorReporter); + domXMLElement.setSource(elem); + Object obj; + Element tempEl; + if ((obj = domXMLElement.getFirstChildElement().getSource()) instanceof Element){ + tempEl = (Element)obj; + while (tempEl != null){ + if (DOMQNameUtils.matches(Constants.Q_ELEM_BINDING, tempEl)){ + domXMLElement.setSource(tempEl); + assertNotNull(domXMLElement.getAttributeValue("name")); + } + tempEl = DOMUtils.getNextSiblingElement(tempEl); + } + } + } + + public void testGetQName() throws WSDLException { + DOMXMLElement domXMLElement = new DOMXMLElement(errorReporter); + domXMLElement.setSource(elem); + Object obj; + Element tempEl; + if ((obj = domXMLElement.getSource()) instanceof Element){ + tempEl = (Element)obj; + while (tempEl != null){ + if (DOMQNameUtils.matches(Constants.Q_ELEM_BINDING, tempEl)){ + domXMLElement.setSource(tempEl); + assertNotNull(domXMLElement.getQName("wsoap:protocol")); + } + tempEl = DOMUtils.getNextSiblingElement(tempEl); + } + } + } + +} Added: webservices/woden/trunk/java/woden-dom/src/test/java/org/apache/woden/WSDLReaderTest.java URL: http://svn.apache.org/viewvc/webservices/woden/trunk/java/woden-dom/src/test/java/org/apache/woden/WSDLReaderTest.java?rev=809836&view=auto ============================================================================== --- webservices/woden/trunk/java/woden-dom/src/test/java/org/apache/woden/WSDLReaderTest.java (added) +++ webservices/woden/trunk/java/woden-dom/src/test/java/org/apache/woden/WSDLReaderTest.java Tue Sep 1 05:55:10 2009 @@ -0,0 +1,205 @@ +/** + * 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; + +import java.io.IOException; +import java.net.URI; +import java.net.URL; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.FactoryConfigurationError; +import javax.xml.parsers.ParserConfigurationException; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; + +import org.apache.woden.tests.TestErrorHandler; +import org.apache.woden.wsdl20.Description; +import org.w3c.dom.Document; +import org.xml.sax.InputSource; +import org.xml.sax.SAXException; + +public class WSDLReaderTest extends TestCase +{ + private WSDLFactory factory = null; + private WSDLReader reader = null; + + public static Test suite() + { + return new TestSuite(WSDLReaderTest.class); + } + + protected void setUp() throws Exception + { + //Create wsdl20 factory and reader. + try + { + factory = WSDLFactory.newInstance(); + reader = factory.newWSDLReader(); + } + catch (Exception e) + { + } + //Set error handler. + reader.getErrorReporter().setErrorHandler(new TestErrorHandler()); + } + + protected void tearDown() throws Exception + { + factory = null; + reader = null; + } + + public void testReadValidWSDL20() + { + Description desc = null; + try + { + URL wsdlURL = getClass().getClassLoader().getResource("org/apache/woden/primer-hotelReservationService.wsdl"); + desc = reader.readWSDL(wsdlURL.toString()); + } + catch(WSDLException e) + { + fail("Unexpected exception: " + e.getMessage()); + } + assertNotNull("The description returned is null.", desc); + } + + public void testReadInvalidWSDL20() + { + try + { + URL wsdlURL = getClass().getClassLoader().getResource("org/apache/woden/badDescriptionTags.wsdl"); + reader.readWSDL(wsdlURL.toString()); + fail("Expected a WSDLException because the \"description\" tag was deliberately misspelt."); + } + catch(WSDLException e) + { + assertTrue("Expected a WSDLException with message containing \"WSDL501\", but got: " + e.getMessage() , + e.getMessage().indexOf("WSDL501") > -1); + } + } + + public void testReadWSDLSourceDoc() + { + Description desc = null; + try + { + URL wsdlURL = getClass().getClassLoader().getResource("org/apache/woden/primer-hotelReservationService.wsdl"); + String wsdlURLStr = wsdlURL.toString(); + URI wsdlURI = URI.create(wsdlURLStr); + + Document doc = null; + try { + DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); + dbFactory.setNamespaceAware(true); + DocumentBuilder builder = dbFactory.newDocumentBuilder(); + doc = builder.parse(new InputSource(wsdlURLStr)); + } catch (FactoryConfigurationError e1) { + fail("Unexpected exception: " + e1.getMessage()); + } catch (ParserConfigurationException e1) { + fail("Unexpected exception: " + e1.getMessage()); + } catch (SAXException e1) { + fail("Unexpected exception: " + e1.getMessage()); + } catch (IOException e1) { + fail("Unexpected exception: " + e1.getMessage()); + } + + WSDLSource wsdlSource = reader.createWSDLSource(); + wsdlSource.setBaseURI(wsdlURI); + wsdlSource.setSource(doc); + desc = reader.readWSDL(wsdlSource); + } + catch(WSDLException e) + { + fail("Unexpected exception: " + e.getMessage()); + } + assertNotNull("The description returned is null.", desc); + } + + public void testReadWSDLSourceIS() + { + Description desc = null; + try + { + URL wsdlURL = getClass().getClassLoader().getResource("org/apache/woden/primer-hotelReservationService.wsdl"); + String wsdlURLStr = wsdlURL.toString(); + + InputSource is = new InputSource(wsdlURLStr); + URI wsdlURI = URI.create(wsdlURLStr); + + WSDLSource wsdlSource = reader.createWSDLSource(); + wsdlSource.setBaseURI(wsdlURI); + wsdlSource.setSource(is); + desc = reader.readWSDL(wsdlSource); + } + catch(WSDLException e) + { + fail("Unexpected exception: " + e.getMessage()); + } + assertNotNull("The description returned is null.", desc); + } + + public void testReadEmbeddedWSDLSource() { + Description desc = null; + //Load in a WSDL 2.0 file + URL wsdlURL = getClass().getClassLoader().getResource("org/apache/woden/embeded.xml"); + + //Good Tests. + String[] goodFragids = new String[]{ + //Shorthand + "#wsdlRoot", + //element() scheme. + "#element(wsdlRoot)", "#element(first/1/2)", "#element(/1/1/2)", "#element(second/2)" + }; + + //Test fragids + for (int i=0; i< goodFragids.length; i++) { + try { + desc = reader.readWSDL(wsdlURL.toString() + goodFragids[i]); + } catch(WSDLException e) { + fail("Failed with unexpected exception: " + e); + } + assertNotNull("Failed to load the embedded wsdl20 description with fragid: " + goodFragids[i], desc); + } + + //Bad Tests - Invalid XPointer. (Can't programmatically see between bad syntax and not pointing unless we modify WSDLException) + String[] badFragids = new String[]{ + //Shorthand - bad syntax. + "#wsdl#Root", "#wsd(Root", + //Shorthand - don't point. + "#wsdlRootElement", "#nonexistentFragment", + //element() scheme. - bad syntax. + "#element(wsdlRoot//)", "#element(/wsdlRoot)", "#element(wsdlRoot/)", "#element(wsdl,Root/1/1/2)", "#element(second/a)", + //element() scheme - don't point. + "#element(wsdlRoot/20)", "#element(/4/1/2)", "#element(second/3)" + }; + + //Test fragids + for (int i=0; i< badFragids.length; i++) { + try { + desc = reader.readWSDL(wsdlURL.toString() + badFragids[i]); + } catch(WSDLException e) { + assertEquals("Expected exception PARSE_ERROR for invalid XPoitner: " + badFragids[i] + ", but got the exception: " + e, "PARSER_ERROR", e.getFaultCode()); + continue; + } + fail("XPointer parse didn't not throw exception for invalid fragid: " + badFragids[i]); + } + } +} Added: webservices/woden/trunk/java/woden-dom/src/test/java/org/apache/woden/schema/SchemaTest.java URL: http://svn.apache.org/viewvc/webservices/woden/trunk/java/woden-dom/src/test/java/org/apache/woden/schema/SchemaTest.java?rev=809836&view=auto ============================================================================== --- webservices/woden/trunk/java/woden-dom/src/test/java/org/apache/woden/schema/SchemaTest.java (added) +++ webservices/woden/trunk/java/woden-dom/src/test/java/org/apache/woden/schema/SchemaTest.java Tue Sep 1 05:55:10 2009 @@ -0,0 +1,85 @@ +/** + * 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.schema; + +import java.io.ByteArrayInputStream; +import java.io.InputStream; +import java.net.URI; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; + +import org.apache.woden.XMLElement; +import org.apache.woden.internal.DOMXMLElement; +import org.apache.woden.internal.ErrorReporterImpl; +import org.apache.woden.internal.schema.ImportedSchemaImpl; +import org.apache.woden.internal.schema.InlinedSchemaImpl; +import org.apache.ws.commons.schema.XmlSchema; +import org.apache.ws.commons.schema.XmlSchemaCollection; +import org.w3c.dom.Document; +import org.w3c.dom.Element; + +public class SchemaTest extends TestCase { + + public static Test suite() + { + return new TestSuite(SchemaTest.class); + } + + public void testSetGetNamespace() { + Schema schema = new InlinedSchemaImpl(); + URI expectedNS = URI.create("http://example.com"); + schema.setNamespace(expectedNS); + URI actualNS = schema.getNamespace(); + assertEquals("Unexpected namespace URI", expectedNS, actualNS); + } + + public void testSetGetSchemaDefinition() throws Exception { + String schemaString = + "<schema targetNamespace=\"urn:abc\" />"; + byte[] schemaBytes = schemaString.getBytes(); + InputStream iStream = new ByteArrayInputStream(schemaBytes); + + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); + factory.setNamespaceAware(true); + DocumentBuilder builder = factory.newDocumentBuilder(); + Document doc = builder.parse(iStream); + Element elem = doc.getDocumentElement(); + + XmlSchemaCollection xsc = new XmlSchemaCollection(); + XmlSchema expectedSchemaDef = xsc.read(elem); + + Schema schema = new InlinedSchemaImpl(); + schema.setSchemaDefinition(expectedSchemaDef); + XmlSchema actualSchemaDef = schema.getSchemaDefinition(); + assertEquals("Unexpected XmlSchema", expectedSchemaDef, actualSchemaDef); + } + + public void testSetGetXMLElement() throws Exception { + XMLElement expectedEl = null; + expectedEl = new DOMXMLElement(new ErrorReporterImpl()); + Schema schema = new ImportedSchemaImpl(); + schema.setXMLElement(expectedEl); + XMLElement actualEl = schema.getXMLElement(); + assertEquals("Unexpected XMLElement", expectedEl, actualEl); + } + +} --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
