Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.io.StringReader;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

public class Main {
    /**
     * Returns the string value specified with the given tag name in the specified XML text. Assumes
     * that there is only one tag of the speicified name in the XML text.
     * 
     * @param xmlText
     * @param tagName
     * @return the string value specified with the given tag name in the specified XML text
     */
    public static String getSingleTagValue(String xmlText, String tagName) {
        Element e = getSingleElement(xmlText, tagName);
        if (e == null) {
            return null;
        }

        return e.getFirstChild().getNodeValue();
    }

    /**
     * Returns the string value specified with the given tag name in the specified element. Assumes
     * that there is only one tag of the speicified name in the element.
     * 
     * @param element
     * @param tagName
     * @return the string value specified with the given tag name in the specified element
     */
    public static String getSingleTagValue(Element element, String tagName) {
        NodeList nodes = element.getElementsByTagName(tagName);
        if (!hasUniqueNode(nodes)) {
            return null;
        }

        Element e = (Element) nodes.item(0);
        if (e == null) {
            return null;
        }

        return e.getFirstChild().getNodeValue();
    }

    /**
     * Returns the element specified with the given tag name in the specified XML text. Assumes that
     * there is only one tag of the speicified name in the XML text.
     * 
     * @param xmlText
     * @param tagName
     * @return
     */
    public static Element getSingleElement(String xmlText, String tagName) {
        if (!hasSingleTag(xmlText, tagName)) {
            return null;
        }

        Document doc = getDomDocument(xmlText);
        NodeList nodes = doc.getElementsByTagName(tagName);
        return (Element) nodes.item(0);
    }

    /**
     * Returns true if the specified node list has one and only one node.
     * 
     * @param articleNodes
     * @throws AnalysisEngineProcessException
     */
    public static boolean hasUniqueNode(NodeList nodes) {
        if (nodes == null) {
            return false;
        }

        if (nodes.getLength() == 1) {
            return true;
        }

        return false;
    }

    /**
     * Checks whether the specified XML text has one and only one tag of the specified tag name.
     * 
     * @param xmlText
     * @param tagName
     * @return True if the specified XML text has one and only one tag of the specified tag name;
     *         false otherwise.
     */
    public static Boolean hasSingleTag(String xmlText, String tagName) {
        Document doc = getDomDocument(xmlText);
        if (doc == null) {
            return false;
        }
        NodeList nodes = doc.getElementsByTagName(tagName);

        return hasUniqueNode(nodes);
    }

    /**
     * Returns the data structure org.w3c.dom.Document that we obtain by parsing the specified XML
     * text.
     * 
     * @param XmlText
     * @return
     */
    public static Document getDomDocument(String xmlText) {
        Document doc = null;

        try {
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = factory.newDocumentBuilder();
            InputSource inStream = new InputSource();
            inStream.setCharacterStream(new StringReader(xmlText));
            doc = db.parse(inStream);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return doc;
    }
}