Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.util.LinkedList;

import java.util.StringTokenizer;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class Main {
    public static String getTextByFullName(Document paramDocument, String paramString) {
        String[] arrayOfString = toStringArray(paramString, ".");
        Element localElement = paramDocument.getDocumentElement();
        for (int i = 1; i < arrayOfString.length; ++i)
            localElement = getChildByName(localElement, arrayOfString[i]);
        return getText(localElement);
    }

    public static String getTextByFullName(Element paramElement, String paramString) {
        String[] arrayOfString = toStringArray(paramString, ".");
        Element localElement = paramElement;
        for (int i = 0; i < arrayOfString.length; ++i)
            localElement = getChildByName(localElement, arrayOfString[i]);
        return getText(localElement);
    }

    public static final String[] toStringArray(String text, String token) {
        if (text == null || text.length() == 0) {
            return new String[0];
        }
        StringTokenizer tokens = new StringTokenizer(text, token);
        String[] words = new String[tokens.countTokens()];
        for (int i = 0; i < words.length; i++) {
            words[i] = tokens.nextToken();
        }
        return words;
    }

    public static Element getChildByName(Element paramElement, String paramString) {
        Element[] arrayOfElement = getChildrenByName(paramElement, paramString);
        if (arrayOfElement.length == 0)
            return null;
        if (arrayOfElement.length > 1)
            throw new IllegalStateException(
                    "Too many (" + arrayOfElement.length + ") '" + paramString + "' elements found!");
        return arrayOfElement[0];
    }

    public static String getText(Element paramElement) {
        NodeList localNodeList = paramElement.getChildNodes();
        int i = localNodeList.getLength();
        for (int j = 0; j < i; ++j) {
            Node localNode = localNodeList.item(j);
            if (localNode.getNodeType() == 3)
                return localNode.getNodeValue();
        }
        return "";
    }

    public static Element[] getChildrenByName(Element element, String paramString) {
        NodeList localNodeList = element.getChildNodes();
        int i = localNodeList.getLength();
        LinkedList<Node> nodes = new LinkedList<Node>();
        for (int j = 0; j < i; ++j) {
            Node localNode = localNodeList.item(j);
            if ((localNode.getNodeType() != 1) || (!localNode.getNodeName().equals(paramString)))
                continue;
            nodes.add(localNode);
        }
        return (Element[]) nodes.toArray(new Element[nodes.size()]);
    }
}