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 ++++++++++++++++++---- 1 file changed, 58 insertions(+), 10 deletions(-) (limited to 'src/main/java/com/c2kernel') 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); -- cgit v1.2.3