21
21
import org .openrewrite .SourceFile ;
22
22
import org .openrewrite .hcl .HclParser ;
23
23
import org .openrewrite .json .JsonParser ;
24
+ import org .openrewrite .marker .Marker ;
25
+ import org .openrewrite .marker .Markers ;
24
26
import org .openrewrite .properties .PropertiesParser ;
25
27
import org .openrewrite .protobuf .ProtoParser ;
26
28
import org .openrewrite .text .PlainTextParser ;
27
29
import org .openrewrite .tree .ParsingExecutionContextView ;
28
30
import org .openrewrite .xml .XmlParser ;
31
+ import org .openrewrite .xml .tree .Xml ;
29
32
import org .openrewrite .yaml .YamlParser ;
30
33
import org .springframework .context .ApplicationEventPublisher ;
31
34
import org .springframework .core .io .Resource ;
@@ -52,37 +55,10 @@ public class ResourceParser {
52
55
private final ResourceFilter resourceFilter ;
53
56
private final ApplicationEventPublisher eventPublisher ;
54
57
55
- public List <SourceFile > parse (Path baseDir , Set <Path > resourcePaths , List <Resource > resources ) {
56
- ParsingExecutionContextView ctx = ParsingExecutionContextView .view (new RewriteExecutionContext (eventPublisher ));
57
- ctx .setParsingListener ((input , sourceFile ) -> eventPublisher .publishEvent (new StartedScanningProjectResourceEvent (sourceFile .getSourcePath ())));
58
-
59
- List <Resource > relevantResources = resourceFilter .filter (resources , baseDir , resourcePaths );
60
-
61
- HashMap <Parser <? extends SourceFile >, List <Parser .Input >> parserAndParserInputMappings = new LinkedHashMap ();
62
- parserAndParserInputMappings .put (jsonParser , new ArrayList <>());
63
- parserAndParserInputMappings .put (xmlParser , new ArrayList <>());
64
- parserAndParserInputMappings .put (yamlParser , new ArrayList <>());
65
- parserAndParserInputMappings .put (propertiesParser , new ArrayList <>());
66
- parserAndParserInputMappings .put (new ProtoParser (), new ArrayList <>());
67
- parserAndParserInputMappings .put (HclParser .builder ().build (), new ArrayList <>());
68
- parserAndParserInputMappings .put (plainTextParser , new ArrayList <>());
69
-
70
- List <Parser .Input > parserInputs = createParserInputs (relevantResources );
71
-
72
- parserInputs .forEach (r -> {
73
- Parser parser = parserAndParserInputMappings .keySet ().stream ()
74
- .filter (p -> p .accept (r ))
75
- .findFirst ()
76
- .orElseThrow (() -> new RuntimeException ("Could not find matching parser for " + r .getPath ()));
77
-
78
- parserAndParserInputMappings .get (parser ).add (r );
79
- });
80
-
81
- return parserAndParserInputMappings .entrySet ().stream ()
82
- .map (e -> e .getKey ().parseInputs (e .getValue (), baseDir , ctx ))
83
- .flatMap (List ::stream )
84
- .collect (Collectors .toList ());
85
-
58
+ List <Resource > filter (Path projectDirectory , Set <Path > resourcePaths , List <Resource > resources , Path relativeModuleDir ) {
59
+ Path comparingPath = relativeModuleDir != null ? projectDirectory .resolve (relativeModuleDir ) : projectDirectory ;
60
+ List <Resource > relevantResources = resourceFilter .filter (resources , comparingPath , resourcePaths );
61
+ return relevantResources ;
86
62
}
87
63
88
64
private List <Parser .Input > createParserInputs (List <Resource > relevantResources ) {
@@ -114,6 +90,42 @@ private InputStream getInputStream(Resource r) {
114
90
}
115
91
}
116
92
93
+ public List <SourceFile > parse (Path baseDir , List <Resource > relevantResources , List <Marker > markers ) {
94
+ List <Parser .Input > parserInputs = createParserInputs (relevantResources );
95
+
96
+ HashMap <Parser <? extends SourceFile >, List <Parser .Input >> parserAndParserInputMappings = new LinkedHashMap ();
97
+ parserAndParserInputMappings .put (jsonParser , new ArrayList <>());
98
+ parserAndParserInputMappings .put (xmlParser , new ArrayList <>());
99
+ parserAndParserInputMappings .put (yamlParser , new ArrayList <>());
100
+ parserAndParserInputMappings .put (propertiesParser , new ArrayList <>());
101
+ parserAndParserInputMappings .put (new ProtoParser (), new ArrayList <>());
102
+ parserAndParserInputMappings .put (HclParser .builder ().build (), new ArrayList <>());
103
+ parserAndParserInputMappings .put (plainTextParser , new ArrayList <>());
104
+
105
+ parserInputs .forEach (r -> {
106
+ Parser parser = parserAndParserInputMappings .keySet ().stream ()
107
+ .filter (p -> p .accept (r ))
108
+ .findFirst ()
109
+ .orElseThrow (() -> new RuntimeException ("Could not find matching parser for " + r .getPath ()));
110
+
111
+ parserAndParserInputMappings .get (parser ).add (r );
112
+ });
113
+
114
+ ParsingExecutionContextView ctx = ParsingExecutionContextView .view (new RewriteExecutionContext (eventPublisher ));
115
+ ctx .setParsingListener ((input , sourceFile ) -> eventPublisher .publishEvent (new StartedScanningProjectResourceEvent (sourceFile .getSourcePath ())));
116
+
117
+ return parserAndParserInputMappings .entrySet ().stream ()
118
+ .map (e -> e .getKey ().parseInputs (e .getValue (), baseDir , ctx ))
119
+ .flatMap (List ::stream )
120
+ .map (e -> addMarkers (e , markers ))
121
+ .collect (Collectors .toList ());
122
+
123
+ }
124
+
125
+ private SourceFile addMarkers (SourceFile e , List <Marker > markers ) {
126
+ return e .withMarkers (Markers .build (markers ));
127
+ }
128
+
117
129
@ Component
118
130
public static class ResourceFilter {
119
131
private List <Resource > filter (List <Resource > resources , Path moduleDir , Set <Path > searchDirs ) {
0 commit comments