Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2012 Firestar Software, Inc.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Firestar Software, Inc. - initial API and implementation
 *
 * Author:
 *     Gabriel Oancea
 *
 *******************************************************************************/

import java.util.*;

import org.w3c.dom.*;

public class Main {
    /**
     * Get all child elements of the specified (root) element. If the root is null, an illegal argument exception is
     * thrown. If the root has no children, an empty array is returned.
     * 
     * @param root The element to search.
     * @return An array of all child elements of the root.
     */
    public static ArrayList<Element> getElements(Element root) {
        if (root == null)
            throw new IllegalArgumentException("Null or invalid root element!");
        NodeList lst = root.getChildNodes();
        final int n = lst.getLength();
        ArrayList<Element> a = new ArrayList<Element>(n);
        for (int i = 0; i < n; i++) {
            Node node = lst.item(i);
            if (node.getNodeType() == Node.ELEMENT_NODE)
                a.add((Element) node);
        }
        return a;
    }

    /**
     * Get all child elements of the specified (root) element, that are named by the specified name. If the root is null,
     * an illegal argument exception is thrown. If the root has no children by the specified name, an empty array is
     * returned.
     * 
     * @param root The element to search.
     * @param name The name of the child elements to look for.
     * @return An array of the child elements named by the specified name, of the root.
     */
    public static ArrayList<Element> getElements(Element root, String name) {
        if (root == null || name == null || name.length() <= 0)
            throw new IllegalArgumentException("Null or invalid root element or name!");
        NodeList lst = root.getChildNodes();
        int size = lst.getLength();
        ArrayList<Element> a = new ArrayList<Element>(size / 2);
        name = localName(name);
        for (int i = 0; i < size; i++) {
            Node node = lst.item(i);
            if (node.getNodeType() == Node.ELEMENT_NODE) {
                String nodeName = localName(node);
                if (name.equals(nodeName))
                    a.add((Element) node);
            }
        }
        return a;
    }

    /**
     * Get the local name of a node (that may have a prefix). For example <code>localName( "xsd:string" )</code> will
     * return <code>"string"</code>
     * 
     * @param name A node name (normally a DOM Element).
     * @return The local part of the name.
     */
    public static String localName(String name) {
        if (name == null || name.length() <= 0)
            return name;
        int p = name.indexOf(':');
        if (p < 0 || p + 1 >= name.length())
            return name;
        return name.substring(p + 1);
    }

    /**
     * Get the local name of a node (that may have a prefix).
     * 
     * @param name The node (normally a DOM Element).
     * @return The local part of the name.
     */
    public static String localName(Node node) {
        if (node == null)
            return null;
        String name = node.getLocalName();
        if (name == null || name.length() <= 0)
            return localName(node.getNodeName());
        return name;
    }
}