Cleansing

This commit is contained in:
Philip Helger
2024-07-09 19:32:54 +02:00
parent 097ad7a0a0
commit ad8fc7e7ff
2 changed files with 13 additions and 25 deletions

View File

@@ -519,10 +519,11 @@ public class Main {
ensureFileNotExists(outName); ensureFileNotExists(outName);
// All params are good! continue... // All params are good! continue...
ZUGFeRDExporterFromA1 ze = new ZUGFeRDExporterFromA1().convertOnly().load(pdfName); try (ZUGFeRDExporterFromA1 ze = new ZUGFeRDExporterFromA1()) {
ze.convertOnly().load(pdfName);
ze.export(outName); ze.export(outName);
System.out.println("Written to " + outName); System.out.println("Written to " + outName);
}
} }
private static void performExtract(String pdfName, String xmlName) throws IOException { private static void performExtract(String pdfName, String xmlName) throws IOException {
@@ -842,13 +843,7 @@ public class Main {
} else { } else {
zvi.toPDF(sourceName, outName); zvi.toPDF(sourceName, outName);
} }
} catch (FileNotFoundException e) { } catch (TransformerException | IOException e) {
LOGGER.error(e.getMessage(), e);
} catch (UnsupportedEncodingException e) {
LOGGER.error(e.getMessage(), e);
} catch (TransformerException e) {
LOGGER.error(e.getMessage(), e);
} catch (IOException e) {
LOGGER.error(e.getMessage(), e); LOGGER.error(e.getMessage(), e);
} }
System.out.println("Written to " + outName); System.out.println("Written to " + outName);
@@ -876,11 +871,8 @@ public class Main {
* @throws Exception e.g. if the specified resource does not exist at the specified location * @throws Exception e.g. if the specified resource does not exist at the specified location
*/ */
static public String ExportResource(String resourceName) throws Exception { static public String ExportResource(String resourceName) throws Exception {
InputStream stream = null;
OutputStream resStreamOut = null;
String jarFolder; String jarFolder;
try { try (InputStream stream = Main.class.getResourceAsStream(resourceName)) {//note that each / is a directory down in the "jar tree" been the jar the root of the tree
stream = Main.class.getResourceAsStream(resourceName);//note that each / is a directory down in the "jar tree" been the jar the root of the tree
if (stream == null) { if (stream == null) {
throw new Exception("Cannot get resource \"" + resourceName + "\" from Jar file."); throw new Exception("Cannot get resource \"" + resourceName + "\" from Jar file.");
} }
@@ -888,15 +880,11 @@ public class Main {
int readBytes; int readBytes;
byte[] buffer = new byte[4096]; byte[] buffer = new byte[4096];
jarFolder = System.getProperty("user.dir"); jarFolder = System.getProperty("user.dir");
resStreamOut = new FileOutputStream(jarFolder + resourceName); try (FileOutputStream resStreamOut = new FileOutputStream(jarFolder + resourceName)) {
while ((readBytes = stream.read(buffer)) > 0) { while ((readBytes = stream.read(buffer)) > 0) {
resStreamOut.write(buffer, 0, readBytes); resStreamOut.write(buffer, 0, readBytes);
}
} }
} catch (Exception ex) {
throw ex;
} finally {
stream.close();
resStreamOut.close();
} }
return jarFolder + resourceName; return jarFolder + resourceName;
@@ -918,7 +906,8 @@ public class Main {
if (fileName == null) if (fileName == null)
return false; return false;
File f = new File(fileName); File f = new File(fileName);
return f.exists(); // "exists" also returns true for directories
return f.isFile();
} }
} }

View File

@@ -4,7 +4,6 @@ import java.io.ByteArrayInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.StringReader; import java.io.StringReader;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Paths; import java.nio.file.Paths;