|
21 | 21 |
|
22 | 22 | import java.util.Stack; |
23 | 23 | import java.util.concurrent.*; |
| 24 | +import java.util.function.Function; |
24 | 25 |
|
25 | 26 | import javax.xml.transform.Source; |
26 | 27 | import javax.xml.transform.Transformer; |
@@ -52,44 +53,76 @@ private EventModelParser() { |
52 | 53 | private static SAXTransformerFactory tFactory |
53 | 54 | = (SAXTransformerFactory)SAXTransformerFactory.newInstance(); |
54 | 55 |
|
| 56 | + abstract static class EventModelParserWorker { |
| 57 | + public abstract EventModel parse(Source src) throws TransformerException; |
| 58 | + } |
| 59 | + |
| 60 | + private static EventModelParserWorker init() { |
| 61 | + String javaVersion = System.getProperty("java.version"); |
| 62 | + if ( javaVersion.compareTo( "25" ) < 0 ) { |
| 63 | + // preserve the classic fop parsing behaviour on java 24 or less |
| 64 | + return new EventModelParserWorker() { |
| 65 | + @Override |
| 66 | + public EventModel parse(Source src) throws TransformerException { |
| 67 | + Transformer transformer = tFactory.newTransformer(); |
| 68 | + transformer.setErrorListener(new DefaultErrorListener(LOG)); |
| 69 | + EventModel model = new EventModel(); |
| 70 | + SAXResult res = new SAXResult(getContentHandler(model)); |
| 71 | + transformer.transform(src, res); |
| 72 | + return model; |
| 73 | + } |
| 74 | + }; |
| 75 | + } else { |
| 76 | + return new EventModelParserWorker() { |
| 77 | + // new parsing behaviour on java 25+ (independent thread) |
| 78 | + @Override |
| 79 | + public EventModel parse(Source src) throws TransformerException { |
| 80 | + EventModel model = new EventModel(); |
| 81 | + // Create a single-thread executor for this parsing operation |
| 82 | + ExecutorService executor = Executors.newSingleThreadExecutor(); |
| 83 | + try ( AutoCloseable executorAc = () -> executor.shutdown() ) { |
| 84 | + // Submit the parsing task and wait for completion |
| 85 | + Future<Void> parsingTask = executor.submit(() -> { |
| 86 | + try { |
| 87 | + Transformer transformer = tFactory.newTransformer(); |
| 88 | + transformer.setErrorListener(new DefaultErrorListener(LOG)); |
| 89 | + SAXResult res = new SAXResult(getContentHandler(model)); |
| 90 | + transformer.transform(src, res); |
| 91 | + return null; |
| 92 | + } catch (TransformerException e) { |
| 93 | + throw new RuntimeException(e); |
| 94 | + } |
| 95 | + }); |
| 96 | + // Block until parsing is complete |
| 97 | + parsingTask.get(); |
| 98 | + return model; |
| 99 | + } catch (ExecutionException e) { |
| 100 | + if (e.getCause() instanceof RuntimeException && |
| 101 | + e.getCause().getCause() instanceof TransformerException) { |
| 102 | + throw (TransformerException) e.getCause().getCause(); |
| 103 | + } |
| 104 | + throw new TransformerException(e.getCause()); |
| 105 | + } catch (InterruptedException e) { |
| 106 | + Thread.currentThread().interrupt(); |
| 107 | + throw new TransformerException("Parsing was interrupted", e); |
| 108 | + } catch (Exception e) { |
| 109 | + throw new TransformerException("Parsing generic error", e); |
| 110 | + } |
| 111 | + } |
| 112 | + }; |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + private static final EventModelParserWorker PARSER_WORKER = init(); |
| 117 | + |
55 | 118 | /** |
56 | 119 | * Parses an event model file into an EventModel instance. |
57 | 120 | * @param src the Source instance pointing to the XML file |
58 | 121 | * @return the created event model structure |
59 | 122 | * @throws TransformerException if an error occurs while parsing the XML file |
60 | 123 | */ |
61 | 124 | public static EventModel parse(Source src) throws TransformerException { |
62 | | - EventModel model = new EventModel(); |
63 | | - // Create a single-thread executor for this parsing operation |
64 | | - ExecutorService executor = Executors.newSingleThreadExecutor(); |
65 | | - try ( AutoCloseable executorAc = () -> executor.shutdown() ) { |
66 | | - // Submit the parsing task and wait for completion |
67 | | - Future<Void> parsingTask = executor.submit(() -> { |
68 | | - try { |
69 | | - Transformer transformer = tFactory.newTransformer(); |
70 | | - transformer.setErrorListener(new DefaultErrorListener(LOG)); |
71 | | - SAXResult res = new SAXResult(getContentHandler(model)); |
72 | | - transformer.transform(src, res); |
73 | | - return null; |
74 | | - } catch (TransformerException e) { |
75 | | - throw new RuntimeException(e); |
76 | | - } |
77 | | - }); |
78 | | - // Block until parsing is complete |
79 | | - parsingTask.get(); |
80 | | - return model; |
81 | | - } catch (ExecutionException e) { |
82 | | - if (e.getCause() instanceof RuntimeException && |
83 | | - e.getCause().getCause() instanceof TransformerException) { |
84 | | - throw (TransformerException) e.getCause().getCause(); |
85 | | - } |
86 | | - throw new TransformerException(e.getCause()); |
87 | | - } catch (InterruptedException e) { |
88 | | - Thread.currentThread().interrupt(); |
89 | | - throw new TransformerException("Parsing was interrupted", e); |
90 | | - } catch (Exception e) { |
91 | | - throw new TransformerException("Parsing generic error", e); |
92 | | - } |
| 125 | + return PARSER_WORKER.parse(src); |
93 | 126 | } |
94 | 127 |
|
95 | 128 | /** |
|
0 commit comments