001: /*
002: * Copyright 2005-2007 Noelios Consulting.
003: *
004: * The contents of this file are subject to the terms of the Common Development
005: * and Distribution License (the "License"). You may not use this file except in
006: * compliance with the License.
007: *
008: * You can obtain a copy of the license at
009: * http://www.opensource.org/licenses/cddl1.txt See the License for the specific
010: * language governing permissions and limitations under the License.
011: *
012: * When distributing Covered Code, include this CDDL HEADER in each file and
013: * include the License file at http://www.opensource.org/licenses/cddl1.txt If
014: * applicable, add the following below this CDDL HEADER, with the fields
015: * enclosed by brackets "[]" replaced with your own identifying information:
016: * Portions Copyright [yyyy] [name of copyright owner]
017: */
018:
019: package org.restlet.data;
020:
021: import java.io.File;
022:
023: /**
024: * Reference to a local (i.e. non remote) resource. It has helper methods for
025: * the three following schemes: {@link Protocol#CLAP}, {@link Protocol#FILE}
026: * and {@link Protocol#JAR}.
027: *
028: * @author Jerome Louvel (contact@noelios.com)
029: */
030: public final class LocalReference extends Reference {
031: /**
032: * The resources will be resolved from the classloader associated with the
033: * local class. Examples: clap://class/rootPkg/subPkg/myClass.class or
034: * clap://class/rootPkg/file.html
035: *
036: * @see java.lang.Class#getClassLoader()
037: */
038: public static final int CLAP_CLASS = 2;
039:
040: /**
041: * The resources will be resolved from the system's classloader. Examples:
042: * clap://system/rootPkg/subPkg/myClass.class or
043: * clap://system/rootPkg/file.html
044: *
045: * @see java.lang.ClassLoader#getSystemClassLoader()
046: */
047: public static final int CLAP_SYSTEM = 3;
048:
049: /**
050: * The resources will be resolved from the current thread's classloader.
051: * Examples: clap://thread/rootPkg/subPkg/myClass.class or
052: * clap://thread/rootPkg/file.html
053: *
054: * @see java.lang.Thread#getContextClassLoader()
055: */
056: public static final int CLAP_THREAD = 4;
057:
058: /**
059: * Constructor.
060: *
061: * @param authorityType
062: * The authority type for the resource path.
063: * @param path
064: * The resource path.
065: */
066: public static LocalReference createClapReference(int authorityType,
067: String path) {
068: return new LocalReference("clap://"
069: + getAuthorityName(authorityType) + path);
070: }
071:
072: /**
073: * Constructor.
074: *
075: * @param file
076: * The file whose path must be used.
077: *
078: */
079: public static LocalReference createFileReference(File file) {
080: return createFileReference(file.getAbsolutePath());
081: }
082:
083: /**
084: * Constructor.
085: *
086: * @param filePath
087: * The local file path.
088: */
089: public static LocalReference createFileReference(String filePath) {
090: return createFileReference("", filePath);
091: }
092:
093: /**
094: * Constructor.
095: *
096: * @param hostName
097: * The authority (can be a host name or the special
098: * "localhost" or an empty value).
099: * @param filePath
100: * The file path.
101: */
102: public static LocalReference createFileReference(String hostName,
103: String filePath) {
104: return new LocalReference("file://" + hostName + "/"
105: + normalizePath(filePath));
106: }
107:
108: /**
109: * Constructor.
110: *
111: * @param jarFile
112: * The JAR file reference.
113: * @param entryPath
114: * The entry path inside the JAR file.
115: */
116: public static LocalReference createJarReference(Reference jarFile,
117: String entryPath) {
118: return new LocalReference("jar:" + jarFile.toString() + "!/"
119: + entryPath);
120: }
121:
122: /**
123: * Returns an authority name.
124: *
125: * @param authority
126: * The authority.
127: * @return The name.
128: */
129: public static String getAuthorityName(int authority) {
130: String result = null;
131:
132: switch (authority) {
133: case CLAP_CLASS:
134: result = "class";
135: break;
136: case CLAP_SYSTEM:
137: result = "system";
138: break;
139: case CLAP_THREAD:
140: result = "thread";
141: break;
142: }
143:
144: return result;
145: }
146:
147: /**
148: * Localize a path by converting all the separator characters to the
149: * system-dependant separator character.
150: *
151: * @param path
152: * The path to localize.
153: * @return The localized path.
154: */
155: public static String localizePath(String path) {
156: StringBuilder result = new StringBuilder();
157: char nextChar;
158: for (int i = 0; i < path.length(); i++) {
159: nextChar = path.charAt(i);
160: if ((nextChar == '/') || (nextChar == '\\')) {
161: // Convert the URI separator to the system dependent path
162: // separator
163: result.append(File.separatorChar);
164: } else {
165: result.append(nextChar);
166: }
167: }
168:
169: return result.toString();
170: }
171:
172: /**
173: * Normalize a path by converting all the system-dependant separator
174: * characters to the standard '/' separator character.
175: *
176: * @param path
177: * The path to normalize.
178: * @return The normalize path.
179: */
180: public static String normalizePath(String path) {
181: StringBuilder result = new StringBuilder();
182: char nextChar;
183: for (int i = 0; i < path.length(); i++) {
184: nextChar = path.charAt(i);
185: if ((nextChar == '\\')) {
186: // Convert the Windows style path separator to the standard path
187: // separator
188: result.append('/');
189: } else {
190: result.append(nextChar);
191: }
192: }
193:
194: return result.toString();
195: }
196:
197: /**
198: * Constructor.
199: *
200: * @param localRef
201: * The local reference.
202: */
203: public LocalReference(Reference localRef) {
204: super (localRef.toString());
205: }
206:
207: /**
208: * Constructor.
209: *
210: * @param localUri
211: * The local URI.
212: */
213: public LocalReference(String localUri) {
214: super (localUri);
215: }
216:
217: /**
218: * Returns the type of authority.
219: *
220: * @return The type of authority.
221: */
222: public int getClapAuthorityType() {
223: int result = 0;
224:
225: if (getSchemeProtocol().equals(Protocol.CLAP)) {
226: String authority = getAuthority();
227:
228: if (authority != null) {
229: if (authority
230: .equalsIgnoreCase(getAuthorityName(CLAP_CLASS))) {
231: result = CLAP_CLASS;
232: } else if (authority
233: .equalsIgnoreCase(getAuthorityName(CLAP_SYSTEM))) {
234: result = CLAP_SYSTEM;
235: } else if (authority
236: .equalsIgnoreCase(getAuthorityName(CLAP_THREAD))) {
237: result = CLAP_THREAD;
238: }
239: }
240: }
241:
242: return result;
243: }
244:
245: /**
246: * Gets the local file corresponding to the reference. Only URIs referring
247: * to the "localhost" or to an empty authority are supported.
248: *
249: * @return The local file corresponding to the reference.
250: */
251: public File getFile() {
252: File result = null;
253:
254: if (getSchemeProtocol().equals(Protocol.FILE)) {
255: String hostName = getAuthority();
256:
257: if ((hostName == null) || hostName.equals("")
258: || hostName.equalsIgnoreCase("localhost")) {
259: String filePath = getPath();
260: result = new File(filePath);
261: } else {
262: throw new RuntimeException(
263: "Can't resolve files on remote host machines");
264: }
265: }
266:
267: return result;
268: }
269:
270: /**
271: * Returns the JAR entry path.
272: *
273: * @return The JAR entry path.
274: */
275: public String getJarEntryPath() {
276: String result = null;
277:
278: if (getSchemeProtocol().equals(Protocol.JAR)) {
279: String ssp = getSchemeSpecificPart();
280:
281: if (ssp != null) {
282: int separatorIndex = ssp.indexOf("!/");
283:
284: if (separatorIndex != -1) {
285: result = ssp.substring(separatorIndex + 2);
286: }
287: }
288: }
289:
290: return result;
291: }
292:
293: /**
294: * Returns the JAR file reference.
295: *
296: * @return The JAR file reference.
297: */
298: public Reference getJarFileRef() {
299: Reference result = null;
300:
301: if (getSchemeProtocol().equals(Protocol.JAR)) {
302: String ssp = getSchemeSpecificPart();
303:
304: if (ssp != null) {
305: int separatorIndex = ssp.indexOf("!/");
306:
307: if (separatorIndex != -1) {
308: result = new Reference(ssp.substring(0,
309: separatorIndex));
310: }
311: }
312: }
313:
314: return result;
315: }
316:
317: }
|