changeset 32:40846c0b344d

working on image annotations...
author casties
date Thu, 31 May 2012 11:57:50 +0200
parents 38465b158de6
children e5f5848892a2
files src/main/java/de/mpiwg/itgroup/annotationManager/RDFHandling/Annotation.java src/main/java/de/mpiwg/itgroup/annotationManager/restlet/AnnotatorResourceImpl.java
diffstat 2 files changed, 47 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/src/main/java/de/mpiwg/itgroup/annotationManager/RDFHandling/Annotation.java	Mon May 14 11:18:53 2012 +0200
+++ b/src/main/java/de/mpiwg/itgroup/annotationManager/RDFHandling/Annotation.java	Thu May 31 11:57:50 2012 +0200
@@ -13,6 +13,8 @@
 	public String page=null;
 	/** URL of a display of the page of the document */
 	public String displayUrl=null;
+	/** String representation of the image area */
+        public String area = null;
 	public String creator=null;
 	public String time=null;
 	public String text=null;
--- a/src/main/java/de/mpiwg/itgroup/annotationManager/restlet/AnnotatorResourceImpl.java	Mon May 14 11:18:53 2012 +0200
+++ b/src/main/java/de/mpiwg/itgroup/annotationManager/restlet/AnnotatorResourceImpl.java	Thu May 31 11:57:50 2012 +0200
@@ -57,7 +57,7 @@
             return null;
         }
     }
-    
+
     public String decodeJsonId(String id) {
         try {
             return new String(Base64.decodeBase64(id), "UTF-8");
@@ -114,7 +114,8 @@
     }
 
     /**
-     * checks Annotator Auth plugin authentication information from headers. returns userId if successful.
+     * checks Annotator Auth plugin authentication information from headers.
+     * returns userId if successful.
      * 
      * @param entity
      * @return
@@ -133,7 +134,7 @@
         if (consumerSecret == null) {
             return null;
         }
-        //logger.debug(String.format("token=%s tokenString=%s signatureAlgorithm=%s",token,token.getTokenString(),token.getSignatureAlgorithm()));
+        // logger.debug(String.format("token=%s tokenString=%s signatureAlgorithm=%s",token,token.getTokenString(),token.getSignatureAlgorithm()));
         try {
             List<Verifier> verifiers = new ArrayList<Verifier>();
             // we only do HS256 yet
@@ -151,7 +152,7 @@
             e.printStackTrace();
         }
         // must be ok then
-        logger.debug("auth OK! user="+userId);
+        logger.debug("auth OK! user=" + userId);
         return userId;
     }
 
@@ -176,7 +177,8 @@
                 // make short user id
                 String userID = annot.creator;
                 if (userID.startsWith(NS.MPIWG_PERSONS_URL)) {
-                    userID = userID.replace(NS.MPIWG_PERSONS_URL, ""); // entferne NAMESPACE
+                    userID = userID.replace(NS.MPIWG_PERSONS_URL, ""); // entferne
+                                                                       // NAMESPACE
                 }
                 // save as id
                 userObject.put("id", userID);
@@ -212,6 +214,16 @@
         return null;
     }
 
+    
+    private String getXpointerType(String xpointer) {
+        if (xpointer.contains("#xpointer")) {
+            return "range";
+        } else if (xpointer.contains("#xywh")) {
+            return "area";
+        }
+        return null;
+    }
+    
     private JSONArray transformToRanges(List<String> xpointers) {
 
         JSONArray ja = new JSONArray();
@@ -256,11 +268,13 @@
     }
 
     /**
-     * creates an Annotation object with data from JSON. 
+     * creates an Annotation object with data from JSON.
      * 
-     * uses the specification from the annotator project: {@link https://github.com/okfn/annotator/wiki/Annotation-format}
+     * uses the specification from the annotator project: {@link https
+     * ://github.com/okfn/annotator/wiki/Annotation-format}
      * 
-     * 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
+     * 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
      * de.mpiwg.itgroup.annotationManager.Constants.NS
      * 
      * @param jo
@@ -270,7 +284,7 @@
     public Annotation createAnnotation(JSONObject jo, Representation entity) throws JSONException {
         return updateAnnotation(new Annotation(), jo, entity);
     }
-        
+
     public Annotation updateAnnotation(Annotation annot, JSONObject jo, Representation entity) throws JSONException {
         // annotated uri
         String url = annot.url;
@@ -315,7 +329,8 @@
         if (username == null) {
             username = authUser;
         }
-        // username should be a URI, if not it will set to the MPIWG namespace defined in
+        // username should be a URI, if not it will set to the MPIWG namespace
+        // defined in
         // de.mpiwg.itgroup.annotationManager.Constants.NS
         if (userUri == null) {
             if (username.startsWith("http")) {
@@ -325,8 +340,8 @@
             }
         }
         // TODO: should we overwrite the creator?
-        
-        // create xpointer
+
+        // create xpointer from the first range/area
         String xpointer = annot.xpointer;
         if (jo.has("ranges")) {
             JSONObject ranges = jo.getJSONArray("ranges").getJSONObject(0);
@@ -347,6 +362,24 @@
                 return null;
             }
         }
+        if (jo.has("areas")) {
+            JSONObject area = jo.getJSONArray("areas").getJSONObject(0);
+            String x = area.getString("x");
+            String y = area.getString("end");
+            String width = "0";
+            String height = "0";
+            if (area.has("width")) {
+                width = area.getString("width");
+                height = area.getString("height");
+            }
+            try {
+                xpointer = url + "#" + URLEncoder.encode(String.format("xywh=fraction:%s,%s,%s,%s", x, y, width, height), "utf-8");
+            } catch (UnsupportedEncodingException e) {
+                e.printStackTrace();
+                setStatus(Status.SERVER_ERROR_INTERNAL);
+                return null;
+            }
+        }
         return new Annotation(xpointer, userUri, annot.time, text, annot.type);
     }