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));
|
30
|
67 /*
|
|
68 * author
|
|
69 */
|
25
|
70 if (annot.creator.startsWith("http")) {
|
|
71 retQuad.add(new Quadruple(annotationUrl, NS.DCTERMS_NS + "creator", annot.creator, context));
|
|
72 } else {
|
|
73 // TODO: this should not happen
|
|
74 retQuad.add(new LiteralQuadruple(annotationUrl, NS.DCTERMS_NS + "creator", annot.creator, context));
|
|
75 }
|
30
|
76 /*
|
|
77 * creation time
|
|
78 */
|
17
|
79 if (annot.time == null) {
|
|
80 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
|
|
81 annot.time = format.format(Calendar.getInstance().getTime());
|
|
82 }
|
25
|
83 retQuad.add(new LiteralQuadruple(annotationUrl, NS.DCTERMS_NS + "created", annot.time, context));
|
17
|
84
|
29
|
85 if (annot.page == null || annot.page == "") {
|
30
|
86 /*
|
|
87 * target without page number (full URL)
|
|
88 */
|
29
|
89 retQuad.add(new Quadruple(annot.xpointer, NS.RDF_NS + "type", NS.OAC_NS + "Target", context));
|
|
90 // is target of annotation
|
|
91 retQuad.add(new Quadruple(annotationUrl, NS.OAC_NS + "hasTarget", annot.xpointer, context));
|
|
92 } else {
|
30
|
93 /*
|
|
94 * ConstrainedTarget with page number
|
|
95 */
|
|
96 String ctUrl = createRessourceURL("annotCT:");
|
|
97 // is ConstrainedTarget
|
|
98 retQuad.add(new Quadruple(ctUrl, NS.RDF_NS + "type", NS.OAC_NS + "ConstrainedTarget", context));
|
29
|
99 // is target of annotation
|
30
|
100 retQuad.add(new Quadruple(annotationUrl, NS.OAC_NS + "hasTarget", ctUrl, context));
|
|
101 // constrains Target (full URL)
|
|
102 retQuad.add(new Quadruple(ctUrl, NS.OAC_NS + "constrains", annot.xpointer, context));
|
29
|
103 // TextPageConstraint
|
30
|
104 String tpcUrl = createRessourceURL("annotC:");
|
|
105 // is TextPageConstraint
|
|
106 retQuad.add(new Quadruple(tpcUrl, NS.RDF_NS + "type", NS.MPIWG_ANNOT_NS + "TextPageConstraint", context));
|
|
107 // page number
|
|
108 retQuad.add(new LiteralQuadruple(tpcUrl, NS.MPIWG_ANNOT_NS + "textPage", annot.page, context));
|
|
109 // textPageDisplay
|
|
110 if (annot.displayUrl != null && annot.displayUrl != "") {
|
|
111 retQuad.add(new LiteralQuadruple(tpcUrl, NS.MPIWG_ANNOT_NS + "textPageDisplay", annot.displayUrl, context));
|
|
112 }
|
|
113 // ConstrainedTarget has this Constraint
|
|
114 retQuad.add(new Quadruple(ctUrl, NS.OAC_NS + "hasConstraint", tpcUrl, context));
|
|
115 //TODO: displayUrl
|
29
|
116 }
|
27
|
117
|
26
|
118 // annotation body
|
27
|
119 if (annot.url != null && annot.url.startsWith("http://")) {
|
30
|
120 /*
|
|
121 * body is resource
|
|
122 */
|
29
|
123 retQuad.add(new Quadruple(annot.url, NS.RDF_NS + "type", NS.OAC_NS + "Body", context));
|
27
|
124 // is body of annotation
|
|
125 retQuad.add(new Quadruple(annotationUrl, NS.OAC_NS + "hasBody", annot.url, context));
|
|
126 } else {
|
30
|
127 /*
|
|
128 * body is literal text
|
|
129 */
|
|
130 String abUrl = createRessourceURL("annotB:");
|
|
131 // is Body and ContentAsText
|
|
132 retQuad.add(new Quadruple(abUrl, NS.RDF_NS + "type", NS.OAC_NS + "Body", context));
|
|
133 retQuad.add(new Quadruple(abUrl, NS.RDF_NS + "type", NS.CNT_NS + "ContentAsText", context));
|
|
134 // has text
|
|
135 retQuad.add(new LiteralQuadruple(abUrl, NS.CNT_NS + "chars", annot.text, context));
|
27
|
136 // is body of annotation
|
30
|
137 retQuad.add(new Quadruple(annotationUrl, NS.OAC_NS + "hasBody", abUrl, context));
|
27
|
138 }
|
17
|
139
|
|
140 for (Quadruple ret : retQuad) {
|
|
141 logger.debug(ret.toString());
|
|
142 }
|
|
143
|
|
144 annot.setAnnotationUri(annotationUrl);
|
|
145 return retQuad;
|
|
146 }
|
|
147
|
|
148 /**
|
30
|
149 * Erzeuge eine urn aus der aktuellen Zeit in millis
|
17
|
150 *
|
|
151 * @return
|
|
152 */
|
|
153 private String createRessourceURL(String prefix) {
|
|
154
|
|
155 Calendar cal = Calendar.getInstance();
|
|
156
|
|
157 long time = cal.getTimeInMillis();
|
|
158
|
|
159 return String.format("%s%s%s", urlBase, prefix, time);
|
|
160
|
|
161 }
|
0
|
162
|
17
|
163 /**
|
|
164 * Stores the Annotation in the TripleStore.
|
|
165 *
|
|
166 * @param annot
|
|
167 * @return
|
|
168 * @throws TripleStoreStoreError
|
|
169 */
|
|
170 public Annotation storeAnnotation(Annotation annot) throws TripleStoreStoreError {
|
|
171 List<Quadruple> annotationRdf = new ArrayList<Quadruple>();
|
|
172 if ((annot.type == null) || annot.type.equals("")) {
|
|
173 annot.type = "Comment";
|
|
174 }
|
27
|
175 annotationRdf = annot2quadruple(annot);
|
17
|
176 try {
|
|
177 TripleStoreHandler th = TripleStoreConnection.newTripleStoreHandler();
|
|
178 th.write(annotationRdf);
|
|
179 } catch (RepositoryException e) {
|
|
180 // TODO Auto-generated catch block
|
|
181 e.printStackTrace();
|
|
182 throw new TripleStoreStoreError();
|
|
183 } catch (TripleStoreHandlerException e) {
|
|
184 // TODO Auto-generated catch block
|
|
185 e.printStackTrace();
|
|
186 throw new TripleStoreStoreError();
|
|
187 }
|
|
188 return annot;
|
|
189 }
|
0
|
190
|
|
191 }
|