44 * ------------------------------------------------------------------------------------------ */
55'use strict' ;
66import { Stream } from 'stream' ;
7- import * as Xml2Object from 'xml2object' ;
87import * as jsonAst from 'json-to-ast' ;
9- import { IPosition , IKeyValueEntry , KeyValueEntry , Variant , ValueType } from './types' ;
8+ import { IPosition , IKeyValueEntry , KeyValueEntry , Variant , ValueType , IDependency , IPositionedString , IDependencyCollector , Dependency } from './types' ;
109import { stream_from_string , getGoLangImportsCmd } from './utils' ;
1110import { config } from './config' ;
1211import { exec } from 'child_process' ;
12+ import { parse , DocumentCstNode } from "@xml-tools/parser" ;
13+ import { buildAst , accept , XMLElement , XMLDocument } from "@xml-tools/ast" ;
1314
1415/* Please note :: There was issue with semverRegex usage in the code. During run time, it extracts
1516 * version with 'v' prefix, but this is not be behavior of semver in CLI and test environment.
@@ -20,40 +21,6 @@ function semVerRegExp(line: string): RegExpExecArray {
2021 return regExp . exec ( line ) ;
2122}
2223
23- /* String value with position */
24- interface IPositionedString {
25- value : string ;
26- position : IPosition ;
27- }
28-
29- /* Dependency specification */
30- interface IDependency {
31- name : IPositionedString ;
32- version : IPositionedString ;
33- }
34-
35- /* Dependency collector interface */
36- interface IDependencyCollector {
37- classes : Array < string > ;
38- collect ( contents : string ) : Promise < Array < IDependency > > ;
39- }
40-
41- /* Dependency class that can be created from `IKeyValueEntry` */
42- class Dependency implements IDependency {
43- name : IPositionedString ;
44- version : IPositionedString ;
45- constructor ( dependency : IKeyValueEntry ) {
46- this . name = {
47- value : dependency . key ,
48- position : dependency . key_position
49- } ;
50- this . version = {
51- value : dependency . value . object ,
52- position : dependency . value_position
53- } ;
54- }
55- }
56-
5724class NaivePyParser {
5825 constructor ( contents : string ) {
5926 this . dependencies = NaivePyParser . parseDependencies ( contents ) ;
@@ -245,87 +212,6 @@ class GomodDependencyCollector implements IDependencyCollector {
245212
246213}
247214
248- class NaivePomXmlSaxParser {
249- constructor ( stream : Stream ) {
250- this . stream = stream ;
251- this . parser = this . createParser ( ) ;
252- }
253-
254- stream : Stream ;
255- parser : Xml2Object ;
256- dependencies : Array < IDependency > = [ ] ;
257- isDependency : boolean = false ;
258- versionStartLine : number = 0 ;
259- versionStartColumn : number = 0 ;
260-
261- createParser ( ) : Xml2Object {
262- let parser = new Xml2Object ( [ "dependency" ] , { strict : true , trackPosition : true } ) ;
263- let deps = this . dependencies ;
264- let versionLine = this . versionStartLine ;
265- let versionColumn = this . versionStartColumn ;
266-
267- parser . on ( "object" , function ( name , obj ) {
268- if ( obj . hasOwnProperty ( "groupId" ) && obj . hasOwnProperty ( "artifactId" ) && obj . hasOwnProperty ( "version" ) &&
269- ( ! obj . hasOwnProperty ( "scope" ) || ( obj . hasOwnProperty ( "scope" ) && obj [ "scope" ] != "test" ) ) ) {
270- let ga = `${ obj [ "groupId" ] } :${ obj [ "artifactId" ] } ` ;
271- let entry : IKeyValueEntry = new KeyValueEntry ( ga , { line : 0 , column : 0 } ) ;
272- entry . value = new Variant ( ValueType . String , obj [ "version" ] ) ;
273- entry . value_position = { line : versionLine , column : versionColumn } ;
274- let dep : IDependency = new Dependency ( entry ) ;
275- deps . push ( dep )
276- }
277- } ) ;
278- parser . saxStream . on ( "opentag" , function ( node ) {
279- if ( node . name == "dependency" ) {
280- this . isDependency = true ;
281- }
282- if ( this . isDependency && node . name == "version" ) {
283- versionLine = parser . saxStream . _parser . line + 1 ;
284- versionColumn = parser . saxStream . _parser . column + 1 ;
285- }
286- } ) ;
287- parser . saxStream . on ( "closetag" , function ( nodeName ) {
288- // TODO: nested deps!
289- if ( nodeName == "dependency" ) {
290- this . isDependency = false ;
291- }
292- } ) ;
293- parser . on ( "error" , function ( e ) {
294- // the XML document doesn't have to be well-formed, that's fine
295- parser . error = null ;
296- } ) ;
297- parser . on ( "end" , function ( ) {
298- // the XML document doesn't have to be well-formed, that's fine
299- // parser.error = null;
300- this . dependencies = deps ;
301- } ) ;
302- return parser
303- }
304-
305- async parse ( ) {
306- return new Promise ( resolve => {
307- this . stream . pipe ( this . parser . saxStream ) . on ( 'end' , ( data ) => {
308- resolve ( this . dependencies ) ;
309- } ) ;
310- } ) ;
311-
312- }
313- }
314-
315- class PomXmlDependencyCollector implements IDependencyCollector {
316- constructor ( public classes : Array < string > = [ "dependencies" ] ) { }
317-
318- async collect ( contents : string ) : Promise < Array < IDependency > > {
319- const file = stream_from_string ( contents ) ;
320- let parser = new NaivePomXmlSaxParser ( file ) ;
321- let dependencies ;
322- await parser . parse ( ) . then ( data => {
323- dependencies = data ;
324- } ) ;
325- return dependencies || [ ] ;
326- }
327- }
328-
329215class PackageJsonCollector implements IDependencyCollector {
330216 constructor ( public classes : Array < string > = [ "dependencies" ] ) { }
331217
@@ -343,4 +229,4 @@ class PackageJsonCollector implements IDependencyCollector {
343229 }
344230}
345231
346- export { IDependencyCollector , PackageJsonCollector , PomXmlDependencyCollector , ReqDependencyCollector , GomodDependencyCollector , IPositionedString , IDependency } ;
232+ export { IDependencyCollector , PackageJsonCollector , ReqDependencyCollector , GomodDependencyCollector , IPositionedString , IDependency } ;
0 commit comments