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

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

changed to oid_title as headline

File size: 5.7 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    //start xpath in xpointer: (?<=start-point\(string-range\()(.*\n?)(?=\,'')
24    //end xpath in xpointer: (?<=range-to\(string-range\()(.*\n?)(?=\,'')
25   
26   
27    om_object.oid = hashCode(annotation['xml:id']);
28    console.log(annotation.targetSources.targetSource);
29    om_object.oid_property = "<PROPERTY><HYPER_ANCHOR>"+annotation.targetSources.targetSource.id+"</HYPER_ANCHOR><NOTE>"+annotation.body+"</NOTE></PROPERTY>";
30   
31    if(annotation.type.toLowerCase() === 'note'){
32        om_object.oid_txt = annotation.body;
33        om_object.oid_type = 'text';
34    }
35    var d = new Date(annotation.timeStamp);
36    om_object.oid_date = (d.getMonth()+1)+'/'+d.getDate()+'/'+d.getFullYear()+' '+d.getHours()+':'+d.getMinutes()+':'+d.getSeconds();
37   
38    return om_object;
39}
40
41/**
42 * converts wired marker annotations to dassish annotations
43 * @param {object} om_object contains wired marker object for annotation
44 * @returns {String} annotation in xml
45 */
46function om_object2annotation(om_object){
47    var note = om_object.oid_property.match(/<NOTE>(.+?)<\/NOTE>/)[1];
48    var hyperanchor = om_object.oid_property.match(/<HYPER_ANCHOR>(.+?)<\/HYPER_ANCHOR>/)[1];
49    var style='';
50    var timestamp = new Date(om_object.oid_date);
51   
52    hyperanchor = unescape(hyperanchor);
53   
54    annotationProxy.log('hyperanchor : '+hyperanchor);
55    hyperanchor.match(/^(.+\([0-9]+\)\([0-9]+\)\([\s\S]*\))&(.+\([0-9]+\)\([0-9]+\)\([\s\S]*\))&(.+)$/);
56   
57    style = RegExp.$3;
58   
59    var path = {};
60   
61    om_object.bgn_dom.match(/(.+)\(([0-9]+)\)\(([0-9]+)\)/);
62    path.start = RegExp.$1;
63    path.startOffset = RegExp.$2;
64    path.startType = RegExp.$3;
65
66    om_object.end_dom.match(/(.+)\(([0-9]+)\)\(([0-9]+)\)/);
67    path.end = RegExp.$1;
68    path.endOffset = RegExp.$2;
69    path.endType = RegExp.$3;
70   
71    var xpointer = '';
72   
73    xpointer += "#xpointer(start-point(string-range("+path.start+"/text()[1],'',"+path.startOffset+"))";
74    xpointer += "/range-to(string-range("+path.end+"/text()[1],'',"+path.endOffset+")))";
75   
76    var annotation = '<?xml version="1.0"?>\n\
77                      <annotation \n\
78                            xmlns="http://dasish.eu/ns/addit" \n\
79                            xmlns:xhtml="http://www.w3.org/1999/xhtml"\n\
80                            URI="tempAIDgfgf" \n\
81                            timeStamp="'+timestamp.toISOString()+'">\n\
82                        <owner ref="http://dasish.eu/users/how_will_this_be_sent_from_the_client"/>\n\
83                        <headline>'+om_object.oid_title+'</headline>\n\
84                        <body type="Note">\n\
85                            <xhtml:span style="'+style+'">'+note+'</xhtml:span>\n\
86                        </body>\n\
87                        <targetSources>\n\
88                            <action>CREATE_CACHED_REPRESENTATION</action>\n\
89                            <link>'+om_object.doc_url+xpointer+'</link>\n\
90                            <version>'+timestamp.toISOString()+'</version>\n\
91                        </targetSources>\n\
92                      </annotation>';
93    return annotation;
94}
95
96function hashCode(str){
97      var hash = 0;
98      if (str.length == 0) return hash;
99      for (i = 0; i < str.length; i++) {
100          char = str.charCodeAt(i);
101          hash = ((hash<<5)-hash)+char;
102          hash = hash & hash; // Convert to 32bit integer
103      }
104      return hash;
105}
106
107if (!Date.prototype.toISOString) {
108    Date.prototype.toISOString = function() {
109        function pad(n) { return n < 10 ? '0' + n : n }
110        return this.getUTCFullYear() + '-'
111            + pad(this.getUTCMonth() + 1) + '-'
112            + pad(this.getUTCDate()) + 'T'
113            + pad(this.getUTCHours()) + ':'
114            + pad(this.getUTCMinutes()) + ':'
115            + pad(this.getUTCSeconds()) + 'Z';
116    };
117}
118
119/*
120From SQL-lite for wired-marker
121<PROPERTY>
122<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>
123<NOTE>text om en fï¿œrfattare</NOTE>
124</PROPERTY>
125unescaped: 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
126*/
Note: See TracBrowser for help on using the repository browser.