Ignore:
Timestamp:
Nov 22, 2012, 4:38:53 PM (11 years ago)
Author:
casties
Branch:
default
Message:

implemented new shape format for image annotations.
minor cleanups.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorResourceImpl.java

    r58 r61  
    133133    /**
    134134     * 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.
    136137     *
    137138     * @param entity
     
    184185     *
    185186     * @param annot
    186      * @param forAnonymous TODO
     187     * @param forAnonymous
     188     *            TODO
    187189     * @return
    188190     */
     
    236238                    jo.put("ranges", transformToRanges(fragments));
    237239                } else if (xt == FragmentTypes.AREA) {
    238                     jo.put("areas", transformToAreas(fragments));
    239                 }
    240             }
    241            
     240                    jo.put("shapes", transformToShapes(fragments));
     241                }
     242            }
     243
    242244            /*
    243245             * permissions
     
    282284                readPerms.put(readPerm.getIdString());
    283285            }
    284            
     286
    285287            /*
    286288             * tags
    287289             */
    288             Set<String> tagset = annot.getTags(); 
     290            Set<String> tagset = annot.getTags();
    289291            if (tagset != null) {
    290292                JSONArray tags = new JSONArray();
     
    294296                }
    295297            }
    296            
     298
    297299            /*
    298300             * id
     
    304306            return jo;
    305307        } catch (JSONException e) {
    306             // TODO Auto-generated catch block
    307             e.printStackTrace();
     308            logger.error("Unable to create AnnotatorJSON!", e);
    308309        }
    309310        return null;
     
    311312
    312313    private JSONArray transformToRanges(List<String> xpointers) {
    313 
    314314        JSONArray ja = new JSONArray();
    315 
    316315        Pattern rg = Pattern
    317316                .compile("xpointer\\(start-point\\(string-range\\(\"([^\"]*)\",([^,]*),1\\)\\)/range-to\\(end-point\\(string-range\\(\"([^\"]*)\",([^,]*),1\\)\\)\\)\\)");
    318317        Pattern rg1 = Pattern.compile("xpointer\\(start-point\\(string-range\\(\"([^\"]*)\",([^,]*),1\\)\\)\\)");
    319 
    320318        try {
    321319            for (String xpointer : xpointers) {
     
    323321                String decoded = xpointer;
    324322                Matcher m = rg.matcher(decoded);
    325 
    326323                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);
    335330                }
    336331                m = rg1.matcher(xpointer);
     
    339334                    jo.put("start", m.group(1));
    340335                    jo.put("startOffset", m.group(2));
    341 
    342336                    ja.put(jo);
    343337                }
    344338            }
    345339        } catch (JSONException e) {
    346             // TODO Auto-generated catch block
    347             e.printStackTrace();
     340            logger.error("Unable to transform to ranges!", e);
    348341        }
    349342        return ja;
    350343    }
    351344
    352     private JSONArray transformToAreas(List<String> xpointers) {
    353 
     345    private JSONArray transformToShapes(List<String> xpointers) {
    354346        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\\.]+)");
    358348        try {
    359349            for (String xpointer : xpointers) {
    360                 // String decoded = URLDecoder.decode(xpointer, "utf-8");
    361350                String decoded = xpointer;
    362351                Matcher m = rg.matcher(decoded);
    363 
    364352                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);
    374371                    }
     372                    ja.put(shape);
    375373                }
    376374            }
    377375        } catch (JSONException e) {
    378             // TODO Auto-generated catch block
    379             e.printStackTrace();
     376            logger.error("Unable to transform to shapes!", e);
    380377        }
    381378        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;
    382399    }
    383400
     
    539556
    540557        /*
    541          * create xpointer from the first range/area
     558         * create fragment from the first range/area
    542559         */
    543560        try {
     
    551568            // nothing to do
    552569        }
     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
    553581        try {
    554582            if (jo.has("areas")) {
     
    601629        }
    602630
    603        
    604631        return annot;
    605632    }
    606633
    607     @SuppressWarnings("unused") // i in for loop
     634    @SuppressWarnings("unused")
     635    // i in for loop
    608636    protected Actor getActorFromPermissions(JSONArray perms) throws JSONException {
    609637        Actor actor = null;
     
    622650    }
    623651
     652    public float getFloat(String s) {
     653        try {
     654            return Float.parseFloat(s);
     655        } catch (NumberFormatException e) {
     656        }
     657        return 0f;
     658    }
    624659}
Note: See TracChangeset for help on using the changeset viewer.