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