Author: mbenson Date: Wed May 16 12:47:47 2007 New Revision: 538698 URL: http://svn.apache.org/viewvc?view=rev&rev=538698 Log: respect xml:space='preserve'
Added: jakarta/commons/proper/jxpath/trunk/src/test/org/apache/commons/jxpath/XmlSpace.xml (with props) jakarta/commons/proper/jxpath/trunk/src/test/org/apache/commons/jxpath/ri/model/XMLSpaceTest.java (with props) Modified: jakarta/commons/proper/jxpath/trunk/src/java/org/apache/commons/jxpath/ri/model/dom/DOMNodePointer.java jakarta/commons/proper/jxpath/trunk/src/java/org/apache/commons/jxpath/ri/model/jdom/JDOMNodePointer.java jakarta/commons/proper/jxpath/trunk/src/test/org/apache/commons/jxpath/JXPathTestSuite.java Modified: jakarta/commons/proper/jxpath/trunk/src/java/org/apache/commons/jxpath/ri/model/dom/DOMNodePointer.java URL: http://svn.apache.org/viewvc/jakarta/commons/proper/jxpath/trunk/src/java/org/apache/commons/jxpath/ri/model/dom/DOMNodePointer.java?view=diff&rev=538698&r1=538697&r2=538698 ============================================================================== --- jakarta/commons/proper/jxpath/trunk/src/java/org/apache/commons/jxpath/ri/model/dom/DOMNodePointer.java (original) +++ jakarta/commons/proper/jxpath/trunk/src/java/org/apache/commons/jxpath/ri/model/dom/DOMNodePointer.java Wed May 16 12:47:47 2007 @@ -293,12 +293,11 @@ : current.toUpperCase().startsWith(lang.toUpperCase()); } - protected String getLanguage() { - Node n = node; + protected static String findEnclosingAttribute(Node n, String attrName) { while (n != null) { if (n.getNodeType() == Node.ELEMENT_NODE) { Element e = (Element) n; - String attr = e.getAttribute("xml:lang"); + String attr = e.getAttribute(attrName); if (attr != null && !attr.equals("")) { return attr; } @@ -308,6 +307,10 @@ return null; } + protected String getLanguage() { + return findEnclosingAttribute(node, "xml:lang"); + } + /** * Sets contents of the node to the specified value. If the value is * a String, the contents of the node are replaced with this text. @@ -628,37 +631,34 @@ } public Object getValue() { + if (node.getNodeType() == Node.COMMENT_NODE) { + String text = ((Comment) node).getData(); + return text == null ? "" : text.trim(); + } return stringValue(node); } private String stringValue(Node node) { int nodeType = node.getNodeType(); if (nodeType == Node.COMMENT_NODE) { - String text = ((Comment) node).getData(); - return text == null ? "" : text.trim(); + return ""; } - if ( - nodeType == Node.TEXT_NODE - || nodeType == Node.CDATA_SECTION_NODE) { + boolean trim = !"preserve".equals(findEnclosingAttribute(node, "xml:space")); + if (nodeType == Node.TEXT_NODE || nodeType == Node.CDATA_SECTION_NODE) { String text = node.getNodeValue(); - return text == null ? "" : text.trim(); + return text == null ? "" : trim ? text.trim() : text; } if (nodeType == Node.PROCESSING_INSTRUCTION_NODE) { String text = ((ProcessingInstruction) node).getData(); - return text == null ? "" : text.trim(); + return text == null ? "" : trim ? text.trim() : text; } NodeList list = node.getChildNodes(); StringBuffer buf = new StringBuffer(16); for (int i = 0; i < list.getLength(); i++) { Node child = list.item(i); - if (child.getNodeType() == Node.TEXT_NODE) { - buf.append(child.getNodeValue()); - } - else { - buf.append(stringValue(child)); - } + buf.append(stringValue(child)); } - return buf.toString().trim(); + return buf.toString(); } /** Modified: jakarta/commons/proper/jxpath/trunk/src/java/org/apache/commons/jxpath/ri/model/jdom/JDOMNodePointer.java URL: http://svn.apache.org/viewvc/jakarta/commons/proper/jxpath/trunk/src/java/org/apache/commons/jxpath/ri/model/jdom/JDOMNodePointer.java?view=diff&rev=538698&r1=538697&r2=538698 ============================================================================== --- jakarta/commons/proper/jxpath/trunk/src/java/org/apache/commons/jxpath/ri/model/jdom/JDOMNodePointer.java (original) +++ jakarta/commons/proper/jxpath/trunk/src/java/org/apache/commons/jxpath/ri/model/jdom/JDOMNodePointer.java Wed May 16 12:47:47 2007 @@ -49,7 +49,7 @@ */ public class JDOMNodePointer extends NodePointer { private static final long serialVersionUID = -6346532297491082651L; - + private Object node; private String id; @@ -237,7 +237,14 @@ public Object getValue() { if (node instanceof Element) { - return ((Element) node).getTextTrim(); + StringBuffer buf = new StringBuffer(); + for (NodeIterator children = childIterator(null, false, null); children.setPosition(children.getPosition() + 1);) { + NodePointer ptr = children.getNodePointer(); + if (ptr.getImmediateNode() instanceof Element || ptr.getImmediateNode() instanceof Text) { + buf.append(ptr.getValue()); + } + } + return buf.toString(); } if (node instanceof Comment) { String text = ((Comment) node).getText(); @@ -246,20 +253,15 @@ } return text; } + String result = null; if (node instanceof Text) { - return ((Text) node).getTextTrim(); - } - if (node instanceof CDATA) { - return ((CDATA) node).getTextTrim(); + result = ((Text) node).getText(); } if (node instanceof ProcessingInstruction) { - String text = ((ProcessingInstruction) node).getData(); - if (text != null) { - text = text.trim(); - } - return text; + result = ((ProcessingInstruction) node).getData(); } - return null; + boolean trim = !"preserve".equals(findEnclosingAttribute(node, "space", Namespace.XML_NAMESPACE)); + return result != null && trim ? result.trim() : result; } public void setValue(Object value) { @@ -433,12 +435,14 @@ } protected String getLanguage() { - Object n = node; + return findEnclosingAttribute(node, "lang", Namespace.XML_NAMESPACE); + } + + protected static String findEnclosingAttribute(Object n, String attrName, Namespace ns) { while (n != null) { if (n instanceof Element) { Element e = (Element) n; - String attr = - e.getAttributeValue("lang", Namespace.XML_NAMESPACE); + String attr = e.getAttributeValue(attrName, ns); if (attr != null && !attr.equals("")) { return attr; } @@ -447,8 +451,8 @@ } return null; } - - private Element nodeParent(Object node) { + + private static Element nodeParent(Object node) { if (node instanceof Element) { Object parent = ((Element) node).getParent(); return parent instanceof Element ? (Element) parent : null; @@ -742,6 +746,7 @@ JDOMNodePointer other = (JDOMNodePointer) object; return node == other.node; } + private AbstractFactory getAbstractFactory(JXPathContext context) { AbstractFactory factory = context.getFactory(); if (factory == null) { @@ -751,4 +756,5 @@ } return factory; } + } Modified: jakarta/commons/proper/jxpath/trunk/src/test/org/apache/commons/jxpath/JXPathTestSuite.java URL: http://svn.apache.org/viewvc/jakarta/commons/proper/jxpath/trunk/src/test/org/apache/commons/jxpath/JXPathTestSuite.java?view=diff&rev=538698&r1=538697&r2=538698 ============================================================================== --- jakarta/commons/proper/jxpath/trunk/src/test/org/apache/commons/jxpath/JXPathTestSuite.java (original) +++ jakarta/commons/proper/jxpath/trunk/src/test/org/apache/commons/jxpath/JXPathTestSuite.java Wed May 16 12:47:47 2007 @@ -30,6 +30,7 @@ import org.apache.commons.jxpath.ri.compiler.ExtensionFunctionTest; import org.apache.commons.jxpath.ri.compiler.VariableTest; import org.apache.commons.jxpath.ri.model.MixedModelTest; +import org.apache.commons.jxpath.ri.model.XMLSpaceTest; import org.apache.commons.jxpath.ri.model.beans.BeanModelTest; import org.apache.commons.jxpath.ri.model.container.ContainerModelTest; import org.apache.commons.jxpath.ri.model.dom.DOMModelTest; @@ -89,6 +90,7 @@ suite.addTestSuite(MixedModelTest.class); suite.addTestSuite(BasicTypeConverterTest.class); suite.addTestSuite(RecursiveAxesTest.class); + suite.addTestSuite(XMLSpaceTest.class); return suite; } } Added: jakarta/commons/proper/jxpath/trunk/src/test/org/apache/commons/jxpath/XmlSpace.xml URL: http://svn.apache.org/viewvc/jakarta/commons/proper/jxpath/trunk/src/test/org/apache/commons/jxpath/XmlSpace.xml?view=auto&rev=538698 ============================================================================== --- jakarta/commons/proper/jxpath/trunk/src/test/org/apache/commons/jxpath/XmlSpace.xml (added) +++ jakarta/commons/proper/jxpath/trunk/src/test/org/apache/commons/jxpath/XmlSpace.xml Wed May 16 12:47:47 2007 @@ -0,0 +1,47 @@ +<?xml version="1.0" ?> +<!-- + 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. +--> + +<!DOCTYPE test [ + <!ELEMENT test (text)> + <!ELEMENT text ANY> + <!ATTLIST text xml:space (default|preserve) 'default'> +]> + +<test> + <text id="unspecified"> foo </text> + <text id="default" xml:space="default"> foo </text> + <text id="preserve" xml:space="preserve"> foo </text> + <text id="nested"> + <text> foo </text> + <text>;</text> + <text xml:space="default"> bar </text> + <text>;</text> + <text xml:space="preserve"> baz </text> + </text> + <text id="nested-with-comments"> + <text> foo </text> + <!-- comment 1 --> + <text>;</text> + <!-- comment 2 --> + <text xml:space="default"> bar </text> + <!-- comment 3 --> + <text>;</text> + <!-- comment 4 --> + <text xml:space="preserve"> baz </text> + </text> +</test> \ No newline at end of file Propchange: jakarta/commons/proper/jxpath/trunk/src/test/org/apache/commons/jxpath/XmlSpace.xml ------------------------------------------------------------------------------ svn:eol-style = native Added: jakarta/commons/proper/jxpath/trunk/src/test/org/apache/commons/jxpath/ri/model/XMLSpaceTest.java URL: http://svn.apache.org/viewvc/jakarta/commons/proper/jxpath/trunk/src/test/org/apache/commons/jxpath/ri/model/XMLSpaceTest.java?view=auto&rev=538698 ============================================================================== --- jakarta/commons/proper/jxpath/trunk/src/test/org/apache/commons/jxpath/ri/model/XMLSpaceTest.java (added) +++ jakarta/commons/proper/jxpath/trunk/src/test/org/apache/commons/jxpath/ri/model/XMLSpaceTest.java Wed May 16 12:47:47 2007 @@ -0,0 +1,97 @@ +/* + * 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.commons.jxpath.ri.model; + +import org.apache.commons.jxpath.JXPathContext; +import org.apache.commons.jxpath.JXPathTestCase; +import org.apache.commons.jxpath.xml.DocumentContainer; + +/** + * Test for text trimming from JXPATH-83. + * + * @author Matt Benson + * @version $Revision$ $Date$ + */ +public class XMLSpaceTest extends JXPathTestCase { + protected JXPathContext context; + + /** + * Construct a new instance of this test case. + * + * @param name Name of the test case + */ + public XMLSpaceTest(String name) { + super(name); + } + + protected DocumentContainer createDocumentContainer(String model) { + return new DocumentContainer(JXPathTestCase.class + .getResource("XmlSpace.xml"), model); + } + + protected JXPathContext createContext(String model) { + JXPathContext context = JXPathContext + .newContext(createDocumentContainer(model)); + return context; + } + + protected void doTest(String id, String model, String expectedValue) { + JXPathContext context = JXPathContext + .newContext(createDocumentContainer(model)); + assertEquals(context.getValue("test/[EMAIL PROTECTED]'" + id + "']"), expectedValue); + } + + public void testUnspecifiedDOM() { + doTest("unspecified", DocumentContainer.MODEL_DOM, "foo"); + } + + public void testDefaultDOM() { + doTest("default", DocumentContainer.MODEL_DOM, "foo"); + } + + public void testPreserveDOM() { + doTest("preserve", DocumentContainer.MODEL_DOM, " foo "); + } + + public void testNestedDOM() { + doTest("nested", DocumentContainer.MODEL_DOM, "foo;bar; baz "); + } + + public void testNestedWithCommentsDOM() { + doTest("nested-with-comments", DocumentContainer.MODEL_DOM, "foo;bar; baz "); + } + + public void testUnspecifiedJDOM() { + doTest("unspecified", DocumentContainer.MODEL_JDOM, "foo"); + } + + public void testDefaultJDOM() { + doTest("default", DocumentContainer.MODEL_JDOM, "foo"); + } + + public void testPreserveJDOM() { + doTest("preserve", DocumentContainer.MODEL_JDOM, " foo "); + } + + public void testNestedJDOM() { + doTest("nested", DocumentContainer.MODEL_JDOM, "foo;bar; baz "); + } + + public void testNestedWithCommentsJDOM() { + doTest("nested-with-comments", DocumentContainer.MODEL_JDOM, "foo;bar; baz "); + } +} \ No newline at end of file Propchange: jakarta/commons/proper/jxpath/trunk/src/test/org/apache/commons/jxpath/ri/model/XMLSpaceTest.java ------------------------------------------------------------------------------ svn:eol-style = native --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]