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