-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathjath.js
188 lines (175 loc) · 4.52 KB
/
jath.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
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
/**
* Jath is free software provided under the MIT license.
* See LICENSE file for full text of the license.
* Copyright 2010 Dan Newcome.
*/
(function() {
Jath = {};
Jath.parse = parse;
Jath.resolver = null;
Jath.namespaces = null;
// values prefixed with literal charactar marker will not be
// treated as xpath expressions and will be output directly
Jath.literalChar = ":";
/**
* Rudimentary check for IE
* Also added support for WSH, uses the same API as IE
*/
var m_browser;
if( typeof WScript != "undefined" ) {
m_browser = 'msie';
}
// TODO: is there a better way to detect node.js?
else if( typeof process != "undefined" ) {
// running under node.js
m_browser = 'node';
var xmljs = require( 'libxmljs' );
exports.parse = parse;
}
else if( navigator.userAgent.toLowerCase().indexOf( 'msie' ) > -1 ) {
m_browser = 'msie';
}
else {
m_browser = 'standards';
}
/**
* parse:
* process xml doc according to the given json template
* @template - output spec as a json template
* @xmldoc - input xml document
* @node - the starting node to use in the document. xpath
* expressions will be evaluated relative to this node.
* If not given, root will be used.
*/
function parse( template, xmldoc, node ) {
if( node === undefined ) {
node = xmldoc;
}
if( typeOf( template ) === 'array' ) {
return parseArray( template, xmldoc, node );
}
else if( typeOf( template ) === 'object' ) {
return parseObject( template, xmldoc, node );
}
else {
return parseItem( template, xmldoc, node );
}
}
function parseArray( template, xmldoc, node ) {
var retVal = [];
if( template[0] != null ) {
if( m_browser == 'msie' ) {
xmldoc.setProperty("SelectionLanguage", "XPath");
xmldoc.setProperty("SelectionNamespaces", createResolverString() );
var nodeList = node.selectNodes( template[0] );
var thisNode;
while( thisNode = nodeList.nextNode() ) {
retVal.push( parse( template[1], xmldoc, thisNode ) );
}
}
else if( m_browser == 'node' ) {
var nodeList = node.find( template[0] );
for( var i=0; i < nodeList.length; i++ ) {
retVal.push( parse( template[1], xmldoc, nodeList[i] ) );
}
}
else {
var xpathResult = xmldoc.evaluate( template[0], node, Jath.resolver, XPathResult.ANY_TYPE, null );
var thisNode;
while( thisNode = xpathResult.iterateNext() ) {
retVal.push( parse( template[1], xmldoc, thisNode ) );
}
}
}
// we can have an array output without iterating over the source
// data - in this case, current node is static
else {
for( var i=1; i < template.length; i++ ) {
retVal.push( parse( template[i], xmldoc, node ) );
}
}
return retVal;
}
function parseObject( template, xmldoc, node ) {
var item;
var newitem = {};
for( item in template ) {
newitem[item] = parse( template[item], xmldoc, node );
}
return newitem;
}
function parseItem( template, xmldoc, node ) {
if( typeOf( template ) == 'string' && template.substring( 0, 1 ) != Jath.literalChar ) {
if( m_browser == 'msie' ) {
xmldoc.setProperty("SelectionLanguage", "XPath");
xmldoc.setProperty("SelectionNamespaces", createResolverString() );
if( node.selectSingleNode( template ) != null ) {
return node.selectSingleNode( template ).text;
}
else {
return null;
}
}
else if( m_browser == 'node' ) {
var itemNode = node.get( template );
if( itemNode && itemNode.text ) {
return itemNode.text();
}
else if( itemNode && itemNode.value ) {
return itemNode.value();
}
else {
return null;
}
}
else {
var itemNode = xmldoc.evaluate( template, node, Jath.resolver, XPathResult.STRING_TYPE, null );
if( itemNode ) {
return itemNode.stringValue;
}
else {
return null;
}
}
}
else {
return template.substring( 1 );
}
}
/**
* typeOf function published by Douglas Crockford in ECMAScript recommendations
* http://www.crockford.com/javascript/recommend.html
*/
function typeOf(value) {
var s = typeof value;
if (s === 'object') {
if (value) {
if (typeof value.length === 'number' &&
!(value.propertyIsEnumerable('length')) &&
typeof value.splice === 'function') {
s = 'array';
}
} else {
s = 'null';
}
}
return s;
}
/**
* IE requires namespaces to be in the form that
* an xml document would provide. Use underscore
* for default ns.
*/
function createResolverString() {
var retval = [];
for( var item in Jath.namespaces ) {
if( item == "_" ) {
retval.push( "xmlns='" + Jath.namespaces[item] + "'" );
}
else {
retval.push( "xmlns:" + item + "='" + Jath.namespaces[item] + "'" );
}
}
return retval.join(" ");
}
})();