KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openlaszlo > compiler > FileResolver


1 /* *****************************************************************************
2  * FileResolver.java
3 * ****************************************************************************/

4
5 /* J_LZ_COPYRIGHT_BEGIN *******************************************************
6 * Copyright 2001-2004 Laszlo Systems, Inc. All Rights Reserved. *
7 * Use is subject to license terms. *
8 * J_LZ_COPYRIGHT_END *********************************************************/

9
10 package org.openlaszlo.compiler;
11 import java.io.File JavaDoc;
12 import java.util.Vector JavaDoc;
13 import java.util.Enumeration JavaDoc;
14 import org.openlaszlo.server.*;
15 import org.apache.log4j.*;
16
17 /**
18  * Provides an interface for resolving a pathname to a File. The
19  * Compiler class uses this to resolve includes (hrefs).
20  *
21  * @author Oliver Steele
22  */

23 public interface FileResolver {
24     /** An instance of the DefaultFileResolver */
25     FileResolver DEFAULT_FILE_RESOLVER = new DefaultFileResolver();
26
27     /** Given a pathname, return the File that it names.
28      * @param pathname a path to resolve
29      * @param base a relative URI against which to resolve it
30      * @return see doc
31      * @exception java.io.FileNotFoundException if an error occurs
32      */

33     File JavaDoc resolve(String JavaDoc pathname, String JavaDoc base)
34         throws java.io.FileNotFoundException JavaDoc;
35 }
36
37 /** DefaultFileResolver maps each pathname onto the File that
38  * it names, without doing any directory resolution or other
39  * magic. (The operating system's notion of a working directory
40  * supplies the context for partial pathnames.)
41  */

42 class DefaultFileResolver implements FileResolver {
43
44     public DefaultFileResolver() {
45     }
46
47     /** @see FileResolver */
48     public File JavaDoc resolve(String JavaDoc pathname, String JavaDoc base)
49         throws java.io.FileNotFoundException JavaDoc
50     {
51         Logger mLogger = Logger.getLogger(FileResolver.class);
52
53         final String JavaDoc FILE_PROTOCOL = "file";
54         String JavaDoc protocol = FILE_PROTOCOL;
55
56         // The >1 test allows file pathnames to start with DOS
57
// drive letters.
58
int pos = pathname.indexOf(':');
59         if (pos > 1) {
60             protocol = pathname.substring(0, pos);
61             pathname = pathname.substring(pos + 1);
62         }
63         mLogger.debug("Resolving pathname: " + pathname + " and base: " + base);
64         if (!FILE_PROTOCOL.equals(protocol)) {
65             throw new CompilationError("unknown protocol: " + protocol);
66         }
67
68
69         File JavaDoc f = new File JavaDoc(base, pathname);
70         // FIXME: [ebloch] this vector should be in a properties file
71
Vector JavaDoc v = new Vector JavaDoc();
72         v.add(base);
73         if (!pathname.startsWith("./") && !pathname.startsWith("../")) {
74             v.add(LPS.getComponentsDirectory());
75             v.add(LPS.getFontDirectory());
76             v.add(LPS.getLFCDirectory());
77         }
78         
79         Enumeration JavaDoc e = v.elements();
80         while (e.hasMoreElements()) {
81             String JavaDoc dir = (String JavaDoc)e.nextElement();
82             f = new File JavaDoc(dir, pathname);
83             mLogger.debug("Trying " + f.getAbsolutePath());
84             if (f.exists()) {
85                 // TODO: [2002-11-23 ows] check for case mismatch
86
mLogger.debug("Resolving " + pathname + " to " +
87                               f.getAbsolutePath());
88                 /*
89                  * TODO: [2004-06-01 bloch] The code below results in weird compiler errors.
90                  * I think I was going to put it here so we could add the canvas info
91                  * resolver elements in one place.
92                 try {
93                     return f.getCanonicalFile();
94                 } catch (java.io.IOException ex) {
95                     throw new CompilationError("Can't get canonical file name " + f.getPath());
96                 }
97                 */

98                 return f;
99             }
100         }
101         throw new java.io.FileNotFoundException JavaDoc(pathname);
102     }
103 }
104
Popular Tags