| %line | %branch | |||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| org.apache.commons.configuration.ConfigurationUtils |
|
|
| 1 | /* |
|
| 2 | * Copyright 2001-2005 The Apache Software Foundation. |
|
| 3 | * |
|
| 4 | * Licensed under the Apache License, Version 2.0 (the "License") |
|
| 5 | * you may not use this file except in compliance with the License. |
|
| 6 | * You may obtain a copy of the License at |
|
| 7 | * |
|
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
|
| 9 | * |
|
| 10 | * Unless required by applicable law or agreed to in writing, software |
|
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
|
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
| 13 | * See the License for the specific language governing permissions and |
|
| 14 | * limitations under the License. |
|
| 15 | */ |
|
| 16 | ||
| 17 | package org.apache.commons.configuration; |
|
| 18 | ||
| 19 | import java.io.File; |
|
| 20 | import java.io.IOException; |
|
| 21 | import java.io.InputStream; |
|
| 22 | import java.io.PrintStream; |
|
| 23 | import java.io.PrintWriter; |
|
| 24 | import java.io.StringWriter; |
|
| 25 | import java.net.MalformedURLException; |
|
| 26 | import java.net.URL; |
|
| 27 | import java.net.URLDecoder; |
|
| 28 | import java.util.Iterator; |
|
| 29 | ||
| 30 | import org.apache.commons.lang.StringUtils; |
|
| 31 | import org.apache.commons.logging.Log; |
|
| 32 | import org.apache.commons.logging.LogFactory; |
|
| 33 | ||
| 34 | /** |
|
| 35 | * Miscellaneous utility methods for configurations. |
|
| 36 | * |
|
| 37 | * @see ConfigurationConverter Utility methods to convert configurations. |
|
| 38 | * |
|
| 39 | * @author <a href="mailto:herve.quiroz@esil.univ-mrs.fr">Herve Quiroz</a> |
|
| 40 | * @author <a href="mailto:oliver.heger@t-online.de">Oliver Heger</a> |
|
| 41 | * @author Emmanuel Bourg |
|
| 42 | 40 | * @version $Revision$, $Date: 2005-12-14 16:20:30 +0100 (Wed, 14 Dec 2005) $ |
| 43 | */ |
|
| 44 | 20 | public final class ConfigurationUtils |
| 45 | { |
|
| 46 | /** Constant for the file URL protocol.*/ |
|
| 47 | static final String PROTOCOL_FILE = "file"; |
|
| 48 | 40 | |
| 49 | /** The logger.*/ |
|
| 50 | 20 | private static Log log = LogFactory.getLog(ConfigurationUtils.class); |
| 51 | ||
| 52 | /** |
|
| 53 | * Private constructor. Prevents instances from being created. |
|
| 54 | */ |
|
| 55 | private ConfigurationUtils() |
|
| 56 | 0 | { |
| 57 | // to prevent instanciation... |
|
| 58 | 0 | } |
| 59 | ||
| 60 | /** |
|
| 61 | * Dump the configuration key/value mappings to some ouput stream. |
|
| 62 | * |
|
| 63 | * @param configuration the configuration |
|
| 64 | * @param out the output stream to dump the configuration to |
|
| 65 | */ |
|
| 66 | public static void dump(Configuration configuration, PrintStream out) |
|
| 67 | { |
|
| 68 | 0 | dump(configuration, new PrintWriter(out)); |
| 69 | 0 | } |
| 70 | ||
| 71 | /** |
|
| 72 | * Dump the configuration key/value mappings to some writer. |
|
| 73 | * |
|
| 74 | * @param configuration the configuration |
|
| 75 | * @param out the writer to dump the configuration to |
|
| 76 | */ |
|
| 77 | 10 | public static void dump(Configuration configuration, PrintWriter out) |
| 78 | 32 | { |
| 79 | 5 | Iterator keys = configuration.getKeys(); |
| 80 | 28 | while (keys.hasNext()) |
| 81 | 12 | { |
| 82 | 18 | String key = (String) keys.next(); |
| 83 | 18 | Object value = configuration.getProperty(key); |
| 84 | 18 | out.print(key); |
| 85 | 6 | out.print("="); |
| 86 | 18 | out.print(value); |
| 87 | ||
| 88 | 10 | if (keys.hasNext()) |
| 89 | { |
|
| 90 | 2 | out.println(); |
| 91 | } |
|
| 92 | 10 | } |
| 93 | 10 | |
| 94 | 5 | out.flush(); |
| 95 | 5 | } |
| 96 | ||
| 97 | /** |
|
| 98 | * Get a string representation of the key/value mappings of a |
|
| 99 | * configuration. |
|
| 100 | * |
|
| 101 | * @param configuration the configuration |
|
| 102 | * @return a string representation of the configuration |
|
| 103 | */ |
|
| 104 | 10 | public static String toString(Configuration configuration) |
| 105 | 10 | { |
| 106 | 15 | StringWriter writer = new StringWriter(); |
| 107 | 5 | dump(configuration, new PrintWriter(writer)); |
| 108 | 5 | return writer.toString(); |
| 109 | } |
|
| 110 | ||
| 111 | /** |
|
| 112 | * Copy all properties from the source configuration to the target |
|
| 113 | * configuration. Properties in the target configuration are replaced with |
|
| 114 | * the properties with the same key in the source configuration. |
|
| 115 | * <em>Note:</em> This method won't work well on hierarchical configurations |
|
| 116 | * because it is not able to copy information about the properties' |
|
| 117 | * structure. So when dealing with hierarchical configuration objects their |
|
| 118 | * <code>{@link HierarchicalConfiguration#clone() clone()}</code> methods |
|
| 119 | * should be used. |
|
| 120 | * |
|
| 121 | * @param source the source configuration |
|
| 122 | * @param target the target configuration |
|
| 123 | * @since 1.1 |
|
| 124 | */ |
|
| 125 | 2 | public static void copy(Configuration source, Configuration target) |
| 126 | 8 | { |
| 127 | 1 | Iterator keys = source.getKeys(); |
| 128 | 8 | while (keys.hasNext()) |
| 129 | 4 | { |
| 130 | 2 | String key = (String) keys.next(); |
| 131 | 4 | target.setProperty(key, source.getProperty(key)); |
| 132 | } |
|
| 133 | 1 | } |
| 134 | ||
| 135 | /** |
|
| 136 | * Append all properties from the source configuration to the target |
|
| 137 | * configuration. Properties in the source configuration are appended to |
|
| 138 | * the properties with the same key in the target configuration. |
|
| 139 | * |
|
| 140 | * @param source the source configuration |
|
| 141 | * @param target the target configuration |
|
| 142 | * @since 1.1 |
|
| 143 | */ |
|
| 144 | 2 | public static void append(Configuration source, Configuration target) |
| 145 | 8 | { |
| 146 | 1 | Iterator keys = source.getKeys(); |
| 147 | 8 | while (keys.hasNext()) |
| 148 | 4 | { |
| 149 | 2 | String key = (String) keys.next(); |
| 150 | 4 | target.addProperty(key, source.getProperty(key)); |
| 151 | } |
|
| 152 | 1 | } |
| 153 | ||
| 154 | /** |
|
| 155 | * Constructs a URL from a base path and a file name. The file name can |
|
| 156 | * be absolute, relative or a full URL. If necessary the base path URL is |
|
| 157 | * applied. |
|
| 158 | * |
|
| 159 | * @param basePath the base path URL (can be <b>null</b>) |
|
| 160 | * @param file the file name |
|
| 161 | * @return the resulting URL |
|
| 162 | * @throws MalformedURLException if URLs are invalid |
|
| 163 | */ |
|
| 164 | 16 | public static URL getURL(String basePath, String file) throws MalformedURLException |
| 165 | 16 | { |
| 166 | 8 | File f = new File(file); |
| 167 | 12 | if (f.isAbsolute()) // already absolute? |
| 168 | { |
|
| 169 | 2 | return f.toURL(); |
| 170 | } |
|
| 171 | ||
| 172 | 12 | try |
| 173 | { |
|
| 174 | 10 | if (basePath == null) |
| 175 | { |
|
| 176 | 2 | return new URL(file); |
| 177 | } |
|
| 178 | 8 | else |
| 179 | 6 | { |
| 180 | 4 | URL base = new URL(basePath); |
| 181 | 3 | return new URL(base, file); |
| 182 | } |
|
| 183 | } |
|
| 184 | 4 | catch (MalformedURLException uex) |
| 185 | { |
|
| 186 | 2 | return constructFile(basePath, file).toURL(); |
| 187 | } |
|
| 188 | } |
|
| 189 | ||
| 190 | /** |
|
| 191 | * Helper method for constructing a file object from a base path and a |
|
| 192 | * file name. This method is called if the base path passed to |
|
| 193 | * <code>getURL()</code> does not seem to be a valid URL. |
|
| 194 | * |
|
| 195 | * @param basePath the base path |
|
| 196 | * @param fileName the file name |
|
| 197 | * @return the resulting file |
|
| 198 | */ |
|
| 199 | 1166 | static File constructFile(String basePath, String fileName) |
| 200 | { |
|
| 201 | 1749 | File file = null; |
| 202 | 1166 | |
| 203 | 583 | File absolute = null; |
| 204 | 1749 | if (fileName != null) |
| 205 | { |
|
| 206 | 583 | absolute = new File(fileName); |
| 207 | 1166 | } |
| 208 | ||
| 209 | 659 | if (StringUtils.isEmpty(basePath) || (absolute != null && absolute.isAbsolute())) |
| 210 | { |
|
| 211 | 38 | file = new File(fileName); |
| 212 | } |
|
| 213 | 1090 | else |
| 214 | 1090 | { |
| 215 | 545 | StringBuffer fName = new StringBuffer(); |
| 216 | 545 | fName.append(basePath); |
| 217 | 1090 | |
| 218 | // My best friend. Paranoia. |
|
| 219 | 1635 | if (!basePath.endsWith(File.separator)) |
| 220 | { |
|
| 221 | 545 | fName.append(File.separator); |
| 222 | } |
|
| 223 | ||
| 224 | // |
|
| 225 | // We have a relative path, and we have |
|
| 226 | // two possible forms here. If we have the |
|
| 227 | // "./" form then just strip that off first |
|
| 228 | 1090 | // before continuing. |
| 229 | // |
|
| 230 | 545 | if (fileName.startsWith("." + File.separator)) |
| 231 | { |
|
| 232 | 0 | fName.append(fileName.substring(2)); |
| 233 | } |
|
| 234 | 1090 | else |
| 235 | { |
|
| 236 | 545 | fName.append(fileName); |
| 237 | 1090 | } |
| 238 | ||
| 239 | 545 | file = new File(fName.toString()); |
| 240 | 1166 | } |
| 241 | ||
| 242 | 583 | return file; |
| 243 | } |
|
| 244 | ||
| 245 | /** |
|
| 246 | * Return the location of the specified resource by searching the user home |
|
| 247 | * directory, the current classpath and the system classpath. |
|
| 248 | * |
|
| 249 | * @param name the name of the resource |
|
| 250 | * |
|
| 251 | * @return the location of the resource |
|
| 252 | */ |
|
| 253 | public static URL locate(String name) |
|
| 254 | { |
|
| 255 | 0 | return locate(null, name); |
| 256 | } |
|
| 257 | ||
| 258 | /** |
|
| 259 | * Return the location of the specified resource by searching the user home |
|
| 260 | * directory, the current classpath and the system classpath. |
|
| 261 | * |
|
| 262 | * @param base the base path of the resource |
|
| 263 | * @param name the name of the resource |
|
| 264 | * |
|
| 265 | * @return the location of the resource |
|
| 266 | */ |
|
| 267 | 1172 | public static URL locate(String base, String name) |
| 268 | { |
|
| 269 | 580 | if (name == null) |
| 270 | 6 | { |
| 271 | // undefined, always return null |
|
| 272 | 3 | return null; |
| 273 | 1166 | } |
| 274 | ||
| 275 | 577 | URL url = null; |
| 276 | ||
| 277 | // attempt to create an URL directly |
|
| 278 | 1166 | try |
| 279 | { |
|
| 280 | 677 | if (base == null) |
| 281 | { |
|
| 282 | 50 | url = new URL(name); |
| 283 | } |
|
| 284 | 1066 | else |
| 285 | 86 | { |
| 286 | 527 | URL baseURL = new URL(base); |
| 287 | 37 | url = new URL(baseURL, name); |
| 288 | 86 | |
| 289 | // check if the file exists |
|
| 290 | 37 | InputStream in = null; |
| 291 | 86 | try |
| 292 | 68 | { |
| 293 | 37 | in = url.openStream(); |
| 294 | 28 | } |
| 295 | 18 | finally |
| 296 | { |
|
| 297 | 77 | if (in != null) |
| 298 | { |
|
| 299 | 28 | in.close(); |
| 300 | } |
|
| 301 | } |
|
| 302 | 68 | } |
| 303 | 68 | |
| 304 | 28 | log.debug("Configuration loaded from the URL " + url); |
| 305 | 28 | } |
| 306 | 1098 | catch (IOException e) |
| 307 | { |
|
| 308 | 549 | url = null; |
| 309 | } |
|
| 310 | 1166 | |
| 311 | // attempt to load from an absolute path |
|
| 312 | 1675 | if (url == null) |
| 313 | 1098 | { |
| 314 | 549 | File file = new File(name); |
| 315 | 549 | if (file.isAbsolute() && file.exists()) // already absolute? |
| 316 | { |
|
| 317 | 36 | try |
| 318 | 36 | { |
| 319 | 54 | url = file.toURL(); |
| 320 | 18 | log.debug("Configuration loaded from the absolute path " + name); |
| 321 | 18 | } |
| 322 | catch (MalformedURLException e) |
|
| 323 | { |
|
| 324 | 0 | e.printStackTrace(); |
| 325 | } |
|
| 326 | } |
|
| 327 | } |
|
| 328 | 1166 | |
| 329 | // attempt to load from the base directory |
|
| 330 | 577 | if (url == null) |
| 331 | { |
|
| 332 | 1062 | try |
| 333 | 1062 | { |
| 334 | 531 | File file = constructFile(base, name); |
| 335 | 1535 | if (file != null && file.exists()) |
| 336 | { |
|
| 337 | 502 | url = file.toURL(); |
| 338 | 1062 | } |
| 339 | ||
| 340 | 1535 | if (url != null) |
| 341 | { |
|
| 342 | 1564 | log.debug("Configuration loaded from the base path " + name); |
| 343 | } |
|
| 344 | 531 | } |
| 345 | catch (IOException e) |
|
| 346 | { |
|
| 347 | 0 | e.printStackTrace(); |
| 348 | } |
|
| 349 | } |
|
| 350 | 1166 | |
| 351 | // attempt to load from the user home directory |
|
| 352 | 577 | if (url == null) |
| 353 | { |
|
| 354 | 58 | try |
| 355 | 58 | { |
| 356 | 29 | File file = constructFile(System.getProperty("user.home"), name); |
| 357 | 31 | if (file != null && file.exists()) |
| 358 | { |
|
| 359 | 1 | url = file.toURL(); |
| 360 | 58 | } |
| 361 | ||
| 362 | 31 | if (url != null) |
| 363 | { |
|
| 364 | 1 | log.debug("Configuration loaded from the home path " + name); |
| 365 | 58 | } |
| 366 | ||
| 367 | 29 | } |
| 368 | catch (IOException e) |
|
| 369 | { |
|
| 370 | 0 | e.printStackTrace(); |
| 371 | } |
|
| 372 | } |
|
| 373 | 1166 | |
| 374 | // attempt to load from the context classpath |
|
| 375 | 633 | if (url == null) |
| 376 | 56 | { |
| 377 | 28 | ClassLoader loader = Thread.currentThread().getContextClassLoader(); |
| 378 | 84 | url = loader.getResource(name); |
| 379 | ||
| 380 | 56 | if (url != null) |
| 381 | { |
|
| 382 | 14 | log.debug("Configuration loaded from the context classpath (" + name + ")"); |
| 383 | } |
|
| 384 | } |
|
| 385 | 1166 | |
| 386 | // attempt to load from the system classpath |
|
| 387 | 605 | if (url == null) |
| 388 | { |
|
| 389 | 42 | url = ClassLoader.getSystemResource(name); |
| 390 | ||
| 391 | 14 | if (url != null) |
| 392 | { |
|
| 393 | 0 | log.debug("Configuration loaded from the system classpath (" + name + ")"); |
| 394 | } |
|
| 395 | 1166 | } |
| 396 | ||
| 397 | 577 | return url; |
| 398 | } |
|
| 399 | ||
| 400 | /** |
|
| 401 | * Return the path without the file name, for example http://xyz.net/foo/bar.xml |
|
| 402 | * results in http://xyz.net/foo/ |
|
| 403 | * |
|
| 404 | * @param url the URL from which to extract the path |
|
| 405 | * @return the path component of the passed in URL |
|
| 406 | */ |
|
| 407 | 20 | static String getBasePath(URL url) |
| 408 | { |
|
| 409 | 11 | if (url == null) |
| 410 | { |
|
| 411 | 0 | return null; |
| 412 | 20 | } |
| 413 | ||
| 414 | 31 | String s = url.toString(); |
| 415 | ||
| 416 | 17 | if (s.endsWith("/") || StringUtils.isEmpty(url.getPath())) |
| 417 | { |
|
| 418 | 3 | return s; |
| 419 | } |
|
| 420 | 14 | else |
| 421 | { |
|
| 422 | 8 | return s.substring(0, s.lastIndexOf("/") + 1); |
| 423 | } |
|
| 424 | } |
|
| 425 | ||
| 426 | /** |
|
| 427 | * Extract the file name from the specified URL. |
|
| 428 | * |
|
| 429 | * @param url the URL from which to extract the file name |
|
| 430 | * @return the extracted file name |
|
| 431 | */ |
|
| 432 | 16 | static String getFileName(URL url) |
| 433 | { |
|
| 434 | 11 | if (url == null) |
| 435 | { |
|
| 436 | 1 | return null; |
| 437 | 14 | } |
| 438 | ||
| 439 | 22 | String path = url.getPath(); |
| 440 | ||
| 441 | 10 | if (path.endsWith("/") || StringUtils.isEmpty(path)) |
| 442 | { |
|
| 443 | 1 | return null; |
| 444 | } |
|
| 445 | 12 | else |
| 446 | { |
|
| 447 | 7 | return path.substring(path.lastIndexOf("/") + 1); |
| 448 | } |
|
| 449 | } |
|
| 450 | ||
| 451 | /** |
|
| 452 | * Tries to convert the specified base path and file name into a file object. |
|
| 453 | * This method is called e.g. by the save() methods of file based |
|
| 454 | * configurations. The parameter strings can be relative files, absolute |
|
| 455 | * files and URLs as well. |
|
| 456 | * |
|
| 457 | * @param basePath the base path |
|
| 458 | * @param fileName the file name |
|
| 459 | * @return the file object (<b>null</b> if no file can be obtained) |
|
| 460 | */ |
|
| 461 | public static File getFile(String basePath, String fileName) |
|
| 462 | { |
|
| 463 | // Check if URLs are involved |
|
| 464 | URL url; |
|
| 465 | 56 | try |
| 466 | 10 | { |
| 467 | 28 | url = new URL(class="keyword">new URL(basePath), fileName); |
| 468 | 5 | } |
| 469 | 46 | catch (MalformedURLException mex1) |
| 470 | { |
|
| 471 | 69 | try |
| 472 | 4 | { |
| 473 | 23 | url = new URL(fileName); |
| 474 | 2 | } |
| 475 | 42 | catch (MalformedURLException mex2) |
| 476 | { |
|
| 477 | 21 | url = null; |
| 478 | } |
|
| 479 | 56 | } |
| 480 | ||
| 481 | 42 | if (url != null) |
| 482 | { |
|
| 483 | 7 | return fileFromURL(url); |
| 484 | 42 | } |
| 485 | ||
| 486 | 21 | return constructFile(basePath, fileName); |
| 487 | } |
|
| 488 | ||
| 489 | /** |
|
| 490 | * Tries to convert the specified URL to a file object. If this fails, |
|
| 491 | * <b>null</b> is returned. |
|
| 492 | * |
|
| 493 | * @param url the URL |
|
| 494 | * @return the resulting file object |
|
| 495 | */ |
|
| 496 | 1208 | public static File fileFromURL(URL url) |
| 497 | { |
|
| 498 | 1808 | if (PROTOCOL_FILE.equals(url.getProtocol())) |
| 499 | { |
|
| 500 | 602 | return new File(URLDecoder.decode(url.getPath())); |
| 501 | } |
|
| 502 | 4 | else |
| 503 | { |
|
| 504 | 2 | return null; |
| 505 | } |
|
| 506 | } |
|
| 507 | } |
| This report is generated by jcoverage, Maven and Maven JCoverage Plugin. |