Skip to content

Commit 102fc94

Browse files
committed
OpParser: split parsing logic to standalone method
1 parent c54cfdd commit 102fc94

File tree

1 file changed

+28
-18
lines changed
  • scijava-ops-ext-parser/src/main/java/org/scijava/ops/parser

1 file changed

+28
-18
lines changed

scijava-ops-ext-parser/src/main/java/org/scijava/ops/parser/OpParser.java

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,28 @@ public static void main(String... args) throws ClassNotFoundException {
7979
+ "that class to op names.");
8080
}
8181

82-
Yaml yaml = new Yaml();
82+
// Parse the config yaml to an Op yaml representation
83+
final String opYaml = parseOpDocument(args[0]);
84+
85+
// Write out the results
86+
try {
87+
outputYamlDoc(opYaml);
88+
}
89+
catch (IOException e) {
90+
throw new RuntimeException(e);
91+
}
92+
}
93+
94+
/**
95+
* Performs the actual parsing logic
96+
*
97+
* @param inputYamlPath YAML file to parse
98+
* @return A string representation of the resulting YAML Op configuration
99+
*/
100+
public static String parseOpDocument(String inputYamlPath)
101+
throws ClassNotFoundException
102+
{
103+
Yaml configYaml = new Yaml();
83104
String namespace = null;
84105
List<OpData> ops = new ArrayList<>();
85106
Set<String> containerClasses = new HashSet<>();
@@ -88,8 +109,8 @@ public static void main(String... args) throws ClassNotFoundException {
88109
Map<String, Object> opsYaml;
89110

90111
// -- Parse the yaml --
91-
try (FileReader reader = new FileReader(args[0])) {
92-
opsYaml = yaml.load(reader);
112+
try (FileReader reader = new FileReader(inputYamlPath)) {
113+
opsYaml = configYaml.load(reader);
93114
}
94115
catch (IOException e) {
95116
throw new RuntimeException(e);
@@ -146,13 +167,8 @@ public static void main(String... args) throws ClassNotFoundException {
146167
}
147168
}
148169

149-
// Write out the results
150-
try {
151-
outputYamlDoc(ops);
152-
}
153-
catch (IOException e) {
154-
throw new RuntimeException(e);
155-
}
170+
var data = ops.stream().map(OpData::dumpData).collect(Collectors.toList());
171+
return new Yaml().dump(data);
156172
}
157173

158174
/**
@@ -234,16 +250,10 @@ private static Multimap<String, Method> makeMultimap(final Class<?> clazz) {
234250
/**
235251
* Helper method to write an {@link OpData} list to an {@code op.yaml} file.
236252
*/
237-
private static void outputYamlDoc(List<OpData> collectedData)
238-
throws IOException
239-
{
240-
var data = collectedData.stream().map(OpData::dumpData).collect(Collectors
241-
.toList());
242-
Yaml yaml = new Yaml();
243-
String doc = yaml.dump(data);
253+
private static void outputYamlDoc(String opYaml) throws IOException {
244254
File f = new File("op.yaml");
245255
try (OutputStream os = new FileOutputStream(f)) {
246-
os.write(doc.getBytes(UTF_8));
256+
os.write(opYaml.getBytes(UTF_8));
247257
}
248258
}
249259
}

0 commit comments

Comments
 (0)