welsonjs/lib/xml.js

170 lines
4.2 KiB
JavaScript
Raw Normal View History

2020-11-05 03:04:50 +00:00
////////////////////////////////////////////////////////////////////////
// XML API
////////////////////////////////////////////////////////////////////////
var FILE = require("lib/file");
2023-01-04 20:37:13 +00:00
var adTypeText = 2;
var adTypeBinary = 1;
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 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 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) {
2022-01-17 15:45:23 +00:00
try {
var dom = this.getDOM();
2023-01-04 19:36:12 +00:00
var node = dom.createElement("XMLNode");
2020-11-09 10:06:34 +00:00
2023-01-04 19:36:12 +00:00
node.dataType = type;
2023-01-04 20:37:13 +00:00
node.nodeTypedValue = (new StreamConvert(value)).toBinary();
2020-11-09 10:06:34 +00:00
2023-01-04 19:36:12 +00:00
return node.text;
2022-01-17 15:45:23 +00:00
} catch (e) {
console.error("XMLObject->encode():", e.message);
}
2020-11-09 10:06:34 +00:00
};
this.decode = function(value, type) {
2022-01-17 15:45:23 +00:00
try {
var dom = this.getDOM();
2023-01-04 19:36:12 +00:00
var node = dom.createElement("XMLNode");
2020-11-09 10:06:34 +00:00
2023-01-04 19:36:12 +00:00
node.dataType = type;
2023-01-04 20:37:13 +00:00
node.text = value;
2020-11-09 10:06:34 +00:00
2023-01-04 20:37:13 +00:00
return (new StreamConvert(node.nodeTypedValue)).toString();
2022-01-17 15:45:23 +00:00
} catch (e) {
console.error("XMLObject->decode():", e.message);
}
2020-11-05 03:04:50 +00:00
};
};
2023-01-04 20:37:13 +00:00
function create() {
return new XMLObject();
}
2022-01-17 15:45:23 +00:00
2023-01-04 20:37:13 +00:00
function load(s) {
return create().load(s);
}
function encode(value, type) {
return create().encode(value, type);
}
function decode(value, type) {
return create().decode(value, type);
}
exports.create = create;
exports.load = load;
exports.encode = encode;
exports.decode = decode;
2020-11-09 11:34:34 +00:00
2023-01-04 20:37:13 +00:00
exports.VERSIONINFO = "XML interface (xml.js) version 0.2.2";
2020-11-09 11:34:34 +00:00
exports.global = global;
exports.require = global.require;