welsonjs/lib/xml.js

153 lines
3.9 KiB
JavaScript
Raw Normal View History

2020-11-05 03:04:50 +00:00
////////////////////////////////////////////////////////////////////////
// XML API
////////////////////////////////////////////////////////////////////////
var FILE = require("lib/file");
2020-11-12 06:39:50 +00:00
var StreamConvert = function(data) {
2020-11-09 10:06:34 +00:00
this.data = data;
this.toBinary = function() {
var adTypeText = 2;
var adTypeBinary = 1;
var BinaryStream = CreateObject("ADODB.Stream");
BinaryStream.Type = adTypeText;
BinaryStream.CharSet = "utf-8";
BinaryStream.Open();
BinaryStream.WriteText(this.data);
BinaryStream.Position = 0;
BinaryStream.Type = adTypeBinary;
BinaryStream.Position = 0;
return BinaryStream.Read();
};
this.toString = function() {
var adTypeText = 2;
var adTypeBinary = 1;
var BinaryStream = CreateObject("ADODB.Stream");
BinaryStream.Type = adTypeBinary;
BinaryStream.Open();
BinaryStream.Write(this.data);
BinaryStream.Position = 0;
BinaryStream.Type = adTypeText;
BinaryStream.CharSet = "utf-8";
return BinaryStream.ReadText();
};
};
2020-11-05 03:04:50 +00:00
2020-11-09 11:34:34 +00:00
var XMLObject = function(dom) {
2020-11-09 10:06:34 +00:00
this.filename = null;
this.dom = null;
this.nodes = [];
2020-11-09 11:34:34 +00:00
if (typeof(dom) !== "undefined") {
this.dom = dom;
2020-11-09 10:06:34 +00:00
} else {
2020-11-09 11:34:34 +00:00
this.dom = CreateObject([
"Msxml2.DOMDocument.6.0",
"Msxml2.DOMDocument.5.0",
"Msxml2.DOMDocument.4.0",
"Msxml2.DOMDocument.3.0",
2020-11-12 06:39:50 +00:00
"Msxml2.DOMDocument",
2020-11-09 11:34:34 +00:00
"MSXML.DOMDocument"
]);
2020-11-09 10:06:34 +00:00
}
2020-11-09 11:34:34 +00:00
this.getDOM = function() {
return this.dom;
};
2020-11-18 04:13:38 +00:00
this.load = function(filename) {
this.filename = filename;
console.info("XMLObject.load() -> Opening XML file: " + filename)
2020-11-05 03:05:52 +00:00
2020-11-18 04:13:38 +00:00
try {
if (FILE.fileExists(filename)) {
this.getDOM().loadXML(FILE.readFile(this.filename, "utf-8"));
} else {
console.error("XMLObject.load() -> The file does not exists or access denied");
}
} catch (e) {
console.error("XMLObject.load() -> " + e.message);
2020-11-05 03:05:52 +00:00
}
2020-11-09 10:06:34 +00:00
return this;
}
2020-11-09 11:34:34 +00:00
this.getAttribute = function(s) {
return this.getDOM().getAttribute(s);
2020-11-09 10:06:34 +00:00
};
this.getText = function() {
2020-11-09 11:34:34 +00:00
return this.getDOM().text;
2020-11-09 10:06:34 +00:00
};
2020-11-09 11:34:34 +00:00
2020-11-09 10:06:34 +00:00
this.select = function(path) {
2020-11-09 11:34:34 +00:00
var nodes = this.getDOM().selectNodes(path);
2020-11-09 10:06:34 +00:00
for (var i = 0; i < nodes.length; i++) {
2020-11-09 11:34:34 +00:00
this.nodes.push(new XMLObject(nodes[i]));
2020-11-09 10:06:34 +00:00
}
2020-11-09 11:34:34 +00:00
return this;
2020-11-09 10:06:34 +00:00
};
this.toArray = function() {
return this.nodes;
};
this.first = function() {
2020-11-09 11:34:34 +00:00
if (this.nodes.length > 0) {
2020-11-09 10:06:34 +00:00
return this.nodes[0];
}
};
2020-11-09 11:34:34 +00:00
2020-11-09 10:06:34 +00:00
this.last = function() {
2020-11-09 11:34:34 +00:00
if (this.nodes.length > 0) {
2020-11-09 10:06:34 +00:00
return this.nodes[this.nodes.length - 1];
}
};
2020-11-09 11:34:34 +00:00
this.eq = function() {
if (this.nodes.length > i) {
return this.nodes[i];
}
2020-11-09 10:06:34 +00:00
};
2020-11-09 11:34:34 +00:00
this.createElement = function(name) {
return new XMLObject(this.getDOM().createElement(name));
2020-11-09 10:06:34 +00:00
}
this.encode = function(value, type) {
2020-11-09 11:34:34 +00:00
var dom = this.getDOM();
2020-11-09 10:06:34 +00:00
2020-11-09 11:34:34 +00:00
dom.dataType = type;
2020-11-09 10:06:34 +00:00
if (type.indexOf("bin.") == 0) {
2020-11-12 06:39:50 +00:00
dom.nodeTypedValue = (new StreamConvert(value)).toBinary();
2020-11-09 10:06:34 +00:00
} else {
2020-11-09 11:34:34 +00:00
dom.nodeTypedValue = value;
2020-11-09 10:06:34 +00:00
}
2020-11-09 11:34:34 +00:00
return dom.text;
2020-11-09 10:06:34 +00:00
};
this.decode = function(value, type) {
2020-11-09 11:34:34 +00:00
var dom = this.getDOM();
2020-11-09 10:06:34 +00:00
2020-11-09 11:34:34 +00:00
dom.dataType = type;
2020-11-09 10:06:34 +00:00
if (type.indexOf("bin.") == 0) {
2020-11-12 06:39:50 +00:00
dom.text = (new StreamConvert(value)).toString();
2020-11-09 10:06:34 +00:00
} else {
2020-11-09 11:34:34 +00:00
dom.text = value;
2020-11-09 10:06:34 +00:00
}
2020-11-09 11:34:34 +00:00
return dom.nodeTypedValue;
2020-11-05 03:04:50 +00:00
};
};
2020-11-09 11:34:34 +00:00
exports.load = function(s) {
return (new XMLObject()).load(s);
2020-11-09 10:06:34 +00:00
};
2020-11-09 11:34:34 +00:00
exports.VERSIONINFO = "XML interface (xml.js) version 0.1";
exports.global = global;
exports.require = global.require;