Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/* $HeadURL::                                                                            $
 * $Id$
 *
 * Copyright (c) 2006-2008 by Topaz, Inc.
 * http://topazproject.org
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

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

import org.w3c.dom.Text;

public class Main {
    /**
     * Get the text from the given node. This assumes the node contains a single child that is a text
     * node.
     *
     * @param node the node from which to extract the text
     * @return the raw text, or the empty string if the element has no children
     * @throws IllegalArgumentException if the first child is not a text node
     */
    public static String getText(Node node) throws IllegalArgumentException {
        Node text = node.getFirstChild();
        if (text == null)
            return "";
        if (!(text instanceof Text))
            throw new IllegalArgumentException("Expected text, but found node '" + text.getNodeName() + "'");
        return ((Text) text).getData();
    }

    /**
     * Get the first child element with the given name.  This differs from {@link
     * org.w3c.dom.Node#getFirstChild Node.getFirstChild} in that this only returns elements.
     *
     * @param parent the parent node for which to get the first child
     * @param name   the name of the child element to get; may be null or "*" to indicate the
     *               first child element of any name
     * @return the child, or null if none was found
     */
    public static Element getFirstChild(Element parent, String name) {
        final String filter = (name != null && !name.equals("*")) ? name : null;

        for (Node n = parent.getFirstChild(); n != null; n = n.getNextSibling()) {
            if (n.getNodeType() == Node.ELEMENT_NODE && (filter == null || n.getNodeName().equals(filter)))
                return (Element) n;
        }

        return null;
    }

    /**
     * Get the next sibling element with the given name.  This differs from {@link
     * org.w3c.dom.Node#getNextSibling Node.getNextSibling} in that this only returns elements.
     *
     * @param node   the current node for which to get the next sibling
     * @param name   the name of the next sibling element to get; may be null or "*" to indicate the
     *               next sibling of any name
     * @return the sibling, or null if none was found
     */
    public static Element getNextSibling(Element node, String name) {
        final String filter = (name != null && !name.equals("*")) ? name : null;

        for (Node n = node.getNextSibling(); n != null; n = n.getNextSibling()) {
            if (n.getNodeType() == Node.ELEMENT_NODE && (filter == null || n.getNodeName().equals(filter)))
                return (Element) n;
        }

        return null;
    }
}