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 // Config API
//////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////
var XML = require("lib/xml"); var XML = require("lib/xml");
var readConfig = function(path) { var readConfig = function(path) {

View File

@ -3,17 +3,6 @@
//////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////
var FILE = require("lib/file"); 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) { var StreamBuilder = function(data) {
this.data = data; this.data = data;
@ -46,22 +35,33 @@ var StreamBuilder = function(data) {
}; };
var XMLObject = function(node) { var XMLObject = function(dom) {
this.filename = null; this.filename = null;
this.dom = null; this.dom = null;
this.nodes = []; this.nodes = [];
if (typeof(node) !== "undefined" && node instanceof this) { if (typeof(dom) !== "undefined") {
this.dom = node; this.dom = dom;
} else { } 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.getDOM = function() {
this.filename = filename; return this.dom;
};
if (FILE.fileExists(filename)) { this.load = function(s) {
this.dom.loadXML(FILE.readFile(this.filename, "utf-8")); this.filename = s;
if (FILE.fileExists(s)) {
this.getDOM().loadXML(FILE.readFile(this.filename, "utf-8"));
} else { } else {
console.error("The file does not exists"); console.error("The file does not exists");
return; return;
@ -69,28 +69,21 @@ var XMLObject = function(node) {
return this; return this;
} }
this.setObject = function(dom) {
this.dom = dom;
};
this.getObject = function() { this.getAttribute = function(s) {
return this.dom; return this.getDOM().getAttribute(s);
};
this.getAttribute = function(name) {
return this.getObject().getAttribute(name);
}; };
this.getText = function() { this.getText = function() {
return this.getObject().text; return this.getDOM().text;
}; };
this.select = function(path) { this.select = function(path) {
var nodes = this.getObject().selectNodes(path); var nodes = this.getDOM().selectNodes(path);
for (var i = 0; i < nodes.length; i++) { 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() { this.toArray = function() {
@ -98,103 +91,64 @@ var XMLObject = function(node) {
}; };
this.first = function() { this.first = function() {
if(this.nodes.length > 0) { if (this.nodes.length > 0) {
return this.nodes[0]; return this.nodes[0];
} }
}; };
this.last = function() { this.last = function() {
if(this.nodes.length > 0) { if (this.nodes.length > 0) {
return this.nodes[this.nodes.length - 1]; 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) { this.eq = function() {
return this.retrieve("find", callback); if (this.nodes.length > i) {
}; return this.nodes[i];
}
this.filter = function(callback) {
return this.retrieve("filter", callback);
};
this.forEach = function(callback) {
return this.retrieve("forEach", callback);
}; };
this.createElement = function(elementname) { this.createElement = function(name) {
this.setObject(this.getObject().createElement(name)); return new XMLObject(this.getDOM().createElement(name));
} }
this.encode = function(value, type) { this.encode = function(value, type) {
this.getObject().dataType = type; var dom = this.getDOM();
dom.dataType = type;
if (type.indexOf("bin.") == 0) { if (type.indexOf("bin.") == 0) {
this.getObject().nodeTypedValue = (new StreamBuilder(value)).toBinary(); dom.nodeTypedValue = (new StreamBuilder(value)).toBinary();
} else { } else {
this.getObject().nodeTypedValue = value; dom.nodeTypedValue = value;
} }
return this.getObject().text; return dom.text;
}; };
this.decode = function(value, type) { this.decode = function(value, type) {
this.getObject().dataType = type; var dom = this.getDOM();
dom.dataType = type;
if (type.indexOf("bin.") == 0) { if (type.indexOf("bin.") == 0) {
this.getObject().text = (new StreamBuilder(value)).toString(); dom.text = (new StreamBuilder(value)).toString();
} else { } else {
this.getObject().text = value; dom.text = value;
} }
return this.getObject().nodeTypedValue; return dom.nodeTypedValue;
}; };
}; };
exports.create = createXMLObject; exports.create = function() {
exports.load = function(filename) { return (new XMLObject()).getDOM();
return (new XMLObject()).load(filename);
}; };
exports.createElement = function(elementname) { exports.load = function(s) {
return (new XMLObject()).createElement(elementname); 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;