This commit is contained in:
Namhyeon Go 2020-11-09 20:34:34 +09:00
parent 0adba310f2
commit 2ca43a791e
2 changed files with 56 additions and 103 deletions

View File

@ -1,7 +1,6 @@
////////////////////////////////////////////////////////////////////////
// Config API
////////////////////////////////////////////////////////////////////////
var XML = require("lib/xml");
var readConfig = function(path) {

View File

@ -3,17 +3,6 @@
////////////////////////////////////////////////////////////////////////
var FILE = require("lib/file");
var createXMLObject = function() {
return CreateObject([
"Msxml2.DOMDocument.6.0",
"Msxml2.DOMDocument.5.0",
"Msxml2.DOMDocument.4.0",
"Msxml2.DOMDocument.3.0",
"MSXML2.DOMDocument",
"MSXML.DOMDocument"
]);
};
var StreamBuilder = function(data) {
this.data = data;
@ -46,22 +35,33 @@ var StreamBuilder = function(data) {
};
var XMLObject = function(node) {
var XMLObject = function(dom) {
this.filename = null;
this.dom = null;
this.nodes = [];
if (typeof(node) !== "undefined" && node instanceof this) {
this.dom = node;
if (typeof(dom) !== "undefined") {
this.dom = dom;
} else {
this.dom = createXMLObject();
this.dom = CreateObject([
"Msxml2.DOMDocument.6.0",
"Msxml2.DOMDocument.5.0",
"Msxml2.DOMDocument.4.0",
"Msxml2.DOMDocument.3.0",
"MSXML2.DOMDocument",
"MSXML.DOMDocument"
]);
}
this.load = function(filename) {
this.filename = filename;
this.getDOM = function() {
return this.dom;
};
if (FILE.fileExists(filename)) {
this.dom.loadXML(FILE.readFile(this.filename, "utf-8"));
this.load = function(s) {
this.filename = s;
if (FILE.fileExists(s)) {
this.getDOM().loadXML(FILE.readFile(this.filename, "utf-8"));
} else {
console.error("The file does not exists");
return;
@ -69,28 +69,21 @@ var XMLObject = function(node) {
return this;
}
this.setObject = function(dom) {
this.dom = dom;
};
this.getObject = function() {
return this.dom;
};
this.getAttribute = function(name) {
return this.getObject().getAttribute(name);
this.getAttribute = function(s) {
return this.getDOM().getAttribute(s);
};
this.getText = function() {
return this.getObject().text;
return this.getDOM().text;
};
this.select = function(path) {
var nodes = this.getObject().selectNodes(path);
var nodes = this.getDOM().selectNodes(path);
for (var i = 0; i < nodes.length; i++) {
this.nodes.push(new XMLObject(node[i]));
this.nodes.push(new XMLObject(nodes[i]));
}
return this;
};
this.toArray = function() {
@ -98,103 +91,64 @@ var XMLObject = function(node) {
};
this.first = function() {
if(this.nodes.length > 0) {
if (this.nodes.length > 0) {
return this.nodes[0];
}
};
this.last = function() {
if(this.nodes.length > 0) {
if (this.nodes.length > 0) {
return this.nodes[this.nodes.length - 1];
}
};
this.eq = function(i) {
if(nodes.length > i) {
return nodes[i];
}
}
this.retrieve = function(mode, callback) {
var nodes = [];
if (typeof(callback) === "function") {
var i = 0, s = 0;
while (s == 0 && i < this.nodes.length) {
if (callback(this.nodes[i])) {
if (mode === "find")
s++;
nodes.push(this.nodes[i]);
}
i++;
}
}
switch (mode) {
case "find":
if (nodes.length > 0)
return nodes.pop();
break;
case "filter":
this.nodes = nodes;
break;
case "forEach":
break;
}
return this;
}
this.find = function(callback) {
return this.retrieve("find", callback);
};
this.filter = function(callback) {
return this.retrieve("filter", callback);
};
this.forEach = function(callback) {
return this.retrieve("forEach", callback);
this.eq = function() {
if (this.nodes.length > i) {
return this.nodes[i];
}
};
this.createElement = function(elementname) {
this.setObject(this.getObject().createElement(name));
this.createElement = function(name) {
return new XMLObject(this.getDOM().createElement(name));
}
this.encode = function(value, type) {
this.getObject().dataType = type;
var dom = this.getDOM();
dom.dataType = type;
if (type.indexOf("bin.") == 0) {
this.getObject().nodeTypedValue = (new StreamBuilder(value)).toBinary();
dom.nodeTypedValue = (new StreamBuilder(value)).toBinary();
} else {
this.getObject().nodeTypedValue = value;
dom.nodeTypedValue = value;
}
return this.getObject().text;
return dom.text;
};
this.decode = function(value, type) {
this.getObject().dataType = type;
var dom = this.getDOM();
dom.dataType = type;
if (type.indexOf("bin.") == 0) {
this.getObject().text = (new StreamBuilder(value)).toString();
dom.text = (new StreamBuilder(value)).toString();
} else {
this.getObject().text = value;
dom.text = value;
}
return this.getObject().nodeTypedValue;
return dom.nodeTypedValue;
};
};
exports.create = createXMLObject;
exports.load = function(filename) {
return (new XMLObject()).load(filename);
exports.create = function() {
return (new XMLObject()).getDOM();
};
exports.createElement = function(elementname) {
return (new XMLObject()).createElement(elementname);
exports.load = function(s) {
return (new XMLObject()).load(s);
};
exports.createElement = function(s) {
return (new XMLObject()).createElement(s);
};
exports.VERSIONINFO = "XML interface (xml.js) version 0.1";
exports.global = global;
exports.require = global.require;