1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.jetspeed.components.util.system;
18
19 import java.io.File;
20 import java.io.IOException;
21 import java.net.MalformedURLException;
22 import java.net.URL;
23
24 /***
25 * <p>
26 * FSSystemResourceUtilImpl
27 * </p>
28 * <p>
29 * Locates resources relative to the root file system path
30 * </p>
31 *
32 * @author <a href="mailto:weaver@apache.org">Scott T. Weaver</a>
33 * @version $Id: FSSystemResourceUtilImpl.java 516448 2007-03-09 16:25:47Z ate $
34 *
35 */
36 public class FSSystemResourceUtilImpl implements SystemResourceUtil
37 {
38 private String systemRoot;
39
40 /***
41 *
42 * @param systemRoot The root from which all resource
43 * URLs will be constructed.
44 */
45 public FSSystemResourceUtilImpl(String systemRoot) throws IOException
46 {
47 String absPath = new File(systemRoot).getCanonicalPath();
48
49 if (endsWithSeperator(absPath))
50 {
51 this.systemRoot = absPath;
52 }
53 else
54 {
55 this.systemRoot = absPath + File.separator;
56 }
57
58 }
59
60 /***
61 * @see org.apache.jetspeed.components.util.system.SystemResourceUtil#getSystemRoot()
62 */
63 public String getSystemRoot()
64 {
65 return systemRoot;
66 }
67
68 /***
69 * @see org.apache.jetspeed.components.util.system.SystemResourceUtil#getURL(java.lang.String)
70 */
71 public URL getURL(String relativePath) throws MalformedURLException
72 {
73 if (beginsWithSeperator(relativePath) && relativePath.length() > 1)
74 {
75 return new File(systemRoot + relativePath.substring(1)).toURL();
76 }
77 else
78 {
79 return new File(systemRoot + relativePath).toURL();
80 }
81
82 }
83
84 private boolean endsWithSeperator(String path)
85 {
86 return path.endsWith("/") || path.endsWith(File.separator);
87 }
88
89 private boolean beginsWithSeperator(String path)
90 {
91 return path.startsWith("/") || path.startsWith(File.separator);
92 }
93
94 }