001: /*
002: * (c) Copyright 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
003: * All rights reserved.
004: * [See end of file]
005: */
006:
007: package com.hp.hpl.jena.util;
008:
009: import java.io.File;
010: import java.io.FileInputStream;
011: import java.io.IOException;
012: import java.io.InputStream;
013: import java.security.AccessControlException;
014:
015: import org.apache.commons.logging.*;
016:
017: /** Location files in the filing system.
018: * A FileLocator can have a "current directory" - this is separate from any
019: * location mapping (see @link{LocationMapping}) as it applies only to files.
020: *
021: * @author Andy Seaborne
022: * @version $Id: LocatorFile.java,v 1.15 2008/01/02 12:07:43 andy_seaborne Exp $
023: */
024:
025: public class LocatorFile implements Locator {
026: static Log log = LogFactory.getLog(LocatorFile.class);
027: private String altDir = null;
028: private String altDirLogStr = "";
029:
030: public LocatorFile(String dir) {
031: // if ( false )
032: // {
033: // if ( dir == null )
034: // {
035: // try {
036: // //String wd = JenaRuntime.getSystemProperty("user.dir") ;
037: // String wd = new File(".").getCanonicalPath() ;
038: // log.debug("Base file directory: "+wd) ;
039: // } catch (IOException ex)
040: // {
041: // log.error("Failed to discover the working directory", ex) ;
042: // }
043: // return ;
044: // }
045: // else
046: // {
047: // log.debug("Base file directory: "+dir) ;
048: // }
049: // }
050: if (dir != null) {
051: if (dir.endsWith("/")
052: || dir.endsWith(java.io.File.separator))
053: dir = dir.substring(0, dir.length() - 1);
054: altDirLogStr = " [" + dir + "]";
055: }
056: altDir = dir;
057: }
058:
059: LocatorFile() {
060: this (null);
061: }
062:
063: public boolean equals(Object other) {
064: return other instanceof LocatorFile
065: && equals(altDir, ((LocatorFile) other).altDir);
066: }
067:
068: private boolean equals(String a, String b) {
069: return a == null ? b == null : a.equals(b);
070: }
071:
072: public int hashCode() {
073: return altDir.hashCode();
074: }
075:
076: private File toFile(String filenameOrURI) {
077: String fn = FileUtils.toFilename(filenameOrURI);
078: if (fn == null)
079: return null;
080:
081: if (altDir != null && !fn.startsWith("/")
082: && !fn.startsWith(FileManager.filePathSeparator))
083: fn = altDir + java.io.File.separator + fn;
084:
085: return new File(fn);
086: }
087:
088: public boolean exists(String filenameOrURI) {
089: File f = toFile(filenameOrURI);
090:
091: if (f == null)
092: return false;
093:
094: return f.exists();
095: }
096:
097: public TypedStream open(String filenameOrURI) {
098: // Worry about %20.
099: // toFile calls FileUtils.toFilename(filenameOrURI) ;
100: File f = toFile(filenameOrURI);
101:
102: try {
103: if (f == null || !f.exists()) {
104: if (FileManager.logAllLookups && log.isTraceEnabled())
105: log.trace("Not found: " + filenameOrURI
106: + altDirLogStr);
107: return null;
108: }
109: } catch (AccessControlException e) {
110: log.warn("Security problem testing for file", e);
111: return null;
112: }
113:
114: try {
115: InputStream in = new FileInputStream(f);
116: if (in == null) {
117: // Should not happen
118: if (FileManager.logAllLookups && log.isTraceEnabled())
119: log.trace("LocatorFile: Failed to open: "
120: + filenameOrURI + altDirLogStr);
121: return null;
122: }
123:
124: if (FileManager.logAllLookups && log.isTraceEnabled())
125: log.trace("Found: " + filenameOrURI + altDirLogStr);
126:
127: // Create base -- Java 1.4-isms
128: //base = f.toURI().toURL().toExternalForm() ;
129: //base = base.replaceFirst("^file:/([^/])", "file:///$1") ;
130: return new TypedStream(in);
131: } catch (IOException ioEx) {
132: // Includes FileNotFoundException
133: // We already tested whether the file exists or not.
134: log.warn("File unreadable (but exists): " + f.getPath()
135: + " Exception: " + ioEx.getMessage());
136: return null;
137: }
138: }
139:
140: public String getName() {
141: String tmp = "LocatorFile";
142: if (altDir != null)
143: tmp = tmp + "(" + altDir + ")";
144: return tmp;
145: }
146: }
147: /*
148: * (c) Copyright 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
149: * All rights reserved.
150: *
151: * Redistribution and use in source and binary forms, with or without
152: * modification, are permitted provided that the following conditions
153: * are met:
154: * 1. Redistributions of source code must retain the above copyright
155: * notice, this list of conditions and the following disclaimer.
156: * 2. Redistributions in binary form must reproduce the above copyright
157: * notice, this list of conditions and the following disclaimer in the
158: * documentation and/or other materials provided with the distribution.
159: * 3. The name of the author may not be used to endorse or promote products
160: * derived from this software without specific prior written permission.
161: *
162: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
163: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
164: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
165: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
166: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
167: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
168: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
169: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
170: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
171: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
172: */
|