From 2ca43a791e5dc2c05392620336e3b4b6efc217da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B3=A0=EB=82=A8=ED=98=84?= Date: Mon, 9 Nov 2020 20:34:34 +0900 Subject: [PATCH] fix --- lib/config.js | 1 - lib/xml.js | 158 ++++++++++++++++++-------------------------------- 2 files changed, 56 insertions(+), 103 deletions(-) diff --git a/lib/config.js b/lib/config.js index 4de09d4..7374557 100644 --- a/lib/config.js +++ b/lib/config.js @@ -1,7 +1,6 @@ //////////////////////////////////////////////////////////////////////// // Config API //////////////////////////////////////////////////////////////////////// - var XML = require("lib/xml"); var readConfig = function(path) { diff --git a/lib/xml.js b/lib/xml.js index 6325b7a..141dc1d 100644 --- a/lib/xml.js +++ b/lib/xml.js @@ -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;