1 18 19 package org.objectweb.jac.core; 20 21 import java.io.FileInputStream ; 22 import java.io.FileNotFoundException ; 23 import java.io.IOException ; 24 import java.util.*; 25 import org.apache.log4j.Logger; 26 27 30 31 public abstract class JacPropTools { 32 static Logger logger = Logger.getLogger("props"); 33 34 44 public static void fillListStringProps(List vector, 45 Properties pList, 46 String propertyName, 47 boolean force) 48 { 49 String prop = pList.getProperty(propertyName); 50 if (prop == null) { 51 logger.warn("\tNo property '"+propertyName +"' found."); 52 } 53 else { 54 logger.debug("\tVECTOR Property '"+propertyName+"' found."); 55 StringTokenizer st = new StringTokenizer( prop ); 56 logger.debug("\tProperty elements added:"); 57 String tmp; 58 while ( st.hasMoreElements() ) { 59 try { 60 tmp = (String )st.nextElement() ; 61 logger.debug("\t\tElement: " + tmp); 62 vector.add ( tmp ); 63 } 64 catch (Exception e) { 65 logger.debug("\t\tCan not get a reference for a class."); 66 e.printStackTrace(); 67 } 68 69 } 70 } 71 } 72 73 84 public static void fillSetProps(Set set, 85 Properties pList, 86 String propertyName, 87 boolean trim) 88 { 89 String prop = pList.getProperty(propertyName); 90 if (prop == null) { 91 logger.debug("\t-- WARNING: no property '"+ 92 propertyName +"' found."); 93 } else { 94 logger.debug("\tSET Property '"+ 95 propertyName+"' found."); 96 StringTokenizer st = new StringTokenizer( prop ); 97 logger.debug("\tProperty tokens added:"); 98 while ( st.hasMoreElements() ) { 99 String element = (String )st.nextElement(); 100 String tmp; 101 tmp = element; 106 tmp = tmp.trim(); 107 set.add(tmp); 108 logger.debug("\t\tToken: " + tmp); 109 } 110 } 111 } 112 113 114 128 public static void fillMapProps(Map hashTable, 129 Properties pList, 130 String propertyName, 131 int nElements, 132 boolean force) 133 { 134 String prop = pList.getProperty(propertyName); 135 if (prop == null) { 136 logger.debug("\t-- WARNING: no property '"+propertyName+"' found."); 137 } 138 else { 139 logger.debug("\tHASHTABLE Property '"+propertyName+"' found."); 140 StringTokenizer st = new StringTokenizer( prop ); 141 logger.debug("\tProperty couple added:"); 142 while ( st.hasMoreElements() ) { 143 String key = (String ) st.nextElement(); 144 Vector vvalue=new Vector(); 145 String value=null; 146 if (nElements>1) { 147 for(int i=0;i<nElements;i++) { 148 vvalue.add(((String )st.nextElement()).trim()); 149 } 150 } else if(nElements==0) { 151 String tmpvalue=""; 152 while(st.hasMoreElements()) { 153 tmpvalue=((String )st.nextElement()).trim(); 154 if(tmpvalue.equals(".")) break; 155 vvalue.add(tmpvalue); 156 } 157 } else { 158 value = ((String )st.nextElement()).trim(); 159 } 160 if (force) { 161 hashTable.put(key.trim(),value==null?(Object )vvalue:(Object )value); 162 logger.debug("\t\t(key,value): ("+ 163 key+","+(value==null?vvalue.toString():value)+")"); 164 } else if (!hashTable.containsKey(key)) { 165 hashTable.put(key.trim(),value==null?(Object )vvalue:(Object )value); 166 logger.debug("\t\t(key,value): ("+ 167 key+","+(value==null?vvalue.toString():value)+")"); 168 } 169 } 170 } 171 } 172 173 174 180 public static String fillStringProp(Properties pList, String propertyName) 181 { 182 String tmp = pList.getProperty(propertyName); 183 if ( tmp == null ) 184 logger.debug("\t-- WARNING: no property '"+propertyName+"' found."); 185 else 186 logger.debug("\tSTRING Property '"+propertyName+"' found."); 187 if ( tmp == null) 188 return null; 189 logger.debug("\tValue is "+tmp); 190 return tmp.trim(); 191 } 192 193 200 public static Properties getPropsFrom(String directory, String name) 201 { 202 Properties pList = new Properties(); 203 try { 204 FileInputStream fis = new FileInputStream (directory + name); 205 pList.load(fis); 206 logger.debug("Properties file '"+name+ 207 "' found in '"+directory +"'."); 208 return pList; 209 } 210 catch (FileNotFoundException e) { 211 logger.warn("No property file '"+ 212 name+"' found in the directory: '"+directory+"'."); 213 return null; 214 } 215 catch (IOException e) { 216 logger.warn("Can not load file '"+ 217 name+"' found in the directory: '" + 218 directory +"'."); 219 e.printStackTrace(); 220 return null; 221 } 222 } 223 224 225 } 226 | Popular Tags |