<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">/*
 * Helper.java
 *
 * Created on June 8, 2006, 11:54 AM
 *
 */

package usda.weru.util.table;

import java.awt.Color;
import de.schlichtherle.truezip.file.TFile;


import java.io.IOException;
import java.io.UTFDataFormatException;
import java.util.List;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;

/**
 * A collection of static helper methods.
 * @author Joseph Levin
 */
public class Helper {
    
    /**
     * Converts a String to the Color it specifies.
     * @param color The String to be decoded.
     * @return The decoded Color.
     */
    public static Color parseColor(String color){        
        if (color == null) return null;
        try {                        
            return Color.decode(color);            
        }
        catch (NumberFormatException nfe){
            return null;
        }        
    }
    
    /**
     * Return an array of Colors that have been decoded from the List of jdom Elements provided.
     * @param list A List of org.jdom2.Elements containing Color information.
     * @return An array of Colors.
     */
    public static Color[] parseColorList(List &lt;Element&gt; list){
        Color[] colors = new Color[list.size()];        
        for (int i = 0; i &lt; list.size(); i++){
            colors[i] = parseColor(list.get(i).getText());
        }
        return colors;        
    }
    
    private static final String[] c_trueStrings = {"1", "true", "yes", "t", "y"};
    /**
     * Test whether the supplied object is considered true.
     * Valid values returning true are "1", "true", "yes", "t", "y", and the number 1.
     * @param o An Object to be tested.
     * @return &lt;b&gt;true&lt;/b&gt; if the object represents the value "true", &lt;b&gt;false&lt;/b&gt; otherwise.
     */
    public static boolean isTrue(Object o){
        if (o == null) return false;
        if (o instanceof String){
            String string = (String) o;
            for (String test : c_trueStrings){
                if (test.equalsIgnoreCase(string)) return true;
            }
            return false;
        }
        if (o instanceof Number){
            Number number = (Number) o;
            return number.intValue() == 1;
        }
        return Boolean.parseBoolean(o.toString());
    }
    
    
    
    /**
     * Loads the root node from an XML file into a jdom Element.
     * @param file A File pointing to an XML document.
     * @return The root node of the XML document.
     */
    public static Element getRootNode(TFile file){
        //TODO: Notify of fail
        if (file == null) return null;
        try{
            if (!file.exists()) return null;
            SAXBuilder builder = new SAXBuilder();
            Document document = builder.build(file);
            Element root = document.getRootElement();
            return root;
        }
        catch (UTFDataFormatException udfe){
            //udfe.printStackTrace();
        }
        catch (JDOMException | IOException jde){
            //jde.printStackTrace();
        }
        return null;
    }   
    
    /**
     * Test a flag in a set of flags.
     * @param flags The bitfield to be tested.
     * @param flag The flag to be tested.
     * @return &lt;B&gt;true&lt;/B&gt; if the flag is set, &lt;B&gt;false&lt;/B&gt; otherwise.
     */
    public static boolean bitFlag(int flags, int flag){
        return (flags &amp; flag) == flag;
    }
}
</pre></body></html>