node-opcua-xml2json
Advanced tools
Comparing version 2.15.0 to 2.16.0
@@ -25,4 +25,3 @@ "use strict"; | ||
parser: BasicType_parser(dataType, parseFunc), | ||
finish() { | ||
}, | ||
finish() { }, | ||
endElement(elementName) { | ||
@@ -64,3 +63,3 @@ this.value.push(this.parser[elementName].value); | ||
finish() { | ||
this.value = (this.text.toLowerCase() === "true"); | ||
this.value = this.text.toLowerCase() === "true"; | ||
} | ||
@@ -133,4 +132,3 @@ }, | ||
parser: { LocalizedText: localizedTextReader }, | ||
finish() { | ||
}, | ||
finish() { }, | ||
endElement() { | ||
@@ -154,3 +152,3 @@ this.value.push(this.parser.LocalizedText.value); | ||
if (a instanceof Array) { | ||
return a.map(x => _clone(x)); | ||
return a.map((x) => _clone(x)); | ||
} | ||
@@ -173,4 +171,3 @@ return Object.assign({}, a); | ||
reader = { | ||
finish() { | ||
}, | ||
finish() { }, | ||
parser: {} | ||
@@ -236,3 +233,3 @@ }; | ||
this._pojo = this.parser[definitionName].value; | ||
}, | ||
} | ||
}; | ||
@@ -239,0 +236,0 @@ reader1.parser[definitionName] = _makeExtensionObjectReader(definitionName, definitionMap, readerMap); |
export * from "./xml2json"; | ||
export * from "./fragmentCloner"; | ||
export * from "./xml2JsonPojo"; | ||
export * from "./fragment_cloner"; | ||
export * from "./xml2json_pojo"; | ||
export * from "./fragment_cloner_parser"; | ||
export * from "./extension_object_parser"; | ||
export * from "./definition_parser"; |
@@ -14,6 +14,7 @@ "use strict"; | ||
__exportStar(require("./xml2json"), exports); | ||
__exportStar(require("./fragmentCloner"), exports); | ||
__exportStar(require("./xml2JsonPojo"), exports); | ||
__exportStar(require("./fragment_cloner"), exports); | ||
__exportStar(require("./xml2json_pojo"), exports); | ||
__exportStar(require("./fragment_cloner_parser"), exports); | ||
__exportStar(require("./extension_object_parser"), exports); | ||
__exportStar(require("./definition_parser"), exports); | ||
//# sourceMappingURL=index.js.map |
@@ -5,4 +5,13 @@ /** | ||
*/ | ||
declare const LtxParser: any; | ||
export declare type SimpleCallback = (err?: Error) => void; | ||
export declare type Callback<T> = (err?: Error | null, result?: T) => void; | ||
declare interface LtxParser { | ||
write(str: string): void; | ||
end(): void; | ||
on(eventName: "startElement", eventHandler: (name: string, attrs: XmlAttributes) => void): void; | ||
on(eventName: "endElement", eventHandler: (name: string) => void): void; | ||
on(eventName: "text", eventHandler: (name: string) => void): void; | ||
on(eventName: "close", eventHandler: () => void): void; | ||
} | ||
export interface Parser { | ||
@@ -154,9 +163,2 @@ [key: string]: ReaderState; | ||
/** | ||
* @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 | ||
@@ -175,3 +177,4 @@ * @param name | ||
_demote(cur_state: IReaderState, level: number, elementName: string): void; | ||
private _prepareParser; | ||
protected _prepareParser(callback: Callback<any> | SimpleCallback): LtxParser; | ||
} | ||
export {}; |
@@ -11,5 +11,3 @@ "use strict"; | ||
// tslint:disable:unified-signatures | ||
const fs = require("fs"); | ||
const node_opcua_assert_1 = require("node-opcua-assert"); | ||
const _ = require("underscore"); | ||
const LtxParser = require("ltx/lib/parsers/ltx.js"); | ||
@@ -58,11 +56,2 @@ /** | ||
if (!(options instanceof ReaderStateBase)) { | ||
const fields = _.keys(options); | ||
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 ! : " + | ||
invalid_fields.join(" - ") + | ||
" t =" + options.constructor.name); | ||
} | ||
this._init = options.init; | ||
@@ -223,4 +212,3 @@ this._finish = options.finish; | ||
this.current_state = null; | ||
const state = (options instanceof ReaderStateBase) | ||
? options : new ReaderState(options); | ||
const state = options instanceof ReaderStateBase ? options : new ReaderState(options); | ||
state.root = this; | ||
@@ -236,30 +224,2 @@ this.state_stack = []; | ||
} | ||
parse(xmlFile, callback) { | ||
if (!callback) { | ||
throw new Error("internal error"); | ||
} | ||
const readWholeFile = true; | ||
if (readWholeFile) { | ||
// slightly faster but require more memory .. | ||
fs.readFile(xmlFile, (err, data) => { | ||
if (err) { | ||
return callback(err); | ||
} | ||
if (data[0] === 0xEF && data[1] === 0xBB && data[2] === 0xBF) { | ||
data = data.slice(3); | ||
} | ||
const dataAsString = data.toString(); | ||
const parser = this._prepareParser(callback); | ||
parser.write(dataAsString); | ||
parser.end(); | ||
}); | ||
} | ||
else { | ||
const Bomstrip = require("bomstrip"); | ||
const parser = this._prepareParser(callback); | ||
fs.createReadStream(xmlFile, { autoClose: true, encoding: "utf8" }) | ||
.pipe(new Bomstrip()) | ||
.pipe(parser); | ||
} | ||
} | ||
/** | ||
@@ -337,6 +297,3 @@ * @param new_state | ||
const opts = { multiArgs: false }; | ||
Xml2Json.prototype.parseString = | ||
thenify.withCallback(Xml2Json.prototype.parseString, opts); | ||
Xml2Json.prototype.parse = | ||
thenify.withCallback(Xml2Json.prototype.parse, opts); | ||
Xml2Json.prototype.parseString = thenify.withCallback(Xml2Json.prototype.parseString, opts); | ||
//# sourceMappingURL=xml2json.js.map |
{ | ||
"name": "node-opcua-xml2json", | ||
"version": "2.15.0", | ||
"version": "2.16.0", | ||
"description": "pure nodejs OPCUA SDK - module -xml2json", | ||
@@ -15,6 +15,5 @@ "main": "dist/source/index.js", | ||
"ltx": "^2.9.2", | ||
"node-opcua-assert": "2.10.0", | ||
"node-opcua-debug": "2.10.0", | ||
"node-opcua-utils": "2.13.0", | ||
"underscore": "^1.10.2", | ||
"node-opcua-assert": "2.16.0", | ||
"node-opcua-debug": "2.16.0", | ||
"node-opcua-utils": "2.16.0", | ||
"xml-writer": "^1.7.0" | ||
@@ -41,3 +40,3 @@ }, | ||
"homepage": "http://node-opcua.github.io/", | ||
"gitHead": "d92ce0bdee1ed90c2b38ee20420f091fe9bf7365" | ||
"gitHead": "9fa7d50952a7c39ff6790888a2bd6f32fe46836d" | ||
} |
@@ -1,17 +0,5 @@ | ||
import { | ||
ReaderState, | ||
ReaderStateParserLike, | ||
ParserLike, | ||
XmlAttributes | ||
} from "./xml2json"; | ||
import { ReaderState, ReaderStateParserLike, ParserLike, XmlAttributes } from "./xml2json"; | ||
import { lowerFirstLetter } from "node-opcua-utils"; | ||
function BasicType_parser( | ||
dataType: string, | ||
parseFunc: (this: any, text: string) => any | ||
): ParserLike { | ||
function BasicType_parser(dataType: string, parseFunc: (this: any, text: string) => any): ParserLike { | ||
const r: ReaderStateParserLike = { | ||
@@ -30,6 +18,3 @@ init(this: any, elementName: string, attrs: XmlAttributes) { | ||
function ListOf( | ||
dataType: string, | ||
parseFunc: any | ||
) { | ||
function ListOf(dataType: string, parseFunc: any) { | ||
return { | ||
@@ -42,4 +27,3 @@ init(this: any) { | ||
finish(this: any) { | ||
}, | ||
finish(this: any) {}, | ||
endElement(this: any, elementName: string) { | ||
@@ -84,3 +68,3 @@ this.value.push(this.parser[elementName].value); | ||
finish(this: any) { | ||
this.value = (this.text.toLowerCase() === "true"); | ||
this.value = this.text.toLowerCase() === "true"; | ||
} | ||
@@ -161,5 +145,4 @@ }, | ||
parser: { LocalizedText: localizedTextReader }, | ||
finish(this: any) { | ||
}, | ||
endElement(this: any/*element*/) { | ||
finish(this: any) {}, | ||
endElement(this: any /*element*/) { | ||
this.value.push(this.parser.LocalizedText.value); | ||
@@ -208,3 +191,3 @@ } | ||
if (a instanceof Array) { | ||
return a.map(x => _clone(x)); | ||
return a.map((x) => _clone(x)); | ||
} | ||
@@ -214,8 +197,3 @@ return { ...a }; | ||
function _makeExtensionObjectReader( | ||
definitionName: string, | ||
definitionMap: DefinitionMap, | ||
readerMap: any | ||
): ReaderStateParserLike { | ||
function _makeExtensionObjectReader(definitionName: string, definitionMap: DefinitionMap, readerMap: any): ReaderStateParserLike { | ||
// is it a basic type ? | ||
@@ -236,4 +214,3 @@ if (partials.hasOwnProperty(definitionName)) { | ||
reader = { | ||
finish(this: any) { | ||
}, | ||
finish(this: any) {}, | ||
parser: {} | ||
@@ -243,3 +220,2 @@ }; | ||
for (const field of definition.fields) { | ||
const fieldReader = _makeExtensionObjectReader(field.dataType, definitionMap, readerMap); | ||
@@ -251,3 +227,2 @@ if (!fieldReader) { | ||
if (field.valueRank === undefined || field.valueRank === -1) { | ||
const parser = fieldReader; | ||
@@ -268,3 +243,2 @@ if (!parser) { | ||
} else if (field.valueRank === 1) { | ||
const listReader: ReaderStateParserLike = { | ||
@@ -302,9 +276,7 @@ init(this: any) { | ||
const reader1: ReaderStateParserLike = { | ||
parser: { | ||
}, | ||
parser: {}, | ||
endElement(this: any) { | ||
// console.log(this.parser[definitionName].value); | ||
this._pojo = this.parser[definitionName].value; | ||
}, | ||
} | ||
}; | ||
@@ -311,0 +283,0 @@ |
export * from "./xml2json"; | ||
export * from "./fragmentCloner"; | ||
export * from "./xml2JsonPojo"; | ||
export * from "./fragment_cloner"; | ||
export * from "./xml2json_pojo"; | ||
export * from "./fragment_cloner_parser"; | ||
export * from "./extension_object_parser"; | ||
export * from "./definition_parser"; |
@@ -10,6 +10,3 @@ /** | ||
import * as fs from "fs"; | ||
import { assert } from "node-opcua-assert"; | ||
import * as _ from "underscore"; | ||
const LtxParser = require("ltx/lib/parsers/ltx.js"); | ||
@@ -32,3 +29,2 @@ | ||
on(eventName: "close", eventHandler: () => void): void; | ||
} | ||
@@ -48,3 +44,2 @@ | ||
function _coerceParser(parser: ParserLike): Parser { | ||
for (const name of Object.keys(parser)) { | ||
@@ -67,9 +62,3 @@ if (parser[name] && !(parser[name] instanceof ReaderState)) { | ||
parser?: ParserLike; | ||
init?: ( | ||
this: IReaderState, | ||
name: string, | ||
attrs: XmlAttributes, | ||
parent: IReaderState, | ||
engine: Xml2Json | ||
) => void; | ||
init?: (this: IReaderState, name: string, attrs: XmlAttributes, parent: IReaderState, engine: Xml2Json) => void; | ||
finish?: (this: IReaderState) => void; | ||
@@ -86,9 +75,3 @@ startElement?: (this: IReaderState, name: string, attrs: XmlAttributes) => void; | ||
parser?: ParserLike; | ||
init?: ( | ||
this: any, | ||
name: string, | ||
attrs: XmlAttributes, | ||
parent: IReaderState, | ||
engine: Xml2Json | ||
) => void; | ||
init?: (this: any, name: string, attrs: XmlAttributes, parent: IReaderState, engine: Xml2Json) => void; | ||
finish?: (this: any) => void; | ||
@@ -100,10 +83,4 @@ startElement?: (this: any, name: string, attrs: XmlAttributes) => void; | ||
export interface IReaderState { | ||
_on_init(elementName: string, attrs: XmlAttributes, parent: IReaderState, level: number, engine: Xml2Json): void; | ||
_on_init( | ||
elementName: string, | ||
attrs: XmlAttributes, | ||
parent: IReaderState, | ||
level: number, | ||
engine: Xml2Json): void; | ||
_on_finish(): void; | ||
@@ -120,7 +97,4 @@ | ||
export class ReaderStateBase { | ||
} | ||
export interface ReaderStateBase extends IReaderState { | ||
} | ||
export class ReaderStateBase {} | ||
export interface ReaderStateBase extends IReaderState {} | ||
/** | ||
@@ -137,7 +111,3 @@ * @class ReaderState | ||
export class ReaderState extends ReaderStateBase { | ||
public _init?: ( | ||
name: string, attrs: XmlAttributes, | ||
parent: IReaderState, engine: Xml2Json | ||
) => void; | ||
public _init?: (name: string, attrs: XmlAttributes, parent: IReaderState, engine: Xml2Json) => void; | ||
public _finish?: () => void; | ||
@@ -162,3 +132,2 @@ public _startElement?: (name: string, attrs: XmlAttributes) => void; | ||
constructor(options: ReaderStateParser | ReaderState) { | ||
super(); | ||
@@ -169,13 +138,2 @@ // ensure options object has only expected properties | ||
if (!(options instanceof ReaderStateBase)) { | ||
const fields = _.keys(options); | ||
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 ! : " + | ||
invalid_fields.join(" - ") + | ||
" t =" + options.constructor.name); | ||
} | ||
this._init = options.init; | ||
@@ -199,10 +157,3 @@ this._finish = options.finish; | ||
*/ | ||
public _on_init( | ||
elementName: string, | ||
attrs: XmlAttributes, | ||
parent: IReaderState, | ||
level: number, | ||
engine: Xml2Json | ||
) { | ||
public _on_init(elementName: string, attrs: XmlAttributes, parent: IReaderState, level: number, engine: Xml2Json) { | ||
this.name = elementName; | ||
@@ -234,3 +185,2 @@ this.parent = parent; | ||
public _on_startElement(level: number, elementName: string, attrs: XmlAttributes) { | ||
this.currentLevel = level; | ||
@@ -267,6 +217,4 @@ // console.log("wxxxx _on_startElement#" + this.name, elementName, this.currentLevel); | ||
if (this.level > level) { | ||
// we end a child element of this node | ||
this._on_endElement2(level, elementName); | ||
} else if (this.level === level) { | ||
@@ -356,3 +304,2 @@ // we received the end event of this node | ||
export class Xml2Json { | ||
public currentLevel: number = 0; | ||
@@ -363,4 +310,3 @@ private state_stack: any[] = []; | ||
constructor(options: ReaderStateParser) { | ||
const state = (options instanceof ReaderStateBase) | ||
? options as ReaderState : new ReaderState(options); | ||
const state = options instanceof ReaderStateBase ? (options as ReaderState) : new ReaderState(options); | ||
state.root = this; | ||
@@ -384,45 +330,3 @@ | ||
} | ||
/** | ||
* @method parse | ||
* @async | ||
* @param xmlFile - the name of the xml file to parse. | ||
*/ | ||
public parse(xmlFile: string): Promise<any>; | ||
public parse(xmlFile: string, callback: Callback<any> | SimpleCallback): void; | ||
public parse(xmlFile: string, callback?: Callback<any> | SimpleCallback): any { | ||
if (!callback) { | ||
throw new Error("internal error"); | ||
} | ||
const readWholeFile = true; | ||
if (readWholeFile) { | ||
// slightly faster but require more memory .. | ||
fs.readFile(xmlFile, (err: Error | null, data: Buffer) => { | ||
if (err) { | ||
return callback(err); | ||
} | ||
if (data[0] === 0xEF && data[1] === 0xBB && data[2] === 0xBF) { | ||
data = data.slice(3); | ||
} | ||
const dataAsString = data.toString(); | ||
const parser = this._prepareParser(callback); | ||
parser.write(dataAsString); | ||
parser.end(); | ||
}); | ||
} else { | ||
const Bomstrip = require("bomstrip"); | ||
const parser = this._prepareParser(callback); | ||
fs.createReadStream(xmlFile, { autoClose: true, encoding: "utf8" }) | ||
.pipe(new Bomstrip()) | ||
.pipe(parser); | ||
} | ||
} | ||
/** | ||
* @param new_state | ||
@@ -460,4 +364,3 @@ * @param name | ||
private _prepareParser(callback: Callback<any> | SimpleCallback): LtxParser { | ||
protected _prepareParser(callback: Callback<any> | SimpleCallback): LtxParser { | ||
assert(callback instanceof Function); | ||
@@ -492,8 +395,7 @@ const parser = new LtxParser(); | ||
}); | ||
parser.on("close", | ||
() => { | ||
if (callback) { | ||
(callback as any)(null, (this.current_state! as any)._pojo); | ||
} | ||
}); | ||
parser.on("close", () => { | ||
if (callback) { | ||
(callback as any)(null, (this.current_state! as any)._pojo); | ||
} | ||
}); | ||
return parser; | ||
@@ -503,9 +405,5 @@ } | ||
// tslint:disable:no-var-requires | ||
const thenify = require("thenify"); | ||
const opts = { multiArgs: false }; | ||
Xml2Json.prototype.parseString = | ||
thenify.withCallback(Xml2Json.prototype.parseString, opts); | ||
Xml2Json.prototype.parse = | ||
thenify.withCallback(Xml2Json.prototype.parse, opts); | ||
Xml2Json.prototype.parseString = thenify.withCallback(Xml2Json.prototype.parseString, opts); |
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
105943
6
44
2168
+ Addednode-opcua-assert@2.16.0(transitive)
+ Addednode-opcua-buffer-utils@2.16.0(transitive)
+ Addednode-opcua-debug@2.16.0(transitive)
+ Addednode-opcua-utils@2.16.0(transitive)
- Removedunderscore@^1.10.2
- Removednode-opcua-assert@2.10.0(transitive)
- Removednode-opcua-buffer-utils@2.10.0(transitive)
- Removednode-opcua-debug@2.10.0(transitive)
- Removednode-opcua-utils@2.13.0(transitive)
Updatednode-opcua-assert@2.16.0
Updatednode-opcua-debug@2.16.0
Updatednode-opcua-utils@2.16.0