Skip to content

Commit

Permalink
Merge pull request #65 from NASA-PDS/i64
Browse files Browse the repository at this point in the history
Improved exception handling and logging to support off-nominal use cases
  • Loading branch information
jordanpadams authored Feb 26, 2024
2 parents 42c8a80 + 02415a6 commit 7453262
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 26 deletions.
14 changes: 14 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,20 @@
<artifactId>pds3-product-tools</artifactId>
<version>${pds3-product-tools-version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>2.0.12</version>
<scope>compile</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-simple -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>2.0.12</version>
<scope>compile</scope>
</dependency>
</dependencies>

<repositories>
Expand Down
27 changes: 15 additions & 12 deletions src/main/java/gov/nasa/pds/imaging/generate/GenerateLauncher.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,15 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.logging.Logger;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.GnuParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.ParseException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Class used as Command-line interface endpoint. Parses command-line arguments
Expand All @@ -62,7 +63,7 @@
public class GenerateLauncher {

/** Logger. **/
private static Logger log = Logger.getLogger(GenerateLauncher.class.getName());
private static Logger LOGGER = LoggerFactory.getLogger(GenerateLauncher.class);

private String basePath;
private List<String> lblList;
Expand Down Expand Up @@ -102,10 +103,10 @@ public final void displayHelp() {
*
*/
public final void displayVersion() {
System.err.println("\n" + ToolInfo.getName());
System.err.println(ToolInfo.getVersion());
System.err.println("Release Date: " + ToolInfo.getReleaseDate());
System.err.println(ToolInfo.getCopyright() + "\n");
LOGGER.info("\n" + ToolInfo.getName());
LOGGER.info(ToolInfo.getVersion());
LOGGER.info("Release Date: " + ToolInfo.getReleaseDate());
LOGGER.info(ToolInfo.getCopyright() + "\n");
}

public final void generate(){
Expand Down Expand Up @@ -199,9 +200,9 @@ public final void query(final CommandLine line) throws Exception {

// Let's default to the one label if -p flag was specified,
// otherwise loop through the lbl list
if (this.lblList == null) {
throw new InvalidOptionException("Missing -p or -l flags. " +
"One or many PDS3 label must be specified.");
if (this.lblList.isEmpty()) {
throw new InvalidOptionException("Missing --pds3-label flag. " +
"One or more PDS3 labels required.");
} else {
String filepath;
PDS3Label pdsLabel;
Expand Down Expand Up @@ -260,7 +261,7 @@ public final void query(final CommandLine line) throws Exception {
this.generatorList.add(new Generator(pdsObj, this.templateFile,
outputFile, this.isXML));
} else {
log.warning(lbl + " does not exist.");
LOGGER.warn(lbl + " does not exist.");
}
}
}
Expand Down Expand Up @@ -293,9 +294,11 @@ public static void main(final String[] args) {
launcher.query(commandline);
launcher.generate();
// launcher.closeHandlers();
} catch (InvalidOptionException e) {
LOGGER.error(e.getMessage());
System.exit(1);
} catch (final ParseException pEx) {
System.err.println("Command-line parse failure: "
+ pEx.getMessage());
LOGGER.error("Command-line parse failure: " + pEx.getMessage());
System.exit(1);
} catch (final Exception e) {
e.printStackTrace();
Expand Down
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);

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);

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 7453262

Please sign in to comment.