comparison src/main/java/de/mpiwg/itgroup/annotationManager/restlet/AnnotatorResourceImpl.java @ 33:e5f5848892a2

new annotation model basically works.
author casties
date Thu, 31 May 2012 19:08:48 +0200
parents 40846c0b344d
children
comparison
equal deleted inserted replaced
32:40846c0b344d 33:e5f5848892a2
112 public boolean isAuthenticated(Representation entity) { 112 public boolean isAuthenticated(Representation entity) {
113 return (checkAuthToken(entity) != null); 113 return (checkAuthToken(entity) != null);
114 } 114 }
115 115
116 /** 116 /**
117 * checks Annotator Auth plugin authentication information from headers. 117 * checks Annotator Auth plugin authentication information from headers. returns userId if successful.
118 * returns userId if successful.
119 * 118 *
120 * @param entity 119 * @param entity
121 * @return 120 * @return
122 */ 121 */
123 public String checkAuthToken(Representation entity) { 122 public String checkAuthToken(Representation entity) {
199 else { 198 else {
200 for (String xpointerString : annot.xpointers) { 199 for (String xpointerString : annot.xpointers) {
201 xpointers.add(xpointerString); 200 xpointers.add(xpointerString);
202 } 201 }
203 } 202 }
204 jo.put("ranges", transformToRanges(xpointers)); 203 if (!xpointers.isEmpty()) {
204 // we only look at the first xpointer
205 String xt = getXpointerType(xpointers.get(0));
206 if (xt == "range") {
207 jo.put("ranges", transformToRanges(xpointers));
208 } else if (xt == "area") {
209 jo.put("areas", transformToAreas(xpointers));
210 }
211 }
205 // encode Annotation URL (=id) in base64 212 // encode Annotation URL (=id) in base64
206 String annotUrl = annot.getAnnotationUri(); 213 String annotUrl = annot.getAnnotationUri();
207 String annotId = encodeJsonId(annotUrl); 214 String annotId = encodeJsonId(annotUrl);
208 jo.put("id", annotId); 215 jo.put("id", annotId);
209 return jo; 216 return jo;
212 e.printStackTrace(); 219 e.printStackTrace();
213 } 220 }
214 return null; 221 return null;
215 } 222 }
216 223
217
218 private String getXpointerType(String xpointer) { 224 private String getXpointerType(String xpointer) {
219 if (xpointer.contains("#xpointer")) { 225 if (xpointer.contains("#xpointer")) {
220 return "range"; 226 return "range";
221 } else if (xpointer.contains("#xywh")) { 227 } else if (xpointer.contains("#xywh")) {
222 return "area"; 228 return "area";
223 } 229 }
224 return null; 230 return null;
225 } 231 }
226 232
227 private JSONArray transformToRanges(List<String> xpointers) { 233 private JSONArray transformToRanges(List<String> xpointers) {
228 234
229 JSONArray ja = new JSONArray(); 235 JSONArray ja = new JSONArray();
230 236
231 Pattern rg = Pattern 237 Pattern rg = Pattern
265 } 271 }
266 272
267 return ja; 273 return ja;
268 } 274 }
269 275
276 private JSONArray transformToAreas(List<String> xpointers) {
277
278 JSONArray ja = new JSONArray();
279
280 Pattern rg = Pattern.compile("#xywh=(\\w*:)([\\d\\.]+),([\\d\\.]+),([\\d\\.]+),([\\d\\.]+)");
281
282 try {
283 for (String xpointer : xpointers) {
284 String decoded = URLDecoder.decode(xpointer, "utf-8");
285 Matcher m = rg.matcher(decoded);
286
287 if (m.find()) {
288 {
289 JSONObject jo = new JSONObject();
290 String unit = m.group(1);
291 jo.put("x", m.group(2));
292 jo.put("y", m.group(3));
293 jo.put("width", m.group(4));
294 jo.put("height", m.group(5));
295 ja.put(jo);
296 }
297 }
298 }
299 } catch (JSONException e) {
300 // TODO Auto-generated catch block
301 e.printStackTrace();
302 } catch (UnsupportedEncodingException e) {
303 // TODO Auto-generated catch block
304 e.printStackTrace();
305 }
306
307 return ja;
308 }
309
270 /** 310 /**
271 * creates an Annotation object with data from JSON. 311 * creates an Annotation object with data from JSON.
272 * 312 *
273 * uses the specification from the annotator project: {@link https 313 * uses the specification from the annotator project: {@link https ://github.com/okfn/annotator/wiki/Annotation-format}
274 * ://github.com/okfn/annotator/wiki/Annotation-format} 314 *
275 * 315 * The username will be transformed to an URI if not given already as URI, if not it will set to the MPIWG namespace defined in
276 * The username will be transformed to an URI if not given already as URI,
277 * if not it will set to the MPIWG namespace defined in
278 * de.mpiwg.itgroup.annotationManager.Constants.NS 316 * de.mpiwg.itgroup.annotationManager.Constants.NS
279 * 317 *
280 * @param jo 318 * @param jo
281 * @return 319 * @return
282 * @throws JSONException 320 * @throws JSONException
363 } 401 }
364 } 402 }
365 if (jo.has("areas")) { 403 if (jo.has("areas")) {
366 JSONObject area = jo.getJSONArray("areas").getJSONObject(0); 404 JSONObject area = jo.getJSONArray("areas").getJSONObject(0);
367 String x = area.getString("x"); 405 String x = area.getString("x");
368 String y = area.getString("end"); 406 String y = area.getString("y");
369 String width = "0"; 407 String width = "0";
370 String height = "0"; 408 String height = "0";
371 if (area.has("width")) { 409 if (area.has("width")) {
372 width = area.getString("width"); 410 width = area.getString("width");
373 height = area.getString("height"); 411 height = area.getString("height");