1 package net.myvietnam.mvncore.configuration; 2 3 56 57 import java.io.File ; 58 import java.io.PrintStream ; 59 import java.io.PrintWriter ; 60 import java.io.StringWriter ; 61 import java.net.MalformedURLException ; 62 import java.net.URL ; 63 import java.util.Iterator ; 64 65 import org.apache.commons.lang.StringUtils; 66 67 73 public class ConfigurationUtils 74 { 75 76 protected static String fileSeparator = System.getProperty("file.separator"); 77 78 private ConfigurationUtils() 79 { 80 } 82 83 89 public static void dump(Configuration configuration, PrintStream out) 90 { 91 for (Iterator i = configuration.getKeys(); i.hasNext(); ) 92 { 93 String key = (String ) i.next(); 94 Object value = configuration.getProperty(key); 95 out.print(key); 96 out.print("="); 97 out.print(value); 98 if (i.hasNext()) 99 { 100 out.println(); 101 } 102 } 103 } 104 105 111 public static void dump(Configuration configuration, PrintWriter out) 112 { 113 for (Iterator i = configuration.getKeys(); i.hasNext();) 114 { 115 String key = (String ) i.next(); 116 Object value = configuration.getProperty(key); 117 out.print(key); 118 out.print("="); 119 out.print(value); 120 121 if (i.hasNext()) 122 { 123 out.println(); 124 } 125 } 126 } 127 128 135 public static String toString(Configuration configuration) 136 { 137 StringWriter writer = new StringWriter (); 138 dump(configuration, new PrintWriter (writer)); 139 return writer.toString(); 140 } 141 142 151 public static URL getURL(String basePath, String file) 152 throws MalformedURLException 153 { 154 File f = new File (file); 155 if(f.isAbsolute()) { 157 return f.toURL(); 158 } 159 160 try 161 { 162 if(basePath == null) 163 { 164 return new URL (file); 165 } 166 else 167 { 168 URL base = new URL (basePath); 169 return new URL (base, file); 170 } 171 } 172 catch(MalformedURLException uex) 173 { 174 return constructFile(basePath, file).toURL(); 175 } 176 } 177 178 186 static File constructFile(String basePath, String fileName) 187 { 188 File file = null; 190 if (StringUtils.isEmpty(basePath)) 191 { 192 file = new File (fileName); 194 } 195 else 196 { 197 StringBuffer fName = new StringBuffer (); 198 fName.append(basePath); 199 200 if (!basePath.endsWith(fileSeparator)) 202 { 203 fName.append(fileSeparator); 204 } 205 206 if (fileName.startsWith("." + fileSeparator)) 213 { 214 fName.append(fileName.substring(2)); 215 } 216 else 217 { 218 fName.append(fileName); 219 } 220 221 file = new File (fName.toString()); 222 } 223 return file; 224 } 225 } 226 | Popular Tags |