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

Last change on this file since 3403 was 3403, checked in by olof, 11 years ago

translation of annotation to om_object first draft done

File size: 7.4 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                        doc_title       :"http://localhost/annotation/test/test-service.html",
10                        doc_url         :"http://localhost/annotation/test/test-service.html",
11                        con_url         :"http://localhost/annotation/test/test-service.html",
12                        bgn_dom         :"",
13                        end_dom         :"",
14                        oid_title       :"http://localhost/annotation/test/test-service.html",
15                        oid_property    :"<PROPERTY><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><NOTE>text om en författare</NOTE></PROPERTY>",
16                        oid_mode        :"0",
17                        oid_type        :"text",
18                        oid_txt         :"An example text about Douglas Adams; a writer",
19                        oid_img         :null,
20                        oid_date        :""
21                    };
22   
23    //example anchor:
24    // http://snd.gu.se/#hyperanchor1.3://div[@id="node-170"]/div[1]/div[1]/div[1]/div[1]/p[1](294)(3)(sse)&
25    // //div[@id="node-170"]/div[1]/div[1]/div[1]/div[1]/p[1](365)(3)(kav)&
26    // border:thin dotted rgb(255,204,0);background-color:rgb(255,255,204);color:rgb(0,0,0);
27   
28    var title = $(annotation).find('headline').text().trim();
29    var body = $(annotation).find('body').text().trim();
30    var style = $(annotation).find('body').find('xhtml\\:span').attr('style');
31    var type = $(annotation).find('body').attr('type').toLowerCase();
32    var link = $(annotation).find('link').text();
33    var time = $(annotation).attr('timeStamp');
34       
35    var urlParts = link.split("#xpointer"); 
36   
37    om_object.doc_url = urlParts[0];
38    om_object.con_url = urlParts[0];
39    var xpointer = urlParts[1];
40   
41    //start dom
42    xpointer.match(/start-point\(string-range\((.+?)\)\)\/range-to\(/);
43    var parts = RegExp.$1.split(',');
44    om_object.bgn_dom = parts[0]+'('+parts[2]+')(3)';
45   
46    //end dom
47    xpointer.match(/range-to\(string-range\((.+?)\)\)\)/);
48    var parts = RegExp.$1.split(',');
49    om_object.end_dom = parts[0]+'('+parts[2]+')(3)';
50   
51    //start xpath in xpointer: (?<=start-point\(string-range\()(.*\n?)(?=\,'')
52    //end xpath in xpointer: (?<=range-to\(string-range\()(.*\n?)(?=\,'')
53   
54    //build hyperanchor
55    var hyperanchor = '#hyperanchor1.3:'+om_object.bgn_dom+'&'+om_object.end_dom+'&'+style;
56   
57   
58   
59    om_object.doc_title = title;
60   
61    om_object.oid = hashCode($(annotation).attr('ref'));
62    console.log(link);
63    om_object.oid_property = "<PROPERTY><HYPER_ANCHOR>"+om_object.doc_url+hyperanchor+"</HYPER_ANCHOR><NOTE>"+body+"</NOTE></PROPERTY>";
64   
65    if(type === 'note'){
66        om_object.oid_txt = body;
67        om_object.oid_type = 'text';
68    }
69    var d = new Date(time);
70    om_object.oid_date = (d.getMonth()+1)+'/'+d.getDate()+'/'+d.getFullYear()+' '+d.getHours()+':'+d.getMinutes()+':'+d.getSeconds();
71   
72    return om_object;
73}
74
75/**
76 * converts wired marker annotations to dassish annotations
77 * @param {object} om_object contains wired marker object for annotation
78 * @returns {String} annotation in xml
79 */
80function om_object2annotation(om_object){
81    var note = om_object.oid_property.match(/<NOTE>(.+?)<\/NOTE>/)[1];
82    var hyperanchor = om_object.oid_property.match(/<HYPER_ANCHOR>(.+?)<\/HYPER_ANCHOR>/)[1];
83    var style='';
84    var timestamp = new Date(om_object.oid_date);
85   
86    hyperanchor = unescape(hyperanchor);
87   
88    hyperanchor.match(/^(.+\([0-9]+\)\([0-9]+\)\([\s\S]*\))&(.+\([0-9]+\)\([0-9]+\)\([\s\S]*\))&(.+)$/);
89   
90    style = RegExp.$3;
91   
92    var path = {};
93   
94    om_object.bgn_dom.match(/(.+)\(([0-9]+)\)\(([0-9]+)\)/);
95    path.start = RegExp.$1;
96    path.startOffset = RegExp.$2;
97    path.startType = RegExp.$3;
98
99    om_object.end_dom.match(/(.+)\(([0-9]+)\)\(([0-9]+)\)/);
100    path.end = RegExp.$1;
101    path.endOffset = RegExp.$2;
102    path.endType = RegExp.$3;
103   
104    var xpointer = '';
105   
106    xpointer += "#xpointer(start-point(string-range("+path.start+"/text()[1],'',"+path.startOffset+"))";
107    xpointer += "/range-to(string-range("+path.end+"/text()[1],'',"+path.endOffset+")))";
108   
109    var annotation = '<?xml version="1.0" encoding="UTF-8"?>\n\
110                      <annotation xmlns="http://www.dasish.eu/ns/addit"\n\
111                            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"\n\
112                            xmlns:xhtml="http://www.w3.org/1999/xhtml"\n\
113                            xsi:schemaLocation="http://www.dasish.eu/ns/addit http://dasish.eu/DASISH-schema.xsd"\n\
114                            URI="tempAID'+om_object.oid+'"\n\
115                            timeStamp="'+timestamp.toISOString()+'">\n\
116                        <owner ref="http://dasish.eu/users/UIxyz/how_will_this_be_sent_from_the_client"/>\n\
117                        <headline>'+om_object.oid_title+'</headline>\n\
118                        <body type="Note" ref="tmp'+om_object.oid+'">\n\
119                            <xhtml:span style="'+style+'">'+note+'</xhtml:span>\n\
120                        </body>\n\
121                        <targetSources>\n\
122                            <target>\n\
123                                <newSource xml:id="tmp'+om_object.oid+'">\n\
124                                    <link>'+om_object.doc_url+xpointer+'</link>\n\
125                                    <version>'+timestamp.toISOString()+'</version>\n\
126                                </newSource>\n\
127                            </target>\n\
128                        </targetSources>\n\
129                        <permissions ref="tmpPermissionsListID'+om_object.oid+'"/>\n\
130                      </annotation>';
131    return annotation;
132}
133
134function hashCode(str){
135      var hash = 0;
136      if (str == undefined || str.length == 0) return hash;
137      for (i = 0; i < str.length; i++) {
138          char = str.charCodeAt(i);
139          hash = ((hash<<5)-hash)+char;
140          hash = hash & hash; // Convert to 32bit integer
141      }
142      return hash;
143}
144
145if (!Date.prototype.toISOString) {
146    Date.prototype.toISOString = function() {
147        function pad(n) { return n < 10 ? '0' + n : n }
148        return this.getUTCFullYear() + '-'
149            + pad(this.getUTCMonth() + 1) + '-'
150            + pad(this.getUTCDate()) + 'T'
151            + pad(this.getUTCHours()) + ':'
152            + pad(this.getUTCMinutes()) + ':'
153            + pad(this.getUTCSeconds()) + 'Z';
154    };
155}
156
157/*
158From SQL-lite for wired-marker
159<PROPERTY>
160<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>
161<NOTE>text om en fï¿œrfattare</NOTE>
162</PROPERTY>
163unescaped: 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
164*/
Note: See TracBrowser for help on using the repository browser.