1/*2 * Licensed to the Apache Software Foundation (ASF) under one or more3 * contributor license agreements. See the NOTICE file distributed with4 * this work for additional information regarding copyright ownership.5 * The ASF licenses this file to You under the Apache License, Version 2.06 * (the "License"); you may not use this file except in compliance with7 * the License. You may obtain a copy of the License at8 * 9 * http://www.apache.org/licenses/LICENSE-2.010 * 11 * Unless required by applicable law or agreed to in writing, software12 * 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 and15 * limitations under the License.16 */17packageorg.apache.jetspeed.components.util.system;
1819import java.io.File;
20import java.io.IOException;
21import java.net.MalformedURLException;
22import java.net.URL;
2324/***25 * <p>26 * FSSystemResourceUtilImpl27 * </p>28 * <p>29 * Locates resources relative to the root file system path30 * </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 */36publicclassFSSystemResourceUtilImpl implements SystemResourceUtil37 {
38private String systemRoot;
3940/***41 * 42 * @param systemRoot The root from which all resource43 * URLs will be constructed.44 */45publicFSSystemResourceUtilImpl(String systemRoot) throws IOException
46 {
47 String absPath = new File(systemRoot).getCanonicalPath();
48// Append trailing seperator49if (endsWithSeperator(absPath))
50 {
51this.systemRoot = absPath;
52 }
53else54 {
55this.systemRoot = absPath + File.separator;
56 }
5758 }
5960/***61 * @see org.apache.jetspeed.components.util.system.SystemResourceUtil#getSystemRoot()62 */63public String getSystemRoot()
64 {
65return systemRoot;
66 }
6768/***69 * @see org.apache.jetspeed.components.util.system.SystemResourceUtil#getURL(java.lang.String)70 */71public URL getURL(String relativePath) throws MalformedURLException
72 {
73if (beginsWithSeperator(relativePath) && relativePath.length() > 1)
74 {
75returnnew File(systemRoot + relativePath.substring(1)).toURL();
76 }
77else78 {
79returnnew File(systemRoot + relativePath).toURL();
80 }
8182 }
8384privateboolean endsWithSeperator(String path)
85 {
86return path.endsWith("/") || path.endsWith(File.separator);
87 }
8889privateboolean beginsWithSeperator(String path)
90 {
91return path.startsWith("/") || path.startsWith(File.separator);
92 }
9394 }