Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;

public class Main {
    /**
     * Gets an integer value of the given attribute
     * @param node
     * @param attrName
     * @param defaultValue
     * @return
     */
    public static int getIntegerValue(Node node, String attrName, int defaultValue) {
        String value = getValue(node, attrName, null);

        if (value == null)
            return defaultValue;

        try {
            return Integer.valueOf(value);
        } catch (NumberFormatException e) {
            return defaultValue;
        }
    }

    /**
     * Gets a string value of the given attribute
     * @param node
     * @param attrName
     * @param defaultValue
     * @return
     */
    public static String getValue(Node node, String attrName, String defaultValue) {
        Node tmp;
        if (node == null)
            return defaultValue;

        NamedNodeMap attrs = node.getAttributes();
        if (attrs == null)
            return defaultValue;

        String value = (tmp = attrs.getNamedItem(attrName)) != null ? tmp.getNodeValue() : null;

        if (value == null)
            return defaultValue;

        return value;
    }
}