0
|
1 package de.mpiwg.itgroup.annotationManager.RDFHandling;
|
|
2
|
|
3 import java.text.DateFormat;
|
|
4 import java.text.SimpleDateFormat;
|
|
5 import java.util.ArrayList;
|
|
6 import java.util.Calendar;
|
|
7 import java.util.Date;
|
|
8 import java.util.List;
|
|
9
|
|
10 import org.apache.log4j.Logger;
|
|
11 import org.openrdf.repository.RepositoryException;
|
|
12 import org.restlet.Context;
|
|
13 import org.restlet.engine.component.ChildContext;
|
|
14
|
|
15 import de.mpiwg.itgroup.annotationManager.Constants.NS;
|
|
16 import de.mpiwg.itgroup.annotationManager.Errors.TripleStoreStoreError;
|
|
17 import de.mpiwg.itgroup.annotationManager.Errors.XPointerError;
|
5
|
18 import de.mpiwg.itgroup.triplestoremanager.exceptions.TripleStoreHandlerException;
|
|
19 import de.mpiwg.itgroup.triplestoremanager.owl.TripleStoreHandler;
|
|
20 import de.mpiwg.itgroup.triplestoremanager.owl.TripleStoreHandler.LiteralQuadruple;
|
|
21 import de.mpiwg.itgroup.triplestoremanager.owl.TripleStoreHandler.Quadruple;
|
0
|
22
|
|
23 /**
|
|
24 * @author dwinter
|
17
|
25 *
|
|
26 * Klasse zum Konvertieren von Annotationen zum mpiwg RDF-Format: http://ontologies.mpiwg-berlin.mpg.de/annotations/
|
0
|
27 */
|
|
28
|
|
29 public class Convert {
|
17
|
30 private String context = "file:///annotations";
|
|
31 private static Logger logger = Logger.getRootLogger();
|
25
|
32 private String urlBase = "http://entities.mpiwg-berlin.mpg.de/annotations/"; // TODO should go into config
|
17
|
33
|
|
34 public Convert(String context) {
|
|
35 this.context = context;
|
|
36 }
|
|
37
|
|
38 /**
|
|
39 *
|
|
40 * @param xpointer
|
|
41 * Beschreibt die Annotation
|
|
42 * @param creator
|
|
43 * Username des Creators
|
|
44 * @param time
|
|
45 * Erstellungszeit, wenn null wird das aktuelle Datum verwenden beim Konvertieren
|
|
46 * @param text
|
|
47 * der Annotation
|
|
48 * @param type
|
|
49 * Annotationstype (Entsprechend den in http://www.w3.org/2000/10/annotationType# definierten.)
|
|
50 * @return
|
|
51 */
|
|
52
|
|
53 private List<Quadruple> annot2quadruple(String xpointer, String creator, String time, String annot, String type) {
|
|
54 return annot2quadruple(new Annotation(xpointer, creator, time, annot, type));
|
|
55 }
|
|
56
|
|
57 /**
|
|
58 * @param annot
|
|
59 * @return
|
|
60 */
|
|
61 public List<Quadruple> annot2quadruple(Annotation annot) {
|
|
62 List<Quadruple> retQuad = new ArrayList<Quadruple>();
|
|
63
|
|
64 // create new URL if annot has no annotationUrl
|
|
65 String annotationUrl = annot.getAnnotationUri();
|
|
66 if (annotationUrl == null) {
|
|
67 annotationUrl = createRessourceURL("annot:");
|
|
68 }
|
25
|
69 // type
|
|
70 // TODO: where to get types?
|
|
71 retQuad.add(new LiteralQuadruple(annotationUrl, NS.RDF + "type", annot.type, context));
|
|
72
|
|
73 // author
|
|
74 if (annot.creator.startsWith("http")) {
|
|
75 retQuad.add(new Quadruple(annotationUrl, NS.DCTERMS_NS + "creator", annot.creator, context));
|
|
76 } else {
|
|
77 // TODO: this should not happen
|
|
78 retQuad.add(new LiteralQuadruple(annotationUrl, NS.DCTERMS_NS + "creator", annot.creator, context));
|
|
79 }
|
|
80 // creation time
|
17
|
81 if (annot.time == null) {
|
|
82 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
|
|
83 annot.time = format.format(Calendar.getInstance().getTime());
|
|
84 }
|
25
|
85 retQuad.add(new LiteralQuadruple(annotationUrl, NS.DCTERMS_NS + "created", annot.time, context));
|
17
|
86
|
|
87 String[] xpointerSplitted = annot.xpointer.split("#");
|
|
88
|
|
89 if (xpointerSplitted.length > 2) {
|
|
90 throw new XPointerError();
|
|
91 }
|
|
92
|
|
93 // now add the xpointers
|
|
94 retQuad.add(new Quadruple(annotationUrl, NS.MPIWG_annot + "annotatesDocuviewerText", xpointerSplitted[0], context));
|
|
95 retQuad.add(new Quadruple(annotationUrl, NS.MPIWG_annot + "textSelection", annot.xpointer, context));
|
|
96
|
|
97 String annotationtext = createRessourceURL("annotText:");
|
|
98
|
|
99 retQuad.add(new Quadruple(annotationUrl, NS.ANNOTATION_NS + "body", annotationtext, context));
|
|
100
|
|
101 retQuad.add(new Quadruple(annotationtext, NS.RDF + "type", NS.MPIWG_annot + "StandardTextNote", context));
|
|
102
|
|
103 retQuad.add(new LiteralQuadruple(annotationtext, NS.MPIWG_annot + "containsText", annot.text, context));
|
|
104
|
|
105 for (Quadruple ret : retQuad) {
|
|
106 logger.debug(ret.toString());
|
|
107 }
|
|
108
|
|
109 annot.setAnnotationUri(annotationUrl);
|
|
110 return retQuad;
|
|
111 }
|
|
112
|
|
113 /**
|
|
114 * Erzeuge eine urn aus der aktullen Zeit in millis
|
|
115 *
|
|
116 * @return
|
|
117 */
|
|
118 private String createRessourceURL(String prefix) {
|
|
119
|
|
120 Calendar cal = Calendar.getInstance();
|
|
121
|
|
122 long time = cal.getTimeInMillis();
|
|
123
|
|
124 return String.format("%s%s%s", urlBase, prefix, time);
|
|
125
|
|
126 }
|
0
|
127
|
17
|
128 /**
|
|
129 * Hier ist die uri der Annotation angegeben.
|
|
130 *
|
|
131 * @param annot
|
|
132 * @return
|
|
133 */
|
|
134
|
|
135 private List<Quadruple> rel2quadruple(Annotation annot) {
|
|
136
|
|
137 List<Quadruple> retQuad = new ArrayList<Quadruple>();
|
|
138
|
|
139 String annotation = createRessourceURL("annot:");
|
|
140
|
|
141 if (annot.time == null) {
|
|
142 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss'Z'");
|
|
143 annot.time = format.format(Calendar.getInstance().getTime());
|
|
144
|
|
145 }
|
|
146
|
|
147 // TODO: check type
|
|
148 retQuad.add(new Quadruple(annotation, NS.RDF + "type", NS.ANNOTATION_TYPE + annot.type, context));
|
0
|
149
|
17
|
150 // add author
|
|
151 if (annot.creator.startsWith("http")) {
|
|
152 retQuad.add(new Quadruple(annotation, NS.ANNOTATION_NS + "author", annot.creator, context));
|
|
153 } else {
|
|
154 retQuad.add(new LiteralQuadruple(annotation, NS.ANNOTATION_NS + "author", annot.creator, context));
|
|
155 }
|
|
156
|
|
157 // creation time
|
|
158 retQuad.add(new LiteralQuadruple(annotation, NS.ANNOTATION_NS + "created", annot.time, context));
|
|
159
|
|
160 String[] xpointerSplitted = annot.xpointer.split("#");
|
|
161
|
|
162 if (xpointerSplitted.length > 2) {
|
|
163 throw new XPointerError();
|
|
164 }
|
|
165
|
|
166 // now add the xpointers
|
|
167 retQuad.add(new Quadruple(annotation, NS.MPIWG_annot + "annotatesDocuviewerText", xpointerSplitted[0], context));
|
|
168 retQuad.add(new Quadruple(annotation, NS.MPIWG_annot + "textSelection", annot.xpointer, context));
|
|
169
|
|
170 // String annotationtext =createRessourceURL("annotText:");
|
|
171
|
|
172 retQuad.add(new Quadruple(annotation, NS.ANNOTATION_NS + "body", annot.url, context));
|
|
173
|
|
174 retQuad.add(new Quadruple(annot.url, NS.RDF + "type", NS.MPIWG_annot + "ExtendedAnnotation", context));
|
|
175
|
|
176 for (Quadruple ret : retQuad) {
|
|
177 logger.debug(ret.toString());
|
|
178 }
|
0
|
179
|
17
|
180 // save new annotation url in annotation object
|
|
181 annot.setAnnotationUri(annotation);
|
|
182 return retQuad;
|
|
183
|
|
184 }
|
0
|
185
|
17
|
186 /**
|
|
187 * Stores the Annotation in the TripleStore.
|
|
188 *
|
|
189 * @param annot
|
|
190 * @return
|
|
191 * @throws TripleStoreStoreError
|
|
192 */
|
|
193 public Annotation storeAnnotation(Annotation annot) throws TripleStoreStoreError {
|
|
194 List<Quadruple> annotationRdf = new ArrayList<Quadruple>();
|
|
195 if ((annot.type == null) || annot.type.equals("")) {
|
|
196 annot.type = "Comment";
|
|
197 }
|
|
198 if (annot.text != null && !annot.text.equals("")) {
|
|
199 annotationRdf = annot2quadruple(annot);
|
|
200 }
|
|
201 if (annot.url != null && !annot.url.equals("")) {
|
|
202 annotationRdf.addAll(rel2quadruple(annot));
|
|
203 }
|
|
204 try {
|
|
205 TripleStoreHandler th = TripleStoreConnection.newTripleStoreHandler();
|
|
206 th.write(annotationRdf);
|
|
207 } catch (RepositoryException e) {
|
|
208 // TODO Auto-generated catch block
|
|
209 e.printStackTrace();
|
|
210 throw new TripleStoreStoreError();
|
|
211 } catch (TripleStoreHandlerException e) {
|
|
212 // TODO Auto-generated catch block
|
|
213 e.printStackTrace();
|
|
214 throw new TripleStoreStoreError();
|
|
215 }
|
|
216 return annot;
|
|
217 }
|
0
|
218
|
17
|
219 /**
|
|
220 * @param args
|
|
221 */
|
|
222 public static void main(String[] args) {
|
|
223 Convert myConvert = new Convert("http://annotations.rdf");
|
|
224 List<Quadruple> rets = myConvert
|
|
225 .annot2quadruple(
|
|
226 "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))",
|
|
227 "mbuchman", null, "myannot", "Example");
|
|
228 for (Quadruple ret : rets) {
|
|
229 System.out.println(ret.toString());
|
|
230 }
|
|
231 }
|
0
|
232
|
|
233 }
|