changeset 443:c74451bbdc9c

XmlValueValueExtractor
author Devel 2
date Fri, 28 Jul 2017 14:47:33 +0200
parents 20d6a963e889
children 6002d2c3f9d1
files stress-tester/src/main/java/com/passus/st/XmlValueValueExtractor.java stress-tester/src/test/java/com/passus/st/XmlValueValueExtractorTest.java
diffstat 2 files changed, 102 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/stress-tester/src/main/java/com/passus/st/XmlValueValueExtractor.java	Fri Jul 28 14:47:33 2017 +0200
@@ -0,0 +1,72 @@
+package com.passus.st;
+
+import com.passus.commons.Assert;
+import com.passus.filter.ValueExtractor;
+import java.io.StringReader;
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.xpath.XPath;
+import javax.xml.xpath.XPathExpression;
+import javax.xml.xpath.XPathExpressionException;
+import javax.xml.xpath.XPathFactory;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+import org.w3c.dom.Document;
+import org.xml.sax.InputSource;
+
+/**
+ *
+ * @author Mirosław Hawrot
+ */
+public class XmlValueValueExtractor implements ValueExtractor {
+
+    public static final Logger LOGGER = LogManager.getLogger(XmlValueValueExtractor.class);
+
+    private final XPathExpression xPathExpr;
+
+    private DocumentBuilder builder;
+
+    public XmlValueValueExtractor(String expression) throws XPathExpressionException {
+        Assert.notNull(expression, "expression");
+        XPath xPath = XPathFactory.newInstance().newXPath();
+        xPathExpr = xPath.compile(expression);
+    }
+
+    public XmlValueValueExtractor(XPathExpression xPathExpr) {
+        Assert.notNull(xPathExpr, "xPathExpr");
+        this.xPathExpr = xPathExpr;
+    }
+
+    @Override
+    public Object extract(Object object) {
+        if (!(object instanceof String)) {
+            return null;
+        }
+
+        try {
+            if (builder == null) {
+                DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
+                builder = builderFactory.newDocumentBuilder();
+            }
+        } catch (Exception e) {
+            if (LOGGER.isDebugEnabled()) {
+                LOGGER.debug(e.getMessage(), e);
+            }
+
+            return null;
+        }
+
+        String str = (String) object;
+        try (StringReader sr = new StringReader(str)) {
+            Document doc = builder.parse(new InputSource(sr));
+            return xPathExpr.evaluate(doc);
+        } catch (Exception e) {
+            if (LOGGER.isDebugEnabled()) {
+                LOGGER.debug(e.getMessage(), e);
+            }
+
+            return null;
+        }
+    }
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/stress-tester/src/test/java/com/passus/st/XmlValueValueExtractorTest.java	Fri Jul 28 14:47:33 2017 +0200
@@ -0,0 +1,30 @@
+package com.passus.st;
+
+import static org.testng.AssertJUnit.assertEquals;
+import org.testng.annotations.Test;
+
+/**
+ *
+ * @author Mirosław Hawrot
+ */
+public class XmlValueValueExtractorTest {
+
+    @Test
+    public void testExtract() throws Exception {
+        Log4jConfigurationFactory.enableFactory("debug");
+        String xml = "<?xml version=\"1.0\"?>"
+                + "<root>"
+                + " <node1>value1</node1>"
+                + " <node2>value2</node2>"
+                + " <node2>value3</node2>"
+                + " <node3><node4>value4</node4></node3>"
+                + "</root>";
+
+        XmlValueValueExtractor extractor = new XmlValueValueExtractor("/root/node1");
+        assertEquals("value1", extractor.extract(xml));
+
+        extractor = new XmlValueValueExtractor("/root/node2[2]");
+        assertEquals("value3", extractor.extract(xml));
+    }
+
+}