Skip to content

Commit

Permalink
XmlHandler Method Reorganization
Browse files Browse the repository at this point in the history
  • Loading branch information
rikkarth committed Dec 1, 2023
1 parent 1da7a65 commit 4cd760d
Showing 1 changed file with 19 additions and 19 deletions.
38 changes: 19 additions & 19 deletions src/main/java/org/toolertools/xml/XmlHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,25 @@ private XmlHandler() {
throw new AssertionError("XmlHandler should not be instantiated.");
}

public static String getStringFromXPath(String expression, Document document) {
try {
XPathExpression xPathExpression = createXPathExpression(expression);
return xPathExpression.evaluate(document);
} catch (XPathExpressionException | NullPointerException e) {
return "";
}
}

public static NodeList getNodeListFromXPath(String expression, Document document) {
try {
XPathExpression xPathExpression = createXPathExpression(expression);
return (NodeList) xPathExpression.evaluate(document, XPathConstants.NODESET);
} catch (XPathExpressionException | NullPointerException e) {
Document doc = createEmptyDocument();
return doc.createDocumentFragment().getChildNodes();
}
}

public static Optional<Document> getOptionalDomFromFile(final File file) {
try {
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
Expand All @@ -48,25 +67,6 @@ private static XPathExpression createXPathExpression(String expression) throws X
return xpath.compile(expression);
}

public static String getStringFromXPath(String expression, Document document) {
try {
XPathExpression xPathExpression = createXPathExpression(expression);
return xPathExpression.evaluate(document);
} catch (XPathExpressionException | NullPointerException e) {
return "";
}
}

public static NodeList getNodeListFromXPath(String expression, Document document) {
try {
XPathExpression xPathExpression = createXPathExpression(expression);
return (NodeList) xPathExpression.evaluate(document, XPathConstants.NODESET);
} catch (XPathExpressionException | NullPointerException e) {
Document doc = createEmptyDocument();
return doc.createDocumentFragment().getChildNodes();
}
}

private static Document createEmptyDocument() {
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
Expand Down

0 comments on commit 4cd760d

Please sign in to comment.