source: DASISH/t5.6/client/trunk/chrome/markingcollection/content/markingcollection/annotator-service/conversion.utils.js @ 4555

Last change on this file since 4555 was 4555, checked in by olof, 10 years ago

removed unuesed test-function an added placeholder return for getLoggedInInfo

File size: 7.9 KB
Line 
1/***
2 *
3 * @param {object} annotation annotation object from webservice
4 * @returns {annotationToOmObject.om_object} an object for Wired Marker
5 */
6function annotation2om_object(annotation) {
7    var om_object = {
8        oid: "",
9        pfid: "0",
10        doc_title: "",
11        doc_url: "",
12        con_url: "",
13        bgn_dom: "",
14        end_dom: "",
15        oid_title: "",
16        oid_property: "<PROPERTY><HYPER_ANCHOR></HYPER_ANCHOR><NOTE></NOTE></PROPERTY>",
17        oid_mode: "0",
18        oid_type: "text",
19        oid_txt: "", //must be the marked text!
20        oid_img: null,
21        oid_date: "",
22        pfid_order: 4
23    };
24
25    //example anchor:
26    // http://snd.gu.se/#hyperanchor1.3://div[@id="node-170"]/div[1]/div[1]/div[1]/div[1]/p[1](294)(3)(sse)&
27    // //div[@id="node-170"]/div[1]/div[1]/div[1]/div[1]/p[1](365)(3)(kav)&
28    // border:thin dotted rgb(255,204,0);background-color:rgb(255,255,204);color:rgb(0,0,0);
29
30    var title = $(annotation).find('headline').text().trim();
31    var body = $(annotation).find('body').find('xhtml\\:span').text().trim();
32    var style = $(annotation).find('body').find('xhtml\\:span').attr('style');
33    var type = $(annotation).find('mimeType').text();
34    var link = $(annotation).find('link').text();
35    var time = $(annotation).find('lastModified').text();
36
37    //get aid
38    var URI = $(annotation).find('annotation').attr('URI');
39    om_object.dasish_aid = URI.split('/annotations/')[1];
40
41    var urlParts = link.split("#xpointer");
42
43    om_object.doc_url = urlParts[0];
44    om_object.con_url = urlParts[0];
45    var xpointer = urlParts[1];
46
47    //start dom
48    xpointer.match(/start-point\(string-range\((.+?)\)\)\/range-to\(/);
49    var parts = RegExp.$1.split(',');
50    var parts0 = parts[0].replace('/text()[1]', '');
51    om_object.bgn_dom = parts0 + '(' + parts[2] + ')(3)';
52
53    //end dom
54    xpointer.match(/range-to\(string-range\((.+?)\)\)\)/);
55    var parts = RegExp.$1.split(',');
56    var parts0 = parts[0].replace('/text()[1]', '');
57    om_object.end_dom = parts0 + '(' + parts[2] + ')(3)';
58
59    //build hyperanchor
60    var hyperanchor = '#hyperanchor1.3' + encodeURIComponent(':' + om_object.bgn_dom.replace(/"/g, '&quot;') + '&' + om_object.end_dom.replace(/"/g, '&quot;') + '&' + style);
61
62    om_object.doc_title = title;
63    om_object.oid_title = title;
64
65    //generate oid from aid (hash)
66    om_object.oid = hashCode(om_object.dasish_aid);
67    if (om_object.oid < 0) {
68        om_object.oid = om_object.oid * -1;
69    }
70   
71    //construct annotation structure
72    om_object.oid_property = "<PROPERTY><HYPER_ANCHOR>" + om_object.doc_url + hyperanchor + "</HYPER_ANCHOR><NOTE>" + body + "</NOTE></PROPERTY>";
73
74    if (type === 'application/xml+xhtml') { //TODO: better handeling of the body
75        om_object.oid_txt = $(annotation).find('body').find('xhtml\\:span').attr('title');
76        om_object.oid_type = 'text';
77    }
78   
79    //reformat date
80    var d = new Date(time);
81    om_object.oid_date = (d.getFullYear() + '/' + (parseInt(d.getMonth()) + 1)) + '/' + d.getDate() + ' ' + d.getHours() + ':' + d.getMinutes() + ':' + d.getSeconds();
82
83    return om_object;
84}
85
86/**
87 * converts wired marker annotations to dassish annotations
88 * @param {object} om_object contains wired marker object for annotation
89 * @returns {String} annotation in xml
90 */
91function om_object2annotation(om_object) {
92    var note = om_object.oid_property.match(/<NOTE>(.+?)<\/NOTE>/)[1];
93    var hyperanchor = om_object.oid_property.match(/<HYPER_ANCHOR>(.+?)<\/HYPER_ANCHOR>/)[1];
94    var style = '';
95    var timestamp = new Date(om_object.oid_date);
96
97    hyperanchor = unescape(hyperanchor);
98
99    hyperanchor.match(/^(.+\([0-9]+\)\([0-9]+\)\([\s\S]*\))&(.+\([0-9]+\)\([0-9]+\)\([\s\S]*\))&(.+)$/);
100
101    style = RegExp.$3;
102
103    var path = {};
104
105    om_object.bgn_dom.match(/(.+)\(([0-9]+)\)\(([0-9]+)\)/);
106    path.start = RegExp.$1;
107    path.startOffset = RegExp.$2;
108    path.startType = RegExp.$3;
109
110    om_object.end_dom.match(/(.+)\(([0-9]+)\)\(([0-9]+)\)/);
111    path.end = RegExp.$1;
112    path.endOffset = RegExp.$2;
113    path.endType = RegExp.$3;
114
115    var xpointer = '';
116
117    xpointer += "#xpointer(start-point(string-range(" + path.start + "/text()[1],''," + path.startOffset + "))";
118    xpointer += "/range-to(string-range(" + path.end + "/text()[1],''," + path.endOffset + ")))";
119
120    var annotation = '<?xml version="1.0" encoding="UTF-8"?>\n\
121                      <annotation xmlns="http://www.dasish.eu/ns/addit"\n\
122                            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"\n\
123                            xsi:schemaLocation="http://www.dasish.eu/ns/addit http://dasish.eu/DASISH-schema.xsd"\n\
124                            xmlns:xhtml="http://www.w3.org/1999/xhtml"\n\
125                            URI="' + annotationFramework.getBackend() + '/api/annotations/00000000-0000-0000-' + om_object.oid.substring(0, 4) + '-' + om_object.oid.substring(4, 14) + '00"\n\
126                            ownerRef="' + annotationFramework.getBackend() + '/api/users/00000000-0000-0000-' + om_object.oid.substring(0, 4) + '-' + om_object.oid.substring(4, 14) + '00">\n\
127                        <headline>' + om_object.oid_title + '</headline>\n\
128                        <lastModified>' + timestamp.toISOString() + '</lastModified>\n\
129                        <body>\n\
130                            <xmlBody>\n\
131                                <mimeType>application/xml+xhtml</mimeType>\n\
132                                <xhtml:span title="'+om_object.oid_txt+'" style="' + style + '">' + note + '</xhtml:span>\n\
133                            </xmlBody>\n\
134                        </body>\n\
135                            <targets>\n\
136                                <targetInfo ref="' + annotationFramework.getBackend() + '/api/targets/00000000-0000-0000-' + om_object.oid.substring(0, 4) + '-' + om_object.oid.substring(4, 14) + '00">\n\
137                                    <link>' + om_object.doc_url + xpointer + '</link>\n\
138                                    <version>' + timestamp.toISOString() + '</version>\n\
139                                </targetInfo>\n\
140                            </targets>\n\
141                            <permissions>\n\
142                                <userWithPermission ref="' + annotationFramework.getBackend() + '/api/users/00000000-0000-0000-' + om_object.oid.substring(0, 4) + '-' + om_object.oid.substring(4, 14) + '00">\n\
143                                    <permission>owner</permission>\n\
144                                </userWithPermission>\n\
145                            </permissions>\n\
146                      </annotation>';
147    return annotation;
148}
149
150function hashCode(str) {
151    var hash = 0;
152    if (str == undefined || str.length == 0)
153        return hash;
154    for (i = 0; i < str.length; i++) {
155        char = str.charCodeAt(i);
156        hash = ((hash << 5) - hash) + char;
157        hash = hash & hash; // Convert to 32bit integer
158    }
159    return hash;
160}
161
162if (!Date.prototype.toISOString) {
163    Date.prototype.toISOString = function() {
164        function pad(n) {
165            return n < 10 ? '0' + n : n
166        }
167        return this.getUTCFullYear() + '-'
168                + pad(this.getUTCMonth() + 1) + '-'
169                + pad(this.getUTCDate()) + 'T'
170                + pad(this.getUTCHours()) + ':'
171                + pad(this.getUTCMinutes()) + ':'
172                + pad(this.getUTCSeconds()) + 'Z';
173    };
174}
175
176/*
177 From SQL-lite for wired-marker
178 <PROPERTY>
179 <HYPER_ANCHOR>http://localhost/annotation/test/test-service.html#hyperanchor1.3%3A%2Fhtml%5B1%5D%2Fbody%5B1%5D%2Fdiv%5B2%5D%2Fp%5B1%5D(0)(3)(Ane)%26%2Fhtml%5B1%5D%2Fbody%5B1%5D%2Fdiv%5B2%5D%2Fp%5B1%5D(45)(3)(ter)%26background-color%3A%20rgb(%2044%2C%20254%2C%20%2081)%3Bcolor%3Argb(0%2C0%2C0)%3B</HYPER_ANCHOR>
180 <NOTE>text om en fï¿œrfattare</NOTE>
181 </PROPERTY>
182 unescaped: http://localhost/annotation/test/test-service.html#hyperanchor1.3:/html[1]/body[1]/div[2]/p[1](0)(3)(Ane)&/html[1]/body[1]/div[2]/p[1](45)(3)(ter)&background-color: rgb( 44, 254, 81);color:rgb(0,0,0);text om en fï¿œrfattare
183 */
Note: See TracBrowser for help on using the repository browser.