forked from splunk/splunk-sdk-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.js
More file actions
98 lines (94 loc) · 3.48 KB
/
Copy pathutils.js
File metadata and controls
98 lines (94 loc) · 3.48 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
/*!*/
// Copyright 2014 Splunk, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License"): you may
// not use this file except in compliance with the License. You may obtain
// a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations
// under the License.
var utils = require('../utils'); // Get all of the existing utils
/**
* Parse the parameters from an `InputDefinition` or `ValidationDefinition`.
*
* This is a helper function for `parseXMLData`.
*
* The XML typically will look like this:
*
* `<configuration>`
* `<stanza name="foobar://aaa">`
* `<param name="param1">value1</param>`
* `<param name="param2">value2</param>`
* `<param name="disabled">0</param>`
* `<param name="index">default</param>`
* `</stanza>`
* `<stanza name="foobar://bbb">`
* `<param name="param1">value11</param>`
* `<param name="param2">value22</param>`
* `<param name="disabled">0</param>`
* `<param name="index">default</param>`
* `<param_list name="multiValue">`
* `<value>value1</value>`
* `<value>value2</value>`
* `</param_list>`
* `<param_list name="multiValue2">`
* `<value>value3</value>`
* `<value>value4</value>`
* `</param_list>`
* `</stanza>`
* `</configuration>`
*
* @param {Object} an `Elementree` object representing the `<configuration>` XML node.
* @return {Object} an `Elementree` object representing the parameters of node passed in.
*/
utils.parseParameters = function(paramNode) {
switch (paramNode.tag) {
case "param":
return paramNode.text;
case "param_list":
var parameters = [];
var paramChildren = paramNode.getchildren();
for (var i = 0; i < paramChildren.length; i++) {
var mvp = paramChildren[i];
parameters.push(mvp.text);
}
return parameters;
default:
throw new Error("Invalid configuration scheme, <" + paramNode.tag + "> tag unexpected.");
}
};
/**
* Parses the parameters from `Elementtree` representations of XML for
* `InputDefinition` and `ValidationDefinition` objects.
*
* @param {Object} a parent `Elementtree` element object.
* @param {String} the name of the child element to parse parameters from.
* @return {Object} an object of the parameters parsed.
*/
utils.parseXMLData = function(parentNode, childNodeTag) {
var data = {};
var children = parentNode.getchildren();
for (var i = 0; i < children.length; i++) {
var child = children[i];
if (child.tag === childNodeTag) {
if (childNodeTag === "stanza") {
data[child.get("name")] = {};
var stanzaChildren = child.getchildren();
for (var p = 0; p < stanzaChildren.length; p++) {
var param = stanzaChildren[p];
data[child.get("name")][param.get("name")] = utils.parseParameters(param);
}
}
}
else if ("item" === parentNode.tag) {
data[child.get("name")] = utils.parseParameters(child);
}
}
return data;
};
module.exports = utils;