/
PrefixMapper.java
285 lines (246 loc) · 9.87 KB
/
PrefixMapper.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/**
* Copyright 2012 Martynas Jusevičius <martynas@atomgraph.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package com.atomgraph.client.locator;
import java.io.IOException;
import org.apache.jena.rdf.model.*;
import org.apache.jena.shared.JenaException;
import org.apache.jena.util.FileManager;
import org.apache.jena.util.FileUtils;
import org.apache.jena.util.LocationMapper;
import org.apache.jena.vocabulary.LocationMappingVocab;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.StringTokenizer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* A custom mapper implementation that maps URI prefixes to file names
*
* @author Martynas Jusevičius {@literal <martynas@atomgraph.com>}
* @see com.atomgraph.client.util.DataManager
*/
public class PrefixMapper extends LocationMapper
{
private static final Logger log = LoggerFactory.getLogger(PrefixMapper.class) ;
private final Map<String, String> altPrefixLocations = new HashMap<>() ;
public PrefixMapper()
{
if (log.isDebugEnabled()) log.debug("PrefixMapper()");
}
public PrefixMapper(String config)
{
initFromPath(config, true) ;
}
public PrefixMapper(LocationMapper locMapper)
{
super(locMapper);
}
public PrefixMapper(PrefixMapper prefixMapper)
{
super(prefixMapper);
altPrefixLocations.putAll(prefixMapper.altPrefixLocations);
}
public void addAltPrefixEntry(String uriPrefix, String alt)
{
if (log.isDebugEnabled()) log.debug("PrefixMapper.addAltPrefixEntry({}, {})", uriPrefix, alt);
altPrefixLocations.put(uriPrefix, alt);
}
public String getPrefixAltEntry(String uriPrefix)
{
return altPrefixLocations.get(uriPrefix) ;
}
public Iterator<String> listAltPrefixEntries() { return altPrefixLocations.keySet().iterator() ; }
public String getPrefix(String uri)
{
String prefix = null;
for (Iterator<String> iter = listAltPrefixEntries(); iter.hasNext();)
{
String candPrefix = iter.next() ;
// make sure we get the longest matching prefix
if (uri.startsWith(candPrefix) &&
(prefix == null || candPrefix.length() > prefix.length()))
prefix = candPrefix;
}
return prefix;
}
@Override
public String altMapping(String uri, String otherwise)
{
if (log.isDebugEnabled()) log.debug("PrefixMapper.altMapping({}, {})", uri, otherwise);
String altEntry = getAltEntry(uri);
if (altEntry != null)
{
if (log.isDebugEnabled()) log.debug("Returning existing altName mapping: {} for URI: {}", altEntry, uri);
return altEntry;
}
String prefix = getPrefix(uri);
String prefixAltEntry = getPrefixAltEntry(prefix);
if (prefix != null && prefixAltEntry != null)
{
if (log.isDebugEnabled()) log.debug("Returning existing altName mapping: {} for prefix: {}", prefixAltEntry, prefix);
return getPrefixAltEntry(prefix);
}
return super.altMapping(uri, otherwise);
}
@Override
public void toModel(Model model)
{
for ( Iterator<String> iter = listAltEntries() ; iter.hasNext() ; )
{
Resource r = model.createResource() ;
Resource e = model.createResource() ;
model.add(r, LocationMappingVocab.mapping, e) ;
String k = iter.next() ;
String v = getAltEntry(k) ;
model.add(e, LocationMappingVocab.name, k) ;
model.add(e, LocationMappingVocab.altName, v) ;
}
for ( Iterator<String> iter = listAltPrefixes() ; iter.hasNext() ; )
{
Resource r = model.createResource() ;
Resource e = model.createResource() ;
model.add(r, LocationMappingVocab.mapping, e) ;
String k = iter.next() ;
String v = getAltPrefix(k) ;
model.add(e, LocationMappingVocab.prefix, k) ;
model.add(e, LocationMappingVocab.altPrefix, v) ;
}
for ( Iterator<String> iter = listAltPrefixEntries() ; iter.hasNext() ; )
{
Resource r = model.createResource() ;
Resource e = model.createResource() ;
model.add(r, LocationMappingVocab.mapping, e) ;
String k = iter.next() ;
String v = getPrefixAltEntry(k) ;
model.add(e, LocationMappingVocab.prefix, k) ;
model.add(e, LocationMappingVocab.altName, v) ;
}
}
@Override
public void processConfig(Model m)
{
StmtIterator mappings =
m.listStatements(null, LocationMappingVocab.mapping, (RDFNode)null) ;
for (; mappings.hasNext();)
{
Statement s = mappings.nextStatement() ;
Resource mapping = s.getResource() ;
if ( mapping.hasProperty(LocationMappingVocab.name) )
{
try
{
String name = mapping.getRequiredProperty(LocationMappingVocab.name)
.getString() ;
String altName = mapping.getRequiredProperty(LocationMappingVocab.altName)
.getString() ;
addAltEntry(name, altName) ;
if (log.isDebugEnabled()) log.debug("Mapping: "+name+" => "+altName) ;
} catch (JenaException ex)
{
if (log.isWarnEnabled()) log.warn("Error processing name mapping: "+ex.getMessage()) ;
return ;
}
}
if ( mapping.hasProperty(LocationMappingVocab.prefix) )
{
try
{
String prefix = mapping.getRequiredProperty(LocationMappingVocab.prefix)
.getString() ;
if (mapping.hasProperty(LocationMappingVocab.altPrefix))
{
String altPrefix = mapping.getRequiredProperty(LocationMappingVocab.altPrefix)
.getString() ;
addAltPrefix(prefix, altPrefix) ;
if (log.isDebugEnabled()) log.debug("Prefix mapping: "+prefix+" => "+altPrefix) ;
}
if (mapping.hasProperty(LocationMappingVocab.altName))
{
String altName = mapping.getRequiredProperty(LocationMappingVocab.altName)
.getString() ;
addAltPrefixEntry(prefix, altName) ;
log.debug("Prefix/name mapping: "+prefix+" => "+altName) ;
}
} catch (JenaException ex)
{
if (log.isWarnEnabled()) log.warn("Error processing prefix mapping: "+ex.getMessage()) ;
return ;
}
}
}
}
private void initFromPath(String configPath, boolean configMustExist)
{
if ( configPath == null || configPath.length() == 0 )
{
if (log.isWarnEnabled()) log.warn("Null configuration") ;
return ;
}
// Make a file manager to look for the location mapping file
FileManager fm = FileManager.create();
fm.addLocatorFile() ;
fm.addLocatorClassLoader(fm.getClass().getClassLoader()) ;
try {
String uriConfig = null ;
InputStream in = null ;
StringTokenizer pathElems = new StringTokenizer( configPath, FileManager.PATH_DELIMITER );
while (pathElems.hasMoreTokens()) {
String uri = pathElems.nextToken();
if ( uri == null || uri.length() == 0 )
break ;
in = fm.openNoMap(uri) ;
if ( in != null )
{
uriConfig = uri ;
break ;
}
}
if ( in == null )
{
if ( ! configMustExist )
if (log.isDebugEnabled()) log.debug("Failed to find configuration: "+configPath) ;
return ;
}
String syntax = FileUtils.guessLang(uriConfig) ;
Model model = ModelFactory.createDefaultModel() ;
model.read(in, uriConfig, syntax) ;
processConfig(model) ;
in.close();
}
catch (JenaException ex)
{
LoggerFactory.getLogger(LocationMapper.class).warn("Error in configuration file: "+ex.getMessage()) ;
} catch (IOException ex)
{
LoggerFactory.getLogger(LocationMapper.class).warn("Error reading configuration: "+ex.getMessage()) ;
}
}
@Override
public String toString()
{
String s = super.toString();
for ( Iterator<String> iter = listAltPrefixEntries() ; iter.hasNext() ; )
{
String k = iter.next() ;
String v = getPrefixAltEntry(k) ;
s = s+"(Prefix:"+k+"=>"+v+") " ;
}
return s ;
}
}