|
20 | 20 | package org.apache.fop.events.model; |
21 | 21 |
|
22 | 22 | import java.util.Stack; |
| 23 | +import java.util.concurrent.*; |
23 | 24 |
|
24 | 25 | import javax.xml.transform.Source; |
25 | 26 | import javax.xml.transform.Transformer; |
@@ -57,16 +58,38 @@ private EventModelParser() { |
57 | 58 | * @return the created event model structure |
58 | 59 | * @throws TransformerException if an error occurs while parsing the XML file |
59 | 60 | */ |
60 | | - public static EventModel parse(Source src) |
61 | | - throws TransformerException { |
62 | | - Transformer transformer = tFactory.newTransformer(); |
63 | | - transformer.setErrorListener(new DefaultErrorListener(LOG)); |
64 | | - |
| 61 | + public static EventModel parse(Source src) throws TransformerException { |
65 | 62 | EventModel model = new EventModel(); |
66 | | - SAXResult res = new SAXResult(getContentHandler(model)); |
67 | | - |
68 | | - transformer.transform(src, res); |
69 | | - return model; |
| 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 | + } |
70 | 93 | } |
71 | 94 |
|
72 | 95 | /** |
|
0 commit comments