Skip to content

Commit 782fe42

Browse files
author
James Leigh
authored
Merge pull request #868 from Fedict/issues/#440-vocabutil
Issues/#440 vocabutil
2 parents 400f22d + 32b4a10 commit 782fe42

2 files changed

Lines changed: 79 additions & 0 deletions

File tree

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* Copyright (c) 2017 Eclipse RDF4J contributors, and others.
3+
* All rights reserved. This program and the accompanying materials
4+
* are made available under the terms of the Eclipse Distribution License v1.0
5+
* which accompanies this distribution, and is available at
6+
* http://www.eclipse.org/org/documents/edl-v10.php.
7+
*/
8+
package org.eclipse.rdf4j.model.util;
9+
10+
import java.lang.reflect.Field;
11+
import java.util.LinkedHashSet;
12+
import java.util.Set;
13+
14+
import org.eclipse.rdf4j.model.IRI;
15+
16+
/**
17+
* Utility functions for working with vocabularies.
18+
*
19+
* @author Bart Hanssens
20+
*/
21+
public class Vocabularies {
22+
/**
23+
* Get all the {@link IRI IRIs} of the classes and properties of a vocabulary.
24+
*
25+
* @param vocabulary RDF vocabulary
26+
* @return set of IRIs
27+
*/
28+
public static Set<IRI> getIRIs(Class vocabulary) {
29+
Set<IRI> iris = new LinkedHashSet<>();
30+
31+
for (Field f : vocabulary.getFields()) {
32+
if (f.getType().equals(IRI.class)) {
33+
try {
34+
iris.add((IRI) f.get(vocabulary));
35+
} catch (IllegalAccessException ex) {
36+
// should not happen
37+
throw new RuntimeException("Cannot access vocabulary field", ex);
38+
}
39+
}
40+
}
41+
return iris;
42+
}
43+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* Copyright (c) 2017 Eclipse RDF4J contributors, and others.
3+
* All rights reserved. This program and the accompanying materials
4+
* are made available under the terms of the Eclipse Distribution License v1.0
5+
* which accompanies this distribution, and is available at
6+
* http://www.eclipse.org/org/documents/edl-v10.php.
7+
*/
8+
package org.eclipse.rdf4j.model.util;
9+
10+
import java.util.Arrays;
11+
import java.util.HashSet;
12+
import java.util.Set;
13+
14+
import org.eclipse.rdf4j.model.IRI;
15+
import org.eclipse.rdf4j.model.vocabulary.DC;
16+
17+
import org.junit.Test;
18+
import static org.junit.Assert.assertEquals;
19+
20+
/**
21+
* @author Bart Hanssens
22+
*/
23+
public class VocabulariesTest {
24+
25+
@Test
26+
public void testVocabAllIRI() throws Exception {
27+
Set<IRI> dcIRIs = new HashSet<>(Arrays.asList(
28+
DC.CONTRIBUTOR, DC.COVERAGE, DC.CREATOR, DC.DATE, DC.DESCRIPTION,
29+
DC.FORMAT, DC.IDENTIFIER, DC.LANGUAGE, DC.PUBLISHER, DC.RELATION,
30+
DC.RIGHTS, DC.SOURCE, DC.SUBJECT, DC.TITLE, DC.TYPE));
31+
32+
Set<IRI> allIRIs = Vocabularies.getIRIs(DC.class);
33+
34+
assertEquals(dcIRIs, allIRIs);
35+
}
36+
}

0 commit comments

Comments
 (0)