summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrew Branson <andrew.branson@cern.ch>2014-04-16 17:46:13 +0200
committerAndrew Branson <andrew.branson@cern.ch>2014-04-16 17:46:13 +0200
commite8645422ab8c50d952f1651f2f0acdf0edc95e51 (patch)
tree6417cde587d601fe4c2e15df573be05276c6a178
parent45e0e3988f5efd6463727d3510022f3cbaa8170a (diff)
XPath fixes, tests, and cleverer get and set FieldByXPath
-rw-r--r--src/main/java/com/c2kernel/persistency/outcome/Outcome.java68
-rw-r--r--src/test/java/OutcomeTest.java37
-rw-r--r--src/test/resources/outcomeTest.xml6
3 files changed, 101 insertions, 10 deletions
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 @@
+<TestOutcome>
+ <Field1>Field1contents</Field1>
+ <Field2 attr="attribute"></Field2>
+ <Field3>repeating</Field3>
+ <Field3>element</Field3>
+</TestOutcome> \ No newline at end of file