Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import org.w3c.dom.*;

public class Main {
    /** Helper method - gets a named attribute's value as a <I>float</I>.
       @param pNodeMap The <I>NamedNodeMap</I>.
       @param strName Name of the attribute.
       @return Value of the attribute.
     */
    public static float getAttributeFloat(NamedNodeMap pNodeMap, String strName)
            throws ClassCastException, NumberFormatException {
        double dblReturn = getAttributeDouble(pNodeMap, strName);

        if (Double.NaN == dblReturn)
            return Float.NaN;

        return (float) dblReturn;
    }

    /** Helper method - gets a named attribute's value as a <I>double</I>.
       @param pNodeMap The <I>NamedNodeMap</I>.
       @param strName Name of the attribute.
       @return Value of the attribute.
     */
    public static double getAttributeDouble(NamedNodeMap pNodeMap, String strName)
            throws ClassCastException, NumberFormatException {
        String strValue = getAttributeString(pNodeMap, strName);

        if (null == strValue)
            return Double.NaN;

        return Double.parseDouble(strValue);
    }

    /** Helper method - gets a named attribute's value as a <I>String</I>.
       @param pNodeMap The <I>NamedNodeMap</I>.
       @param strName Name of the attribute.
       @return Value of the attribute.
     */
    public static String getAttributeString(NamedNodeMap pNodeMap, String strName) throws ClassCastException {
        Attr pAttr = getAttribute(pNodeMap, strName);

        if (null == pAttr)
            return null;

        return pAttr.getValue();
    }

    /** Helper methods - gets the <I>Attr</I> <I>Node</I> object from a
    <I>NamedNodeMap</I> of attributes.
       @param pNodeMap The <I>NamedNodeMap</I>.
       @param strName Name of the attribute.
       @return An <I>Attr</I> object.
     */
    public static Attr getAttribute(NamedNodeMap pNodeMap, String strName) throws ClassCastException {
        Node pReturn = pNodeMap.getNamedItem(strName);

        if ((null == pReturn) || (pReturn instanceof Attr))
            return (Attr) pReturn;

        throw new ClassCastException("The node retrieved from the NamedNodeMap is not an Attr object.");
    }
}