1
- using System ;
2
- using System . Collections . Generic ;
3
- using System . Linq ;
4
- using System . Xml . Linq ;
5
-
6
- namespace ApiExtractor . Extraction ;
7
-
8
- public class EventReader {
9
-
10
- private readonly ExtractedDocumentation docs ;
11
-
12
- public EventReader ( ExtractedDocumentation docs ) {
13
- this . docs = docs ;
14
- }
15
-
16
- public void parseEventXml ( string xmlFilename ) {
17
- XDocument doc = XDocument . Load ( xmlFilename ) ;
18
-
19
- foreach ( XElement secondLevelElements in doc . Root ! . Elements ( ) ) {
20
- visit ( secondLevelElements , new [ ] { "xEvent" } ) ;
21
- }
22
-
23
- Console . WriteLine ( $ "Parsed { docs . events . Count : N0} xEvents from XML") ;
24
- }
25
-
26
- private void visit ( XElement el , IList < string > path ) {
27
- path = path . Append ( el . Name . LocalName ) . ToList ( ) ;
28
-
29
- if ( attributeEquals ( el , "event" , "True" ) ) {
30
- DocXEvent xEvent = new ( ) {
31
- name = path ,
32
- access = EventAccessParser . parse ( el . Attribute ( "access" ) ! . Value ) ,
33
- requiresUserRole = ( el . Attribute ( "role" ) ? . Value . Split ( ";" ) . Select ( rawRole => Enum . Parse < UserRole > ( rawRole , true ) ) ?? Enumerable . Empty < UserRole > ( ) ) . ToHashSet ( ) ,
34
- } ;
35
-
36
- if ( xEvent . access == EventAccess . PUBLIC_API ) {
37
- docs . events . Add ( xEvent ) ;
38
-
39
- foreach ( XElement childEl in el . Elements ( ) ) {
40
- visit ( childEl , xEvent ) ;
41
- }
42
- }
43
- } else {
44
- foreach ( XElement childEl in el . Elements ( ) ) {
45
- visit ( childEl , path ) ;
46
- }
47
- }
48
- }
49
-
50
- private static void visit ( XElement el , IEventParent parent ) {
51
- IList < string > name = parent . name . Append ( el . Attribute ( "className" ) ? . Value ?? el . Name . LocalName ) . ToList ( ) ;
52
- bool required = ! attributeEquals ( el , "optional" , "True" ) ;
53
-
54
- if ( attributeEquals ( el , "type" , "literal" ) && el . HasElements ) {
55
- parent . children . Add ( new EnumChild {
56
- name = name ,
57
- required = required ,
58
- possibleValues = el . Elements ( "Value" ) . Select ( valueEl => new EnumValue ( valueEl . Value ) ) . ToHashSet ( )
59
- } ) ;
60
- } else if ( attributeEquals ( el , "type" , "string" ) || ( attributeEquals ( el , "type" , "literal" ) && ! el . HasElements ) ) {
61
- parent . children . Add ( new StringChild {
62
- name = name ,
63
- required = required
64
- } ) ;
65
- } else if ( attributeEquals ( el , "type" , "int" ) ) {
66
- parent . children . Add ( new IntChild {
67
- name = name ,
68
- required = required ,
69
- implicitAnonymousSingleton = attributeEquals ( el , "onlyTextNode" , "true" )
70
- } ) ;
71
- } else if ( attributeEquals ( el , "multiple" , "True" ) ) {
72
- ListContainer listContainer = new ( ) { name = name } ;
73
- parent . children . Add ( listContainer ) ;
74
-
75
- foreach ( XElement childEl in el . Elements ( ) ) {
76
- visit ( childEl , listContainer ) ;
77
- }
78
- } else /*if (attributeEquals(el, "basenode", "True"))*/ {
79
- ObjectContainer objectContainer = new ( ) {
80
- name = name ,
81
- required = required
82
- } ;
83
- parent . children . Add ( objectContainer ) ;
84
-
85
- foreach ( XElement childEl in el . Elements ( ) ) {
86
- visit ( childEl , objectContainer ) ;
87
- }
88
- }
89
- }
90
-
91
- private static bool attributeEquals ( XElement el , string attributeName , string ? comparisonValue ) {
92
- return string . Equals ( el . Attribute ( attributeName ) ? . Value , comparisonValue , StringComparison . InvariantCultureIgnoreCase ) ;
93
- }
94
-
1
+ using System ;
2
+ using System . Collections . Generic ;
3
+ using System . Linq ;
4
+ using System . Xml . Linq ;
5
+
6
+ namespace ApiExtractor . Extraction ;
7
+
8
+ public class EventReader {
9
+
10
+ private readonly ExtractedDocumentation docs ;
11
+
12
+ public EventReader ( ExtractedDocumentation docs ) {
13
+ this . docs = docs ;
14
+ }
15
+
16
+ public void parseEventXml ( string xmlFilename ) {
17
+ XDocument doc = XDocument . Load ( xmlFilename ) ;
18
+
19
+ foreach ( XElement secondLevelElements in doc . Root ! . Elements ( ) ) {
20
+ visit ( secondLevelElements , new [ ] { "xEvent" } ) ;
21
+ }
22
+
23
+ Console . WriteLine ( $ "Parsed { docs . events . Count : N0} xEvents from XML") ;
24
+ }
25
+
26
+ private void visit ( XElement el , IList < string > path ) {
27
+ path = path . Append ( el . Name . LocalName ) . ToList ( ) ;
28
+
29
+ if ( attributeEquals ( el , "event" , "True" ) ) {
30
+ DocXEvent xEvent = new ( ) {
31
+ name = path ,
32
+ access = EventAccessParser . parse ( el . Attribute ( "access" ) ! . Value ) ,
33
+ requiresUserRole = ( el . Attribute ( "role" ) ? . Value . Split ( ";" ) . Select ( rawRole => Enum . Parse < UserRole > ( rawRole , true ) ) ?? Enumerable . Empty < UserRole > ( ) ) . ToHashSet ( ) ,
34
+ } ;
35
+
36
+ if ( xEvent . access == EventAccess . PUBLIC_API ) {
37
+ docs . events . Add ( xEvent ) ;
38
+
39
+ foreach ( XElement childEl in el . Elements ( ) ) {
40
+ visit ( childEl , xEvent ) ;
41
+ }
42
+ }
43
+ } else {
44
+ foreach ( XElement childEl in el . Elements ( ) ) {
45
+ visit ( childEl , path ) ;
46
+ }
47
+ }
48
+ }
49
+
50
+ private static void visit ( XElement el , IEventParent parent ) {
51
+ IList < string > name = parent . name . Append ( el . Attribute ( "className" ) ? . Value ?? el . Name . LocalName ) . ToList ( ) ;
52
+ bool required = ! attributeEquals ( el , "optional" , "True" ) ;
53
+
54
+ if ( attributeEquals ( el , "type" , "literal" ) && el . HasElements ) {
55
+ parent . children . Add ( new EnumChild {
56
+ name = name ,
57
+ required = required ,
58
+ possibleValues = el . Elements ( "Value" ) . Select ( valueEl => new EnumValue ( valueEl . Value ) ) . ToHashSet ( )
59
+ } ) ;
60
+ } else if ( attributeEquals ( el , "type" , "string" ) || ( attributeEquals ( el , "type" , "literal" ) && ! el . HasElements ) ) {
61
+ parent . children . Add ( new StringChild {
62
+ name = name ,
63
+ required = required
64
+ } ) ;
65
+ } else if ( attributeEquals ( el , "type" , "int" ) ) {
66
+ parent . children . Add ( new IntChild {
67
+ name = name ,
68
+ required = required ,
69
+ implicitAnonymousSingleton = attributeEquals ( el , "onlyTextNode" , "true" )
70
+ } ) ;
71
+ } else if ( attributeEquals ( el , "multiple" , "True" ) ) {
72
+ ListContainer listContainer = new ( ) { name = name } ;
73
+ parent . children . Add ( listContainer ) ;
74
+
75
+ foreach ( XElement childEl in el . Elements ( ) ) {
76
+ visit ( childEl , listContainer ) ;
77
+ }
78
+ } else /*if (attributeEquals(el, "basenode", "True"))*/ {
79
+ ObjectContainer objectContainer = new ( ) {
80
+ name = name ,
81
+ required = required
82
+ } ;
83
+ parent . children . Add ( objectContainer ) ;
84
+
85
+ foreach ( XElement childEl in el . Elements ( ) ) {
86
+ visit ( childEl , objectContainer ) ;
87
+ }
88
+ }
89
+ }
90
+
91
+ private static bool attributeEquals ( XElement el , string attributeName , string ? comparisonValue ) {
92
+ return string . Equals ( el . Attribute ( attributeName ) ? . Value , comparisonValue , StringComparison . InvariantCultureIgnoreCase ) ;
93
+ }
94
+
95
95
}
0 commit comments