01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * 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: private String systemRoot;
38:
39: /**
40: *
41: * @param systemRoot The root from which all resource
42: * URLs will be constructed.
43: */
44: public FSSystemResourceUtilImpl(String systemRoot)
45: throws IOException {
46: String absPath = new File(systemRoot).getCanonicalPath();
47: // Append trailing seperator
48: if (endsWithSeperator(absPath)) {
49: this .systemRoot = absPath;
50: } else {
51: this .systemRoot = absPath + File.separator;
52: }
53:
54: }
55:
56: /**
57: * @see org.apache.jetspeed.components.util.system.SystemResourceUtil#getSystemRoot()
58: */
59: public String getSystemRoot() {
60: return systemRoot;
61: }
62:
63: /**
64: * @see org.apache.jetspeed.components.util.system.SystemResourceUtil#getURL(java.lang.String)
65: */
66: public URL getURL(String relativePath) throws MalformedURLException {
67: if (beginsWithSeperator(relativePath)
68: && relativePath.length() > 1) {
69: return new File(systemRoot + relativePath.substring(1))
70: .toURL();
71: } else {
72: return new File(systemRoot + relativePath).toURL();
73: }
74:
75: }
76:
77: private boolean endsWithSeperator(String path) {
78: return path.endsWith("/") || path.endsWith(File.separator);
79: }
80:
81: private boolean beginsWithSeperator(String path) {
82: return path.startsWith("/") || path.startsWith(File.separator);
83: }
84:
85: }
|