parse rawXML into document only once

This commit is contained in:
Stefan Schmiedl
2019-08-05 15:04:51 +02:00
parent c74e21ca87
commit bbd22852d4

View File

@@ -68,6 +68,7 @@ public class ZUGFeRDImporter {
*/ */
private byte[] rawXML = null; private byte[] rawXML = null;
private String xmpString = null; // XMP metadata private String xmpString = null; // XMP metadata
private Document document;
public ZUGFeRDImporter(String pdfFilename) { public ZUGFeRDImporter(String pdfFilename) {
@@ -146,8 +147,7 @@ public class ZUGFeRDImporter {
// ByteArrayOutputStream(); // ByteArrayOutputStream();
// FileOutputStream fos = new FileOutputStream(file); // FileOutputStream fos = new FileOutputStream(file);
rawXML = embeddedFile.toByteArray(); setRawXML(embeddedFile.toByteArray());
setMeta(new String(rawXML));
// fos.write(embeddedFile.getByteArray()); // fos.write(embeddedFile.getByteArray());
// fos.close(); // fos.close();
@@ -176,15 +176,25 @@ public class ZUGFeRDImporter {
System.err.println(output); 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(); DocumentBuilderFactory xmlFact = DocumentBuilderFactory.newInstance();
xmlFact.setNamespaceAware(false); xmlFact.setNamespaceAware(false);
DocumentBuilder builder = xmlFact.newDocumentBuilder(); DocumentBuilder builder = xmlFact.newDocumentBuilder();
ByteArrayInputStream is = new ByteArrayInputStream(rawXML); ByteArrayInputStream is = new ByteArrayInputStream(rawXML);
is.skip(guessBOMSize(is)); is.skip(guessBOMSize(is));
Document doc = builder.parse(is); document = builder.parse(is);
//prettyPrint(doc);
return doc;
} }
/** /**
@@ -224,10 +234,7 @@ public class ZUGFeRDImporter {
XPathFactory xpathFact = XPathFactory.newInstance(); XPathFactory xpathFact = XPathFactory.newInstance();
XPath xpath = xpathFact.newXPath(); XPath xpath = xpathFact.newXPath();
result = xpath.evaluate(xpathStr, document); result = xpath.evaluate(xpathStr, document);
} catch (ParserConfigurationException e) { } catch (XPathExpressionException e) {
Logger.getLogger(ZUGFeRDImporter.class.getName()).log(Level.SEVERE, null, e);
throw new ZUGFeRDExportException(e);
} catch (IOException | SAXException | TransformerException | XPathExpressionException e) {
Logger.getLogger(ZUGFeRDImporter.class.getName()).log(Level.SEVERE, null, e); Logger.getLogger(ZUGFeRDImporter.class.getName()).log(Level.SEVERE, null, e);
throw new ZUGFeRDExportException(e); throw new ZUGFeRDExportException(e);
} }
@@ -332,8 +339,8 @@ public class ZUGFeRDImporter {
/** /**
* @param meta raw XML to be set * @param meta raw XML to be set
*/ */
public void setMeta(String meta) { public void setMeta(String meta) throws IOException {
this.rawXML = meta.getBytes(); setRawXML(meta.getBytes());
} }
/** /**