comparison inkscape/firefox/content/jqXMLUtils.js @ 339:63aaf96209cd

* Move location of files so that we can produce XPI file from them. * Add the produce to compile the inkscape for madbutterfly * Add makefile to produce inkscape and madbuilder.xpi
author wycc
date Sun, 08 Mar 2009 10:05:22 +0800
parents inkscape/firefox/jqXMLUtils.js@01439f28d0bd
children
comparison
equal deleted inserted replaced
336:995eb2c1a1aa 339:63aaf96209cd
1 /*
2 This program is free software: you can redistribute it and/or modify
3 it under the terms of the GNU General Public License as published by
4 the Free Software Foundation, either version 3 of the License, or
5 (at your option) any later version.
6
7 This program is distributed in the hope that it will be useful,
8 but WITHOUT ANY WARRANTY; without even the implied warranty of
9 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 GNU General Public License for more details.
11
12 You should have received a copy of the GNU General Public License
13 along with this program. If not, see <http://www.gnu.org/licenses/>.
14 */
15 (function($) {
16 //Converts XML DOM to JSON
17 $.extend ({
18 xmlToJSON: function(xdoc) {
19 try {
20 if(!xdoc){ return null; }
21 var tmpObj = {};
22 tmpObj.typeOf = "JSXBObject";
23 var xroot = (xdoc.nodeType == 9)?xdoc.documentElement:xdoc;
24 tmpObj.RootName = xroot.nodeName || "";
25 if(xdoc.nodeType == 3 || xdoc.nodeType == 4) {
26 return xdoc.nodeValue;
27 }
28 var isNumeric = function(s) {
29 var testStr = "";
30 if(s && typeof s == "string") { testStr = s; }
31 var pattern = /^((-)?([0-9]*)((\.{0,1})([0-9]+))?$)/;
32 return pattern.test(testStr);
33 };
34 //Alters attribute and collection names to comply with JS
35 function formatName(name) {
36 var regEx = /-/g;
37 var tName = String(name).replace(regEx,"_");
38 return tName;
39 }
40 //Set Attributes of an object
41 function setAttributes(obj, node) {
42 if(node.attributes.length > 0) {
43 var a = node.attributes.length-1;
44 var attName;
45 obj._attributes = [];
46 do { //Order is irrelevant (speed-up)
47 attName = String(formatName(node.attributes[a].name));
48 obj._attributes.push(attName);
49 obj[attName] = $.trim(node.attributes[a].value);
50 } while(a--);
51 }
52 }
53 //Set collections
54 function setHelpers(grpObj) {
55 //Selects a node withing array where attribute = value
56 grpObj.getNodeByAttribute = function(attr, obj) {
57 if(this.length > 0) {
58 var cNode;
59 var maxLen = this.length -1;
60 try {
61 do {
62 cNode = this[maxLen];
63 if(cNode[attr] == obj) {
64 return cNode;
65 }
66 } while(maxLen--);
67 } catch(e) {return false;}
68 return false;
69 }
70 };
71
72 grpObj.contains = function(attr, obj) {
73 if(this.length > 0) {
74 var maxLen = this.length -1;
75 try {
76 do {
77 if(this[maxLen][attr] == obj) {
78 return true;
79 }
80 } while(maxLen--);
81 } catch(e) {return false;}
82 return false;
83 }
84 };
85
86 grpObj.indexOf = function(attr, obj) {
87 var pos = -1;
88 if(this.length > 0) {
89 var maxLen = this.length -1;
90 try {
91 do {
92 if(this[maxLen][attr] == obj) {
93 pos = maxLen;
94 }
95 } while(maxLen--);
96 } catch(e) {return -1;}
97 return pos;
98 }
99 };
100
101 grpObj.SortByAttribute = function(col, dir) {
102 if(this.length) {
103 function getValue(pair, idx) {
104 var out = pair[idx];
105 out = (isNumeric(out))?parseFloat(out):out;
106 return out;
107 }
108 function sortFn(a, b) {
109 var res = 0;
110 var tA, tB;
111 tA = getValue(a, col);
112 tB = getValue(b, col);
113 if(tA < tB) { res = -1; } else if(tB < tA) { res = 1; }
114 if(dir) {
115 res = (dir.toUpperCase() == "DESC")?(0 - res):res;
116 }
117 return res;
118 }
119 this.sort(sortFn);
120 }
121 };
122
123 grpObj.SortByValue = function(dir) {
124 if(this.length) {
125 function getValue(pair) {
126 var out = pair.Text;
127 out = (isNumeric(out))?parseFloat(out):out;
128 return out;
129 }
130 function sortFn(a, b) {
131 var res = 0;
132 var tA, tB;
133 tA = getValue(a);
134 tB = getValue(b);
135 if(tA < tB) { res = -1; } else if(tB < tA) { res = 1; }
136 if(dir) {
137 res = (dir.toUpperCase() == "DESC")?(0 - res):res;
138 }
139 return res;
140 }
141 this.sort(sortFn);
142 }
143 };
144 grpObj.SortByNode = function(node, dir) {
145 if(this.length) {
146 function getValue(pair, node) {
147 var out = pair[node][0].Text;
148 out = (isNumeric(out))?parseFloat(out):out;
149 return out;
150 }
151 function sortFn(a, b) {
152 var res = 0;
153 var tA, tB;
154 tA = getValue(a, node);
155 tB = getValue(b, node);
156 if(tA < tB) { res = -1; } else if(tB < tA) { res = 1; }
157 if(dir) {
158 res = (dir.toUpperCase() == "DESC")?(0 - res):res;
159 }
160 return res;
161 }
162 this.sort(sortFn);
163 }
164 };
165 }
166 //Recursive JSON Assembler
167 //Set Object Nodes
168 function setObjects(obj, node) {
169 var elemName; //Element name
170 var cnode; //Current Node
171 var tObj; //New subnode
172 var cName = "";
173 if(!node) { return null; }
174 //Set node attributes if any
175 if(node.attributes.length > 0){setAttributes(obj, node);}
176 obj.Text = "";
177 if(node.hasChildNodes()) {
178 var nodeCount = node.childNodes.length - 1;
179 var n = 0;
180 do { //Order is irrelevant (speed-up)
181 cnode = node.childNodes[n];
182 switch(cnode.nodeType) {
183 case 1: //Node
184 //Process child nodes
185 obj._children = [];
186 //SOAP XML FIX to remove namespaces (i.e. soapenv:)
187 elemName = (cnode.localName)?cnode.localName:cnode.baseName;
188 elemName = formatName(elemName);
189 if(cName != elemName) { obj._children.push(elemName); }
190 //Create sub elemns array
191 if(!obj[elemName]) {
192 obj[elemName] = []; //Create Collection
193 }
194 tObj = {};
195 obj[elemName].push(tObj);
196 if(cnode.attributes.length > 0) {
197 setAttributes(tObj, cnode);
198 }
199 //Set Helper functions (contains, indexOf, sort, etc);
200 if(!obj[elemName].contains) {
201 setHelpers(obj[elemName]);
202 }
203 cName = elemName;
204 if(cnode.hasChildNodes()) {
205 setObjects(tObj, cnode); //Recursive Call
206 }
207 break;
208 case 3: //Text Value
209 obj.Text += $.trim(cnode.nodeValue);
210 break;
211 case 4: //CDATA
212 obj.Text += (cnode.text)?$.trim(cnode.text):$.trim(cnode.nodeValue);
213 break;
214 }
215 } while(n++ < nodeCount);
216 }
217 }
218 //RUN
219 setObjects(tmpObj, xroot);
220 //Clean-up memmory
221 xdoc = null;
222 xroot = null;
223 return tmpObj;
224
225 } catch(e) {
226 return null;
227 }
228 }
229 });
230
231 //Converts Text to XML DOM
232 $.extend({
233 textToXML: function(strXML) {
234 var xmlDoc = null;
235 try {
236 xmlDoc = ($.browser.msie)?new ActiveXObject("Microsoft.XMLDOM"):new DOMParser();
237 xmlDoc.async = false;
238 } catch(e) {throw new Error("XML Parser could not be instantiated");}
239 var out;
240 try {
241 if($.browser.msie) {
242 out = (xmlDoc.loadXML(strXML))?xmlDoc:false;
243 } else {
244 out = xmlDoc.parseFromString(strXML, "text/xml");
245 }
246 } catch(e) { throw new Error("Error parsing XML string"); }
247 return out;
248 }
249 });
250 })(jQuery);