1 37 package net.sourceforge.cruisecontrol.util; 38 39 import net.sourceforge.cruisecontrol.CruiseControlException; 40 import org.jdom.Element; 41 import org.jdom.input.SAXBuilder; 42 43 import java.io.BufferedInputStream ; 44 import java.io.BufferedOutputStream ; 45 import java.io.BufferedReader ; 46 import java.io.File ; 47 import java.io.FileInputStream ; 48 import java.io.FileNotFoundException ; 49 import java.io.FileOutputStream ; 50 import java.io.FileReader ; 51 import java.io.IOException ; 52 import java.io.InputStream ; 53 import java.util.Properties ; 54 55 public final class Util { 56 57 private Util() { 58 } 59 60 public static Element loadConfigFile(File configFile) throws CruiseControlException { 61 try { 62 SAXBuilder builder = new SAXBuilder("org.apache.xerces.parsers.SAXParser"); 63 return builder.build(configFile).getRootElement(); 64 } catch (Exception e) { 65 throw new CruiseControlException( 66 "failed to load config file [" + (configFile != null 67 ? configFile.getName() 68 : "") + "]", 69 e); 70 } 71 } 72 73 public static Element parseConfig(InputStream in) throws CruiseControlException { 74 try { 75 SAXBuilder builder = new SAXBuilder(); 76 return builder.build(in).getRootElement(); 77 } catch (Exception e) { 78 throw new CruiseControlException("failed to parse configuration", e); 79 } 80 } 81 82 86 public static void deleteFile(File file) { 87 if (file == null || !file.exists()) { 88 return; 89 } 90 if (file.isDirectory()) { 91 File [] children = file.listFiles(); 92 for (int i = 0; i < children.length; i++) { 93 File child = children[i]; 94 deleteFile(child); 95 } 96 } 97 file.delete(); 98 } 99 100 public static boolean isWindows() { 101 String osName = Util.getOsName(); 102 return osName.indexOf("Windows") > -1; 103 } 104 105 public static String getOsName() { 106 return System.getProperty("os.name"); 107 } 108 109 121 public static Properties loadPropertiesFromFile(File file) 122 throws CruiseControlException, IOException { 123 Properties properties = new Properties (); 124 125 BufferedInputStream bis = null; 127 try { 128 bis = new BufferedInputStream (new FileInputStream (file)); 129 properties.load(bis); 130 } catch (FileNotFoundException e) { 131 throw new CruiseControlException( 132 "Could not load properties from file " 133 + file.getAbsolutePath() + ". It does not exist.", 134 e); 135 } finally { 136 if (bis != null) { 137 bis.close(); 138 } 139 } 140 141 return properties; 142 } 143 144 160 public static void storePropertiesToFile(Properties properties, 161 String header, File file) throws CruiseControlException, 162 IOException { 163 BufferedOutputStream bos = null; 164 165 try { 166 bos = new BufferedOutputStream (new FileOutputStream (file)); 167 properties.store(bos, header); 168 } catch (FileNotFoundException e) { 169 throw new CruiseControlException( 170 "Could not store properties to file " 171 + file.getAbsolutePath() + ". It does not exist.", 172 e); 173 } finally { 174 if (bos != null) { 175 bos.close(); 176 } 177 } 178 } 179 180 183 public static String readFileToString(String fileName) throws IOException { 184 StringBuffer out = new StringBuffer (); 185 appendFileToBuffer(fileName, out); 186 return out.toString(); 187 } 188 189 public static String readFileToString(File file) throws IOException { 190 BufferedReader reader = new BufferedReader (new FileReader (file)); 191 StringBuffer result = new StringBuffer (); 192 193 String s = reader.readLine(); 194 while (s != null) { 195 result.append(s.trim()); 196 s = reader.readLine(); 197 } 198 reader.close(); 199 200 return result.toString(); 201 } 202 203 206 public static void appendFileToBuffer(String fileName, StringBuffer out) throws IOException { 207 FileReader fr = null; 208 try { 209 fr = new FileReader (fileName); 210 char[] buff = new char[4096]; 211 int size = fr.read(buff, 0, 4096); 212 while (size > 0) { 213 out.append(buff, 0, size); 214 size = fr.read(buff, 0, 4096); 215 } 216 } finally { 217 if (fr != null) { 218 try { 219 fr.close(); 220 } catch (IOException ioe) { 221 } 223 } 224 } 225 } 226 } 227 | Popular Tags |