From e8645422ab8c50d952f1651f2f0acdf0edc95e51 Mon Sep 17 00:00:00 2001 From: Andrew Branson Date: Wed, 16 Apr 2014 17:46:13 +0200 Subject: XPath fixes, tests, and cleverer get and set FieldByXPath --- .../com/c2kernel/persistency/outcome/Outcome.java | 68 ++++++++++++++++++---- src/test/java/OutcomeTest.java | 37 ++++++++++++ src/test/resources/outcomeTest.xml | 6 ++ 3 files changed, 101 insertions(+), 10 deletions(-) create mode 100644 src/test/java/OutcomeTest.java create mode 100644 src/test/resources/outcomeTest.xml diff --git a/src/main/java/com/c2kernel/persistency/outcome/Outcome.java b/src/main/java/com/c2kernel/persistency/outcome/Outcome.java index 5604332..9ad84b2 100644 --- a/src/main/java/com/c2kernel/persistency/outcome/Outcome.java +++ b/src/main/java/com/c2kernel/persistency/outcome/Outcome.java @@ -20,6 +20,7 @@ import org.w3c.dom.ls.DOMImplementationLS; import org.w3c.dom.ls.LSSerializer; import org.xml.sax.InputSource; +import com.c2kernel.common.InvalidDataException; import com.c2kernel.common.ObjectNotFoundException; import com.c2kernel.common.PersistencyException; import com.c2kernel.entity.C2KLocalObject; @@ -73,7 +74,7 @@ public class Outcome implements C2KLocalObject { public Outcome(String path, String data) throws PersistencyException { // derive all the meta data from the path StringTokenizer tok = new StringTokenizer(path,"/"); - if (tok.countTokens() != 3 && !(tok.nextToken().equals("Outcome"))) + if (tok.countTokens() != 3 && !(tok.nextToken().equals(ClusterStorage.OUTCOME))) throw new PersistencyException("Outcome() - Outcome path must have three components: "+path, null); mSchemaType = tok.nextToken(); String verstring = tok.nextToken(); @@ -123,9 +124,63 @@ public class Outcome implements C2KLocalObject { mData = null; } - public void setFieldByXPath(String xpath, String data) throws XPathExpressionException { + public String getFieldByXPath(String xpath) throws XPathExpressionException, InvalidDataException { Node field = getNodeByXPath(xpath); - field.setNodeValue(data); + if (field == null) + throw new InvalidDataException(xpath, ""); + + else if (field.getNodeType()==Node.TEXT_NODE || field.getNodeType()==Node.CDATA_SECTION_NODE) + return field.getNodeValue(); + + else if (field.getNodeType()==Node.ELEMENT_NODE) { + NodeList fieldChildren = field.getChildNodes(); + if (fieldChildren.getLength() == 0) + throw new InvalidDataException("No child node for element", ""); + + else if (fieldChildren.getLength() == 1) { + Node child = fieldChildren.item(0); + if (child.getNodeType()==Node.TEXT_NODE || child.getNodeType()==Node.CDATA_SECTION_NODE) + return child.getNodeValue(); + else + throw new InvalidDataException("Can't get data from child node of type "+child.getNodeName(), ""); + } + else + throw new InvalidDataException("Element "+xpath+" has too many children", ""); + } + else if (field.getNodeType()==Node.ATTRIBUTE_NODE) + return field.getNodeValue(); + else + throw new InvalidDataException("Don't know what to do with node "+field.getNodeName(), ""); + } + + public void setFieldByXPath(String xpath, String data) throws XPathExpressionException, InvalidDataException { + Node field = getNodeByXPath(xpath); + if (field == null) + throw new InvalidDataException(xpath, ""); + + else if (field.getNodeType()==Node.ELEMENT_NODE) { + NodeList fieldChildren = field.getChildNodes(); + if (fieldChildren.getLength() == 0) { + field.appendChild(dom.createTextNode(data)); + } + else if (fieldChildren.getLength() == 1) { + Node child = fieldChildren.item(0); + switch (child.getNodeType()) { + case Node.TEXT_NODE: + case Node.CDATA_SECTION_NODE: + child.setNodeValue(data); + break; + default: + throw new InvalidDataException("Can't set child node of type "+child.getNodeName(), ""); + } + } + else + throw new InvalidDataException("Element "+xpath+" has too many children", ""); + } + else if (field.getNodeType()==Node.ATTRIBUTE_NODE) + field.setNodeValue(data); + else + throw new InvalidDataException("Don't know what to do with node "+field.getNodeName(), ""); } @@ -188,13 +243,6 @@ public class Outcome implements C2KLocalObject { return null; } - public String getFieldByXPath(String xpathExpr) throws XPathExpressionException { - - XPathExpression expr = xpath.compile(xpathExpr); - return (String)expr.evaluate(getDOM(), XPathConstants.STRING); - - } - public NodeList getNodesByXPath(String xpathExpr) throws XPathExpressionException { XPathExpression expr = xpath.compile(xpathExpr); diff --git a/src/test/java/OutcomeTest.java b/src/test/java/OutcomeTest.java new file mode 100644 index 0000000..d90f2ea --- /dev/null +++ b/src/test/java/OutcomeTest.java @@ -0,0 +1,37 @@ +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; + +import com.c2kernel.persistency.outcome.Outcome; +import com.c2kernel.utils.FileStringUtility; + + +public class OutcomeTest { + + Outcome testOc; + + public OutcomeTest() throws Exception { + String ocData = FileStringUtility.url2String(OutcomeTest.class.getResource("outcomeTest.xml")); + testOc = new Outcome("/Outcome/Test/0/0", ocData); + } + + public void testDOMAccess() throws Exception { + assert "Field1contents".equals(testOc.getField("Field1")) : "getField() failed"; + } + + public void testXPath() throws Exception { + Node field1Node = testOc.getNodeByXPath("//Field1/text()"); + assert field1Node!=null : "XPath for Element query failed"; + assert field1Node.getNodeValue() != null : "Field1 node was null"; + assert field1Node.getNodeValue().equals("Field1contents") : "Incorrect value for element node through XPath"; + assert "Field1contents".equals(testOc.getFieldByXPath("//Field1")): "getFieldByXPath failed"; + testOc.setFieldByXPath("//Field2", "NewField2"); + assert "NewField2".equals(testOc.getFieldByXPath("//Field2")): "getFieldByXPath failed to retrieve updated value"; + assert testOc.getNodeByXPath("//Field2/text()").getNodeValue() != null : "Field2 text node is null"; + assert testOc.getNodeByXPath("//Field2/text()").getNodeValue().equals("NewField2") : "Failed to setFieldByXPath for element"; + Node field2attr = testOc.getNodeByXPath("//Field2/@attr"); + assert field2attr.getNodeValue().equals("attribute"): "Failed to retrieve attribute value via XPath"; + NodeList field3nodes = testOc.getNodesByXPath("//Field3"); + assert field3nodes.getLength()==2 : "getNodesByXPath returned wrong number of nodes"; + + } +} diff --git a/src/test/resources/outcomeTest.xml b/src/test/resources/outcomeTest.xml new file mode 100644 index 0000000..563c72c --- /dev/null +++ b/src/test/resources/outcomeTest.xml @@ -0,0 +1,6 @@ + + Field1contents + + repeating + element + \ No newline at end of file -- cgit v1.2.3