forked from rpgoldman/angular-xmlrpc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathservices_xmlrpc.js
More file actions
332 lines (305 loc) · 10.5 KB
/
services_xmlrpc.js
File metadata and controls
332 lines (305 loc) · 10.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
// Fix an IE problem (another one)
var HAS_ACTIVEX = false;
try {
new ActiveXObject('MSXML2.DOMDocument');
HAS_ACTIVEX = true;
} catch(e) {}
/**
* XML-RPC communication service.
*/
angular.module('Dashboards.services')
.factory('xmlrpc', ['$http', 'helperXmlRpc', 'js2xml', 'xml2js', function($http, helperXmlRpc, js2xml, xml2js){
var configuration = {};
/**
* Serialize a XML document to string.
*/
function serialize(xml){
var text = xml.xml;
if (text) {
return text;
}
if (typeof XMLSerializer != 'undefined') {
return new XMLSerializer().serializeToString(xml);
}
throw Error('Your browser does not support serializing XML documents');
};
/**
* Creates a xmlrpc call of the given method with given params.
*/
function createCall(method, params){
var doc = helperXmlRpc.createDocument('methodCall');
doc.firstChild.appendChild(
helperXmlRpc.createNode(doc, 'methodName', method)
);
if (arguments.length > 2) {
params = helperXmlRpc.cloneArray(arguments);
params.shift();
}
if (params && params.length > 0) {
var paramsNode = helperXmlRpc.createNode(doc, 'params');
for (var i=0; i < params.length; i++) {
paramsNode.appendChild(helperXmlRpc.createNode(doc, 'param',
js2xml.js2xml(doc, params[i])
));
}
doc.firstChild.appendChild(paramsNode);
}
return (serialize(doc)).replace(/[\s\xa0]+$/, '');
};
// Use the promise system from angular.
// This method return a promise with the response
function callMethod(method, params){
var xmlstr = createCall(method, params);
var targetAddr = configuration.hostName + "" + configuration.pathName;
return $http.post(targetAddr, xmlstr, {headers: {'Content-Type': 'text/xml'}})
.then(function(responseFromServer) {
var responseText = responseFromServer.data;
var response = null;
try {
response = parseResponse(responseText);
} catch (err) {
response = err;
}
return response;
}, function(responseFromServer){
if(responseFromServer.status in configuration){
if(typeof configuration[responseFromServer.status] == "function"){
return configuration[responseFromServer.status].call();
}
}
});
};
/**
* Parse an xmlrpc response and return the js object.
*/
function parseResponse(response){
var doc = helperXmlRpc.loadXml(response);
var rootNode = doc.firstChild;
if (!rootNode)
return undefined;
//else
var node = helperXmlRpc.selectSingleNode(rootNode, '//fault');
var isFault = (node != undefined);
node = helperXmlRpc.selectSingleNode(rootNode, '//value');
var value = xml2js.xml2js(node);
if (isFault) {
throw value;
}
//else
return value;
};
/**
* Configure the service (Host name and service path).
* Actually, 401, 404 and 500 server errors are originally defined, but any error code can be added
*/
function config(conf) {
angular.extend(configuration, {
hostName:"",
pathName:"/rpc2",
500:function(){},
401:function(){},
404:function(){}
}, conf);
};
config();
return {
callMethod : callMethod,
config : config
};
}])
.factory('helperXmlRpc', function(){
/**
* Clones an array object
*/
function cloneArray_(object){
var length = object.length;
if (length > 0) {
var rv = new Array(length);
for (var i = 0; i < length; i++) {
rv[i] = object[i];
}
return rv;
}
return [];
};
/**
* Creates a XML document for IEs browsers
*/
function createMsXmlDocument_(){
var doc = new ActiveXObject('MSXML2.DOMDocument');
if (doc) {
doc.resolveExternals = false;
doc.validateOnParse = false;
try {
doc.setProperty('ProhibitDTD', true);
doc.setProperty('MaxXMLSize', 2 * 1024);
doc.setProperty('MaxElementDepth', 256);
} catch (e) {
// No-op.
}
}
return doc;
};
/**
* Creates a XML document
*/
function createDocument(opt_rootTagName, opt_namespaceUri){
if (opt_namespaceUri && !opt_rootTagName) {
throw Error("Can't create document with namespace and no root tag");
}
if (HAS_ACTIVEX) {
var doc = createMsXmlDocument_();
if (doc) {
if (opt_rootTagName) {
doc.appendChild(doc.createNode(1,
opt_rootTagName,
opt_namespaceUri || ''));
}
return doc;
}
}
else if (document.implementation && document.implementation.createDocument) {
return document.implementation.createDocument(opt_namespaceUri || '',
opt_rootTagName || '',
null);
}
throw Error('Your browser does not support creating new documents');
};
/**
* Creates a XML node and set the child(ren) node(s)
*/
function createNode(doc, nodeName, children){
var elt = doc.createElement(nodeName);
var appendChild = function(child) {
if(typeof child == 'object' && child.nodeType !== 1){
for(var i in child){
elt.appendChild(
(typeof child == 'string') ? doc.createTextNode(child[i]) : child[i]
);
}
} else {
elt.appendChild(
(typeof child == 'string') ? doc.createTextNode(child) : child
);
}
}
if (arguments.length > 3) {
children = cloneArray_(arguments);
children.shift(); //shift doc
children.shift(); //shift nodeName
}
if (typeof children == 'array') {
angular.forEach(children, appendChild);
} else if (children) {
appendChild(children);
}
return elt;
};
/**
* Generate an ID for XMLRPC request
*/
function generateId(){
return 'xmlrpc-'+(new Date().getTime())+'-'+Math.floor(Math.random()*1000);
};
/**
* Creates an XML document from a string
*/
function loadXml_(xml) {
if (HAS_ACTIVEX) {
var doc = createMsXmlDocument_();
doc.loadXML(xml);
return doc;
}
else if (typeof DOMParser != 'undefined') {
return new DOMParser().parseFromString(xml, 'application/xml');
}
throw Error('Your browser does not support loading xml documents');
};
/**
* Returns the document in which the node is.
*/
function getOwnerDocument_(node) {
return (
node.nodeType == 9 ? node :
node.ownerDocument || node.document);
};
/**
* Return a single node with the given name in the given node
*/
function selectSingleNode_(node, path) {
if (typeof node.selectSingleNode != 'undefined') {
var doc = getOwnerDocument_(node);
if (typeof doc.setProperty != 'undefined') {
doc.setProperty('SelectionLanguage', 'XPath');
}
return node.selectSingleNode(path);
} else if (document.implementation.hasFeature('XPath', '3.0')) {
var doc = getOwnerDocument_(node);
var resolver = doc.createNSResolver(doc.documentElement);
var result = doc.evaluate(path, node, resolver,
XPathResult.FIRST_ORDERED_NODE_TYPE, null);
return result.singleNodeValue;
}
return null;
};
/**
* Returns the string content of a node
*/
function getTextContent_(node, buf, normalizedWhitespace){
var PREDEFINED_TAG_VALUES_ = {'IMG': ' ', 'BR': '\n'};
if (node.nodeName in ['SCRIPT', 'STYLE', 'HEAD', 'IFRAME', 'OBJECT']) {
// ignore certain tags
} else if (node.nodeType == 3) {
if (normalizedWhitespace) {
buf.push(String(node.nodeValue).replace(/(\r\n|\r|\n)/g, ''));
} else {
buf.push(node.nodeValue);
}
} else if (node.nodeName in PREDEFINED_TAG_VALUES_) {
buf.push(PREDEFINED_TAG_VALUES_[node.nodeName]);
} else {
var child = node.firstChild;
while (child) {
getTextContent_(child, buf, normalizedWhitespace);
child = child.nextSibling;
}
}
return buf.join('');
};
/**
* Returns all the nodes in a array that are inside the given node with the given path
*/
function selectNodes_(node, path) {
if (typeof node.selectNodes != 'undefined') {
var doc = getOwnerDocument_(node);
if (typeof doc.setProperty != 'undefined') {
doc.setProperty('SelectionLanguage', 'XPath');
}
return node.selectNodes(path);
} else if (document.implementation.hasFeature('XPath', '3.0')) {
var doc = getOwnerDocument_(node);
var resolver = doc.createNSResolver(doc.documentElement);
var nodes = doc.evaluate(path, node, resolver,
XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
var results = [];
var count = nodes.snapshotLength;
for (var i = 0; i < count; i++) {
results.push(nodes.snapshotItem(i));
}
return results;
} else {
return [];
}
};
return {
cloneArray:cloneArray_,
createDocument: createDocument,
createNode: createNode,
generateId: generateId,
loadXml: loadXml_,
getOwnerDocument:getOwnerDocument_,
selectNodes: selectNodes_,
getTextContent : getTextContent_,
selectSingleNode: selectSingleNode_
};
});