Skip to content

Commit

Permalink
Improve exception handling and logging
Browse files Browse the repository at this point in the history
Resolves #64
  • Loading branch information
jordanpadams committed Feb 23, 2024
1 parent 4a930df commit 1e47199
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 14 deletions.
25 changes: 14 additions & 11 deletions src/main/java/gov/nasa/pds/imaging/generate/Generator.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,17 +61,22 @@
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;
import org.xml.sax.SAXException;
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<String, PDSObject> pdsObjects ;
private Template template;
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
}
}
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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 {
Expand Down
12 changes: 11 additions & 1 deletion src/main/java/gov/nasa/pds/imaging/generate/label/ItemNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>{
private static final Logger LOGGER = LoggerFactory.getLogger(ItemNode.class.getName());

private String name;
public String units;
Expand Down Expand Up @@ -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
Expand Down
10 changes: 8 additions & 2 deletions src/main/java/gov/nasa/pds/imaging/generate/label/PDS3Label.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit 1e47199

Please sign in to comment.