Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/**
 * Copyright 2003, 2004  ONCE Corporation
 *
 * LICENSE:
 * This file is part of BuilditMPI. It may be redistributed and/or modified
 * under the terms of the Common Public License, version 1.0.
 * You should have received a copy of the Common Public License along with this
 * software. See LICENSE.txt for details. Otherwise, you may find it online at:
 *   http://www.oncecorp.com/CPL10/ or http://opensource.org/licenses/cpl.php
 *
 * DISCLAIMER OF WARRANTIES AND LIABILITY:
 * THE SOFTWARE IS PROVIDED "AS IS".  THE AUTHOR MAKES NO REPRESENTATIONS OR
 * WARRANTIES, EITHER EXPRESS OR IMPLIED.  TO THE EXTENT NOT PROHIBITED BY LAW,
 * IN NO EVENT WILL THE AUTHOR BE LIABLE FOR ANY DAMAGES, INCLUDING WITHOUT
 * LIMITATION, LOST REVENUE, PROFITS OR DATA, OR FOR SPECIAL, INDIRECT,
 * CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS
 * OF THE THEORY OF LIABILITY, ARISING OUT OF OR RELATED TO ANY FURNISHING,
 * PRACTICING, MODIFYING OR ANY USE OF THE SOFTWARE, EVEN IF THE AUTHOR HAVE
 * BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
 *
 * -----------------------------------------------------
 * $Id$
 */

import org.w3c.dom.Element;

import org.w3c.dom.NodeList;

import java.util.StringTokenizer;

public class Main {
    /**
     * Find the xpath element under the given root. As the xpath requirement is very
     * simple, so we avoid using XPath API
     * @param root - the root element that search begins
     * @param xpath - the path from the root
     * @return - the xpath defined element under the given root.
     */
    public static Element findXPathElement(Element root, String xpath) {
        if (root == null)
            return null;
        if (xpath == null || xpath.trim().equals(""))
            return root;
        xpath = toRelativePath(xpath, root.getNodeName());
        StringTokenizer st = new StringTokenizer(xpath, "/", false);
        String sitem;
        Element item = root;
        NodeList list;

        boolean first = true;
        while (st.hasMoreTokens()) {
            sitem = st.nextToken();
            if (first && sitem.equals(item.getNodeName())) {
                first = false;
            } else {
                list = item.getElementsByTagName(sitem);
                if (list.getLength() < 1)
                    return null;
                item = (Element) (list.item(0));
            }
        }

        return item;
    }

    /**
     * Cut xpath to relative path beginning from the node
     * @param xpath - xpath definition
     * @param node - beginning node
     * @return - the cutted xpath
     */
    private static String toRelativePath(String xpath, String node) {
        if (node == null || node.trim().equals(""))
            return xpath;

        if (xpath.equals(node))
            return xpath;
        else if (xpath.startsWith(node))
            return xpath;
        else if (xpath.startsWith("/" + xpath))
            return xpath.substring(1);
        else {
            int place = xpath.indexOf("/" + node + "/");
            if (place != -1) {
                return xpath.substring(place + 1);
            }
        }

        return xpath;
    }
}