XMPParse.java 6.0 KB
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.airport.util.parse;

import com.airport.util.Utils;
import java.io.IOException;
import java.io.StringReader;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.xml.sax.InputSource;

public class XMPParse {
    public XMPParse() {
    }

    public static final Date nodeValue2Date(String xmlFile, String xmlNodePath) {
        String str = getNodeValue(xmlFile, xmlNodePath);
        return !"".equals(str) ? Utils.iso8601DateStrToDate(str) : null;
    }

    public static final Date nodeValue2Date(String xmlFile, String xmlNodePath, Date date) {
        String str = getNodeValue(xmlFile, xmlNodePath);
        return !"".equals(str) ? Utils.iso8601DateStrToDate(str) : date;
    }

    public static final Date nodeAttribute2Date(String xmlFile, String xmlNodePath, String attributeName, Date date) {
        String str = getNodeAttribute(xmlFile, xmlNodePath, attributeName, "");
        return !"".equals(str) ? Utils.iso8601DateStrToDate(str) : date;
    }

    public static final String getNodeValue(String xmlFile, String xmlNodePath) {
        String[] nodes = xmlNodePath.split("/");
        new Document();
        SAXBuilder builder = new SAXBuilder();
        Element root = null;
        LinkedList<SubNode> listNode = new LinkedList();
        String value = "";

        Document document;
        try {
            document = builder.build(xmlFile);

            for(int i = 1; i < nodes.length; ++i) {
                SubNode snode;
                if (i > 1) {
                    snode = (SubNode)listNode.getLast();
                    List<Element> eList = snode.getElement().getChildren(nodes[i]);
                    if (eList != null && eList.size() > 0) {
                        SubNode s1 = new SubNode((Element)eList.get(0));
                        listNode.add(s1);
                    }
                } else if (1 == i) {
                    root = document.getRootElement();
                    snode = new SubNode(root);
                    listNode.add(snode);
                }
            }

            SubNode n = (SubNode)listNode.getLast();
            value = n.getElement().getText();
            n = null;
        } catch (Exception var15) {
            var15.printStackTrace();
        } finally {
            listNode.clear();
            document = null;
            root = null;
            builder = null;
            nodes = null;
        }

        System.out.println("key=" + xmlNodePath + " value=" + value);
        return value;
    }

    public static final String getNodeValueFromXmlString(String xmlString, String xmlNodePath) throws Exception {
        String[] nodes = xmlNodePath.split("/");
        new Document();
        SAXBuilder builder = new SAXBuilder();
        Element root = null;
        LinkedList<SubNode> listNode = new LinkedList();
        String value = "";

        Document document;
        try {
            StringReader xmlReader = new StringReader(xmlString);
            InputSource xmlSource = new InputSource(xmlReader);
            document = builder.build(xmlSource);

            for(int i = 1; i < nodes.length; ++i) {
                SubNode snode;
                if (i > 1) {
                    snode = (SubNode)listNode.getLast();
                    List<Element> eList = snode.getElement().getChildren(nodes[i]);
                    if (eList != null && eList.size() > 0) {
                        SubNode s1 = new SubNode((Element)eList.get(0));
                        listNode.add(s1);
                    }
                } else if (1 == i) {
                    root = document.getRootElement();
                    snode = new SubNode(root);
                    listNode.add(snode);
                }
            }

            SubNode n = (SubNode)listNode.getLast();
            value = n.getElement().getText();
            n = null;
            return value;
        } catch (Exception var17) {
            var17.printStackTrace();
            throw var17;
        } finally {
            listNode.clear();
            document = null;
            root = null;
            builder = null;
            nodes = null;
        }
    }

    public static final String getNodeAttribute(String xmlFile, String xmlNodePath, String attributeName, String defaultValue) {
        String[] nodes = xmlNodePath.split("/");
        new Document();
        SAXBuilder builder = new SAXBuilder();
        Element root = null;
        LinkedList<SubNode> listNode = new LinkedList();
        String value = "";

        Document document;
        try {
            document = builder.build(xmlFile);

            for(int i = 1; i < nodes.length; ++i) {
                SubNode snode;
                if (i > 1) {
                    snode = (SubNode)listNode.getLast();
                    List<Element> eList = snode.getElement().getChildren(nodes[i]);
                    if (eList != null && eList.size() > 0) {
                        SubNode s1 = new SubNode((Element)eList.get(0));
                        listNode.add(s1);
                    }
                } else if (1 == i) {
                    root = document.getRootElement();
                    snode = new SubNode(root);
                    listNode.add(snode);
                }
            }

            SubNode n = (SubNode)listNode.getLast();
            value = n.getElement().getAttributeValue(attributeName, defaultValue);
            n = null;
        } catch (JDOMException var18) {
            var18.printStackTrace();
        } catch (IOException var19) {
            var19.printStackTrace();
        } finally {
            listNode.clear();
            document = null;
            root = null;
            builder = null;
            nodes = null;
        }

        System.out.println("key=" + xmlNodePath + " node attrivte value=" + value);
        return value;
    }
}