KickJava   Java API By Example, From Geeks To Geeks.

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


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.InputStream JavaDoc;
9 import java.util.*;
10
11 import com.hp.hpl.jena.JenaRuntime;
12 import com.hp.hpl.jena.vocabulary.LocationMappingVocab;
13 import com.hp.hpl.jena.rdf.model.*;
14 import com.hp.hpl.jena.shared.JenaException;
15
16 import org.apache.commons.logging.*;
17
18 /**
19  * Alternative locations for URIs. Maintains two maps:
20  * single item alternatives and alternative prefixes.
21  * To suggest an alternative location, first check the single items,
22  * then check the prefixes.
23  *
24  * A LocationMapper can be configured by an RDF file. The default for this
25  * is "etc/location-mapping.n3".
26  *
27  * There is a default LocationMapper which is used by the global @link{FileManager}.
28  *
29  * @see FileManager
30  *
31  * @author Andy Seaborne
32  * @version $Id: LocationMapper.java,v 1.9 2005/04/11 17:38:58 andy_seaborne Exp $
33  */

34
35 public class LocationMapper
36 {
37     static Log log = LogFactory.getLog(LocationMapper.class) ;
38     /** The default path for searching for the location mapper */
39     public static final String JavaDoc DEFAULT_PATH =
40         "file:location-mapping.rdf;file:location-mapping.n3;" +
41         "file:etc/location-mapping.rdf;file:etc/location-mapping.n3;" ;
42     public static final String JavaDoc GlobalMapperSystemProperty1 = "http://jena.hpl.hp.com/2004/08/LocationMap" ;
43     public static final String JavaDoc GlobalMapperSystemProperty2 = "LocationMap" ;
44     
45     static String JavaDoc s_globalMapperPath = null ;
46         
47     Map altLocations = new HashMap() ;
48     Map altPrefixes = new HashMap() ;
49     
50     static LocationMapper theMapper = null ;
51     
52     /** Get the global LocationMapper */
53     public static LocationMapper get()
54     {
55         if ( theMapper == null )
56         {
57             theMapper = new LocationMapper() ;
58             if ( getGlobalConfigPath() != null )
59             theMapper.initFromPath(getGlobalConfigPath(), false) ;
60         }
61         return theMapper ;
62     }
63
64     /** Create a LocationMapper with no mapping yet */
65     public LocationMapper() { }
66     
67     /** Create a LocationMapper from an existing model
68      * @see com.hp.hpl.jena.vocabulary.LocationMappingVocab
69      */

70     public LocationMapper(Model model)
71     {
72         processConfig(model) ;
73     }
74     
75     /** Create a LocationMapper from a config file */
76     public LocationMapper(String JavaDoc config)
77     {
78         initFromPath(config, true) ;
79     }
80     
81     private void initFromPath(String JavaDoc configPath, boolean configMustExist)
82     {
83         if ( configPath == null )
84         {
85             log.warn("Null configuration") ;
86             return ;
87         }
88         
89         // Make a file manager to look for the location mapping file
90
FileManager fm = new FileManager() ;
91         fm.addLocatorFile() ;
92         fm.addLocatorClassLoader(fm.getClass().getClassLoader()) ;
93         
94         try {
95             String JavaDoc uriConfig = null ;
96             InputStream JavaDoc in = null ;
97             
98             StringTokenizer pathElems = new StringTokenizer( configPath, FileManager.PATH_DELIMITER );
99             while (pathElems.hasMoreTokens()) {
100                 String JavaDoc uri = pathElems.nextToken();
101                 in = fm.openNoMap(uri) ;
102                 if ( in != null )
103                 {
104                     uriConfig = uri ;
105                     break ;
106                 }
107             }
108
109             if ( in == null )
110             {
111                 if ( ! configMustExist )
112                     log.debug("Failed to find configuration: "+configPath) ;
113                 return ;
114             }
115             String JavaDoc syntax = FileUtils.guessLang(uriConfig) ;
116             Model model = ModelFactory.createDefaultModel() ;
117             model.read(in, null, syntax) ;
118             processConfig(model) ;
119         } catch (JenaException ex)
120         {
121             LogFactory.getLog(LocationMapper.class).warn("Error in configuration file: "+ex.getMessage()) ;
122         }
123     }
124     
125     public String JavaDoc altMapping(String JavaDoc uri)
126     {
127         return altMapping(uri, uri) ;
128     }
129
130     /** Apply mappings: first try for an exact alternative location, then
131      * try to remap by prefix, finally, try the special case of filenames
132      * in a specific base directory.
133      * @param uri
134      * @param otherwise
135      * @return The alternative location choosen
136      */

137     public String JavaDoc altMapping(String JavaDoc uri, String JavaDoc otherwise)
138     {
139         if ( altLocations.containsKey(uri))
140             return (String JavaDoc)altLocations.get(uri) ;
141         String JavaDoc newStart = null ;
142         String JavaDoc oldStart = null ;
143         for ( Iterator iter = altPrefixes.keySet().iterator() ; iter.hasNext() ;)
144         {
145             String JavaDoc prefix = (String JavaDoc)iter.next() ;
146             if ( uri.startsWith(prefix) )
147             {
148                 String JavaDoc s = (String JavaDoc)altPrefixes.get(prefix) ;
149                 if ( newStart == null || newStart.length() < s.length() )
150                 {
151                     oldStart = prefix ;
152                     newStart = s ;
153                 }
154             }
155         }
156         
157         if ( newStart != null )
158             return newStart+uri.substring(oldStart.length()) ;
159         
160         return otherwise ;
161     }
162     
163
164     public void addAltEntry(String JavaDoc uri, String JavaDoc alt)
165     {
166         altLocations.put(uri, alt) ;
167     }
168
169     public void addAltPrefix(String JavaDoc uriPrefix, String JavaDoc altPrefix)
170     {
171         altPrefixes.put(uriPrefix, altPrefix) ;
172     }
173
174     /** Iterate over all the entries registered */
175     public Iterator listAltEntries() { return altLocations.keySet().iterator() ; }
176     /** Iterate over all the prefixes registered */
177     public Iterator listAltPrefixes() { return altPrefixes.keySet().iterator() ; }
178     
179     public void removeAltEntry(String JavaDoc uri)
180     {
181         altLocations.remove(uri) ;
182     }
183
184     public void removeAltPrefix(String JavaDoc uriPrefix)
185     {
186         altPrefixes.remove(uriPrefix) ;
187     }
188     public String JavaDoc getAltEntry(String JavaDoc uri)
189     {
190         return (String JavaDoc)altLocations.get(uri) ;
191     }
192
193     public String JavaDoc getAltPrefix(String JavaDoc uriPrefix)
194     {
195         return (String JavaDoc)altPrefixes.get(uriPrefix) ;
196     }
197     
198     
199     static private String JavaDoc getGlobalConfigPath()
200     {
201         if ( s_globalMapperPath == null )
202             s_globalMapperPath = JenaRuntime.getSystemProperty(GlobalMapperSystemProperty1,null) ;
203         if ( s_globalMapperPath == null )
204             s_globalMapperPath = JenaRuntime.getSystemProperty(GlobalMapperSystemProperty2,null) ;
205         if ( s_globalMapperPath == null )
206             s_globalMapperPath = DEFAULT_PATH ;
207         return s_globalMapperPath ;
208     }
209     
210     public String JavaDoc toString()
211     {
212         String JavaDoc s = "" ;
213         for ( Iterator iter = altLocations.keySet().iterator() ; iter.hasNext() ; )
214         {
215             String JavaDoc k = (String JavaDoc)iter.next() ;
216             String JavaDoc v = (String JavaDoc)altLocations.get(k) ;
217             s = s+"(Loc:"+k+"=>"+v+") " ;
218         }
219
220         for ( Iterator iter = altPrefixes.keySet().iterator() ; iter.hasNext() ; )
221         {
222             String JavaDoc k = (String JavaDoc)iter.next() ;
223             String JavaDoc v = (String JavaDoc)altLocations.get(k) ;
224             s = s+"(Prefix:"+k+"=>"+v+") " ;
225         }
226         return s ;
227     }
228     
229     private void processConfig(Model m)
230     {
231         StmtIterator mappings =
232             m.listStatements(null, LocationMappingVocab.mapping, (RDFNode)null) ;
233
234         for (; mappings.hasNext();)
235         {
236             Statement s = mappings.nextStatement() ;
237             Resource mapping = s.getResource() ;
238             
239             if ( mapping.hasProperty(LocationMappingVocab.name) )
240             {
241                 try
242                 {
243                     String JavaDoc name = mapping.getRequiredProperty(LocationMappingVocab.name)
244                                         .getString() ;
245                     String JavaDoc altName = mapping.getRequiredProperty(LocationMappingVocab.altName)
246                                         .getString() ;
247                     addAltEntry(name, altName) ;
248                     log.debug("Mapping: "+name+" => "+altName) ;
249                 } catch (JenaException ex)
250                 {
251                     log.warn("Error processing name mapping: "+ex.getMessage()) ;
252                     return ;
253                 }
254                 
255             }
256             
257             if ( mapping.hasProperty(LocationMappingVocab.prefix) )
258             {
259                 try
260                 {
261                     String JavaDoc prefix = mapping.getRequiredProperty(LocationMappingVocab.prefix)
262                                         .getString() ;
263                     String JavaDoc altPrefix = mapping.getRequiredProperty(LocationMappingVocab.altPrefix)
264                                         .getString() ;
265                     addAltPrefix(prefix, altPrefix) ;
266                     log.debug("Prefix mapping: "+prefix+" => "+altPrefix) ;
267                 } catch (JenaException ex)
268                 {
269                     log.warn("Error processing prefix mapping: "+ex.getMessage()) ;
270                     return ;
271                 }
272             }
273         }
274     }
275 }
276
277 /*
278  * (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
279  * All rights reserved.
280  *
281  * Redistribution and use in source and binary forms, with or without
282  * modification, are permitted provided that the following conditions
283  * are met:
284  * 1. Redistributions of source code must retain the above copyright
285  * notice, this list of conditions and the following disclaimer.
286  * 2. Redistributions in binary form must reproduce the above copyright
287  * notice, this list of conditions and the following disclaimer in the
288  * documentation and/or other materials provided with the distribution.
289  * 3. The name of the author may not be used to endorse or promote products
290  * derived from this software without specific prior written permission.
291  *
292  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
293  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
294  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
295  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
296  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
297  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
298  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
299  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
300  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
301  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
302  */

303
Popular Tags