From bbd22852d4708c6f8dedd10b4896ae4ece978031 Mon Sep 17 00:00:00 2001 From: Stefan Schmiedl Date: Mon, 5 Aug 2019 15:04:51 +0200 Subject: [PATCH] parse rawXML into document only once --- .../ZUGFeRD/ZUGFeRDImporter.java | 31 ++++++++++++------- 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/src/main/java/org/mustangproject/ZUGFeRD/ZUGFeRDImporter.java b/src/main/java/org/mustangproject/ZUGFeRD/ZUGFeRDImporter.java index a9f1c9c2..639230ed 100644 --- a/src/main/java/org/mustangproject/ZUGFeRD/ZUGFeRDImporter.java +++ b/src/main/java/org/mustangproject/ZUGFeRD/ZUGFeRDImporter.java @@ -68,6 +68,7 @@ public class ZUGFeRDImporter { */ private byte[] rawXML = null; private String xmpString = null; // XMP metadata + private Document document; public ZUGFeRDImporter(String pdfFilename) { @@ -146,8 +147,7 @@ public class ZUGFeRDImporter { // ByteArrayOutputStream(); // FileOutputStream fos = new FileOutputStream(file); - rawXML = embeddedFile.toByteArray(); - setMeta(new String(rawXML)); + setRawXML(embeddedFile.toByteArray()); // fos.write(embeddedFile.getByteArray()); // fos.close(); @@ -176,15 +176,25 @@ public class ZUGFeRDImporter { System.err.println(output); } - private Document getDocument() throws ParserConfigurationException, IOException, SAXException, TransformerException { + private Document getDocument() { return document; } + + public void setRawXML(byte[] rawXML) throws IOException { + this.rawXML = rawXML; + try { + setDocument(); + } catch (ParserConfigurationException | SAXException e) { + Logger.getLogger(ZUGFeRDImporter.class.getName()).log(Level.SEVERE, null, e); + throw new ZUGFeRDExportException(e); + } + } + + private void setDocument() throws ParserConfigurationException, IOException, SAXException { DocumentBuilderFactory xmlFact = DocumentBuilderFactory.newInstance(); xmlFact.setNamespaceAware(false); DocumentBuilder builder = xmlFact.newDocumentBuilder(); ByteArrayInputStream is = new ByteArrayInputStream(rawXML); is.skip(guessBOMSize(is)); - Document doc = builder.parse(is); - //prettyPrint(doc); - return doc; + document = builder.parse(is); } /** @@ -224,10 +234,7 @@ public class ZUGFeRDImporter { XPathFactory xpathFact = XPathFactory.newInstance(); XPath xpath = xpathFact.newXPath(); result = xpath.evaluate(xpathStr, document); - } catch (ParserConfigurationException e) { - Logger.getLogger(ZUGFeRDImporter.class.getName()).log(Level.SEVERE, null, e); - throw new ZUGFeRDExportException(e); - } catch (IOException | SAXException | TransformerException | XPathExpressionException e) { + } catch (XPathExpressionException e) { Logger.getLogger(ZUGFeRDImporter.class.getName()).log(Level.SEVERE, null, e); throw new ZUGFeRDExportException(e); } @@ -332,8 +339,8 @@ public class ZUGFeRDImporter { /** * @param meta raw XML to be set */ - public void setMeta(String meta) { - this.rawXML = meta.getBytes(); + public void setMeta(String meta) throws IOException { + setRawXML(meta.getBytes()); } /**