1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.any23.rdf;
19
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.net.URI;
26 import java.net.URISyntaxException;
27 import java.util.Locale;
28 import java.util.Map;
29 import java.util.Properties;
30
31
32
33
34 public class PopularPrefixes {
35
36 private static final Logger logger = LoggerFactory.getLogger(PopularPrefixes.class);
37
38 private static final String RESOURCE_NAME = "/org/apache/any23/prefixes/prefixes.properties";
39
40 private static final Prefixes popularPrefixes = getPrefixes();
41
42 private static Prefixes getPrefixes() {
43 Prefixesml#Prefixes">Prefixes prefixes = new Prefixes();
44 Properties properties = new Properties();
45 try {
46 logger.trace(String.format(Locale.ROOT, "Loading prefixes from %s", RESOURCE_NAME));
47 properties.load(getResourceAsStream());
48 } catch (IOException e) {
49 logger.error(String.format(Locale.ROOT, "Error while loading prefixes from %s", RESOURCE_NAME), e);
50 throw new RuntimeException(
51 String.format(Locale.ROOT, "Error while loading prefixes from %s", RESOURCE_NAME));
52 }
53 for (Map.Entry entry : properties.entrySet()) {
54 if (testIRICompliance((String) entry.getValue())) {
55 prefixes.add((String) entry.getKey(), (String) entry.getValue());
56 } else {
57 logger.warn(String.format(Locale.ROOT, "Prefixes entry '%s' is not a well-formad IRI. Skipped.",
58 entry.getValue()));
59 }
60 }
61 return prefixes;
62 }
63
64
65
66
67
68
69
70
71
72
73 public static Prefixes createSubset(String... prefixes) {
74 return popularPrefixes.createSubset(prefixes);
75 }
76
77
78
79
80 public static Prefixes get() {
81 return popularPrefixes;
82 }
83
84
85
86
87
88
89
90
91
92 private static boolean testIRICompliance(String stringUri) {
93 try {
94 new URI(stringUri);
95 } catch (URISyntaxException e) {
96 return false;
97 }
98 return true;
99 }
100
101
102
103
104
105
106 private static InputStream getResourceAsStream() {
107 InputStream result;
108 result = PopularPrefixes.class.getResourceAsStream(RESOURCE_NAME);
109 if (result == null) {
110 result = PopularPrefixes.class.getClassLoader().getResourceAsStream(RESOURCE_NAME);
111 if (result == null) {
112 result = ClassLoader.getSystemResourceAsStream(RESOURCE_NAME);
113 }
114 }
115 return result;
116 }
117
118 }