Changeset 61:b8ef15c8c4a5 in AnnotationManagerN4J for src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorResourceImpl.java
Legend:
- Unmodified
- Added
- Removed
-
src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorResourceImpl.java
r58 r61 133 133 /** 134 134 * Checks Annotator Auth plugin authentication information from headers. 135 * Returns userId if successful. Returns "anonymous" in non-authorization mode. 135 * Returns userId if successful. Returns "anonymous" in non-authorization 136 * mode. 136 137 * 137 138 * @param entity … … 184 185 * 185 186 * @param annot 186 * @param forAnonymous TODO 187 * @param forAnonymous 188 * TODO 187 189 * @return 188 190 */ … … 236 238 jo.put("ranges", transformToRanges(fragments)); 237 239 } else if (xt == FragmentTypes.AREA) { 238 jo.put(" areas", transformToAreas(fragments));239 } 240 } 241 240 jo.put("shapes", transformToShapes(fragments)); 241 } 242 } 243 242 244 /* 243 245 * permissions … … 282 284 readPerms.put(readPerm.getIdString()); 283 285 } 284 286 285 287 /* 286 288 * tags 287 289 */ 288 Set<String> tagset = annot.getTags(); 290 Set<String> tagset = annot.getTags(); 289 291 if (tagset != null) { 290 292 JSONArray tags = new JSONArray(); … … 294 296 } 295 297 } 296 298 297 299 /* 298 300 * id … … 304 306 return jo; 305 307 } catch (JSONException e) { 306 // TODO Auto-generated catch block 307 e.printStackTrace(); 308 logger.error("Unable to create AnnotatorJSON!", e); 308 309 } 309 310 return null; … … 311 312 312 313 private JSONArray transformToRanges(List<String> xpointers) { 313 314 314 JSONArray ja = new JSONArray(); 315 316 315 Pattern rg = Pattern 317 316 .compile("xpointer\\(start-point\\(string-range\\(\"([^\"]*)\",([^,]*),1\\)\\)/range-to\\(end-point\\(string-range\\(\"([^\"]*)\",([^,]*),1\\)\\)\\)\\)"); 318 317 Pattern rg1 = Pattern.compile("xpointer\\(start-point\\(string-range\\(\"([^\"]*)\",([^,]*),1\\)\\)\\)"); 319 320 318 try { 321 319 for (String xpointer : xpointers) { … … 323 321 String decoded = xpointer; 324 322 Matcher m = rg.matcher(decoded); 325 326 323 if (m.find()) { 327 { 328 JSONObject jo = new JSONObject(); 329 jo.put("start", m.group(1)); 330 jo.put("startOffset", m.group(2)); 331 jo.put("end", m.group(3)); 332 jo.put("endOffset", m.group(4)); 333 ja.put(jo); 334 } 324 JSONObject jo = new JSONObject(); 325 jo.put("start", m.group(1)); 326 jo.put("startOffset", m.group(2)); 327 jo.put("end", m.group(3)); 328 jo.put("endOffset", m.group(4)); 329 ja.put(jo); 335 330 } 336 331 m = rg1.matcher(xpointer); … … 339 334 jo.put("start", m.group(1)); 340 335 jo.put("startOffset", m.group(2)); 341 342 336 ja.put(jo); 343 337 } 344 338 } 345 339 } catch (JSONException e) { 346 // TODO Auto-generated catch block 347 e.printStackTrace(); 340 logger.error("Unable to transform to ranges!", e); 348 341 } 349 342 return ja; 350 343 } 351 344 352 private JSONArray transformToAreas(List<String> xpointers) { 353 345 private JSONArray transformToShapes(List<String> xpointers) { 354 346 JSONArray ja = new JSONArray(); 355 356 Pattern rg = Pattern.compile("xywh=(\\w*:)([\\d\\.]+),([\\d\\.]+),([\\d\\.]+),([\\d\\.]+)"); 357 347 Pattern rg = Pattern.compile("xywh=(\\w*):([\\d\\.]+),([\\d\\.]+),([\\d\\.]+),([\\d\\.]+)"); 358 348 try { 359 349 for (String xpointer : xpointers) { 360 // String decoded = URLDecoder.decode(xpointer, "utf-8");361 350 String decoded = xpointer; 362 351 Matcher m = rg.matcher(decoded); 363 364 352 if (m.find()) { 365 { 366 JSONObject jo = new JSONObject(); 367 @SuppressWarnings("unused") 368 String unit = m.group(1); 369 jo.put("x", m.group(2)); 370 jo.put("y", m.group(3)); 371 jo.put("width", m.group(4)); 372 jo.put("height", m.group(5)); 373 ja.put(jo); 353 String units = m.group(1); 354 float x = getFloat(m.group(2)); 355 float y = getFloat(m.group(3)); 356 float width = getFloat(m.group(4)); 357 float height = getFloat(m.group(5)); 358 JSONObject shape = new JSONObject(); 359 JSONObject geom = new JSONObject(); 360 geom.put("units", units); 361 geom.put("x", x); 362 geom.put("y", y); 363 if (width == 0 || height == 0) { 364 shape.put("type", "point"); 365 shape.put("geometry", geom); 366 } else { 367 shape.put("type", "rectangle"); 368 geom.put("width", width); 369 geom.put("height", height); 370 shape.put("geometry", geom); 374 371 } 372 ja.put(shape); 375 373 } 376 374 } 377 375 } catch (JSONException e) { 378 // TODO Auto-generated catch block 379 e.printStackTrace(); 376 logger.error("Unable to transform to shapes!", e); 380 377 } 381 378 return ja; 379 } 380 381 protected String parseShape(JSONObject shape) throws JSONException { 382 String fragment = null; 383 String type = shape.getString("type"); 384 JSONObject geom = shape.getJSONObject("geometry"); 385 if (type.equalsIgnoreCase("point")) { 386 String x = geom.getString("x"); 387 String y = geom.getString("y"); 388 fragment = String.format("xywh=fraction:%s,%s,0,0", x, y); 389 } else if (type.equalsIgnoreCase("point")) { 390 String x = geom.getString("x"); 391 String y = geom.getString("y"); 392 String width = shape.getString("width"); 393 String height = shape.getString("height"); 394 fragment = String.format("xywh=fraction:%s,%s,%s,%s", x, y, width, height); 395 } else { 396 logger.error("Unable to parse this shape: " + shape); 397 } 398 return fragment; 382 399 } 383 400 … … 539 556 540 557 /* 541 * create xpointerfrom the first range/area558 * create fragment from the first range/area 542 559 */ 543 560 try { … … 551 568 // nothing to do 552 569 } 570 try { 571 if (jo.has("shapes")) { 572 JSONObject shapes = jo.getJSONArray("shapes").getJSONObject(0); 573 annot.setFragmentType(FragmentTypes.AREA); 574 String fragment = parseShape(shapes); 575 annot.setTargetFragment(fragment); 576 } 577 } catch (JSONException e) { 578 // nothing to do 579 } 580 // deprecated areas type 553 581 try { 554 582 if (jo.has("areas")) { … … 601 629 } 602 630 603 604 631 return annot; 605 632 } 606 633 607 @SuppressWarnings("unused") // i in for loop 634 @SuppressWarnings("unused") 635 // i in for loop 608 636 protected Actor getActorFromPermissions(JSONArray perms) throws JSONException { 609 637 Actor actor = null; … … 622 650 } 623 651 652 public float getFloat(String s) { 653 try { 654 return Float.parseFloat(s); 655 } catch (NumberFormatException e) { 656 } 657 return 0f; 658 } 624 659 }
Note: See TracChangeset
for help on using the changeset viewer.