View Javadoc

1   /***
2    * Copyright 2007 the original author or authors.
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   * For more information visit
11   *         http://72miles.com and
12   *         http://architecturerules.googlecode.com/svn/docs/index.html
13   */
14  
15  package org.seventytwomiles.springframework.util;
16  
17  
18  import java.io.File;
19  import java.io.FileNotFoundException;
20  import java.net.URL;
21  import java.net.URLDecoder;
22  
23  
24  
25  /***
26   * <p>Utility methods for resolving resource locations to files in the file
27   * system. Mainly for internal use within the framework. <a
28   * href="http://code.google.com/p/architecturerules/issues/detail?id=2&can=1">
29   * issue 2 (remove unnecessary dependencies)</a></p>
30   *
31   * @author Juergen Hoeller
32   */
33  public abstract class ResourceUtils {
34  
35  
36      /***
37       * <p>URL protocol for a file in the file system: "file"</p>
38       *
39       * @parameter
40       */
41      private static final String URL_PROTOCOL_FILE = "file";
42  
43  
44      /***
45       * <p>Resolve the given resource URL to a <code>java.io.File</code>, i.e. to
46       * a file in the file system.</p>
47       *
48       * @param resourceUrl the resource URL to resolve
49       * @param description a description of the original resource that the URL
50       * was created for (for example, a class path location)
51       * @return a corresponding File object
52       * @throws FileNotFoundException if the URL cannot be resolved to a file in
53       * the file system
54       */
55      public static File getFile(final URL resourceUrl, final String description)
56              throws FileNotFoundException {
57  
58          if (null == resourceUrl || "".equals(resourceUrl))
59              throw new IllegalArgumentException("resourceUrl must not be null");
60  
61          if (!URL_PROTOCOL_FILE.equals(resourceUrl.getProtocol()))
62              throw new FileNotFoundException(
63                      description + " cannot be resolved to absolute file path  because it does not reside in the file system: " + resourceUrl);
64  
65          return new File(URLDecoder.decode(resourceUrl.getFile()));
66      }
67  
68  
69  }