From 696ec0bc6f005ea75db10cff9eded999456e47f2 Mon Sep 17 00:00:00 2001 From: Andrew Branson Date: Thu, 6 Mar 2014 16:28:55 +0100 Subject: New methods in Outcome to support XPath queries to make extraction of XML data in scripts easier. Fixes #167 --- .../com/c2kernel/persistency/outcome/Outcome.java | 24 +++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) (limited to 'src/main/java/com/c2kernel/persistency/outcome/Outcome.java') diff --git a/src/main/java/com/c2kernel/persistency/outcome/Outcome.java b/src/main/java/com/c2kernel/persistency/outcome/Outcome.java index b2f706b..f25922c 100644 --- a/src/main/java/com/c2kernel/persistency/outcome/Outcome.java +++ b/src/main/java/com/c2kernel/persistency/outcome/Outcome.java @@ -5,6 +5,11 @@ import java.util.StringTokenizer; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; +import javax.xml.xpath.XPath; +import javax.xml.xpath.XPathConstants; +import javax.xml.xpath.XPathExpression; +import javax.xml.xpath.XPathExpressionException; +import javax.xml.xpath.XPathFactory; import org.w3c.dom.Document; import org.w3c.dom.NodeList; @@ -29,6 +34,7 @@ public class Outcome implements C2KLocalObject { Document dom; static DocumentBuilder parser; static DOMImplementationLS impl; + static XPath xpath; static { // Set up parser @@ -50,7 +56,9 @@ public class Outcome implements C2KLocalObject { Logger.error(e); Logger.die("Cannot function without XML serialiser"); } - + + XPathFactory xPathFactory = XPathFactory.newInstance(); + xpath = xPathFactory.newXPath(); } //id is the eventID @@ -169,6 +177,20 @@ public class Outcome implements C2KLocalObject { else 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); + return (NodeList)expr.evaluate(getDOM(), XPathConstants.NODESET); + + } static public String serialize(Document doc, boolean prettyPrint) { -- cgit v1.2.3 From 391c7935ae40f60672b88312b02151c3f941796e Mon Sep 17 00:00:00 2001 From: Andrew Branson Date: Mon, 31 Mar 2014 16:18:42 +0200 Subject: Outcome.setFieldByXPath (refs #167) --- .../com/c2kernel/persistency/outcome/Outcome.java | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) (limited to 'src/main/java/com/c2kernel/persistency/outcome/Outcome.java') diff --git a/src/main/java/com/c2kernel/persistency/outcome/Outcome.java b/src/main/java/com/c2kernel/persistency/outcome/Outcome.java index f25922c..5604332 100644 --- a/src/main/java/com/c2kernel/persistency/outcome/Outcome.java +++ b/src/main/java/com/c2kernel/persistency/outcome/Outcome.java @@ -12,6 +12,7 @@ import javax.xml.xpath.XPathExpressionException; import javax.xml.xpath.XPathFactory; import org.w3c.dom.Document; +import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.w3c.dom.Text; import org.w3c.dom.bootstrap.DOMImplementationRegistry; @@ -118,11 +119,20 @@ public class Outcome implements C2KLocalObject { } public void setData(Document data) { - mData = serialize(data, false); dom = data; + mData = null; } + + public void setFieldByXPath(String xpath, String data) throws XPathExpressionException { + Node field = getNodeByXPath(xpath); + field.setNodeValue(data); + } + public String getData() { + if (mData == null && dom != null) { + mData = serialize(dom, false); + } return mData; } @@ -158,7 +168,7 @@ public class Outcome implements C2KLocalObject { * @return a DOM Document */ public Document getDOM() { - if (dom == null) + if (dom == null && mData != null) try { synchronized (parser) { dom = parser.parse(new InputSource(new StringReader(mData))); @@ -191,6 +201,13 @@ public class Outcome implements C2KLocalObject { return (NodeList)expr.evaluate(getDOM(), XPathConstants.NODESET); } + + public Node getNodeByXPath(String xpathExpr) throws XPathExpressionException { + + XPathExpression expr = xpath.compile(xpathExpr); + return (Node)expr.evaluate(getDOM(), XPathConstants.NODE); + + } static public String serialize(Document doc, boolean prettyPrint) { -- cgit v1.2.3 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 (limited to 'src/main/java/com/c2kernel/persistency/outcome/Outcome.java') 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 From 5bf7c8dabdfc7eaf26d2748a6a05eebb1023ef83 Mon Sep 17 00:00:00 2001 From: Andrew Branson Date: Thu, 8 May 2014 16:29:54 +0200 Subject: Allow getDOM to return an empty Document if the outcome isn't set. Then applications can build Outcomes themselves without having to create their own DOMImplementations. Fixes #184 --- src/main/java/com/c2kernel/persistency/outcome/Outcome.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'src/main/java/com/c2kernel/persistency/outcome/Outcome.java') diff --git a/src/main/java/com/c2kernel/persistency/outcome/Outcome.java b/src/main/java/com/c2kernel/persistency/outcome/Outcome.java index 9ad84b2..08fd75b 100644 --- a/src/main/java/com/c2kernel/persistency/outcome/Outcome.java +++ b/src/main/java/com/c2kernel/persistency/outcome/Outcome.java @@ -226,7 +226,10 @@ public class Outcome implements C2KLocalObject { if (dom == null && mData != null) try { synchronized (parser) { - dom = parser.parse(new InputSource(new StringReader(mData))); + if (mData!=null) + dom = parser.parse(new InputSource(new StringReader(mData))); + else + dom = parser.newDocument(); } } catch (Exception e) { Logger.error(e); -- cgit v1.2.3 From 989655901ebfc4952e48efef682b1fab9f0f7b1c Mon Sep 17 00:00:00 2001 From: Andrew Branson Date: Wed, 14 May 2014 13:24:09 +0200 Subject: Bug in cherry pick - create new DOM when there is no String outcome data. --- src/main/java/com/c2kernel/persistency/outcome/Outcome.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/main/java/com/c2kernel/persistency/outcome/Outcome.java') diff --git a/src/main/java/com/c2kernel/persistency/outcome/Outcome.java b/src/main/java/com/c2kernel/persistency/outcome/Outcome.java index 08fd75b..70a2a24 100644 --- a/src/main/java/com/c2kernel/persistency/outcome/Outcome.java +++ b/src/main/java/com/c2kernel/persistency/outcome/Outcome.java @@ -223,7 +223,7 @@ public class Outcome implements C2KLocalObject { * @return a DOM Document */ public Document getDOM() { - if (dom == null && mData != null) + if (dom == null) try { synchronized (parser) { if (mData!=null) -- cgit v1.2.3