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 }
|
26
|
69
|
|
70 // annotation class
|
|
71 retQuad.add(new Quadruple(annotationUrl, NS.RDF_NS + "type", NS.OAC_NS + "Annotation", context));
|
|
72 // TODO: what types?
|
|
73 //retQuad.add(new LiteralQuadruple(annotationUrl, NS.RDF + "type", annot.type, context));
|
25
|
74
|
|
75 // author
|
|
76 if (annot.creator.startsWith("http")) {
|
|
77 retQuad.add(new Quadruple(annotationUrl, NS.DCTERMS_NS + "creator", annot.creator, context));
|
|
78 } else {
|
|
79 // TODO: this should not happen
|
|
80 retQuad.add(new LiteralQuadruple(annotationUrl, NS.DCTERMS_NS + "creator", annot.creator, context));
|
|
81 }
|
|
82 // creation time
|
17
|
83 if (annot.time == null) {
|
|
84 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
|
|
85 annot.time = format.format(Calendar.getInstance().getTime());
|
|
86 }
|
25
|
87 retQuad.add(new LiteralQuadruple(annotationUrl, NS.DCTERMS_NS + "created", annot.time, context));
|
17
|
88
|
26
|
89 // target (full URL)
|
|
90 retQuad.add(new Quadruple(annot.xpointer, NS.RDF_NS + "type", NS.OAC_NS + "Target", context));
|
|
91 // is target of annotation
|
|
92 retQuad.add(new Quadruple(annotationUrl, NS.OAC_NS + "hasTarget", annot.xpointer, context));
|
|
93
|
|
94 // now add the xpointers
|
|
95 /* String[] xpointerSplitted = annot.xpointer.split("#");
|
17
|
96 if (xpointerSplitted.length > 2) {
|
|
97 throw new XPointerError();
|
|
98 }
|
26
|
99 retQuad.add(new Quadruple(annotationUrl, NS.MPIWG_ANNOT_URL + "annotatesDocuviewerText", xpointerSplitted[0], context));
|
|
100 retQuad.add(new Quadruple(annotationUrl, NS.MPIWG_ANNOT_URL + "textSelection", annot.xpointer, context));
|
|
101 */
|
17
|
102
|
26
|
103 // annotation body
|
|
104 String annotationtext = createRessourceURL("annotationBody:");
|
|
105
|
|
106 retQuad.add(new Quadruple(annotationUrl, NS.OAC_NS + "Body", annotationtext, context));
|
17
|
107
|
26
|
108 retQuad.add(new Quadruple(annotationtext, NS.RDF_NS + "type", NS.MPIWG_ANNOT_URL + "StandardTextNote", context));
|
17
|
109
|
26
|
110 retQuad.add(new LiteralQuadruple(annotationtext, NS.MPIWG_ANNOT_URL + "containsText", annot.text, context));
|
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
|
17
|
135 /**
|
|
136 * Hier ist die uri der Annotation angegeben.
|
|
137 *
|
|
138 * @param annot
|
|
139 * @return
|
|
140 */
|
|
141
|
|
142 private List<Quadruple> rel2quadruple(Annotation annot) {
|
|
143
|
|
144 List<Quadruple> retQuad = new ArrayList<Quadruple>();
|
|
145
|
|
146 String annotation = createRessourceURL("annot:");
|
|
147
|
|
148 if (annot.time == null) {
|
|
149 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss'Z'");
|
|
150 annot.time = format.format(Calendar.getInstance().getTime());
|
|
151
|
|
152 }
|
|
153
|
|
154 // TODO: check type
|
26
|
155 retQuad.add(new Quadruple(annotation, NS.RDF_NS + "type", NS.ANNOTATION_TYPE + annot.type, context));
|
0
|
156
|
17
|
157 // add author
|
|
158 if (annot.creator.startsWith("http")) {
|
26
|
159 retQuad.add(new Quadruple(annotation, NS.OAC_NS + "author", annot.creator, context));
|
17
|
160 } else {
|
26
|
161 retQuad.add(new LiteralQuadruple(annotation, NS.OAC_NS + "author", annot.creator, context));
|
17
|
162 }
|
|
163
|
|
164 // creation time
|
26
|
165 retQuad.add(new LiteralQuadruple(annotation, NS.OAC_NS + "created", annot.time, context));
|
17
|
166
|
|
167 String[] xpointerSplitted = annot.xpointer.split("#");
|
|
168
|
|
169 if (xpointerSplitted.length > 2) {
|
|
170 throw new XPointerError();
|
|
171 }
|
|
172
|
|
173 // now add the xpointers
|
26
|
174 retQuad.add(new Quadruple(annotation, NS.MPIWG_ANNOT_URL + "annotatesDocuviewerText", xpointerSplitted[0], context));
|
|
175 retQuad.add(new Quadruple(annotation, NS.MPIWG_ANNOT_URL + "textSelection", annot.xpointer, context));
|
17
|
176
|
|
177 // String annotationtext =createRessourceURL("annotText:");
|
|
178
|
26
|
179 retQuad.add(new Quadruple(annotation, NS.OAC_NS + "body", annot.url, context));
|
17
|
180
|
26
|
181 retQuad.add(new Quadruple(annot.url, NS.RDF_NS + "type", NS.MPIWG_ANNOT_URL + "ExtendedAnnotation", context));
|
17
|
182
|
|
183 for (Quadruple ret : retQuad) {
|
|
184 logger.debug(ret.toString());
|
|
185 }
|
0
|
186
|
17
|
187 // save new annotation url in annotation object
|
|
188 annot.setAnnotationUri(annotation);
|
|
189 return retQuad;
|
|
190
|
|
191 }
|
0
|
192
|
17
|
193 /**
|
|
194 * Stores the Annotation in the TripleStore.
|
|
195 *
|
|
196 * @param annot
|
|
197 * @return
|
|
198 * @throws TripleStoreStoreError
|
|
199 */
|
|
200 public Annotation storeAnnotation(Annotation annot) throws TripleStoreStoreError {
|
|
201 List<Quadruple> annotationRdf = new ArrayList<Quadruple>();
|
|
202 if ((annot.type == null) || annot.type.equals("")) {
|
|
203 annot.type = "Comment";
|
|
204 }
|
|
205 if (annot.text != null && !annot.text.equals("")) {
|
|
206 annotationRdf = annot2quadruple(annot);
|
|
207 }
|
|
208 if (annot.url != null && !annot.url.equals("")) {
|
|
209 annotationRdf.addAll(rel2quadruple(annot));
|
|
210 }
|
|
211 try {
|
|
212 TripleStoreHandler th = TripleStoreConnection.newTripleStoreHandler();
|
|
213 th.write(annotationRdf);
|
|
214 } catch (RepositoryException e) {
|
|
215 // TODO Auto-generated catch block
|
|
216 e.printStackTrace();
|
|
217 throw new TripleStoreStoreError();
|
|
218 } catch (TripleStoreHandlerException e) {
|
|
219 // TODO Auto-generated catch block
|
|
220 e.printStackTrace();
|
|
221 throw new TripleStoreStoreError();
|
|
222 }
|
|
223 return annot;
|
|
224 }
|
0
|
225
|
17
|
226 /**
|
|
227 * @param args
|
|
228 */
|
|
229 public static void main(String[] args) {
|
|
230 Convert myConvert = new Convert("http://annotations.rdf");
|
|
231 List<Quadruple> rets = myConvert
|
|
232 .annot2quadruple(
|
|
233 "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))",
|
|
234 "mbuchman", null, "myannot", "Example");
|
|
235 for (Quadruple ret : rets) {
|
|
236 System.out.println(ret.toString());
|
|
237 }
|
|
238 }
|
0
|
239
|
|
240 }
|