You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jena.apache.org by rv...@apache.org on 2012/06/29 19:46:48 UTC

svn commit: r1355484 - in /jena/trunk/jena-arq/src: main/java/com/hp/hpl/jena/sparql/resultset/TSVInputIterator.java test/java/com/hp/hpl/jena/sparql/resultset/TestResultSetFormat2.java

Author: rvesse
Date: Fri Jun 29 17:46:46 2012
New Revision: 1355484

URL: http://svn.apache.org/viewvc?rev=1355484&view=rev
Log:
Improves error handling in the TSVInputIterator by tracking current line number so the parser can report which line an error occurred at in the event of malformed input

Modified:
    jena/trunk/jena-arq/src/main/java/com/hp/hpl/jena/sparql/resultset/TSVInputIterator.java
    jena/trunk/jena-arq/src/test/java/com/hp/hpl/jena/sparql/resultset/TestResultSetFormat2.java

Modified: jena/trunk/jena-arq/src/main/java/com/hp/hpl/jena/sparql/resultset/TSVInputIterator.java
URL: http://svn.apache.org/viewvc/jena/trunk/jena-arq/src/main/java/com/hp/hpl/jena/sparql/resultset/TSVInputIterator.java?rev=1355484&r1=1355483&r2=1355484&view=diff
==============================================================================
--- jena/trunk/jena-arq/src/main/java/com/hp/hpl/jena/sparql/resultset/TSVInputIterator.java (original)
+++ jena/trunk/jena-arq/src/main/java/com/hp/hpl/jena/sparql/resultset/TSVInputIterator.java Fri Jun 29 17:46:46 2012
@@ -45,6 +45,7 @@ public class TSVInputIterator extends Qu
 	private BindingMap binding;
 	private int expectedItems;
 	private List<Var> vars;
+	private long lineNum = 1;
 	
 	/**
 	 * Creates a new TSV Input Iterator
@@ -88,6 +89,7 @@ public class TSVInputIterator extends Qu
 	        line = this.reader.readLine();
 	        //Once EOF has been reached we'll see null for this call so we can return false because there are no further bindings
 	        if (line == null) return false;
+	        this.lineNum++;
 	    } 
 	    catch (IOException e) 
 	    { throw new QueryException("Error parsing TSV results - " + e.getMessage()); }
@@ -97,7 +99,7 @@ public class TSVInputIterator extends Qu
 	        // Empty input line - no bindings.
 	    	// Only valid when we expect zero/one values as otherwise we should get a sequence of tab characters
 	    	// which means a non-empty string which we handle normally
-	    	if (expectedItems > 1) throw new QueryException(String.format("Error Parsing TSV results - A result row had 0/1 values when %d were expected", expectedItems));
+	    	if (expectedItems > 1) throw new QueryException(String.format("Error Parsing TSV results at Line " + this.lineNum + " - A result row had 0/1 values when %d were expected", expectedItems));
 	        this.binding = BindingFactory.create() ;
 	        return true ;
 	    }
@@ -105,21 +107,28 @@ public class TSVInputIterator extends Qu
         String[] tokens = TSVInput.pattern.split(line, -1);
 	    
         if (tokens.length != expectedItems)
-        	 throw new QueryException(String.format("Error Parsing TSV results - A result row had %d values instead of the expected %d.", tokens.length, expectedItems));
+        	 throw new QueryException(String.format("Error Parsing TSV results at Line " + this.lineNum + " - A result row had %d values instead of the expected %d.", tokens.length, expectedItems));
 
         this.binding = BindingFactory.create();
-        
+
+
         for ( int i = 0; i < tokens.length; i++ ) 
         {
         	String token = tokens[i];
-        	
+
         	//If we see an empty string this denotes an unbound value
         	if (token.equals("")) continue; 
-        	
-        	//Bound value so parse it and add to the binding
-        	Node node = NodeFactory.parseNode(token, null);
-        	this.binding.add(this.vars.get(i), node);
+
+        	try
+        	{
+        		//Bound value so parse it and add to the binding
+        		Node node = NodeFactory.parseNode(token, null);
+        		this.binding.add(this.vars.get(i), node);
+        	} catch (Exception e) {
+        		throw new QueryException(String.format("Error Parsing TSV results at Line " + this.lineNum + " - %s is not a valid encoding of a Node", token));
+        	}
         }
+
         return true;
 	}
 

Modified: jena/trunk/jena-arq/src/test/java/com/hp/hpl/jena/sparql/resultset/TestResultSetFormat2.java
URL: http://svn.apache.org/viewvc/jena/trunk/jena-arq/src/test/java/com/hp/hpl/jena/sparql/resultset/TestResultSetFormat2.java?rev=1355484&r1=1355483&r2=1355484&view=diff
==============================================================================
--- jena/trunk/jena-arq/src/test/java/com/hp/hpl/jena/sparql/resultset/TestResultSetFormat2.java (original)
+++ jena/trunk/jena-arq/src/test/java/com/hp/hpl/jena/sparql/resultset/TestResultSetFormat2.java Fri Jun 29 17:46:46 2012
@@ -79,6 +79,13 @@ public class TestResultSetFormat2
     	parseTSV(x);
     }
     
+    @Test
+    public void resultset_tsv_07()
+    {
+    	//Three vars, one row of no values
+    	String x = "?x\t?y\t?z\n\t\t";
+    }
+    
     @Test (expected=QueryException.class) 
     public void resultset_bad_tsv_01()
     {
@@ -109,7 +116,7 @@ public class TestResultSetFormat2
     	String x = "?x\t?y\n\n";
     	parseTSV(x);
     }
-    
+        
     public void parseTSV(String x)
     {
         byte[] b = StrUtils.asUTF8bytes(x) ;