package edu.stanford.rt.util; import java.io.*; import java.util.*; //import org.apache.xerces.validators.datatype.*; import edu.stanford.rt.credential.*; /** * @author Ninghui Li, Sandra Qiu
* * Utility class * */ public class RTUtil { // print debug msg to specified outstream. private static PrintStream out = System.out; private static boolean DEBUG = false; static public void setOutputStream(PrintStream out) { RTUtil.out = out; } /** * Method debugInfo. * @param s */ static public void debugInfo(String s) { if (DEBUG) out.println(s); } /** * Method parseLong. * parses the given attribute value as a long. If attrValue cannot * be correctly parsed, then returns the given default value. * @param attrValue * the string to be parsed. * @param defValue * the default value * @return long */ static public long parseLong(String attrValue, long defValue) { if (attrValue.length() == 0) return defValue; long res; try { res = Long.parseLong(attrValue); } catch (NumberFormatException e) { res = defValue; } return res; } /** * Method parseInt. * parses the given attribute value as an integer. If the attrValue * cannot be correctly parsed, returns the given default value. * @param attrValue * the string to be parsed. * @param defValue * the default value. * @return int */ static public int parseInt(String attrValue, int defValue) { if (attrValue.length() == 0) return defValue; int res; try { res = Integer.parseInt(attrValue); } catch (NumberFormatException e) { res = defValue; } return res; } /** * Method parseBoolean. * parses the given attribute value as a boolean. * If attrValue is null or an empty string, return the * given default value. * @param attrValue * the string to be parsed. * @param defValue * the default value * @return boolean */ static public boolean parseBoolean( String attrValue, boolean defValue) { if (attrValue == null || attrValue.length() == 0) return defValue; return Boolean.valueOf(attrValue).booleanValue(); } // static public boolean validYear(String year) // { // boolean validYear = true; // try // { // int yr = Integer.parseInt(year); // if (yr < 0 || yr > 9999) // validYear = false; // } // catch (NumberFormatException e) // { // validYear = false; // } // return validYear; // } // // static public boolean isLeapYear(String year) // { // boolean isLeap = true; // // try // { // int yr = Integer.parseInt(year); // if (((yr % 4 == 0) && (yr % 100 != 0)) // || (yr % 400 == 0)) // isLeap = true; // else // isLeap = false; // } // catch (NumberFormatException e) // { // isLeap = false; // } // return isLeap; // // } // // static public boolean validMonth(String month) // { // boolean validMonth = true; // try // { // int mon = Integer.parseInt(month); // if (mon < 1 || mon > 12) // validMonth = false; // } // catch (NumberFormatException e) // { // validMonth = false; // } // return validMonth; // } // // static int[] legalDaysOfMonthInLeapYear = // { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; // static int[] legalDaysOfMonth = // { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; // static public boolean validDay( // String year, // String month, // String day) // { // boolean validDay = true; // if (!validYear(year)) // validDay = false; // if (!validMonth(month)) // validDay = false; // // try // { // int mon = Integer.parseInt(month); // int dy = Integer.parseInt(day); // if (dy < 0) // validDay = false; // if (isLeapYear(year)) // { // if (dy > legalDaysOfMonthInLeapYear[mon]) // validDay = false; // } // else // { // if (dy > legalDaysOfMonth[mon]) // validDay = false; // } // } // catch (NumberFormatException e) // { // validDay = false; // } // return validDay; // } // // static public boolean validHour(String hour) // { // boolean validHour = true; // try // { // int hr = Integer.parseInt(hour); // if (hr < 0 || hr > 23) // validHour = false; // } // catch (NumberFormatException e) // { // validHour = false; // } // return validHour; // } // // static public boolean validMinSec(String ms) // { // boolean valid = true; // try // { // int minSec = Integer.parseInt(ms); // if (minSec < 0 || minSec > 59) // valid = false; // } // catch (NumberFormatException e) // { // valid = false; // } // return valid; // } }