diff src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorResourceImpl.java @ 61:b8ef15c8c4a5

implemented new shape format for image annotations. minor cleanups.
author casties
date Thu, 22 Nov 2012 17:38:53 +0100
parents f5c0e6df7e88
children 9f8c9611848a
line wrap: on
line diff
--- a/src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorResourceImpl.java	Tue Nov 20 19:43:12 2012 +0100
+++ b/src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorResourceImpl.java	Thu Nov 22 17:38:53 2012 +0100
@@ -132,7 +132,8 @@
 
     /**
      * Checks Annotator Auth plugin authentication information from headers.
-     * Returns userId if successful. Returns "anonymous" in non-authorization mode.
+     * Returns userId if successful. Returns "anonymous" in non-authorization
+     * mode.
      * 
      * @param entity
      * @return
@@ -183,7 +184,8 @@
      * creates Annotator-JSON from an Annotation object.
      * 
      * @param annot
-     * @param forAnonymous TODO
+     * @param forAnonymous
+     *            TODO
      * @return
      */
     public JSONObject createAnnotatorJson(Annotation annot, boolean forAnonymous) {
@@ -235,10 +237,10 @@
                 if (xt == FragmentTypes.XPOINTER) {
                     jo.put("ranges", transformToRanges(fragments));
                 } else if (xt == FragmentTypes.AREA) {
-                    jo.put("areas", transformToAreas(fragments));
+                    jo.put("shapes", transformToShapes(fragments));
                 }
             }
-            
+
             /*
              * permissions
              */
@@ -281,11 +283,11 @@
             if (readPerm != null) {
                 readPerms.put(readPerm.getIdString());
             }
-            
+
             /*
              * tags
              */
-            Set<String> tagset = annot.getTags(); 
+            Set<String> tagset = annot.getTags();
             if (tagset != null) {
                 JSONArray tags = new JSONArray();
                 jo.put("tags", tags);
@@ -293,7 +295,7 @@
                     tags.put(tag);
                 }
             }
-            
+
             /*
              * id
              */
@@ -303,84 +305,99 @@
             jo.put("id", annotId);
             return jo;
         } catch (JSONException e) {
-            // TODO Auto-generated catch block
-            e.printStackTrace();
+            logger.error("Unable to create AnnotatorJSON!", e);
         }
         return null;
     }
 
     private JSONArray transformToRanges(List<String> xpointers) {
-
         JSONArray ja = new JSONArray();
-
         Pattern rg = Pattern
                 .compile("xpointer\\(start-point\\(string-range\\(\"([^\"]*)\",([^,]*),1\\)\\)/range-to\\(end-point\\(string-range\\(\"([^\"]*)\",([^,]*),1\\)\\)\\)\\)");
         Pattern rg1 = Pattern.compile("xpointer\\(start-point\\(string-range\\(\"([^\"]*)\",([^,]*),1\\)\\)\\)");
-
         try {
             for (String xpointer : xpointers) {
                 // String decoded = URLDecoder.decode(xpointer, "utf-8");
                 String decoded = xpointer;
                 Matcher m = rg.matcher(decoded);
-
                 if (m.find()) {
-                    {
-                        JSONObject jo = new JSONObject();
-                        jo.put("start", m.group(1));
-                        jo.put("startOffset", m.group(2));
-                        jo.put("end", m.group(3));
-                        jo.put("endOffset", m.group(4));
-                        ja.put(jo);
-                    }
+                    JSONObject jo = new JSONObject();
+                    jo.put("start", m.group(1));
+                    jo.put("startOffset", m.group(2));
+                    jo.put("end", m.group(3));
+                    jo.put("endOffset", m.group(4));
+                    ja.put(jo);
                 }
                 m = rg1.matcher(xpointer);
                 if (m.find()) {
                     JSONObject jo = new JSONObject();
                     jo.put("start", m.group(1));
                     jo.put("startOffset", m.group(2));
-
                     ja.put(jo);
                 }
             }
         } catch (JSONException e) {
-            // TODO Auto-generated catch block
-            e.printStackTrace();
+            logger.error("Unable to transform to ranges!", e);
         }
         return ja;
     }
 
-    private JSONArray transformToAreas(List<String> xpointers) {
-
+    private JSONArray transformToShapes(List<String> xpointers) {
         JSONArray ja = new JSONArray();
-
-        Pattern rg = Pattern.compile("xywh=(\\w*:)([\\d\\.]+),([\\d\\.]+),([\\d\\.]+),([\\d\\.]+)");
-
+        Pattern rg = Pattern.compile("xywh=(\\w*):([\\d\\.]+),([\\d\\.]+),([\\d\\.]+),([\\d\\.]+)");
         try {
             for (String xpointer : xpointers) {
-                // String decoded = URLDecoder.decode(xpointer, "utf-8");
                 String decoded = xpointer;
                 Matcher m = rg.matcher(decoded);
-
                 if (m.find()) {
-                    {
-                        JSONObject jo = new JSONObject();
-                        @SuppressWarnings("unused")
-                        String unit = m.group(1);
-                        jo.put("x", m.group(2));
-                        jo.put("y", m.group(3));
-                        jo.put("width", m.group(4));
-                        jo.put("height", m.group(5));
-                        ja.put(jo);
+                    String units = m.group(1);
+                    float x = getFloat(m.group(2));
+                    float y = getFloat(m.group(3));
+                    float width = getFloat(m.group(4));
+                    float height = getFloat(m.group(5));
+                    JSONObject shape = new JSONObject();
+                    JSONObject geom = new JSONObject();
+                    geom.put("units", units);
+                    geom.put("x", x);
+                    geom.put("y", y);
+                    if (width == 0 || height == 0) {
+                        shape.put("type", "point");
+                        shape.put("geometry", geom);
+                    } else {
+                        shape.put("type", "rectangle");
+                        geom.put("width", width);
+                        geom.put("height", height);
+                        shape.put("geometry", geom);
                     }
+                    ja.put(shape);
                 }
             }
         } catch (JSONException e) {
-            // TODO Auto-generated catch block
-            e.printStackTrace();
+            logger.error("Unable to transform to shapes!", e);
         }
         return ja;
     }
 
+    protected String parseShape(JSONObject shape) throws JSONException {
+        String fragment = null;
+        String type = shape.getString("type");
+        JSONObject geom = shape.getJSONObject("geometry");
+        if (type.equalsIgnoreCase("point")) {
+            String x = geom.getString("x");
+            String y = geom.getString("y");
+            fragment = String.format("xywh=fraction:%s,%s,0,0", x, y);
+        } else if (type.equalsIgnoreCase("point")) {
+            String x = geom.getString("x");
+            String y = geom.getString("y");
+            String width = shape.getString("width");
+            String height = shape.getString("height");
+            fragment = String.format("xywh=fraction:%s,%s,%s,%s", x, y, width, height);
+        } else {
+            logger.error("Unable to parse this shape: " + shape);
+        }
+        return fragment;
+    }
+
     protected String parseArea(JSONObject area) throws JSONException {
         String x = area.getString("x");
         String y = area.getString("y");
@@ -538,7 +555,7 @@
         }
 
         /*
-         * create xpointer from the first range/area
+         * create fragment from the first range/area
          */
         try {
             if (jo.has("ranges")) {
@@ -551,6 +568,17 @@
             // nothing to do
         }
         try {
+            if (jo.has("shapes")) {
+                JSONObject shapes = jo.getJSONArray("shapes").getJSONObject(0);
+                annot.setFragmentType(FragmentTypes.AREA);
+                String fragment = parseShape(shapes);
+                annot.setTargetFragment(fragment);
+            }
+        } catch (JSONException e) {
+            // nothing to do
+        }
+        // deprecated areas type
+        try {
             if (jo.has("areas")) {
                 JSONObject area = jo.getJSONArray("areas").getJSONObject(0);
                 annot.setFragmentType(FragmentTypes.AREA);
@@ -600,11 +628,11 @@
             annot.setTags(tagset);
         }
 
-        
         return annot;
     }
 
-    @SuppressWarnings("unused") // i in for loop
+    @SuppressWarnings("unused")
+    // i in for loop
     protected Actor getActorFromPermissions(JSONArray perms) throws JSONException {
         Actor actor = null;
         for (int i = 0; i < perms.length(); ++i) {
@@ -621,4 +649,11 @@
         return actor;
     }
 
+    public float getFloat(String s) {
+        try {
+            return Float.parseFloat(s);
+        } catch (NumberFormatException e) {
+        }
+        return 0f;
+    }
 }