Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/**
 * Copyright  Microsoft Open Technologies, Inc.
 *
 * All Rights Reserved
 *
 * 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
 *
 * THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS
 * OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION
 * ANY IMPLIED WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A
 * PARTICULAR PURPOSE, MERCHANTABILITY OR NON-INFRINGEMENT.
 *
 * See the Apache License, Version 2.0 for the specific language
 * governing permissions and limitations under the License.
 */

import java.util.ArrayList;

import java.util.List;

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

public class Main {
    /**
     * Checks if the given node has only text children.
     *
     * @param node parent.
     * @return 'TRUE' if the given node has only text children; 'FALSE' otherwise.
     */
    public static boolean hasOnlyTextChildNodes(final Node node) {
        boolean result = true;
        final NodeList children = node.getChildNodes();
        for (int i = 0; result && i < children.getLength(); i++) {
            final Node child = children.item(i);
            if (child.getNodeType() != Node.TEXT_NODE) {
                result = false;
            }
        }

        return result;
    }

    /**
     * Gets the given node's children of the given type.
     *
     * @param node parent.
     * @param nodetype searched child type.
     * @return children.
     */
    public static List<Node> getChildNodes(final Node node, final short nodetype) {
        final List<Node> result = new ArrayList<Node>();

        final NodeList children = node.getChildNodes();
        for (int i = 0; i < children.getLength(); i++) {
            final Node child = children.item(i);
            if (child.getNodeType() == nodetype) {
                result.add(child);
            }
        }

        return result;
    }
}