Mercurial > hg > AnnotationManager
diff src/de/mpiwg/itgroup/annotationManager/RDFHandling/Convert.java @ 17:b0ef5c860464
updating and deleting annotations works now!
more cleanup.
author | casties |
---|---|
date | Thu, 22 Mar 2012 21:37:16 +0100 |
parents | 9393c9c9b916 |
children |
line wrap: on
line diff
--- a/src/de/mpiwg/itgroup/annotationManager/RDFHandling/Convert.java Thu Mar 22 10:54:56 2012 +0100 +++ b/src/de/mpiwg/itgroup/annotationManager/RDFHandling/Convert.java Thu Mar 22 21:37:16 2012 +0100 @@ -22,306 +22,214 @@ /** * @author dwinter - * - * Klasse zum Konvertieren von Annotationen zum mpiwg RDF-Format: - * http://ontologies.mpiwg-berlin.mpg.de/annotations/ + * + * Klasse zum Konvertieren von Annotationen zum mpiwg RDF-Format: http://ontologies.mpiwg-berlin.mpg.de/annotations/ */ public class Convert { - private String ctx; - private Logger logger = Logger.getRootLogger(); - private String urlBase="http://ontologies.mpiwg-berlin.mpg.de/annotations/"; //TODO should go into config - public Convert(String c){ - ctx=c; - } - - /** - * Fasst alle Parameter zusammen, die eine Annotation bilden - * @author dwinter - * - */ - static public class Annotation { - public String xpointer=null; //if queried xpointer should contain the first xpointer in the xpointers list, if there is more than one. - public String creator=null; - public String time=null; - public String text=null; - public String type=null; - public String url; - public List<String> xpointers=null; // list of xpointers on the page url, can be empty or null if there is only one. - public String annotationUri=null; // uri der annotation im triplestore - - - /** - * @param xpointer Beschreibt die Annotation - * @param creator Username des Creators oder URI der Creators - * @param time Erstellungszeit, wenn null wird das aktuelle Datum verwenden beim Konvertieren - * @param text der Annotation - * @param type Annotationstype (Entsprechend den in http://www.w3.org/2000/10/annotationType# definierten.) - * @param url Url einer Annotation - * @param annotationUri Uri der Annotation im Triplestore - */ - public Annotation(String xpointer, String creator, String time, String text, String type, String url,String annotationUri){ - this.xpointer=xpointer; - this.creator=creator; - this.time=time; - this.text=text; - this.type=type; - this.url=url; - this.annotationUri=annotationUri; - } - /** - * @param xpointer Beschreibt die Annotation - * @param creator Username des Creators - * @param time Erstellungszeit, wenn null wird das aktuelle Datum verwenden beim Konvertieren - * @param text der Annotation - * @param type Annotationstype (Entsprechend den in http://www.w3.org/2000/10/annotationType# definierten.) - * @param url Url einer Annotation - */ - public Annotation(String xpointer, String creator, String time, String text, String type, String url){ - this.xpointer=xpointer; - this.creator=creator; - this.time=time; - this.text=text; - this.type=type; - this.url=url; - } - - /** - * @param xpointer Beschreibt die Annotation - * @param creator Username des Creators - * @param time Erstellungszeit, wenn null wird das aktuelle Datum verwenden beim Konvertieren - * @param text der Annotation - * @param type Annotationstype (Entsprechend den in http://www.w3.org/2000/10/annotationType# definierten.) - */ - public Annotation(String xpointer, String creator, String time, String annot, String type){ - this.xpointer=xpointer; - this.creator=creator; - this.time=time; - this.text=annot; - this.type=type; - this.url=null; - } - - - } - private String annotUrl=null; - - /** - * - * @param xpointer Beschreibt die Annotation - * @param creator Username des Creators - * @param time Erstellungszeit, wenn null wird das aktuelle Datum verwenden beim Konvertieren - * @param text der Annotation - * @param type Annotationstype (Entsprechend den in http://www.w3.org/2000/10/annotationType# definierten.) - * @return - */ - - private List<Quadruple> annot2quadruple(String xpointer, String creator, String time, String annot, String type){ - return annot2quadruple(new Annotation(xpointer, creator, time, annot, type)); - } - - - - /** - * @param annot - * @return - */ - public List<Quadruple> annot2quadruple(Annotation annot){ - List<Quadruple> retQuad = new ArrayList<Quadruple>(); - - String annotation = createRessourceURL("annot:"); - - if (annot.time==null){ - SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"); - annot.time=format.format(Calendar.getInstance().getTime()); - - } - - //TODO: check type - retQuad.add(new Quadruple(annotation, NS.RDF+"type", NS.ANNOTATION_TYPE+annot.type, ctx)); - - //add author - - - if (annot.creator.startsWith("http")){ - retQuad.add(new Quadruple(annotation, NS.ANNOTATION_NS+"author", annot.creator, ctx)); - } else { - retQuad.add(new LiteralQuadruple(annotation, NS.ANNOTATION_NS+"author", annot.creator, ctx)); - } - - // creation time - retQuad.add(new LiteralQuadruple(annotation, NS.ANNOTATION_NS+"created", annot.time, ctx)); - - String[] xpointerSplitted = annot.xpointer.split("#"); - - if (xpointerSplitted.length>2){ - annotUrl=null; - throw new XPointerError(); - } - - // now add the xpointers - retQuad.add(new Quadruple(annotation, NS.MPIWG_annot+"annotatesDocuviewerText", xpointerSplitted[0], ctx)); - retQuad.add(new Quadruple(annotation, NS.MPIWG_annot+"textSelection", annot.xpointer, ctx)); - - String annotationtext =createRessourceURL("annotText:"); - - retQuad.add(new Quadruple(annotation, NS.ANNOTATION_NS+"body", annotationtext, ctx)); - - retQuad.add(new Quadruple(annotationtext, NS.RDF+"type", NS.MPIWG_annot+"StandardTextNote", ctx)); - - retQuad.add(new LiteralQuadruple(annotationtext, NS.MPIWG_annot+"containsText", annot.text, ctx)); - - for (Quadruple ret:retQuad){ - logger.debug(ret.toString()); - } - - annotUrl=annotation; - return retQuad; - } - - /** - * Erzeuge eine urn aus der aktullen Zeit in millis - * @return - */ - private String createRessourceURL(String prefix) { - - Calendar cal = Calendar.getInstance(); + private String context = "file:///annotations"; + private static Logger logger = Logger.getRootLogger(); + private String urlBase = "http://ontologies.mpiwg-berlin.mpg.de/annotations/"; // TODO should go into config + + public Convert(String context) { + this.context = context; + } + + /** + * + * @param xpointer + * Beschreibt die Annotation + * @param creator + * Username des Creators + * @param time + * Erstellungszeit, wenn null wird das aktuelle Datum verwenden beim Konvertieren + * @param text + * der Annotation + * @param type + * Annotationstype (Entsprechend den in http://www.w3.org/2000/10/annotationType# definierten.) + * @return + */ + + private List<Quadruple> annot2quadruple(String xpointer, String creator, String time, String annot, String type) { + return annot2quadruple(new Annotation(xpointer, creator, time, annot, type)); + } + + /** + * @param annot + * @return + */ + public List<Quadruple> annot2quadruple(Annotation annot) { + List<Quadruple> retQuad = new ArrayList<Quadruple>(); + + // create new URL if annot has no annotationUrl + String annotationUrl = annot.getAnnotationUri(); + if (annotationUrl == null) { + annotationUrl = createRessourceURL("annot:"); + } + + if (annot.time == null) { + SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"); + annot.time = format.format(Calendar.getInstance().getTime()); + } + + // TODO: check type + retQuad.add(new Quadruple(annotationUrl, NS.RDF + "type", NS.ANNOTATION_TYPE + annot.type, context)); + + // add author + + if (annot.creator.startsWith("http")) { + retQuad.add(new Quadruple(annotationUrl, NS.ANNOTATION_NS + "author", annot.creator, context)); + } else { + retQuad.add(new LiteralQuadruple(annotationUrl, NS.ANNOTATION_NS + "author", annot.creator, context)); + } + + // creation time + retQuad.add(new LiteralQuadruple(annotationUrl, NS.ANNOTATION_NS + "created", annot.time, context)); + + String[] xpointerSplitted = annot.xpointer.split("#"); + + if (xpointerSplitted.length > 2) { + throw new XPointerError(); + } + + // now add the xpointers + retQuad.add(new Quadruple(annotationUrl, NS.MPIWG_annot + "annotatesDocuviewerText", xpointerSplitted[0], context)); + retQuad.add(new Quadruple(annotationUrl, NS.MPIWG_annot + "textSelection", annot.xpointer, context)); + + String annotationtext = createRessourceURL("annotText:"); + + retQuad.add(new Quadruple(annotationUrl, NS.ANNOTATION_NS + "body", annotationtext, context)); + + retQuad.add(new Quadruple(annotationtext, NS.RDF + "type", NS.MPIWG_annot + "StandardTextNote", context)); + + retQuad.add(new LiteralQuadruple(annotationtext, NS.MPIWG_annot + "containsText", annot.text, context)); + + for (Quadruple ret : retQuad) { + logger.debug(ret.toString()); + } + + annot.setAnnotationUri(annotationUrl); + return retQuad; + } + + /** + * Erzeuge eine urn aus der aktullen Zeit in millis + * + * @return + */ + private String createRessourceURL(String prefix) { + + Calendar cal = Calendar.getInstance(); + + long time = cal.getTimeInMillis(); + + return String.format("%s%s%s", urlBase, prefix, time); + + } - long time = cal.getTimeInMillis(); - - return String.format("%s%s%s", urlBase,prefix,time); - + /** + * Hier ist die uri der Annotation angegeben. + * + * @param annot + * @return + */ + + private List<Quadruple> rel2quadruple(Annotation annot) { + + List<Quadruple> retQuad = new ArrayList<Quadruple>(); + + String annotation = createRessourceURL("annot:"); + + if (annot.time == null) { + SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss'Z'"); + annot.time = format.format(Calendar.getInstance().getTime()); + + } + + // TODO: check type + retQuad.add(new Quadruple(annotation, NS.RDF + "type", NS.ANNOTATION_TYPE + annot.type, context)); - - } - - /** - * Hier ist die uri der Annotation angegeben. - * @param annot - * @return - */ + // add author + if (annot.creator.startsWith("http")) { + retQuad.add(new Quadruple(annotation, NS.ANNOTATION_NS + "author", annot.creator, context)); + } else { + retQuad.add(new LiteralQuadruple(annotation, NS.ANNOTATION_NS + "author", annot.creator, context)); + } + + // creation time + retQuad.add(new LiteralQuadruple(annotation, NS.ANNOTATION_NS + "created", annot.time, context)); + + String[] xpointerSplitted = annot.xpointer.split("#"); + + if (xpointerSplitted.length > 2) { + throw new XPointerError(); + } + + // now add the xpointers + retQuad.add(new Quadruple(annotation, NS.MPIWG_annot + "annotatesDocuviewerText", xpointerSplitted[0], context)); + retQuad.add(new Quadruple(annotation, NS.MPIWG_annot + "textSelection", annot.xpointer, context)); + + // String annotationtext =createRessourceURL("annotText:"); + + retQuad.add(new Quadruple(annotation, NS.ANNOTATION_NS + "body", annot.url, context)); + + retQuad.add(new Quadruple(annot.url, NS.RDF + "type", NS.MPIWG_annot + "ExtendedAnnotation", context)); + + for (Quadruple ret : retQuad) { + logger.debug(ret.toString()); + } - private List<Quadruple> rel2quadruple(Annotation annot) { - - List<Quadruple> retQuad = new ArrayList<Quadruple>(); - - String annotation = createRessourceURL("annot:"); - - if (annot.time==null){ - SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss'Z'"); - annot.time=format.format(Calendar.getInstance().getTime()); - - } - - //TODO: check type - retQuad.add(new Quadruple(annotation, NS.RDF+"type", NS.ANNOTATION_TYPE+annot.type, ctx)); - - //add author - - - if (annot.creator.startsWith("http")){ - retQuad.add(new Quadruple(annotation, NS.ANNOTATION_NS+"author", annot.creator, ctx)); - } else { - retQuad.add(new LiteralQuadruple(annotation, NS.ANNOTATION_NS+"author", annot.creator, ctx)); - } - - // creation time - retQuad.add(new LiteralQuadruple(annotation, NS.ANNOTATION_NS+"created", annot.time, ctx)); - - String[] xpointerSplitted = annot.xpointer.split("#"); - - if (xpointerSplitted.length>2){ - annotUrl=null; - throw new XPointerError(); - } - - // now add the xpointers - retQuad.add(new Quadruple(annotation, NS.MPIWG_annot+"annotatesDocuviewerText", xpointerSplitted[0], ctx)); - retQuad.add(new Quadruple(annotation, NS.MPIWG_annot+"textSelection", annot.xpointer, ctx)); - - //String annotationtext =createRessourceURL("annotText:"); - - retQuad.add(new Quadruple(annotation, NS.ANNOTATION_NS+"body", annot.url, ctx)); - - retQuad.add(new Quadruple(annot.url, NS.RDF+"type", NS.MPIWG_annot+"ExtendedAnnotation", ctx)); - - - for (Quadruple ret:retQuad){ - logger.debug(ret.toString()); - } - - annotUrl=annotation; - return retQuad; - - } - /** - * gibt nach die nach aufruf eines Converters erzeuge aktuelle url der annotation zurueck - * @return - */ - public String getAnnotUrl() { - - return annotUrl; - } + // save new annotation url in annotation object + annot.setAnnotationUri(annotation); + return retQuad; + + } - /** - * @param args - */ - public static void main(String[] args) { - Convert myConvert = new Convert("http://annotations.rdf"); - List<Quadruple> rets = myConvert.annot2quadruple("http://mpdl-dev.mpiwg-berlin.mpg.de/ECHOdocuViewfullTest?url=/mpiwg/online/permanent/library/163127KK&viewMode=text&pn=7#xpointer(string-range(id("s1"), "", 66, 12))", "mbuchman", null, "myannot", "Example"); - for (Quadruple ret:rets){ - System.out.println(ret.toString()); - } - - } - - - public Annotation storeAnnotation(Convert.Annotation annot) throws TripleStoreStoreError { - - //Convert convert = new Convert("<file:///annotations>"); - - - if ((annot.type==null) || annot.type.equals("")){ - annot.type="Comment"; - } - - List<Quadruple> annots = new ArrayList<Quadruple>(); - if (annot.text!=null && !annot.text.equals("")) - annots=annot2quadruple(annot); - if (annot.url!=null && !annot.url.equals("")) - annots.addAll(rel2quadruple(annot)); + /** + * Stores the Annotation in the TripleStore. + * + * @param annot + * @return + * @throws TripleStoreStoreError + */ + public Annotation storeAnnotation(Annotation annot) throws TripleStoreStoreError { + List<Quadruple> annotationRdf = new ArrayList<Quadruple>(); + if ((annot.type == null) || annot.type.equals("")) { + annot.type = "Comment"; + } + if (annot.text != null && !annot.text.equals("")) { + annotationRdf = annot2quadruple(annot); + } + if (annot.url != null && !annot.url.equals("")) { + annotationRdf.addAll(rel2quadruple(annot)); + } + try { + TripleStoreHandler th = TripleStoreConnection.newTripleStoreHandler(); + th.write(annotationRdf); + } catch (RepositoryException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + throw new TripleStoreStoreError(); + } catch (TripleStoreHandlerException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + throw new TripleStoreStoreError(); + } + return annot; + } - - - try { - ChildContext context = (ChildContext)Context.getCurrent(); - String user = context.getParameters().getFirstValue("de.mpiwg.itgroup.annotationManager.virtuoso.tripleStoreUser"); - String pw = context.getParameters().getFirstValue("de.mpiwg.itgroup.annotationManager.virtuoso.tripleStoreUserPassword"); - String connectionURL = context.getParameters().getFirstValue("de.mpiwg.itgroup.annotationManager.virtuoso.tripleStoreConnectionURL"); - if (user == null || pw == null || connectionURL == null) { - logger.error(String.format("Missing triplestore parameters! (user=%s pw=%s url=%s)", user, pw, connectionURL)); - throw new TripleStoreStoreError(); - } - //TripleStoreHandler th = new TripleStoreHandler("jdbc:virtuoso://virtuoso.mpiwg-berlin.mpg.de:1111", user, pw); - TripleStoreHandler th = new TripleStoreHandler(connectionURL, user, pw); - - th.write(annots); - } catch (RepositoryException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - throw new TripleStoreStoreError(); - } catch (TripleStoreHandlerException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - throw new TripleStoreStoreError(); - } - - - annot.annotationUri=getAnnotUrl(); - return annot; -} - - - + /** + * @param args + */ + public static void main(String[] args) { + Convert myConvert = new Convert("http://annotations.rdf"); + List<Quadruple> rets = myConvert + .annot2quadruple( + "http://mpdl-dev.mpiwg-berlin.mpg.de/ECHOdocuViewfullTest?url=/mpiwg/online/permanent/library/163127KK&viewMode=text&pn=7#xpointer(string-range(id("s1"), "", 66, 12))", + "mbuchman", null, "myannot", "Example"); + for (Quadruple ret : rets) { + System.out.println(ret.toString()); + } + } }