Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
Vocabulary |
|
| 2.466666666666667;2.467 | ||||
Vocabulary$Comment |
|
| 2.466666666666667;2.467 |
1 | /* | |
2 | * Licensed to the Apache Software Foundation (ASF) under one or more | |
3 | * contributor license agreements. See the NOTICE file distributed with | |
4 | * this work for additional information regarding copyright ownership. | |
5 | * The ASF licenses this file to You under the Apache License, Version 2.0 | |
6 | * (the "License"); you may not use this file except in compliance with | |
7 | * the License. You may obtain a copy of the License at | |
8 | * | |
9 | * http://www.apache.org/licenses/LICENSE-2.0 | |
10 | * | |
11 | * Unless required by applicable law or agreed to in writing, software | |
12 | * distributed under the License is distributed on an "AS IS" BASIS, | |
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
14 | * See the License for the specific language governing permissions and | |
15 | * limitations under the License. | |
16 | */ | |
17 | ||
18 | package org.apache.any23.vocab; | |
19 | ||
20 | import org.apache.any23.rdf.RDFUtils; | |
21 | import org.openrdf.model.URI; | |
22 | ||
23 | import java.lang.annotation.Retention; | |
24 | import java.lang.annotation.Target; | |
25 | import java.lang.reflect.Field; | |
26 | import java.util.Collection; | |
27 | import java.util.Collections; | |
28 | import java.util.HashMap; | |
29 | import java.util.Map; | |
30 | ||
31 | import static java.lang.annotation.ElementType.FIELD; | |
32 | import static java.lang.annotation.RetentionPolicy.RUNTIME; | |
33 | ||
34 | /** | |
35 | * Base class for the definition of a vocabulary. | |
36 | * | |
37 | * @author Michele Mostarda ( michele.mostarda@gmail.com ) | |
38 | * @version $Id$ | |
39 | */ | |
40 | public abstract class Vocabulary { | |
41 | ||
42 | /** | |
43 | * Allows to add comments to <code>namespaces</code>, | |
44 | * <code>classes</code> and <code>properties</code>. | |
45 | */ | |
46 | @Target({FIELD}) | |
47 | @Retention(RUNTIME) | |
48 | @interface Comment { | |
49 | String value(); | |
50 | } | |
51 | ||
52 | /** | |
53 | * Vocabulary namespace. | |
54 | */ | |
55 | private final URI namespace; | |
56 | ||
57 | /** | |
58 | * Map of vocabulary resources. | |
59 | */ | |
60 | private Map<String,URI> classes; | |
61 | ||
62 | /** | |
63 | * Map of vocabulary properties. | |
64 | */ | |
65 | private Map<String,URI> properties; | |
66 | ||
67 | /** | |
68 | * Map any resource with the relative comment. | |
69 | */ | |
70 | private Map<URI,String> resourceToCommentMap; | |
71 | ||
72 | /** | |
73 | * Constructor. | |
74 | * | |
75 | * @param namespace the namespace URI prefix. | |
76 | */ | |
77 | 0 | public Vocabulary(String namespace) { |
78 | try { | |
79 | 0 | this.namespace = RDFUtils.uri(namespace); |
80 | 0 | } catch (Exception e) { |
81 | 0 | throw new IllegalArgumentException("Invalid namespace '" + namespace + "'", e); |
82 | 0 | } |
83 | 0 | } |
84 | ||
85 | /** | |
86 | * @return the namespace associated to this vocabulary. | |
87 | */ | |
88 | public URI getNamespace() { | |
89 | 0 | return namespace; |
90 | } | |
91 | ||
92 | /** | |
93 | * Returns a class defined within this vocabulary. | |
94 | * | |
95 | * @param name class name. | |
96 | * @return the URI associated to such resource. | |
97 | */ | |
98 | public URI getClass(String name) { | |
99 | 0 | URI res = classes.get(name); |
100 | 0 | if (null == res) { |
101 | 0 | throw new IllegalArgumentException("Unknown resource name '" + name + "'"); |
102 | } | |
103 | 0 | return res; |
104 | } | |
105 | ||
106 | /** | |
107 | * Returns a property defined within this vocabulary. | |
108 | * | |
109 | * @param name property name. | |
110 | * @return the URI associated to such property. | |
111 | */ | |
112 | public URI getProperty(String name) { | |
113 | 0 | URI prop = properties.get(name); |
114 | 0 | if (null == prop) { |
115 | 0 | throw new IllegalArgumentException("Unknown property name '" + name + "'"); |
116 | } | |
117 | 0 | return prop; |
118 | } | |
119 | ||
120 | /** | |
121 | * Returns a property defined within this vocabulary, if not found the | |
122 | * <code>defaultValue</code> will be returned. | |
123 | * | |
124 | * @param name property name. | |
125 | * @param defaultValue the default value if property name not found. | |
126 | * @return the URI associated to such property. | |
127 | */ | |
128 | public URI getProperty(String name, URI defaultValue) { | |
129 | 0 | URI prop = properties.get(name); |
130 | 0 | if (null == prop) { |
131 | 0 | return defaultValue; |
132 | } | |
133 | 0 | return prop; |
134 | } | |
135 | ||
136 | /** | |
137 | * Returns the property URI for the specified property string. | |
138 | * If the string contains a list of words separated by blank chars, | |
139 | * such words are merged and camel case separated. | |
140 | * | |
141 | * @param property property name. | |
142 | * @return property URI. | |
143 | */ | |
144 | public URI getPropertyCamelCase(String property) { | |
145 | 0 | String[] names = property.split("\\W"); |
146 | 0 | String camelCase = names[0]; |
147 | 0 | for (int i = 1; i < names.length; i++) { |
148 | 0 | String tmp = names[i]; |
149 | 0 | camelCase += tmp.replaceFirst("(.)", tmp.substring(0, 1).toUpperCase()); |
150 | } | |
151 | 0 | return getProperty(camelCase); |
152 | } | |
153 | ||
154 | /** | |
155 | * @return the list of all defined classes. | |
156 | */ | |
157 | public URI[] getClasses() { | |
158 | 0 | if(classes == null) { |
159 | 0 | return new URI[0]; |
160 | } | |
161 | 0 | final Collection<URI> uris = classes.values(); |
162 | 0 | return uris.toArray( new URI[ uris.size() ] ); |
163 | } | |
164 | ||
165 | /** | |
166 | * @return the list of all defined properties. | |
167 | */ | |
168 | public URI[] getProperties() { | |
169 | 0 | if(properties == null) { |
170 | 0 | return new URI[0]; |
171 | } | |
172 | 0 | final Collection<URI> uris = properties.values(); |
173 | 0 | return uris.toArray( new URI[ uris.size() ] ); |
174 | } | |
175 | ||
176 | /** | |
177 | * Returns all the defined comments for resources. | |
178 | * | |
179 | * @return unmodifiable list of comments. | |
180 | */ | |
181 | public Map<URI,String> getComments() { | |
182 | 0 | fillResourceToCommentMap(); |
183 | 0 | return Collections.unmodifiableMap(resourceToCommentMap); |
184 | } | |
185 | ||
186 | /** | |
187 | * Returns the comment for the given resource. | |
188 | * | |
189 | * @param resource input resource to have a comment. | |
190 | * @return the human readable comment associated to the | |
191 | * given resource. | |
192 | */ | |
193 | public String getCommentFor(URI resource) { | |
194 | 0 | fillResourceToCommentMap(); |
195 | 0 | return resourceToCommentMap.get(resource); |
196 | } | |
197 | ||
198 | /** | |
199 | * Creates a URI. | |
200 | * | |
201 | * @param uriStr the URI string | |
202 | * @return the URI instance. | |
203 | */ | |
204 | protected URI createURI(String uriStr) { | |
205 | 0 | return RDFUtils.uri(uriStr); |
206 | } | |
207 | ||
208 | /** | |
209 | * Creates a resource and register it to the {@link #classes} map. | |
210 | * | |
211 | * @param namespace vocabulary namespace. | |
212 | * @param resource name of the resource. | |
213 | * @return the created resource URI. | |
214 | */ | |
215 | protected URI createClass(String namespace, String resource) { | |
216 | 0 | URI res = createURI(namespace, resource); |
217 | 0 | if(classes == null) { |
218 | 0 | classes = new HashMap<String, URI>(10); |
219 | } | |
220 | 0 | classes.put(resource, res); |
221 | 0 | return res; |
222 | } | |
223 | ||
224 | /** | |
225 | * Creates a property and register it to the {@link #properties} map. | |
226 | * | |
227 | * @param namespace vocabulary namespace. | |
228 | * @param property name of the property. | |
229 | * @return the created property URI. | |
230 | */ | |
231 | protected URI createProperty(String namespace, String property) { | |
232 | 0 | URI res = createURI(namespace, property); |
233 | 0 | if(properties == null) { |
234 | 0 | properties = new HashMap<String, URI>(10); |
235 | } | |
236 | 0 | properties.put(property, res); |
237 | 0 | return res; |
238 | } | |
239 | ||
240 | /** | |
241 | * Creates a URI. | |
242 | * | |
243 | * @param namespace | |
244 | * @param localName | |
245 | * @return | |
246 | */ | |
247 | private URI createURI(String namespace, String localName) { | |
248 | 0 | return RDFUtils.uri(namespace, localName); |
249 | } | |
250 | ||
251 | private void fillResourceToCommentMap() { | |
252 | 0 | if(resourceToCommentMap != null) return; |
253 | 0 | final Map<URI,String> newMap = new HashMap<URI, String>(); |
254 | 0 | for (Field field : this.getClass().getFields()) { |
255 | try { | |
256 | 0 | final Object value = field.get(this); |
257 | 0 | if(value instanceof URI) { |
258 | 0 | final Comment comment = field.getAnnotation(Comment.class); |
259 | 0 | if(comment != null) newMap.put((URI) value, comment.value()); |
260 | } | |
261 | 0 | } catch (IllegalAccessException iae) { |
262 | 0 | throw new RuntimeException("Error while creating resource to comment map.", iae); |
263 | 0 | } |
264 | } | |
265 | 0 | resourceToCommentMap = newMap; |
266 | 0 | } |
267 | ||
268 | } |