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

Last change on this file since 4331 was 4331, checked in by sroth, 10 years ago

Adjusted xml output of the client to latest schema version.

File size: 7.6 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: "Test test test",
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').text().trim();
32    var style = $(annotation).find('body').find('xhtml\\:span').attr('style');
33    var type = $(annotation).find('body').attr('type').toLowerCase();
34    var link = $(annotation).find('link').text();
35    var time = $(annotation).attr('timeStamp');
36
37    var urlParts = link.split("#xpointer");
38
39    om_object.doc_url = urlParts[0];
40    om_object.con_url = urlParts[0];
41    var xpointer = urlParts[1];
42
43    //start dom
44    xpointer.match(/start-point\(string-range\((.+?)\)\)\/range-to\(/);
45    var parts = RegExp.$1.split(',');
46    var parts0 = parts[0].replace('/text()[1]', '');
47    om_object.bgn_dom = parts0 + '(' + parts[2] + ')(3)';
48
49    //end dom
50    xpointer.match(/range-to\(string-range\((.+?)\)\)\)/);
51    var parts = RegExp.$1.split(',');
52    var parts0 = parts[0].replace('/text()[1]', '');
53    om_object.end_dom = parts0 + '(' + parts[2] + ')(3)';
54
55    //build hyperanchor
56    console.log('fragment: ' + om_object.bgn_dom + '&' + om_object.end_dom + '&' + style);
57    var hyperanchor = '#hyperanchor1.3' + encodeURIComponent(':' + om_object.bgn_dom.replace(/"/g, '&quot;') + '&' + om_object.end_dom.replace(/"/g, '&quot;') + '&' + style);
58
59    om_object.doc_title = title;
60
61    om_object.oid = hashCode($(annotation).attr('URI'));
62    if (om_object.oid < 0) {
63        om_object.oid = om_object.oid * -1;
64    }
65    console.log(link);
66    om_object.oid_property = "<PROPERTY><HYPER_ANCHOR>" + om_object.doc_url + hyperanchor + "</HYPER_ANCHOR><NOTE>" + body + "</NOTE></PROPERTY>";
67
68    if (type === 'note') {
69        om_object.oid_txt = body;
70        om_object.oid_type = 'text';
71    }
72    var d = new Date(time);
73    om_object.oid_date = (d.getFullYear() + '/' + (parseInt(d.getMonth()) + 1)) + '/' + d.getDate() + ' ' + d.getHours() + ':' + d.getMinutes() + ':' + d.getSeconds();
74
75    return om_object;
76}
77
78/**
79 * converts wired marker annotations to dassish annotations
80 * @param {object} om_object contains wired marker object for annotation
81 * @returns {String} annotation in xml
82 */
83function om_object2annotation(om_object) {
84    var note = om_object.oid_property.match(/<NOTE>(.+?)<\/NOTE>/)[1];
85    var hyperanchor = om_object.oid_property.match(/<HYPER_ANCHOR>(.+?)<\/HYPER_ANCHOR>/)[1];
86    var style = '';
87    var timestamp = new Date(om_object.oid_date);
88
89    hyperanchor = unescape(hyperanchor);
90
91    hyperanchor.match(/^(.+\([0-9]+\)\([0-9]+\)\([\s\S]*\))&(.+\([0-9]+\)\([0-9]+\)\([\s\S]*\))&(.+)$/);
92
93    style = RegExp.$3;
94
95    var path = {};
96
97    om_object.bgn_dom.match(/(.+)\(([0-9]+)\)\(([0-9]+)\)/);
98    path.start = RegExp.$1;
99    path.startOffset = RegExp.$2;
100    path.startType = RegExp.$3;
101
102    om_object.end_dom.match(/(.+)\(([0-9]+)\)\(([0-9]+)\)/);
103    path.end = RegExp.$1;
104    path.endOffset = RegExp.$2;
105    path.endType = RegExp.$3;
106
107    var xpointer = '';
108
109    xpointer += "#xpointer(start-point(string-range(" + path.start + "/text()[1],''," + path.startOffset + "))";
110    xpointer += "/range-to(string-range(" + path.end + "/text()[1],''," + path.endOffset + ")))";
111
112    var annotation = '<?xml version="1.0" encoding="UTF-8"?>\n\
113                      <annotation xmlns="http://www.dasish.eu/ns/addit"\n\
114                            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"\n\
115                            xsi:schemaLocation="http://www.dasish.eu/ns/addit http://dasish.eu/DASISH-schema.xsd"\n\
116                            xmlns:xhtml="http://www.w3.org/1999/xhtml"\n\
117                            URI="' + annotationFramework.getBackend() + '/api/annotations/00000000-0000-0000-' + om_object.oid.substring(0, 4) + '-' + om_object.oid.substring(4, 14) + '00"\n\
118                            ownerRef="' + annotationFramework.getBackend() + '/api/users/00000000-0000-0000-' + om_object.oid.substring(0, 4) + '-' + om_object.oid.substring(4, 14) + '00">\n\
119                        <headline>' + om_object.oid_title + '</headline>\n\
120                        <lastModified>' + timestamp.toISOString() + '</lastModified>\n\
121                        <body>\n\
122                            <xmlBody>\n\
123                                <mimeType>application/xml+xhtml</mimeType>\n\
124                                <xhtml:span style="' + style + '">' + note + '</xhtml:span>\n\
125                            </xmlBody>\n\
126                        </body>\n\
127                            <targets>\n\
128                                <targetInfo ref="' + annotationFramework.getBackend() + '/api/targets/00000000-0000-0000-' + om_object.oid.substring(0, 4) + '-' + om_object.oid.substring(4, 14) + '00">\n\
129                                    <link>' + om_object.doc_url + xpointer + '</link>\n\
130                                    <version>' + timestamp.toISOString() + '</version>\n\
131                                </targetInfo>\n\
132                            </targets>\n\
133                            <permissions>\n\
134                                <userWithPermission ref="' + annotationFramework.getBackend() + '/api/users/00000000-0000-0000-' + om_object.oid.substring(0, 4) + '-' + om_object.oid.substring(4, 14) + '00">\n\
135                                    <permission>owner</permission>\n\
136                                </userWithPermission>\n\
137                            </permissions>\n\
138                      </annotation>';
139    return annotation;
140}
141
142function hashCode(str) {
143    var hash = 0;
144    if (str == undefined || str.length == 0)
145        return hash;
146    for (i = 0; i < str.length; i++) {
147        char = str.charCodeAt(i);
148        hash = ((hash << 5) - hash) + char;
149        hash = hash & hash; // Convert to 32bit integer
150    }
151    return hash;
152}
153
154if (!Date.prototype.toISOString) {
155    Date.prototype.toISOString = function() {
156        function pad(n) {
157            return n < 10 ? '0' + n : n
158        }
159        return this.getUTCFullYear() + '-'
160                + pad(this.getUTCMonth() + 1) + '-'
161                + pad(this.getUTCDate()) + 'T'
162                + pad(this.getUTCHours()) + ':'
163                + pad(this.getUTCMinutes()) + ':'
164                + pad(this.getUTCSeconds()) + 'Z';
165    };
166}
167
168/*
169 From SQL-lite for wired-marker
170 <PROPERTY>
171 <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>
172 <NOTE>text om en fï¿œrfattare</NOTE>
173 </PROPERTY>
174 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
175 */
Note: See TracBrowser for help on using the repository browser.