|
16 | 16 | package io.serverlessworkflow.api;
|
17 | 17 |
|
18 | 18 | import io.serverlessworkflow.api.types.Workflow;
|
19 |
| -import java.io.ByteArrayInputStream; |
20 | 19 | import java.io.FileNotFoundException;
|
21 | 20 | import java.io.IOException;
|
22 | 21 | import java.io.InputStream;
|
23 | 22 | import java.io.Reader;
|
24 |
| -import java.io.StringReader; |
25 | 23 | import java.nio.file.Files;
|
26 | 24 | import java.nio.file.Path;
|
27 | 25 |
|
28 | 26 | public class WorkflowReader {
|
29 | 27 |
|
30 | 28 | public static Workflow readWorkflow(InputStream input, WorkflowFormat format) throws IOException {
|
31 |
| - return format.mapper().readValue(input, Workflow.class); |
| 29 | + return defaultReader().read(input, format); |
32 | 30 | }
|
33 | 31 |
|
34 | 32 | public static Workflow readWorkflow(Reader input, WorkflowFormat format) throws IOException {
|
35 |
| - return format.mapper().readValue(input, Workflow.class); |
| 33 | + return defaultReader().read(input, format); |
36 | 34 | }
|
37 | 35 |
|
38 |
| - public static Workflow readWorkflow(Path path, WorkflowFormat format) throws IOException { |
39 |
| - return format.mapper().readValue(Files.readAllBytes(path), Workflow.class); |
| 36 | + public static Workflow readWorkflow(byte[] input, WorkflowFormat format) throws IOException { |
| 37 | + return defaultReader().read(input, format); |
40 | 38 | }
|
41 | 39 |
|
42 |
| - public static Workflow readWorkflow(byte[] content, WorkflowFormat format) throws IOException { |
43 |
| - try (InputStream input = new ByteArrayInputStream(content)) { |
44 |
| - return readWorkflow(input, format); |
45 |
| - } |
| 40 | + public static Workflow readWorkflow(Path path) throws IOException { |
| 41 | + return readWorkflow(defaultReader(), path, WorkflowFormat.fromPath(path)); |
| 42 | + } |
| 43 | + |
| 44 | + public static Workflow readWorkflow(Path path, WorkflowFormat format) throws IOException { |
| 45 | + return readWorkflow(defaultReader(), path, format); |
46 | 46 | }
|
47 | 47 |
|
48 |
| - public static Workflow readWorkflowFromString(String content, WorkflowFormat format) |
| 48 | + public static Workflow readWorkflowFromString(String input, WorkflowFormat format) |
49 | 49 | throws IOException {
|
50 |
| - try (Reader reader = new StringReader(content)) { |
51 |
| - return readWorkflow(reader, format); |
52 |
| - } |
| 50 | + return defaultReader().read(input, format); |
53 | 51 | }
|
54 | 52 |
|
55 | 53 | public static Workflow readWorkflowFromClasspath(String classpath) throws IOException {
|
| 54 | + return readWorkflowFromClasspath(defaultReader(), classpath); |
| 55 | + } |
| 56 | + |
| 57 | + public static Workflow readWorkflowFromClasspath( |
| 58 | + String classpath, ClassLoader cl, WorkflowFormat format) throws IOException { |
| 59 | + return readWorkflowFromClasspath(defaultReader(), classpath); |
| 60 | + } |
| 61 | + |
| 62 | + public static Workflow readWorkflow(WorkflowReaderOperations reader, Path path) |
| 63 | + throws IOException { |
| 64 | + return readWorkflow(reader, path, WorkflowFormat.fromPath(path)); |
| 65 | + } |
| 66 | + |
| 67 | + public static Workflow readWorkflow( |
| 68 | + WorkflowReaderOperations reader, Path path, WorkflowFormat format) throws IOException { |
| 69 | + return reader.read(Files.readAllBytes(path), format); |
| 70 | + } |
| 71 | + |
| 72 | + public static Workflow readWorkflowFromClasspath( |
| 73 | + WorkflowReaderOperations reader, String classpath) throws IOException { |
56 | 74 | return readWorkflowFromClasspath(
|
| 75 | + reader, |
57 | 76 | classpath,
|
58 | 77 | Thread.currentThread().getContextClassLoader(),
|
59 | 78 | WorkflowFormat.fromFileName(classpath));
|
60 | 79 | }
|
61 | 80 |
|
62 | 81 | public static Workflow readWorkflowFromClasspath(
|
63 |
| - String classpath, ClassLoader cl, WorkflowFormat format) throws IOException { |
| 82 | + WorkflowReaderOperations reader, String classpath, ClassLoader cl, WorkflowFormat format) |
| 83 | + throws IOException { |
64 | 84 | try (InputStream in = cl.getResourceAsStream(classpath)) {
|
65 | 85 | if (in == null) {
|
66 | 86 | throw new FileNotFoundException(classpath);
|
67 | 87 | }
|
68 |
| - return readWorkflow(in, format); |
| 88 | + return reader.read(in, format); |
69 | 89 | }
|
70 | 90 | }
|
71 | 91 |
|
| 92 | + public static WorkflowReaderOperations noValidation() { |
| 93 | + return NoValidationHolder.instance; |
| 94 | + } |
| 95 | + |
| 96 | + public static WorkflowReaderOperations validation() { |
| 97 | + return ValidationHolder.instance; |
| 98 | + } |
| 99 | + |
| 100 | + private static class NoValidationHolder { |
| 101 | + private static final WorkflowReaderOperations instance = new DirectReader(); |
| 102 | + } |
| 103 | + |
| 104 | + private static class ValidationHolder { |
| 105 | + private static final WorkflowReaderOperations instance = new ValidationReader(); |
| 106 | + } |
| 107 | + |
| 108 | + private static WorkflowReaderOperations defaultReader() { |
| 109 | + return NoValidationHolder.instance; |
| 110 | + } |
| 111 | + |
72 | 112 | private WorkflowReader() {}
|
73 | 113 | }
|
0 commit comments