node-opcua-xml2json
Advanced tools
Comparing version 2.6.0-alpha.7 to 2.6.1
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.definitionReaderStateParser = exports._definitionParser = void 0; | ||
// <Definition Name="SomeName"> | ||
// <Field Name="Running" Value="0" dataType: [ValueRank="1"]> | ||
// [<Description>text</Description>] | ||
// <Field> | ||
// </Definition> | ||
const node_opcua_assert_1 = require("node-opcua-assert"); | ||
// <Definition Name="SomeName"> | ||
// <Field Name="Running" Value="0" dataType: [ValueRank="1"]> | ||
// [<Description>text</Description>] | ||
// <Field> | ||
// </Definition> | ||
// | ||
// Or | ||
// | ||
// (IsOptionSet) | ||
// | ||
// | ||
exports._definitionParser = { | ||
@@ -42,2 +58,3 @@ init(name, attrs) { | ||
else { | ||
// when not specified valueRank means Scalar and Scalar is -1 | ||
obj.valueRank = -1; | ||
@@ -44,0 +61,0 @@ } |
@@ -156,2 +156,3 @@ "use strict"; | ||
function _makeExtensionObjectReader(definitionName, definitionMap, readerMap) { | ||
// is it a basic type ? | ||
if (partials.hasOwnProperty(definitionName)) { | ||
@@ -185,2 +186,3 @@ return partials[definitionName]; | ||
parser: fieldReader.parser, | ||
// endElement: fieldReader.endElement, | ||
finish() { | ||
@@ -207,2 +209,3 @@ const elName = node_opcua_utils_1.lowerFirstLetter(field.name); | ||
startElement(name, attrs) { | ||
// empty | ||
}, | ||
@@ -220,2 +223,4 @@ endElement(element) { | ||
} | ||
// xx const parser: ParserLike = {}; | ||
// xx parser[definition.name] = reader; | ||
readerMap[definitionName] = reader; | ||
@@ -228,2 +233,3 @@ return reader; | ||
endElement() { | ||
// console.log(this.parser[definitionName].value); | ||
this._pojo = this.parser[definitionName].value; | ||
@@ -230,0 +236,0 @@ }, |
@@ -0,1 +1,5 @@ | ||
/** | ||
* @module node-opcua-xml2json | ||
* node -> see if https://github.com/isaacs/sax-js could be used instead | ||
*/ | ||
export declare type SimpleCallback = (err?: Error) => void; | ||
@@ -38,2 +42,12 @@ export declare type Callback<T> = (err?: Error | null, result?: T) => void; | ||
} | ||
/** | ||
* @class ReaderState | ||
* @private | ||
* @param options | ||
* @param [options.parser=null] {map<ReaderState|options}} | ||
* @param [options.init|null] | ||
* @param [options.finish] | ||
* @param [options.startElement] | ||
* @param [options.endElement] | ||
*/ | ||
export declare class ReaderState extends ReaderStateBase { | ||
@@ -56,9 +70,74 @@ _init?: (name: string, attrs: XmlAttributes, parent: IReaderState, engine: Xml2Json) => void; | ||
constructor(options: ReaderStateParser | ReaderState); | ||
/** | ||
* @method _on_init | ||
* @param elementName - the name of the element | ||
* @param attrs | ||
* @param parent | ||
* @param level | ||
* @param engine | ||
* @protected | ||
*/ | ||
_on_init(elementName: string, attrs: XmlAttributes, parent: IReaderState, level: number, engine: Xml2Json): void; | ||
_on_finish(): void; | ||
/** | ||
* @param level | ||
* @param elementName - the name of the element | ||
* @param attrs | ||
* @protected | ||
*/ | ||
_on_startElement(level: number, elementName: string, attrs: XmlAttributes): void; | ||
_on_endElement2(level: number, elementName: string): void; | ||
/** | ||
* @method _on_endElement | ||
* @protected | ||
*/ | ||
_on_endElement(level: number, elementName: string): void; | ||
/** | ||
* @method _on_text | ||
* @param text {String} the text found inside the element | ||
* @protected | ||
*/ | ||
_on_text(text: string): void; | ||
} | ||
/** | ||
* @class Xml2Json | ||
* @param options - the state machine as a ReaderState node. | ||
* @param [options.parser=null] {ReaderState} | ||
* @param [options.init|null] | ||
* @param [options.finish] | ||
* @param [options.startElement] | ||
* @param [options.endElement] | ||
* @constructor | ||
* | ||
* @example | ||
* var parser = new Xml2Json({ | ||
* parser: { | ||
* 'person': { | ||
* init: function(name,attrs) { | ||
* this.parent.root.obj = {}; | ||
* this.obj = this.parent.root.obj; | ||
* this.obj['name'] = attrs['name']; | ||
* }, | ||
* parser: { | ||
* 'address': { | ||
* finish: function(){ | ||
* this.parent.obj['address'] = this.text; | ||
* } | ||
* } | ||
* } | ||
* } | ||
* } | ||
* }); | ||
* | ||
* var xml_string = "<employees>" + | ||
* " <person name='John'>" + | ||
* " <address>Paris</address>" + | ||
* " </person>" + | ||
* "</employees>"; | ||
* | ||
* parser.parseString(xml_string, function() { | ||
* parser.obj.should.eql({name: 'John',address: 'Paris'}); | ||
* done(); | ||
* }); | ||
*/ | ||
export declare class Xml2Json { | ||
@@ -69,9 +148,30 @@ currentLevel: number; | ||
constructor(options: ReaderStateParser); | ||
/** | ||
* @method parseString | ||
* @async | ||
*/ | ||
parseString(xml_text: string): Promise<any>; | ||
parseString(xml_text: string, callback: Callback<any> | SimpleCallback): void; | ||
/** | ||
* @method parse | ||
* @async | ||
* @param xmlFile - the name of the xml file to parse. | ||
*/ | ||
parse(xmlFile: string): Promise<any>; | ||
parse(xmlFile: string, callback: Callback<any> | SimpleCallback): void; | ||
/** | ||
* @param new_state | ||
* @param name | ||
* @param attr | ||
* @private | ||
* @internal | ||
*/ | ||
_promote(new_state: IReaderState, level: number, name?: string, attr?: XmlAttributes): void; | ||
/** | ||
* | ||
* @private | ||
* @internal | ||
*/ | ||
_demote(cur_state: IReaderState, level: number, elementName: string): void; | ||
private _prepareParser; | ||
} |
"use strict"; | ||
/** | ||
* @module node-opcua-xml2json | ||
* node -> see if https://github.com/isaacs/sax-js could be used instead | ||
*/ | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.Xml2Json = exports.ReaderState = exports.ReaderStateBase = void 0; | ||
// tslint:disable:max-classes-per-file | ||
// tslint:disable:no-var-requires | ||
// tslint:disable:unified-signatures | ||
const fs = require("fs"); | ||
@@ -8,5 +15,13 @@ const node_opcua_assert_1 = require("node-opcua-assert"); | ||
const LtxParser = require("ltx/lib/parsers/ltx.js"); | ||
/** | ||
* @static | ||
* @private | ||
* @method _coerceParser | ||
* @param parser {map<ReaderState|options>} | ||
* @return {map} | ||
*/ | ||
function _coerceParser(parser) { | ||
for (const name of Object.keys(parser)) { | ||
if (parser[name] && !(parser[name] instanceof ReaderState)) { | ||
// this is to prevent recursion | ||
const tmp = parser[name]; | ||
@@ -22,2 +37,12 @@ delete parser[name]; | ||
exports.ReaderStateBase = ReaderStateBase; | ||
/** | ||
* @class ReaderState | ||
* @private | ||
* @param options | ||
* @param [options.parser=null] {map<ReaderState|options}} | ||
* @param [options.init|null] | ||
* @param [options.finish] | ||
* @param [options.startElement] | ||
* @param [options.endElement] | ||
*/ | ||
class ReaderState extends ReaderStateBase { | ||
@@ -31,2 +56,3 @@ constructor(options) { | ||
this.currentLevel = -1; | ||
// ensure options object has only expected properties | ||
options.parser = options.parser || {}; | ||
@@ -36,3 +62,5 @@ if (!(options instanceof ReaderStateBase)) { | ||
const invalid_fields = _.difference(fields, ["parser", "init", "finish", "startElement", "endElement"]); | ||
/* istanbul ignore next*/ | ||
if (invalid_fields.length !== 0) { | ||
// tslint:disable:no-console | ||
throw new Error("Invalid filed detected in ReaderState Parser ! : " + | ||
@@ -49,2 +77,11 @@ invalid_fields.join(" - ") + | ||
} | ||
/** | ||
* @method _on_init | ||
* @param elementName - the name of the element | ||
* @param attrs | ||
* @param parent | ||
* @param level | ||
* @param engine | ||
* @protected | ||
*/ | ||
_on_init(elementName, attrs, parent, level, engine) { | ||
@@ -68,4 +105,11 @@ this.name = elementName; | ||
} | ||
/** | ||
* @param level | ||
* @param elementName - the name of the element | ||
* @param attrs | ||
* @protected | ||
*/ | ||
_on_startElement(level, elementName, attrs) { | ||
this.currentLevel = level; | ||
// console.log("wxxxx _on_startElement#" + this.name, elementName, this.currentLevel); | ||
this.chunks = []; | ||
@@ -77,2 +121,3 @@ this.text = ""; | ||
if (this.engine && this.parser.hasOwnProperty(elementName)) { | ||
// console.log("promoting ", elementName, this.level); | ||
this.engine._promote(this.parser[elementName], level, elementName, attrs); | ||
@@ -86,13 +131,23 @@ } | ||
} | ||
/** | ||
* @method _on_endElement | ||
* @protected | ||
*/ | ||
_on_endElement(level, elementName) { | ||
// console.log("wxxxx _on_endElement#" + this.name, elementName, level, this.currentLevel); | ||
node_opcua_assert_1.assert(this.attrs); | ||
this.chunks = this.chunks || []; | ||
if (this.level > level) { | ||
// we end a child element of this node | ||
this._on_endElement2(level, elementName); | ||
} | ||
else if (this.level === level) { | ||
// we received the end event of this node | ||
// we need to finish | ||
this.text = this.chunks.join(""); | ||
this.chunks = []; | ||
// this is the end | ||
this._on_finish(); | ||
if (this.parent && this.parent.parser && this.parent.parser.hasOwnProperty(elementName)) { | ||
// console.log("xxx demoting#" + this.name, elementName, this.level); | ||
this.engine._demote(this, level, elementName); | ||
@@ -102,2 +157,7 @@ } | ||
} | ||
/** | ||
* @method _on_text | ||
* @param text {String} the text found inside the element | ||
* @protected | ||
*/ | ||
_on_text(text) { | ||
@@ -124,2 +184,43 @@ this.chunks = this.chunks || []; | ||
} | ||
/** | ||
* @class Xml2Json | ||
* @param options - the state machine as a ReaderState node. | ||
* @param [options.parser=null] {ReaderState} | ||
* @param [options.init|null] | ||
* @param [options.finish] | ||
* @param [options.startElement] | ||
* @param [options.endElement] | ||
* @constructor | ||
* | ||
* @example | ||
* var parser = new Xml2Json({ | ||
* parser: { | ||
* 'person': { | ||
* init: function(name,attrs) { | ||
* this.parent.root.obj = {}; | ||
* this.obj = this.parent.root.obj; | ||
* this.obj['name'] = attrs['name']; | ||
* }, | ||
* parser: { | ||
* 'address': { | ||
* finish: function(){ | ||
* this.parent.obj['address'] = this.text; | ||
* } | ||
* } | ||
* } | ||
* } | ||
* } | ||
* }); | ||
* | ||
* var xml_string = "<employees>" + | ||
* " <person name='John'>" + | ||
* " <address>Paris</address>" + | ||
* " </person>" + | ||
* "</employees>"; | ||
* | ||
* parser.parseString(xml_string, function() { | ||
* parser.obj.should.eql({name: 'John',address: 'Paris'}); | ||
* done(); | ||
* }); | ||
*/ | ||
class Xml2Json { | ||
@@ -148,2 +249,3 @@ constructor(options) { | ||
if (readWholeFile) { | ||
// slightly faster but require more memory .. | ||
fs.readFile(xmlFile, (err, data) => { | ||
@@ -170,2 +272,9 @@ if (err) { | ||
} | ||
/** | ||
* @param new_state | ||
* @param name | ||
* @param attr | ||
* @private | ||
* @internal | ||
*/ | ||
_promote(new_state, level, name, attr) { | ||
@@ -181,3 +290,9 @@ attr = attr || {}; | ||
} | ||
/** | ||
* | ||
* @private | ||
* @internal | ||
*/ | ||
_demote(cur_state, level, elementName) { | ||
/// assert(this.current_state === cur_state); | ||
const { state, backup } = this.state_stack.pop(); | ||
@@ -228,2 +343,3 @@ this.current_state = state; | ||
exports.Xml2Json = Xml2Json; | ||
// tslint:disable:no-var-requires | ||
const thenify = require("thenify"); | ||
@@ -230,0 +346,0 @@ const opts = { multiArgs: false }; |
@@ -28,2 +28,3 @@ "use strict"; | ||
_on_finish() { | ||
/* empy */ | ||
} | ||
@@ -59,2 +60,3 @@ _on_startElement(level, elementName, attrs) { | ||
_on_endElement2(level, elementName) { | ||
/* empty */ | ||
} | ||
@@ -66,2 +68,3 @@ _on_endElement(level, elementName) { | ||
this._element[elName] = this.text; | ||
// this.engine!._pojo = this._pojo; | ||
} | ||
@@ -68,0 +71,0 @@ else { |
{ | ||
"name": "node-opcua-xml2json", | ||
"version": "2.6.0-alpha.7", | ||
"version": "2.6.1", | ||
"description": "pure nodejs OPCUA SDK - module -xml2json", | ||
@@ -15,5 +15,5 @@ "main": "dist/source/index.js", | ||
"ltx": "^2.9.2", | ||
"node-opcua-assert": "^2.6.0-alpha.1", | ||
"node-opcua-debug": "^2.6.0-alpha.1", | ||
"node-opcua-utils": "^2.6.0-alpha.1", | ||
"node-opcua-assert": "^2.6.1", | ||
"node-opcua-debug": "^2.6.1", | ||
"node-opcua-utils": "^2.6.1", | ||
"underscore": "^1.10.2", | ||
@@ -25,3 +25,3 @@ "xml-writer": "^1.7.0" | ||
"devDependencies": { | ||
"node-opcua-nodesets": "^2.6.0-alpha.1", | ||
"node-opcua-nodesets": "^2.6.1", | ||
"should": "^13.2.3" | ||
@@ -42,3 +42,3 @@ }, | ||
"homepage": "http://node-opcua.github.io/", | ||
"gitHead": "6af0c6f183dcb96ddc5a2befc98851d0960c5fd0" | ||
"gitHead": "15f0c0f83232fc63310dc04fea187048c7a01e4b" | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
92124
2020
0
Updatednode-opcua-assert@^2.6.1
Updatednode-opcua-debug@^2.6.1
Updatednode-opcua-utils@^2.6.1