KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > util > LocatorFile


1 /*
2  * (c) Copyright 2004, 2005 Hewlett-Packard Development Company, LP
3  * All rights reserved.
4  * [See end of file]
5  */

6
7 package com.hp.hpl.jena.util;
8
9 import java.io.File JavaDoc;
10 import java.io.FileInputStream JavaDoc;
11 import java.io.IOException JavaDoc;
12 import java.io.InputStream JavaDoc;
13
14 import org.apache.commons.logging.*;
15
16 /** Location files in the filing system.
17  * A FileLocator can have a "current directory" - this is separate from any
18  * location mapping (see @link{LocationMapping}) as it applies only to files.
19  *
20  * @author Andy Seaborne
21  * @version $Id: LocatorFile.java,v 1.5 2005/02/21 12:18:57 andy_seaborne Exp $
22  */

23
24 public class LocatorFile implements Locator
25 {
26     static Log log = LogFactory.getLog(LocatorFile.class) ;
27     private String JavaDoc altDir = null ;
28     private String JavaDoc altDirLogStr = "" ;
29     
30     LocatorFile(String JavaDoc dir)
31     {
32         if ( false )
33         {
34             if ( dir == null )
35             {
36                 try {
37                     //String wd = System.getProperty("user.dir") ;
38
String JavaDoc wd = new File JavaDoc(".").getCanonicalPath() ;
39                     log.debug("Base file directory: "+wd) ;
40                 } catch (IOException JavaDoc ex)
41                 {
42                     log.error("Failed to discover the working directory", ex) ;
43                 }
44                 return ;
45             }
46             else
47             {
48                 log.debug("Base file directory: "+dir) ;
49             }
50         }
51         if ( dir != null )
52         {
53             if ( dir.endsWith("/") || dir.endsWith(java.io.File.separator) )
54                 dir = dir.substring(0,dir.length()-1) ;
55             altDirLogStr = " ["+dir+"]" ;
56         }
57         altDir = dir ;
58     }
59
60     LocatorFile()
61     {
62         this(null) ;
63     }
64     
65     private File JavaDoc toFile(String JavaDoc filenameOrURI)
66     {
67         String JavaDoc fn = FileUtils.toFilename(filenameOrURI) ;
68         if ( fn == null )
69             return null ;
70         
71         if ( altDir != null && ! fn.startsWith("/") && ! fn.startsWith(FileManager.filePathSeparator) )
72             fn = altDir+java.io.File.separator+fn ;
73                      
74         return new File JavaDoc(fn) ;
75     }
76     
77     
78     public boolean exists(String JavaDoc filenameOrURI)
79     {
80         File JavaDoc f = toFile(filenameOrURI) ;
81         
82         if ( f == null )
83             return false ;
84         
85         return f.exists() ;
86     }
87     
88     public InputStream JavaDoc open(String JavaDoc filenameOrURI)
89     {
90         File JavaDoc f = toFile(filenameOrURI) ;
91
92         if ( f == null || !f.exists() )
93         {
94             if ( FileManager.logAllLookups && log.isTraceEnabled())
95                 log.trace("Not found: "+filenameOrURI+altDirLogStr) ;
96             return null ;
97         }
98         
99         try {
100             InputStream JavaDoc in = new FileInputStream JavaDoc(f) ;
101             if ( in == null )
102             {
103                 // Should not happen
104
if ( FileManager.logAllLookups && log.isTraceEnabled() )
105                     log.trace("LocatorFile: Failed to open: "+filenameOrURI+altDirLogStr) ;
106                 return null ;
107             }
108             
109             if ( FileManager.logAllLookups && log.isTraceEnabled() )
110                 log.trace("Found: "+filenameOrURI+altDirLogStr) ;
111                 
112             
113             // Create base -- Java 1.4-isms
114
//base = f.toURI().toURL().toExternalForm() ;
115
//base = base.replaceFirst("^file:/([^/])", "file:///$1") ;
116
return in ;
117         } catch (IOException JavaDoc ioEx)
118         {
119             // Includes FileNotFoundException
120
// We already tested whether the file exists or not.
121
log.warn("File unreadable (but exists): "+f.getPath()+" Exception: "+ioEx.getMessage()) ;
122             return null ;
123         }
124     }
125     public String JavaDoc getName()
126     {
127         String JavaDoc tmp = "LocatorFile" ;
128         if ( altDir != null )
129             tmp = tmp+"("+altDir+")" ;
130         return tmp ;
131     }
132 }
133 /*
134  * (c) Copyright 2004, 2005 Hewlett-Packard Development Company, LP
135  * All rights reserved.
136  *
137  * Redistribution and use in source and binary forms, with or without
138  * modification, are permitted provided that the following conditions
139  * are met:
140  * 1. Redistributions of source code must retain the above copyright
141  * notice, this list of conditions and the following disclaimer.
142  * 2. Redistributions in binary form must reproduce the above copyright
143  * notice, this list of conditions and the following disclaimer in the
144  * documentation and/or other materials provided with the distribution.
145  * 3. The name of the author may not be used to endorse or promote products
146  * derived from this software without specific prior written permission.
147  *
148  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
149  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
150  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
151  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
152  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
153  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
154  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
155  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
156  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
157  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
158  */
Popular Tags