0
|
1 package de.mpiwg.itgroup.annotationManager.RDFHandling;
|
|
2
|
|
3 import java.text.SimpleDateFormat;
|
|
4 import java.util.ArrayList;
|
|
5 import java.util.Calendar;
|
|
6 import java.util.List;
|
|
7
|
|
8 import org.apache.log4j.Logger;
|
|
9 import org.openrdf.repository.RepositoryException;
|
|
10
|
|
11 import de.mpiwg.itgroup.annotationManager.Constants.NS;
|
|
12 import de.mpiwg.itgroup.annotationManager.Errors.TripleStoreStoreError;
|
5
|
13 import de.mpiwg.itgroup.triplestoremanager.exceptions.TripleStoreHandlerException;
|
|
14 import de.mpiwg.itgroup.triplestoremanager.owl.TripleStoreHandler;
|
|
15 import de.mpiwg.itgroup.triplestoremanager.owl.TripleStoreHandler.LiteralQuadruple;
|
|
16 import de.mpiwg.itgroup.triplestoremanager.owl.TripleStoreHandler.Quadruple;
|
0
|
17
|
|
18 /**
|
|
19 * @author dwinter
|
17
|
20 *
|
|
21 * Klasse zum Konvertieren von Annotationen zum mpiwg RDF-Format: http://ontologies.mpiwg-berlin.mpg.de/annotations/
|
0
|
22 */
|
|
23
|
|
24 public class Convert {
|
17
|
25 private String context = "file:///annotations";
|
|
26 private static Logger logger = Logger.getRootLogger();
|
25
|
27 private String urlBase = "http://entities.mpiwg-berlin.mpg.de/annotations/"; // TODO should go into config
|
17
|
28
|
|
29 public Convert(String context) {
|
|
30 this.context = context;
|
|
31 }
|
|
32
|
|
33 /**
|
|
34 *
|
|
35 * @param xpointer
|
|
36 * Beschreibt die Annotation
|
|
37 * @param creator
|
|
38 * Username des Creators
|
|
39 * @param time
|
|
40 * Erstellungszeit, wenn null wird das aktuelle Datum verwenden beim Konvertieren
|
|
41 * @param text
|
|
42 * der Annotation
|
|
43 * @param type
|
|
44 * Annotationstype (Entsprechend den in http://www.w3.org/2000/10/annotationType# definierten.)
|
|
45 * @return
|
|
46 */
|
|
47
|
|
48 private List<Quadruple> annot2quadruple(String xpointer, String creator, String time, String annot, String type) {
|
|
49 return annot2quadruple(new Annotation(xpointer, creator, time, annot, type));
|
|
50 }
|
|
51
|
|
52 /**
|
|
53 * @param annot
|
|
54 * @return
|
|
55 */
|
|
56 public List<Quadruple> annot2quadruple(Annotation annot) {
|
|
57 List<Quadruple> retQuad = new ArrayList<Quadruple>();
|
|
58
|
|
59 // create new URL if annot has no annotationUrl
|
|
60 String annotationUrl = annot.getAnnotationUri();
|
|
61 if (annotationUrl == null) {
|
|
62 annotationUrl = createRessourceURL("annot:");
|
|
63 }
|
26
|
64
|
|
65 // annotation class
|
|
66 retQuad.add(new Quadruple(annotationUrl, NS.RDF_NS + "type", NS.OAC_NS + "Annotation", context));
|
|
67 // TODO: what types?
|
27
|
68 // retQuad.add(new LiteralQuadruple(annotationUrl, NS.RDF + "type", annot.type, context));
|
25
|
69
|
|
70 // author
|
|
71 if (annot.creator.startsWith("http")) {
|
|
72 retQuad.add(new Quadruple(annotationUrl, NS.DCTERMS_NS + "creator", annot.creator, context));
|
|
73 } else {
|
|
74 // TODO: this should not happen
|
|
75 retQuad.add(new LiteralQuadruple(annotationUrl, NS.DCTERMS_NS + "creator", annot.creator, context));
|
|
76 }
|
|
77 // creation time
|
17
|
78 if (annot.time == null) {
|
|
79 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
|
|
80 annot.time = format.format(Calendar.getInstance().getTime());
|
|
81 }
|
25
|
82 retQuad.add(new LiteralQuadruple(annotationUrl, NS.DCTERMS_NS + "created", annot.time, context));
|
17
|
83
|
26
|
84 // target (full URL)
|
|
85 retQuad.add(new Quadruple(annot.xpointer, NS.RDF_NS + "type", NS.OAC_NS + "Target", context));
|
|
86 // is target of annotation
|
|
87 retQuad.add(new Quadruple(annotationUrl, NS.OAC_NS + "hasTarget", annot.xpointer, context));
|
27
|
88
|
26
|
89 // now add the xpointers
|
27
|
90 /*
|
|
91 * String[] xpointerSplitted = annot.xpointer.split("#"); if (xpointerSplitted.length > 2) { throw new XPointerError(); }
|
|
92 * retQuad.add(new Quadruple(annotationUrl, NS.MPIWG_ANNOT_URL + "annotatesDocuviewerText", xpointerSplitted[0], context));
|
|
93 * retQuad.add(new Quadruple(annotationUrl, NS.MPIWG_ANNOT_URL + "textSelection", annot.xpointer, context));
|
|
94 */
|
17
|
95
|
26
|
96 // annotation body
|
27
|
97 if (annot.url != null && annot.url.startsWith("http://")) {
|
|
98 // body is resource
|
|
99 retQuad.add(new Quadruple(annot.url, NS.RDF_NS + "type", NS.OAC_NS + "Body", context));
|
|
100 // is body of annotation
|
|
101 retQuad.add(new Quadruple(annotationUrl, NS.OAC_NS + "hasBody", annot.url, context));
|
|
102 } else {
|
|
103 // body is literal text
|
|
104 String annotationtext = createRessourceURL("annotationBody:");
|
|
105 retQuad.add(new Quadruple(annotationtext, NS.RDF_NS + "type", NS.OAC_NS + "Body", context));
|
|
106 retQuad.add(new Quadruple(annotationtext, NS.RDF_NS + "type", NS.CNT_NS + "ContentAsText", context));
|
|
107 retQuad.add(new LiteralQuadruple(annotationtext, NS.CNT_NS + "chars", annot.text, context));
|
|
108 // is body of annotation
|
|
109 retQuad.add(new Quadruple(annotationUrl, NS.OAC_NS + "hasBody", annotationtext, context));
|
|
110 }
|
17
|
111
|
|
112 for (Quadruple ret : retQuad) {
|
|
113 logger.debug(ret.toString());
|
|
114 }
|
|
115
|
|
116 annot.setAnnotationUri(annotationUrl);
|
|
117 return retQuad;
|
|
118 }
|
|
119
|
|
120 /**
|
|
121 * Erzeuge eine urn aus der aktullen Zeit in millis
|
|
122 *
|
|
123 * @return
|
|
124 */
|
|
125 private String createRessourceURL(String prefix) {
|
|
126
|
|
127 Calendar cal = Calendar.getInstance();
|
|
128
|
|
129 long time = cal.getTimeInMillis();
|
|
130
|
|
131 return String.format("%s%s%s", urlBase, prefix, time);
|
|
132
|
|
133 }
|
0
|
134
|
27
|
135
|
17
|
136 /**
|
|
137 * Stores the Annotation in the TripleStore.
|
|
138 *
|
|
139 * @param annot
|
|
140 * @return
|
|
141 * @throws TripleStoreStoreError
|
|
142 */
|
|
143 public Annotation storeAnnotation(Annotation annot) throws TripleStoreStoreError {
|
|
144 List<Quadruple> annotationRdf = new ArrayList<Quadruple>();
|
|
145 if ((annot.type == null) || annot.type.equals("")) {
|
|
146 annot.type = "Comment";
|
|
147 }
|
27
|
148 annotationRdf = annot2quadruple(annot);
|
17
|
149 try {
|
|
150 TripleStoreHandler th = TripleStoreConnection.newTripleStoreHandler();
|
|
151 th.write(annotationRdf);
|
|
152 } catch (RepositoryException e) {
|
|
153 // TODO Auto-generated catch block
|
|
154 e.printStackTrace();
|
|
155 throw new TripleStoreStoreError();
|
|
156 } catch (TripleStoreHandlerException e) {
|
|
157 // TODO Auto-generated catch block
|
|
158 e.printStackTrace();
|
|
159 throw new TripleStoreStoreError();
|
|
160 }
|
|
161 return annot;
|
|
162 }
|
0
|
163
|
17
|
164 /**
|
|
165 * @param args
|
|
166 */
|
|
167 public static void main(String[] args) {
|
|
168 Convert myConvert = new Convert("http://annotations.rdf");
|
|
169 List<Quadruple> rets = myConvert
|
|
170 .annot2quadruple(
|
|
171 "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))",
|
|
172 "mbuchman", null, "myannot", "Example");
|
|
173 for (Quadruple ret : rets) {
|
|
174 System.out.println(ret.toString());
|
|
175 }
|
|
176 }
|
0
|
177
|
|
178 }
|