KickJava   Java API By Example, From Geeks To Geeks.

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


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

5
6 package com.hp.hpl.jena.util;
7
8 import java.io.* ;
9 import java.util.* ;
10
11 import org.apache.commons.logging.*;
12 //import javax.servlet.* ;
13

14 import com.hp.hpl.jena.rdf.model.* ;
15 import com.hp.hpl.jena.shared.*;
16
17 /** FileManager
18  *
19  * A FileManager provides access to named file-like resources by opening
20  * InputStreams to things in the filing system, by URL (http: and file:) amd
21  * found by the classloader. It can also load RDF data from such a system
22  * resource into an existing model or create a new (Memory-based) model.
23  * There is a global FileManager which provide uniform access to system
24  * resources: applications may also create specialised FileManagers.
25  *
26  * A FileManager contains a list of location functions to try: the global
27  * FileManger has one {@link LocatorFile}, one {@link LocatorClassLoader} and
28  * one {@link LocatorURL}
29  *
30  * A FileManager works in conjunction with a LocationMapper.
31  * A {@link LocationMapper} is a set of alternative locations for system
32  * resources and a set of alternative prefix locations. For example, a local
33  * copy of a common RDF dataset may be used whenever the usual URL is used by
34  * the application.
35  *
36  * The FileManager also supports the idea of "current directory". This only
37  * applies to the LocatorFile and not, for example, items found by a classloader.
38  *
39  *
40  * @see LocationMapper
41  *
42  * @author Andy Seaborne
43  * @version $Id: FileManager.java,v 1.18 2005/03/08 13:12:47 andy_seaborne Exp $
44  */

45  
46 public class FileManager
47 {
48     /** Delimiter between path entries : because URI scheme names use : we only allow ; */
49     public static final String JavaDoc PATH_DELIMITER = ";";
50     public static final String JavaDoc filePathSeparator = java.io.File.separator ;
51     static Log log = LogFactory.getLog(FileManager.class) ;
52
53     static FileManager instance = null ;
54
55     static boolean logAllLookups = true ;
56     List handlers = new ArrayList() ;
57     LocationMapper mapper = null ;
58     boolean cacheModelLoads = false ;
59     Map modelCache = null ;
60     
61     
62     /** Get the global file manager.
63      * @return the global file manager
64      */

65     public static FileManager get()
66     {
67         // Singleton pattern adopted in case we later have several file managers.
68
if ( instance == null )
69             instance = makeGlobal() ;
70         return instance ;
71     }
72     
73     /** Create an uninitialized FileManager */
74     public FileManager() {}
75
76     /** Create a standard FileManager. */
77     private static FileManager makeGlobal()
78     {
79         FileManager fMgr = new FileManager(LocationMapper.get()) ;
80         fMgr.addLocatorFile() ;
81         fMgr.addLocatorURL() ;
82         fMgr.addLocatorClassLoader(fMgr.getClass().getClassLoader()) ;
83         return fMgr ;
84     }
85     
86     /** Create with the given location mapper */
87     public FileManager(LocationMapper _mapper)
88     {
89         setMapper(_mapper) ;
90     }
91
92     /** Set the location mapping */
93     public void setMapper(LocationMapper _mapper)
94     {
95         mapper = _mapper ;
96     }
97     
98     
99     /** Return an iterator over all the handlers */
100     public Iterator locators() { return handlers.listIterator() ; }
101
102     /** Add a locator to the end of the locators list */
103     public void addLocator(Locator loc)
104     {
105         log.debug("Add location: "+loc.getName()) ;
106         handlers.add(loc) ; }
107
108     /** Add a file locator */
109     public void addLocatorFile() { addLocatorFile(null) ; }
110
111     /** Add a file locator which uses dir as its working directory */
112     public void addLocatorFile(String JavaDoc dir)
113     {
114         LocatorFile fLoc = new LocatorFile(dir) ;
115         addLocator(fLoc) ;
116     }
117     
118     /** Add a class loader locator */
119     public void addLocatorClassLoader(ClassLoader JavaDoc cLoad)
120     {
121         LocatorClassLoader cLoc = new LocatorClassLoader(cLoad) ;
122         addLocator(cLoc) ;
123     }
124
125     /** Add a URL locator */
126     public void addLocatorURL()
127     {
128         Locator loc = new LocatorURL() ;
129         addLocator(loc) ;
130     }
131
132     /** Add a zip file locator */
133     public void addLocatorZip(String JavaDoc zfn)
134     {
135         Locator loc = new LocatorZip(zfn) ;
136         addLocator(loc) ;
137     }
138
139     
140     /** Remove a locator */
141     public void remove(Locator loc) { handlers.remove(loc) ; }
142
143     /** Reset the model cache */
144     public void resetCache()
145     {
146         if ( modelCache != null )
147         {
148             for ( Iterator iter = modelCache.keySet().iterator() ; iter.hasNext() ; )
149             {
150                 String JavaDoc name = (String JavaDoc)iter.next() ;
151                 Model m = (Model)modelCache.remove(name) ;
152                 if ( m != null )
153                     m.close() ;
154             }
155             modelCache = new HashMap() ;
156         }
157     }
158     
159     /** Change the state of model cache : does not clear the cache */
160     
161     public void setModelCaching(boolean state)
162     {
163         cacheModelLoads = state ;
164         if ( cacheModelLoads && modelCache == null )
165             modelCache = new HashMap() ;
166     }
167     
168     /** Load a model from a file (local or remote).
169      * Guesses the syntax of the file based on filename extension,
170      * defaulting to RDF/XML.
171      * @param filenameOrURI The filename or a URI (file:, http:)
172      * @return a new model
173      * @exception JenaException if there is syntax error in file.
174      */

175
176     public Model loadModel(String JavaDoc filenameOrURI)
177     { return loadModel(filenameOrURI, null) ; }
178
179     /** Load a model from a file (local or remote).
180      * URI is teh base for reading the model.
181      *
182      * @param filenameOrURI The filename or a URI (file:, http:)
183      * @param rdfSyntax RDF Serialization syntax.
184      * @return a new model
185      * @exception JenaException if there is syntax error in file.
186      */

187
188     public Model loadModel(String JavaDoc filenameOrURI, String JavaDoc rdfSyntax)
189     {
190         return loadModel(filenameOrURI, null, rdfSyntax) ;
191     }
192     
193     /** Load a model from a file (local or remote).
194      *
195      * @param filenameOrURI The filename or a URI (file:, http:)
196      * @param baseURI Base URI for loading the RDF model.
197      * @param rdfSyntax RDF Serialization syntax.
198      * @return a new model
199      * @exception JenaException if there is syntax error in file.
200     */

201
202     public Model loadModel(String JavaDoc filenameOrURI, String JavaDoc baseURI, String JavaDoc rdfSyntax)
203     {
204         if ( modelCache != null && modelCache.containsKey(filenameOrURI) )
205         {
206             log.debug("Model cache hit: "+filenameOrURI) ;
207             return (Model)modelCache.get(filenameOrURI) ;
208         }
209         
210         Model m = ModelFactory.createDefaultModel() ;
211         readModel(m, filenameOrURI, baseURI, rdfSyntax) ;
212         
213         if ( this.cacheModelLoads )
214         {
215             if ( modelCache == null )
216                 modelCache = new HashMap() ;
217             modelCache.put(filenameOrURI, m) ;
218         }
219         return m ;
220     }
221     
222     /**
223      * Read a file of RDF into a model.
224      * @param model
225      * @param filenameOrURI
226      * @return The model or null, if there was an error.
227      * @exception JenaException if there is syntax error in file.
228      */

229
230     public Model readModel(Model model, String JavaDoc filenameOrURI)
231     { return readModel(model, filenameOrURI, null); }
232     
233     /**
234      * Read a file of RDF into a model.
235      * @param model
236      * @param filenameOrURI
237      * @param rdfSyntax RDF Serialization syntax.
238      * @return The model or null, if there was an error.
239      * @exception JenaException if there is syntax error in file.
240      */

241
242     public Model readModel(Model model, String JavaDoc filenameOrURI, String JavaDoc rdfSyntax)
243     {
244         return readModel(model, filenameOrURI, null, rdfSyntax);
245     }
246
247     /**
248      * Read a file of RDF into a model.
249      * @param model
250      * @param filenameOrURI
251      * @param baseURI
252      * @param syntax
253      * @return The model
254      * @exception JenaException if there is syntax error in file.
255      */

256
257     public Model readModel(Model model, String JavaDoc filenameOrURI, String JavaDoc baseURI, String JavaDoc syntax)
258     {
259         if ( baseURI == null )
260             baseURI = chooseBaseURI(filenameOrURI) ;
261
262         String JavaDoc mappedURI = remap(filenameOrURI) ;
263
264         if ( syntax == null )
265         {
266             syntax = FileUtils.guessLang(mappedURI) ;
267             if ( syntax == null || syntax.equals("") )
268                 syntax = FileUtils.langXML ;
269             if ( log.isDebugEnabled() )
270                 log.debug("Syntax guess: "+syntax);
271         }
272
273         InputStream in = openNoMap(mappedURI) ;
274         if ( in == null )
275         {
276             if ( log.isTraceEnabled() )
277                 log.trace("Failed to locate '"+mappedURI+"'") ;
278             throw new NotFoundException("Not found: "+filenameOrURI) ;
279         }
280         model.read(in, baseURI, syntax) ;
281         return model ;
282     }
283
284     // replace with RelURI.resolve()
285
private static String JavaDoc chooseBaseURI(String JavaDoc baseURI)
286     {
287         String JavaDoc scheme = FileUtils.getScheme(baseURI) ;
288         
289         if ( scheme != null )
290         {
291             if ( scheme.equals("file") )
292             {
293                 if ( ! baseURI.startsWith("file:///") )
294                 {
295                     try {
296                         // Fix up file URIs. Yuk.
297
String JavaDoc tmp = baseURI.substring("file:".length()) ;
298                         File f = new File(tmp) ;
299                         baseURI = "file:///"+f.getCanonicalPath() ;
300                         baseURI = baseURI.replace('\\','/') ;
301                         // Convert to URI. Except that it removes ///
302
// Could do that and fix up (again)
303
//java.net.URL u = new java.net.URL(baseURI) ;
304
//baseURI = u.toExternalForm() ;
305
} catch (Exception JavaDoc ex) {}
306                 }
307             }
308             return baseURI ;
309         }
310             
311         if ( baseURI.startsWith("/") )
312             return "file://"+baseURI ;
313         return "file:"+baseURI ;
314     }
315     
316     /** Open a file using the locators of this FileManager */
317     public InputStream open(String JavaDoc filenameOrURI)
318     {
319         if ( log.isDebugEnabled())
320             log.debug("open("+filenameOrURI+")") ;
321         
322         String JavaDoc uri = remap(filenameOrURI) ;
323         
324         return openNoMap(uri) ;
325     }
326
327     /** Apply the mapping of a filename or URI */
328     
329     public String JavaDoc remap(String JavaDoc filenameOrURI)
330     {
331         if ( mapper == null )
332             return filenameOrURI ;
333             
334         String JavaDoc uri = mapper.altMapping(filenameOrURI, null) ;
335
336         if ( uri == null )
337         {
338             if ( FileManager.logAllLookups && log.isDebugEnabled() )
339                 log.debug("Not mapped: "+filenameOrURI) ;
340             uri = filenameOrURI ;
341         }
342         else
343         {
344             if ( log.isDebugEnabled() )
345                 log.debug("Mapped: "+filenameOrURI+" => "+uri) ;
346         }
347         return uri ;
348     }
349     
350     /** Slurp up a whole file */
351     public String JavaDoc readWholeFileAsUTF8(InputStream in)
352     {
353         try {
354             Reader r = FileUtils.asBufferedUTF8(in) ;
355             StringWriter sw = new StringWriter(1024);
356             char buff[] = new char[1024];
357             while (r.ready()) {
358                 int l = r.read(buff);
359                 if (l <= 0)
360                     break;
361                 sw.write(buff, 0, l);
362             }
363             r.close();
364             sw.close();
365             return sw.toString();
366         } catch (IOException ex)
367         {
368             throw new WrappedIOException(ex) ;
369         }
370     }
371     
372     /** Slurp up a whole file: map filename as necessary */
373     public String JavaDoc readWholeFileAsUTF8(String JavaDoc filename)
374     {
375         InputStream in = open(filename) ;
376         if ( in == null )
377             throw new NotFoundException("File not found: "+filename) ;
378         return readWholeFileAsUTF8(in) ;
379     }
380         
381     /** Open a file using the locators of this FileManager
382      * but without location mapping */

383     public InputStream openNoMap(String JavaDoc filenameOrURI)
384     {
385         for ( Iterator iter = handlers.iterator() ; iter.hasNext() ; )
386         {
387             Locator loc = (Locator)iter.next() ;
388             InputStream in = loc.open(filenameOrURI) ;
389             if ( in != null )
390             {
391                 if ( log.isDebugEnabled() )
392                     log.debug("Found: "+filenameOrURI+" ("+loc.getName()+")") ;
393                 return in ;
394             }
395         }
396         return null;
397     }
398 }
399
400
401
402 /*
403  * (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
404  * All rights reserved.
405  *
406  * Redistribution and use in source and binary forms, with or without
407  * modification, are permitted provided that the following conditions
408  * are met:
409  * 1. Redistributions of source code must retain the above copyright
410  * notice, this list of conditions and the following disclaimer.
411  * 2. Redistributions in binary form must reproduce the above copyright
412  * notice, this list of conditions and the following disclaimer in the
413  * documentation and/or other materials provided with the distribution.
414  * 3. The name of the author may not be used to endorse or promote products
415  * derived from this software without specific prior written permission.
416  *
417  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
418  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
419  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
420  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
421  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
422  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
423  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
424  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
425  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
426  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
427  */

428  
429
Popular Tags