31
31
import com .google .gson .JsonParseException ;
32
32
import com .google .gson .annotations .SerializedName ;
33
33
34
+ import java .io .BufferedReader ;
34
35
import java .io .IOException ;
35
36
import java .io .InputStream ;
36
37
import java .io .InputStreamReader ;
37
38
import java .io .Reader ;
39
+ import java .io .StringReader ;
38
40
import java .lang .reflect .Constructor ;
39
41
import java .lang .reflect .Method ;
40
42
import java .lang .reflect .Type ;
49
51
import java .util .Map .Entry ;
50
52
import java .util .Objects ;
51
53
import java .util .concurrent .ConcurrentHashMap ;
54
+ import java .util .stream .Collectors ;
52
55
53
56
/**
54
57
* This class is for parsing the JSON or YAML definition for a {@link Workflow}.
@@ -93,16 +96,21 @@ public static WorkflowDefinition parse(Path path) throws IOException {
93
96
/**
94
97
* Parses a new {@link WorkflowDefinition} from an input stream.
95
98
*
96
- * @param name the workflow name
99
+ * @param name the workflow name (null for no name)
97
100
* @param uri the uri of the file
98
101
* @return the parsed {@link WorkflowDefinition}
99
102
* @throws IOException if read from uri failed
100
103
*/
101
104
public static WorkflowDefinition parse (String name , URI uri ) throws IOException {
105
+ return parse (name , uri , null );
106
+ }
107
+
108
+ static WorkflowDefinition parse (String name , URI uri , Map <String , String > templateReplacements )
109
+ throws IOException {
102
110
String type = FilenameUtils .getFileExtension (Objects .requireNonNull (uri .toString ()));
103
111
try (InputStream is = uri .toURL ().openStream ();
104
112
Reader reader = new InputStreamReader (is , StandardCharsets .UTF_8 )) {
105
- WorkflowDefinition wd = parse (type , reader );
113
+ WorkflowDefinition wd = parse (type , reader , templateReplacements );
106
114
if (name != null ) {
107
115
wd .name = name ;
108
116
}
@@ -113,7 +121,26 @@ public static WorkflowDefinition parse(String name, URI uri) throws IOException
113
121
}
114
122
}
115
123
116
- private static WorkflowDefinition parse (String type , Reader input ) {
124
+ private static WorkflowDefinition parse (
125
+ String type , Reader input , Map <String , String > templateReplacements ) {
126
+ if (templateReplacements != null ) {
127
+ String updatedInput =
128
+ new BufferedReader (input )
129
+ .lines ()
130
+ .map (
131
+ l -> {
132
+ for (Entry <String , String > replacement :
133
+ templateReplacements .entrySet ()) {
134
+ l =
135
+ l .replace (
136
+ "$" + replacement .getKey (),
137
+ replacement .getValue ());
138
+ }
139
+ return l ;
140
+ })
141
+ .collect (Collectors .joining ("\n " ));
142
+ input = new StringReader (updatedInput );
143
+ }
117
144
if ("yml" .equalsIgnoreCase (type ) || "yaml" .equalsIgnoreCase (type )) {
118
145
try {
119
146
ClassLoader cl = ClassLoaderUtils .getContextClassLoader ();
0 commit comments