1 5 6 package com.hp.hpl.jena.util; 7 8 import java.io.InputStream ; 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 34 35 public class LocationMapper 36 { 37 static Log log = LogFactory.getLog(LocationMapper.class) ; 38 39 public static final String 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 GlobalMapperSystemProperty1 = "http://jena.hpl.hp.com/2004/08/LocationMap" ; 43 public static final String GlobalMapperSystemProperty2 = "LocationMap" ; 44 45 static String s_globalMapperPath = null ; 46 47 Map altLocations = new HashMap() ; 48 Map altPrefixes = new HashMap() ; 49 50 static LocationMapper theMapper = null ; 51 52 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 65 public LocationMapper() { } 66 67 70 public LocationMapper(Model model) 71 { 72 processConfig(model) ; 73 } 74 75 76 public LocationMapper(String config) 77 { 78 initFromPath(config, true) ; 79 } 80 81 private void initFromPath(String configPath, boolean configMustExist) 82 { 83 if ( configPath == null ) 84 { 85 log.warn("Null configuration") ; 86 return ; 87 } 88 89 FileManager fm = new FileManager() ; 91 fm.addLocatorFile() ; 92 fm.addLocatorClassLoader(fm.getClass().getClassLoader()) ; 93 94 try { 95 String uriConfig = null ; 96 InputStream in = null ; 97 98 StringTokenizer pathElems = new StringTokenizer( configPath, FileManager.PATH_DELIMITER ); 99 while (pathElems.hasMoreTokens()) { 100 String 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 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 altMapping(String uri) 126 { 127 return altMapping(uri, uri) ; 128 } 129 130 137 public String altMapping(String uri, String otherwise) 138 { 139 if ( altLocations.containsKey(uri)) 140 return (String )altLocations.get(uri) ; 141 String newStart = null ; 142 String oldStart = null ; 143 for ( Iterator iter = altPrefixes.keySet().iterator() ; iter.hasNext() ;) 144 { 145 String prefix = (String )iter.next() ; 146 if ( uri.startsWith(prefix) ) 147 { 148 String s = (String )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 uri, String alt) 165 { 166 altLocations.put(uri, alt) ; 167 } 168 169 public void addAltPrefix(String uriPrefix, String altPrefix) 170 { 171 altPrefixes.put(uriPrefix, altPrefix) ; 172 } 173 174 175 public Iterator listAltEntries() { return altLocations.keySet().iterator() ; } 176 177 public Iterator listAltPrefixes() { return altPrefixes.keySet().iterator() ; } 178 179 public void removeAltEntry(String uri) 180 { 181 altLocations.remove(uri) ; 182 } 183 184 public void removeAltPrefix(String uriPrefix) 185 { 186 altPrefixes.remove(uriPrefix) ; 187 } 188 public String getAltEntry(String uri) 189 { 190 return (String )altLocations.get(uri) ; 191 } 192 193 public String getAltPrefix(String uriPrefix) 194 { 195 return (String )altPrefixes.get(uriPrefix) ; 196 } 197 198 199 static private String 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 toString() 211 { 212 String s = "" ; 213 for ( Iterator iter = altLocations.keySet().iterator() ; iter.hasNext() ; ) 214 { 215 String k = (String )iter.next() ; 216 String v = (String )altLocations.get(k) ; 217 s = s+"(Loc:"+k+"=>"+v+") " ; 218 } 219 220 for ( Iterator iter = altPrefixes.keySet().iterator() ; iter.hasNext() ; ) 221 { 222 String k = (String )iter.next() ; 223 String v = (String )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 name = mapping.getRequiredProperty(LocationMappingVocab.name) 244 .getString() ; 245 String 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 prefix = mapping.getRequiredProperty(LocationMappingVocab.prefix) 262 .getString() ; 263 String 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 303 | Popular Tags |