57
57
public final class RdfMap extends AbstractReadOnlyMap <String , String > {
58
58
public static final String TARGET = "target" ;
59
59
public static final String TARGET_LANGUAGE = "target_language" ;
60
+ public static final String SELECT = "select" ;
60
61
private static final int MAX_REDIRECTIONS = 10 ;
61
62
private static final int MIN_HTTP_STATUS_CODE = 299 ;
62
63
private static final int MAX_HTTP_STATUS_CODE = 400 ;
@@ -66,6 +67,7 @@ public final class RdfMap extends AbstractReadOnlyMap<String, String> {
66
67
private final Map <String , String > map = new HashMap <>();
67
68
private String targetLanguage ;
68
69
private String target ;
70
+ private Select select = Select .DEFAULT ;
69
71
70
72
/**
71
73
* Creates an instance of {@link RdfMap}.
@@ -145,7 +147,7 @@ private void loadFile(final String file) {
145
147
*/
146
148
@ Override
147
149
public String get (final Object key ) {
148
- String ret ;
150
+ String ret = null ;
149
151
if (map .containsKey (key .toString ())) {
150
152
ret = map .get (key .toString ());
151
153
}
@@ -156,20 +158,29 @@ public String get(final Object key) {
156
158
final Resource resource = ResourceFactory .createResource (key .toString ());
157
159
final Property targetProperty = ResourceFactory .createProperty (target );
158
160
try {
159
- //first try to get LITERAL using SUBJECT and PROPERTY
160
- if (!targetLanguage .isEmpty ()) {
161
- ret = model .getRequiredProperty (resource , targetProperty , targetLanguage ).getString ();
161
+ if (select .equals (Select .SUBJECT )) {
162
+ ret = getSubjectUsingPropertyAndLiteral (key , targetProperty );
162
163
}
163
164
else {
164
- ret = model .getRequiredProperty (resource , targetProperty ).getString ();
165
+ //first try to get LITERAL using SUBJECT and PROPERTY
166
+ if (!targetLanguage .isEmpty ()) {
167
+ ret = model .getRequiredProperty (resource , targetProperty , targetLanguage ).getString ();
168
+ }
169
+ else {
170
+ ret = model .getRequiredProperty (resource , targetProperty ).getString ();
171
+ }
165
172
}
166
173
}
167
174
catch (final PropertyNotFoundException | NullPointerException | NoSuchElementException e ) {
168
175
//second try to get SUBJECT using PROPERTY and LITERAL
169
- ret = getSubjectUsingPropertyAndLiteral (key , targetProperty );
176
+ if (select .equals (Select .DEFAULT )) {
177
+ ret = getSubjectUsingPropertyAndLiteral (key , targetProperty );
178
+ }
170
179
//third try: get LITERAL of PREDICATE A using PREDICATE B
171
- if (ret == null ) {
172
- ret = getLiteralOfPredicateUsingOtherPredicate (key , targetProperty );
180
+ if (!select .equals (Select .SUBJECT )) {
181
+ if (ret == null ) {
182
+ ret = getLiteralOfPredicateUsingOtherPredicate (key , targetProperty );
183
+ }
173
184
}
174
185
}
175
186
map .put (key .toString (), ret );
@@ -270,6 +281,37 @@ public void setTarget(final String target) {
270
281
this .target = target ;
271
282
}
272
283
284
+ /**
285
+ * Gets whether the Subject or the Object or a mixture of both should be retrieved in the RDF.
286
+ * <br>
287
+ * Setting "select" is optional.
288
+ *
289
+ * @return the selected position to be retrieved
290
+ **/
291
+ public String getSelect () {
292
+ return select .toString ();
293
+ }
294
+
295
+ /**
296
+ * Sets whether the Subject or the Object or a mixture of both should be retrieved in the RDF.
297
+ * <br>
298
+ * Setting "select" is optional.
299
+ * <strong>Defaults to retrieve both: tries to get "objects" and as a fallback "subjects".</strong>
300
+ *
301
+ * @param position the position to be retrieved. Can be "subject" or "object".
302
+ */
303
+ public void setSelect (final String position ) {
304
+ if ("subject" .equalsIgnoreCase (position )) {
305
+ select = Select .SUBJECT ;
306
+ }
307
+ else if ("object" .equalsIgnoreCase (position )) {
308
+ select = Select .OBJECT ;
309
+ }
310
+ else {
311
+ throw new FixExecutionException ("Couldn't set parameter - use 'subject' or 'object' as value" );
312
+ }
313
+ }
314
+
273
315
/**
274
316
* Sets the default value returned if the key couldn't be found.
275
317
* <br>
@@ -283,8 +325,9 @@ public void setDefault(final String defaultValue) {
283
325
284
326
/**
285
327
* Gets a redirected URL, if any redirection takes place. Adapted predated code from org.apache.jena.rdfxml.xmlinput.JenaReader.
286
- *
328
+ * <p>
287
329
* Note: Using newer jena version (needs java 11) this method would be obsolete.
330
+ *
288
331
* @param url the URL to resolve
289
332
* @return the (redirected) URL
290
333
* @throws IOException if any IO error occurs
@@ -324,4 +367,9 @@ private String read(final String url) throws IOException {
324
367
}
325
368
return connectionURL ;
326
369
}
370
+
371
+ private enum Select {
372
+ SUBJECT , OBJECT , DEFAULT
373
+ }
374
+
327
375
}
0 commit comments