From 0611d1bfea9469b7bedbe3ca8fc214bef6a8c1e3 Mon Sep 17 00:00:00 2001 From: rtgdk Date: Tue, 18 Jul 2017 16:27:48 +0530 Subject: [PATCH] Add try catch in TagToRDF for returning line no. error by throwing exceptions. * It still shows 2 exceptions when it is an invalid file. One for Tag Value Exception and one for RDF. Signed-off-by: rtgdk --- src/org/spdx/tag/HandBuiltParser.java | 2 +- src/org/spdx/tools/CompareSpdxDocs.java | 18 +++++++++++++----- src/org/spdx/tools/TagToRDF.java | 19 ++++++++++++------- 3 files changed, 26 insertions(+), 13 deletions(-) diff --git a/src/org/spdx/tag/HandBuiltParser.java b/src/org/spdx/tag/HandBuiltParser.java index 0c7d9ffa..7442d6d7 100644 --- a/src/org/spdx/tag/HandBuiltParser.java +++ b/src/org/spdx/tag/HandBuiltParser.java @@ -111,7 +111,7 @@ public void data() throws Exception { nextLine = textInput.readLine(); } if (inTextBlock) { - throw(new RecognitionException("Unterminated text block. Expecting "+END_TEXT)); + throw(new RecognitionException("Unterminated text block at line " + (textInput.getCurrentLineNo()) + " Expecting "+END_TEXT )); } this.buildDocument.exit(); } finally { diff --git a/src/org/spdx/tools/CompareSpdxDocs.java b/src/org/spdx/tools/CompareSpdxDocs.java index bb50d03b..8f1329d9 100644 --- a/src/org/spdx/tools/CompareSpdxDocs.java +++ b/src/org/spdx/tools/CompareSpdxDocs.java @@ -55,6 +55,8 @@ import com.google.common.base.Joiner; +import antlr.RecognitionException; + /** * Command line application to compare two SPDX documents * Usage: CompareSpdxDoc doc1 doc2 [output] @@ -1145,6 +1147,7 @@ protected static SpdxDocument openRdfOrTagDoc(String spdxDocFileName, List FileInputStream in = null; try { in = new FileInputStream(tagValueFile); - return TagToRDF.convertTagFileToRdf(in, TagToRDF.DEFAULT_OUTPUT_FORMAT, warnings).getSpdxDocument(); - } catch (Exception e) { + SpdxDocument result = TagToRDF.convertTagFileToRdf(in, TagToRDF.DEFAULT_OUTPUT_FORMAT, warnings).getSpdxDocument(); + return result; + } + catch (RecognitionException e){ + throw(new SpdxCompareException(e.getMessage(),e)); + } + catch (Exception e) { throw(new SpdxCompareException("Error converting tag/value to RDF/XML format: "+e.getMessage(),e)); } finally { if (in != null) { diff --git a/src/org/spdx/tools/TagToRDF.java b/src/org/spdx/tools/TagToRDF.java index 2f1a4fb6..2dd6d446 100644 --- a/src/org/spdx/tools/TagToRDF.java +++ b/src/org/spdx/tools/TagToRDF.java @@ -206,14 +206,19 @@ public static SpdxDocumentContainer convertTagFileToRdf( NoCommentInputStream nci = new NoCommentInputStream(spdxTagFile); // TagValueLexer lexer = new TagValueLexer(new DataInputStream(nci)); // TagValueParser parser = new TagValueParser(lexer); - HandBuiltParser parser = new HandBuiltParser(nci); - SpdxDocumentContainer[] result = new SpdxDocumentContainer[1]; - parser.setBehavior(new BuildDocument(result, constants, warnings)); - parser.data(); - if (result[0] == null) { - throw(new RuntimeException("Unexpected error parsing SPDX tag document - the result is null.")); + try{ + HandBuiltParser parser = new HandBuiltParser(nci); + SpdxDocumentContainer[] result = new SpdxDocumentContainer[1]; + parser.setBehavior(new BuildDocument(result, constants, warnings)); + parser.data(); + if (result[0] == null) { + throw(new RuntimeException("Unexpected error parsing SPDX tag document - the result is null.")); + } + return result[0]; + } + catch (RecognitionException e) { + throw(new RecognitionException(e.getMessage())); } - return result[0]; }