diff --git a/pom.xml b/pom.xml index 8139a89..a731ba4 100644 --- a/pom.xml +++ b/pom.xml @@ -206,6 +206,20 @@ pds3-product-tools ${pds3-product-tools-version} + + + org.slf4j + slf4j-api + 2.0.12 + compile + + + + org.slf4j + slf4j-simple + 2.0.12 + compile + diff --git a/src/main/java/gov/nasa/pds/imaging/generate/GenerateLauncher.java b/src/main/java/gov/nasa/pds/imaging/generate/GenerateLauncher.java index 725ad2d..d4f7441 100644 --- a/src/main/java/gov/nasa/pds/imaging/generate/GenerateLauncher.java +++ b/src/main/java/gov/nasa/pds/imaging/generate/GenerateLauncher.java @@ -43,7 +43,6 @@ 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; @@ -51,6 +50,8 @@ 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 @@ -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 lblList; @@ -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(){ @@ -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; @@ -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."); } } } @@ -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(); 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..c7f7dca 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); 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..8af2446 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); 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"