Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/*
 * Copyright 2015 the original author or authors.
 *
 * 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.*;

public class Main {
    /**
     * Returns the long attribute value for the passed name in the passed node.
     * @param node the node to get the attribute from
     * @param name the name of the attribute
     * @param defaultValue the value to return if the node did not contain the attribute
     * @return The attribute value or the default value if it is not found.
     */
    public static long getLongAttributeByName(Node node, String name, long defaultValue) {
        String s = getAttributeByName(node, name, null);
        if (s == null)
            return defaultValue;
        try {
            return Long.parseLong(s.trim());
        } catch (Exception e) {
            return defaultValue;
        }
    }

    /**
     * Returns the attribute value for the passed name in the passed node.
     * @param node the node to get the attribute from
     * @param name the name of the attribute
     * @param defaultValue the value to return if the node did not contain the attribute
     * @return The attribute value or the default value if it is not found.
     */
    public static String getAttributeByName(Node node, String name, String defaultValue) {
        try {
            String val = getAttributeValueByName(node, name);
            if (val != null && !val.trim().isEmpty())
                return val.trim();
        } catch (Exception e) {
        }
        return defaultValue;
    }

    /**
     * Returns the value of a named node attribute in the form of a boolean
     * @param node The node to retrieve the attribute from
     * @param name The name of the attribute
     * @param defaultValue The default value if the attribute cannot be located or converted.
     * @return true or false
     */
    public static boolean getAttributeByName(Node node, String name, boolean defaultValue) {
        if (node == null || name == null)
            return defaultValue;
        try {
            return getAttributeBooleanByName(node.getAttributes(), name);
        } catch (Exception e) {
            return defaultValue;
        }
    }

    /**
     * Returns the value of a named node attribute in the form of an int
     * @param node The node to retrieve the attribute from
     * @param name The name of the attribute
     * @param defaultValue The default value if the attribute cannot be located or converted.
     * @return an int
     */
    public static int getAttributeByName(Node node, String name, int defaultValue) {
        if (node == null || name == null)
            return defaultValue;
        try {
            return new Double(getAttributeValueByName(node, name).trim()).intValue();
        } catch (Exception e) {
            return defaultValue;
        }
    }

    /**
     * Returns the value of a named node attribute in the form of a long
     * @param node The node to retrieve the attribute from
     * @param name The name of the attribute
     * @param defaultValue The default value if the attribute cannot be located or converted.
     * @return a long
     */
    public static long getAttributeByName(Node node, String name, long defaultValue) {
        if (node == null || name == null)
            return defaultValue;
        try {
            return new Double(getAttributeValueByName(node, name).trim()).longValue();
        } catch (Exception e) {
            return defaultValue;
        }
    }

    /**
     * Searches throgh the passed NamedNodeMap for an attribute and returns it if it is found.
     * If it is not found, returns a null.
     * @param nnm NamedNodeMap
     * @param name String
     * @return String
     */
    public static String getAttributeValueByName(final NamedNodeMap nnm, final String name) {
        for (int i = 0; i < nnm.getLength(); i++) {
            Attr attr = (Attr) nnm.item(i);
            if (attr.getName().equalsIgnoreCase(name)) {
                return attr.getValue();
            }
        }
        return null;
    }

    /**
     * Returns the attribute value for the passed name in the passed node.
     * @param node the node to get the attribute from
     * @param name the name of the attribute
     * @return The attribute value or null if it is not found.
     */
    public static String getAttributeValueByName(Node node, String name) {
        return getAttributeValueByName(node.getAttributes(), name);
    }

    /**
     * Searches throgh the passed NamedNodeMap for an attribute. If it is found, it will try to convert it to a boolean.
     * @param nnm NamedNodeMap
     * @param name String
     * @throws RuntimeException on any failure to parse a boolean
     * @return boolean
     */
    public static boolean getAttributeBooleanByName(NamedNodeMap nnm, String name) throws RuntimeException {
        for (int i = 0; i < nnm.getLength(); i++) {
            Attr attr = (Attr) nnm.item(i);
            if (attr.getName().equalsIgnoreCase(name)) {
                String tmp = attr.getValue().toLowerCase();
                if (tmp.equalsIgnoreCase("true"))
                    return true;
                if (tmp.equalsIgnoreCase("false"))
                    return false;
                throw new RuntimeException("Attribute " + name + " value not boolean:" + tmp);
            }
        }
        throw new RuntimeException("Attribute " + name + " not found.");
    }
}