Home | History | Annotate | Download | only in annotations
      1 package org.testng.internal.annotations;
      2 
      3 import org.testng.collections.Lists;
      4 import org.testng.internal.ClassHelper;
      5 import org.testng.internal.Utils;
      6 
      7 import java.util.List;
      8 import java.util.StringTokenizer;
      9 
     10 /**
     11  * Convert a string values into primitive types.
     12  *
     13  * Created on Dec 20, 2005
     14  * @author cbeust
     15  */
     16 public class Converter {
     17 
     18   public static boolean  getBoolean(String tagValue, boolean def) {
     19     boolean result = def;
     20     if (tagValue != null) {
     21       result = Boolean.valueOf(tagValue);
     22     }
     23     return result;
     24   }
     25 
     26   public static int getInt(String tagValue, int def) {
     27     int result = def;
     28     if (tagValue != null) {
     29       result = Integer.parseInt(tagValue);
     30     }
     31     return result;
     32   }
     33 
     34   public static String getString(String tagValue, String def) {
     35     String result = def;
     36     if (tagValue != null) {
     37       result = tagValue;
     38     }
     39     return result;
     40   }
     41 
     42   public static long getLong(String tagValue, long def) {
     43     long result = def;
     44     if (tagValue != null) {
     45       result = Long.parseLong(tagValue);
     46     }
     47     return result;
     48   }
     49 
     50   public static String[] getStringArray(String tagValue, String[] def) {
     51     String[] result = def;
     52     if (tagValue != null) {
     53       result = Utils.stringToArray(tagValue);
     54     }
     55 
     56     return result;
     57   }
     58 
     59   public static Class[] getClassArray(String tagValue, Class[] def) {
     60     Class[] result = def;
     61     List vResult = Lists.newArrayList();
     62     if (tagValue != null) {
     63       StringTokenizer st = new StringTokenizer(tagValue, " ,");
     64       while (st.hasMoreElements()) {
     65         String className = (String) st.nextElement();
     66         try {
     67           Class cls = Class.forName(className);
     68           vResult.add(cls);
     69         }
     70         catch (ClassNotFoundException e) {
     71           e.printStackTrace();
     72         }
     73       }
     74       result = (Class[]) vResult.toArray(new Class[vResult.size()]);
     75     }
     76 
     77     return result;
     78   }
     79 
     80   public static Class getClass(String namedParameter) {
     81     Class result = null;
     82     if (namedParameter != null) {
     83       result = ClassHelper.forName(namedParameter);
     84     }
     85 
     86     return result;
     87   }
     88 
     89 }
     90