DOMUtils.java
3.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* org.apache.commons.lang.StringUtils
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.cq.dam.scene7.impl.utils;
import com.day.cq.dam.scene7.impl.utils.ThreadUtils;
import java.text.ParseException;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.xml.namespace.QName;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
public class DOMUtils {
private static Logger LOGGER = LoggerFactory.getLogger(DOMUtils.class);
private static XPath XPATH = XPathFactory.newInstance().newXPath();
public static String getNodeValue(Element element, String tagName) {
String nodeValue = "";
if (element != null && tagName != null && !"".equals(tagName)) {
try {
nodeValue = (String)XPATH.evaluate(tagName + "[1]/text()[1]", element, XPathConstants.STRING);
}
catch (XPathExpressionException e) {
nodeValue = "";
}
}
return nodeValue;
}
public static String getSubNodeValue(Element element, String nodeName, String subNodeName) {
String subNodeValue = "";
if (element != null && StringUtils.isNotEmpty((String)nodeName) && StringUtils.isNotEmpty((String)subNodeName)) {
try {
subNodeValue = (String)XPATH.evaluate(nodeName + "[1]/" + subNodeName + "[1]/text()[1]", element, XPathConstants.STRING);
}
catch (XPathExpressionException e) {
subNodeName = "";
}
}
return subNodeValue;
}
public static Date getDateNodeValue(Element element, String tagName, String dateFormat) {
String nodeValue = DOMUtils.getNodeValue(element, tagName);
Date dateNodeValue = null;
if (StringUtils.isNotEmpty((String)nodeValue)) {
try {
dateNodeValue = ThreadUtils.parse(nodeValue, dateFormat);
}
catch (ParseException e) {
LOGGER.error("Could not parse a valid date from following string: " + nodeValue, (Throwable)e);
}
}
return dateNodeValue;
}
public static Date getDateNodeValue4ISO8601Timezone(Element element, String tagName) {
String DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSSZ";
Pattern patternTimeStr = Pattern.compile("^(.+\\d\\d):(\\d\\d)$");
String nodeValue = DOMUtils.getNodeValue(element, tagName).trim();
Date dateNodeValue = null;
if (StringUtils.isNotEmpty((String)nodeValue)) {
try {
Matcher m = patternTimeStr.matcher(nodeValue);
if (m.matches()) {
nodeValue = m.group(1) + m.group(2);
}
dateNodeValue = ThreadUtils.parse(nodeValue, "yyyy-MM-dd'T'HH:mm:ss.SSSZ");
}
catch (ParseException e) {
LOGGER.error("Could not parse a valid date from following string: " + nodeValue, (Throwable)e);
}
}
return dateNodeValue;
}
public static NodeList getChildNodes(Element element, String childPath) {
NodeList childNodes = null;
if (element != null && StringUtils.isNotEmpty((String)childPath)) {
try {
childNodes = (NodeList)XPATH.evaluate(childPath, element, XPathConstants.NODESET);
}
catch (XPathExpressionException e) {
childNodes = null;
}
}
return childNodes;
}
}