View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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          // Append trailing seperator
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  }