DOMUtils.java 3.78 KB
/*
 * 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;
    }
}