-
Notifications
You must be signed in to change notification settings - Fork 21
/
xml-formatter.js
67 lines (56 loc) · 1.89 KB
/
xml-formatter.js
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
/*
* Orange angular-swagger-ui - v0.3.0
*
* (C) 2015 Orange, all right reserved
* MIT Licensed
*/
'use strict';
angular.module('sw.plugin.xmlFormater', ['sw.plugins'])
.factory('xmlFormatter', function ($q, $log) {
$log.debug('sw:plugin', 'xmlFormater');
return {
execute: execute
};
function execute (response) {
$log.debug('sw:execute', 'xmlFormater');
var executed = false;
var deferred = $q.defer();
var contentType = response.headers && response.headers()['content-type'];
if (contentType && contentType.toLowerCase().indexOf('/xml') > 0) {
response.data = formatXml(response.data);
executed = true;
}
deferred.resolve(executed);
return deferred.promise;
}
function formatXml (xml) {
var formatted = '';
var reg = /(>)(<)(\/*)/g;
var pad = 0;
xml = xml.replace(reg, '$1\r\n$2$3');
angular.forEach(xml.split('\r\n'), function (node) {
var indent = 0;
var padding = '';
if (node.match(/.+<\/\w[^>]*>$/)) {
indent = 0;
} else if (node.match(/^<\/\w/)) {
if (pad !== 0) {
pad -= 1;
}
} else if (node.match(/^<\w[^>]*[^\/]>.*$/)) {
indent = 1;
} else {
indent = 0;
}
for (var i = 0; i < pad; i++) {
padding += ' ';
}
formatted += padding + node + '\r\n';
pad += indent;
});
return formatted;
}
})
.run(function (plugins, xmlFormatter) {
plugins.add(plugins.AFTER_EXPLORER_LOAD, xmlFormatter);
});