comparison src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorResourceImpl.java @ 63:9f8c9611848a

fixed bug with new rectangle shapes. added limit, offset and sortBy parameters to annotator/ and annotator/search.
author casties
date Fri, 23 Nov 2012 17:55:04 +0100
parents b8ef15c8c4a5
children c48435e7f312
comparison
equal deleted inserted replaced
62:15569aa35d62 63:9f8c9611848a
384 JSONObject geom = shape.getJSONObject("geometry"); 384 JSONObject geom = shape.getJSONObject("geometry");
385 if (type.equalsIgnoreCase("point")) { 385 if (type.equalsIgnoreCase("point")) {
386 String x = geom.getString("x"); 386 String x = geom.getString("x");
387 String y = geom.getString("y"); 387 String y = geom.getString("y");
388 fragment = String.format("xywh=fraction:%s,%s,0,0", x, y); 388 fragment = String.format("xywh=fraction:%s,%s,0,0", x, y);
389 } else if (type.equalsIgnoreCase("point")) { 389 } else if (type.equalsIgnoreCase("rectangle")) {
390 String x = geom.getString("x"); 390 String x = geom.getString("x");
391 String y = geom.getString("y"); 391 String y = geom.getString("y");
392 String width = shape.getString("width"); 392 String width = geom.getString("width");
393 String height = shape.getString("height"); 393 String height = geom.getString("height");
394 fragment = String.format("xywh=fraction:%s,%s,%s,%s", x, y, width, height); 394 fragment = String.format("xywh=fraction:%s,%s,%s,%s", x, y, width, height);
395 } else { 395 } else {
396 logger.error("Unable to parse this shape: " + shape); 396 logger.error("Unable to parse this shape: " + shape);
397 } 397 }
398 return fragment; 398 return fragment;
414 protected String parseRange(JSONObject range) throws JSONException { 414 protected String parseRange(JSONObject range) throws JSONException {
415 String start = range.getString("start"); 415 String start = range.getString("start");
416 String end = range.getString("end"); 416 String end = range.getString("end");
417 String startOffset = range.getString("startOffset"); 417 String startOffset = range.getString("startOffset");
418 String endOffset = range.getString("endOffset"); 418 String endOffset = range.getString("endOffset");
419
420 String fragment = String.format( 419 String fragment = String.format(
421 "xpointer(start-point(string-range(\"%s\",%s,1))/range-to(end-point(string-range(\"%s\",%s,1))))", start, 420 "xpointer(start-point(string-range(\"%s\",%s,1))/range-to(end-point(string-range(\"%s\",%s,1))))", start,
422 startOffset, end, endOffset); 421 startOffset, end, endOffset);
423 return fragment; 422 return fragment;
424 } 423 }
557 /* 556 /*
558 * create fragment from the first range/area 557 * create fragment from the first range/area
559 */ 558 */
560 try { 559 try {
561 if (jo.has("ranges")) { 560 if (jo.has("ranges")) {
562 JSONObject ranges = jo.getJSONArray("ranges").getJSONObject(0); 561 JSONArray ranges = jo.getJSONArray("ranges");
563 annot.setFragmentType(FragmentTypes.XPOINTER); 562 if (ranges.length() > 0) {
564 String fragment = parseRange(ranges); 563 JSONObject range = ranges.getJSONObject(0);
565 annot.setTargetFragment(fragment); 564 annot.setFragmentType(FragmentTypes.XPOINTER);
565 String fragment = parseRange(range);
566 annot.setTargetFragment(fragment);
567 }
566 } 568 }
567 } catch (JSONException e) { 569 } catch (JSONException e) {
568 // nothing to do 570 // nothing to do
569 } 571 }
570 try { 572 try {
571 if (jo.has("shapes")) { 573 if (jo.has("shapes")) {
572 JSONObject shapes = jo.getJSONArray("shapes").getJSONObject(0); 574 JSONArray shapes = jo.getJSONArray("shapes");
573 annot.setFragmentType(FragmentTypes.AREA); 575 if (shapes.length() > 0) {
574 String fragment = parseShape(shapes); 576 JSONObject shape = shapes.getJSONObject(0);
575 annot.setTargetFragment(fragment); 577 annot.setFragmentType(FragmentTypes.AREA);
578 String fragment = parseShape(shape);
579 annot.setTargetFragment(fragment);
580 }
576 } 581 }
577 } catch (JSONException e) { 582 } catch (JSONException e) {
578 // nothing to do 583 // nothing to do
579 } 584 }
580 // deprecated areas type 585 // deprecated areas type
581 try { 586 try {
582 if (jo.has("areas")) { 587 if (jo.has("areas")) {
583 JSONObject area = jo.getJSONArray("areas").getJSONObject(0); 588 JSONArray areas = jo.getJSONArray("areas");
584 annot.setFragmentType(FragmentTypes.AREA); 589 if (areas.length() > 0) {
585 String fragment = parseArea(area); 590 JSONObject area = areas.getJSONObject(0);
586 annot.setTargetFragment(fragment); 591 annot.setFragmentType(FragmentTypes.AREA);
592 String fragment = parseArea(area);
593 annot.setTargetFragment(fragment);
594 }
587 } 595 }
588 } catch (JSONException e) { 596 } catch (JSONException e) {
589 // nothing to do 597 // nothing to do
590 } 598 }
591 599 // no fragment is an error
600 if (annot.getFragmentType() == null || annot.getTargetFragment() == null) {
601 throw new JSONException("Annotation has no valid target fragment!");
602 }
603
592 /* 604 /*
593 * permissions 605 * permissions
594 */ 606 */
595 if (jo.has("permissions")) { 607 if (jo.has("permissions")) {
596 JSONObject permissions = jo.getJSONObject("permissions"); 608 JSONObject permissions = jo.getJSONObject("permissions");
647 break; 659 break;
648 } 660 }
649 return actor; 661 return actor;
650 } 662 }
651 663
652 public float getFloat(String s) { 664 public static float getFloat(String s) {
653 try { 665 try {
654 return Float.parseFloat(s); 666 return Float.parseFloat(s);
655 } catch (NumberFormatException e) { 667 } catch (NumberFormatException e) {
656 } 668 }
657 return 0f; 669 return 0f;
658 } 670 }
671
672 public static int getInt(String s) {
673 try {
674 return Integer.parseInt(s);
675 } catch (NumberFormatException e) {
676 }
677 return 0;
678 }
659 } 679 }