Helper.java [src/usda/weru/util/table] Revision: default Date:
/*
* Helper.java
*
* Created on June 8, 2006, 11:54 AM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package usda.weru.util.table;
import java.io.File;
import java.io.IOException;
import java.io.UTFDataFormatException;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.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.jdom.Elements containing Color information.
// * @return An array of Colors.
// */
// public static Color[] parseColorList(List <Element> list){
// Color[] colors = new Color[list.size()];
// for (int i = 0; i < list.size(); i++){
// colors[i] = parseColor(list.get(i).getText());
// }
// return colors;
// }
//
private static 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 <b>true</b> if the object represents the value "true", <b>false</b> 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;
if (number.intValue() == 1) return true;
else return false;
}
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(File 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 jde){
//jde.printStackTrace();
}
catch (IOException ioe){
//ioe.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 <B>true</B> if the flag is set, <B>false</B> otherwise.
*/
public static boolean bitFlag(int flags, int flag){
return (flags & flag) == flag;
}
}