Java XML Attribute Get getAttribFloat(Element ele, String name)

Here you can find the source of getAttribFloat(Element ele, String name)

Description

Parses the given attribute of this tag and returns it as a float

License

Open Source License

Declaration

public static float getAttribFloat(Element ele, String name) 

Method Source Code


//package com.java2s;
import org.w3c.dom.*;

import java.util.regex.*;

public class Main {
    static final Matcher fpMatch = Pattern
            .compile("([-+]?((\\d*\\.\\d+)|(\\d+))([eE][+-]?\\d+)?)(\\%|in|cm|mm|pt|pc|px|em|ex)?").matcher("");

    /**//from  w ww  . ja  v a2 s. c o m
     * Parses the given attribute of this tag and returns it as a float
     */
    public static float getAttribFloat(Element ele, String name) {
        String sval = ele.getAttribute(name);
        float val = 0.0f;
        try {
            val = Float.parseFloat(sval);
        } catch (Exception e) {
        }

        return val;
    }

    public static float parseFloat(String val) {
        /*
        if (val == null) return 0f;
            
        float retVal = 0f;
        try
        { retVal = Float.parseFloat(val); }
        catch (Exception e)
        {}
        return retVal;
         */
        return findFloat(val);
    }

    /**
     * Searches the given string for the first floating point number it contains,
     * parses and returns it.
     */
    public synchronized static float findFloat(String val) {
        if (val == null)
            return 0f;

        fpMatch.reset(val);
        if (!fpMatch.find())
            return 0f;

        val = fpMatch.group(1);
        //System.err.println("Parsing " + val);

        float retVal = 0f;
        try {
            retVal = Float.parseFloat(val);
            String units = fpMatch.group(6);
            if ("%".equals(units))
                retVal /= 100;
        } catch (Exception e) {
        }
        return retVal;
    }
}

Related

  1. getAttrib(NamedNodeMap attribs, String name)
  2. getAttribAsBoolean(Element e, String name, Boolean dft)
  3. getAttribAsFloat(Element e, String name, Float dft)
  4. getAttribBoolean(Element ele, String name)
  5. getAttribByName(Element node, String name)
  6. getAttribIntHex(Element ele, String name)
  7. getAttribString(Element ele, String name)
  8. getAttribURL(Element ele, String name, URL docRoot)
  9. getAttribute(Element aElement, String aAttr, String aDefault)