From 1e47199641ee1b7b9d7315923068708a64a9b75c Mon Sep 17 00:00:00 2001 From: Jordan Padams Date: Fri, 23 Feb 2024 12:04:48 -0800 Subject: [PATCH] Improve exception handling and logging Resolves #64 --- .../nasa/pds/imaging/generate/Generator.java | 25 +++++++++++-------- .../pds/imaging/generate/label/ItemNode.java | 12 ++++++++- .../pds/imaging/generate/label/PDS3Label.java | 10 ++++++-- 3 files changed, 33 insertions(+), 14 deletions(-) diff --git a/src/main/java/gov/nasa/pds/imaging/generate/Generator.java b/src/main/java/gov/nasa/pds/imaging/generate/Generator.java index 5406564..3bb6917 100644 --- a/src/main/java/gov/nasa/pds/imaging/generate/Generator.java +++ b/src/main/java/gov/nasa/pds/imaging/generate/Generator.java @@ -61,6 +61,8 @@ import org.apache.velocity.exception.ParseErrorException; import org.apache.velocity.exception.ResourceNotFoundException; import org.apache.velocity.tools.ToolManager; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.w3c.dom.Document; import org.w3c.dom.Node; import org.w3c.dom.NodeList; @@ -68,10 +70,13 @@ import org.xml.sax.SAXParseException; import gov.nasa.pds.imaging.generate.automatic.elements.ExistTemplate; import gov.nasa.pds.imaging.generate.context.ContextMappings; +import gov.nasa.pds.imaging.generate.label.ItemNode; import gov.nasa.pds.imaging.generate.label.PDSObject; import gov.nasa.pds.imaging.generate.util.Debugger; public class Generator { + + private static final Logger LOGGER = LoggerFactory.getLogger(Generator.class.getName()); private Map pdsObjects ; private Template template; @@ -264,11 +269,12 @@ private String clean(final StringWriter sw) { return outputXmlString; } catch (SAXParseException e) { - System.err.println("\n\nError applying XSLT to output XML. Verify label and template are correctly formatted."); - System.err.println(e.getMessage()); - System.err.println("Outputting without formatting. \n\n"); + LOGGER.error("\n\nError applying XSLT to output XML. Verify label and template are correctly formatted."); + LOGGER.error(e.getMessage()); + LOGGER.error("Outputting without formatting. \n\n"); } catch (Exception e) { - System.err.println("Error attempting to format XML. Malformed XML expected."); + LOGGER.error("Error attempting to format XML. Malformed XML expected."); + e.printStackTrace(); } return sw.toString(); } else { @@ -317,7 +323,7 @@ private void handleNode(Node node) { // first lets check we have no child nodes (text or other) if (node.getChildNodes().getLength() == 0) { // next check if the value is null or empty string - if (node.getNodeValue().isEmpty() || node.getNodeValue() == null) { + if (node.getNodeValue() == null || node.getNodeValue().isEmpty()) { // finally make sure it does not have xsi:nil attribute if (node.getAttributes().getLength() == 0 || node.getAttributes().getNamedItem("xsi:nil") == null) { node.getParentNode().removeChild(node); @@ -421,7 +427,7 @@ public void generate(ImageOutputStream ios) throws Exception { // ios.close(); // it seems writers do not close the stream. It will be done later } catch (final NullPointerException e) { - System.out.println("NullPointerException "+ e); + LOGGER.error("NullPointerException "+ e); } } } @@ -481,10 +487,9 @@ public void generate(OutputStream os) throws Exception { if (output.equals("null")) { // TODO Need to validate products prior to this step to find WHY output == null throw new Exception("Error generating PDS4 Label. No output found. Validate input files."); } else { - - Debugger.debug("New PDS4 Label:"); out = new PrintWriter(os); out.write(output); + LOGGER.info("New PDS4 Label: " + this.outputFile.getAbsolutePath()); } } finally { sw.close(); @@ -556,11 +561,9 @@ public void generate(final boolean toStdOut) throws FileNotFoundException, Templ } else if (this.outputStream != null) { // FIXME Need to implement this here } else { - Debugger.debug("New PDS4 Label: " + this.outputFile.getAbsolutePath()); - out = new PrintWriter(this.outputFile); out.write(output); - + LOGGER.info("New PDS4 Label: " + this.outputFile.getAbsolutePath()); } } } finally { diff --git a/src/main/java/gov/nasa/pds/imaging/generate/label/ItemNode.java b/src/main/java/gov/nasa/pds/imaging/generate/label/ItemNode.java index 3ebce58..4e88cfc 100644 --- a/src/main/java/gov/nasa/pds/imaging/generate/label/ItemNode.java +++ b/src/main/java/gov/nasa/pds/imaging/generate/label/ItemNode.java @@ -35,8 +35,11 @@ import java.util.List; import org.apache.commons.lang.StringEscapeUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public class ItemNode extends ArrayList{ + private static final Logger LOGGER = LoggerFactory.getLogger(ItemNode.class.getName()); private String name; public String units; @@ -90,7 +93,14 @@ public String get(int index) { return ""; } - return super.get(index); + + try { + return super.get(index); + } catch (IndexOutOfBoundsException e) { + LOGGER.error(String.format("IndexOutOfBoundsException: %s[%d] does not exist. %d values found.", this.name, index, this.size())); + } + + return ""; } @Override diff --git a/src/main/java/gov/nasa/pds/imaging/generate/label/PDS3Label.java b/src/main/java/gov/nasa/pds/imaging/generate/label/PDS3Label.java index cf27dbb..40211fc 100755 --- a/src/main/java/gov/nasa/pds/imaging/generate/label/PDS3Label.java +++ b/src/main/java/gov/nasa/pds/imaging/generate/label/PDS3Label.java @@ -39,8 +39,13 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; import javax.imageio.stream.ImageInputStream; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.w3c.dom.Document; import org.w3c.dom.Node; + +import gov.nasa.pds.imaging.generate.Generator; import gov.nasa.pds.imaging.generate.TemplateException; import gov.nasa.pds.imaging.generate.collections.PDSTreeMap; import gov.nasa.pds.imaging.generate.context.ContextUtil; @@ -60,7 +65,8 @@ * */ public class PDS3Label implements PDSObject { - + private static final Logger LOGGER = LoggerFactory.getLogger(PDS3Label.class); + public static final String CONTEXT = "label"; public ContextUtil ctxtUtil; @@ -412,7 +418,7 @@ public Long getImageStartByte() { @Override public void setMappings() throws TemplateException, LabelParserException { - Debugger.debug("PDS3Label.setMapping parserType = "+this.parserType); + LOGGER.info("ParserType: "+this.parserType); if (this.parserType.equals(ParserType.VICAR)) { Debugger.debug("+++++++++++++++++++++++++++\n" + "PDS3Label.setMapping()\n"