jena-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From casta...@apache.org
Subject svn commit: r1136378 - in /incubator/jena/Jena2/ARQ/trunk: src-test/com/hp/hpl/jena/sparql/resultset/TestResultSetFormat2.java src/com/hp/hpl/jena/sparql/resultset/TSVInput.java
Date Thu, 16 Jun 2011 11:07:53 GMT
Author: castagna
Date: Thu Jun 16 11:07:53 2011
New Revision: 1136378

URL: http://svn.apache.org/viewvc?rev=1136378&view=rev
Log:
JENA-72 - TSVInput uses .split() to be able to distinguish between, for example: "a \t \t
b \t \t c"  and "a \t \t \t b \t c".

Modified:
    incubator/jena/Jena2/ARQ/trunk/src-test/com/hp/hpl/jena/sparql/resultset/TestResultSetFormat2.java
    incubator/jena/Jena2/ARQ/trunk/src/com/hp/hpl/jena/sparql/resultset/TSVInput.java

Modified: incubator/jena/Jena2/ARQ/trunk/src-test/com/hp/hpl/jena/sparql/resultset/TestResultSetFormat2.java
URL: http://svn.apache.org/viewvc/incubator/jena/Jena2/ARQ/trunk/src-test/com/hp/hpl/jena/sparql/resultset/TestResultSetFormat2.java?rev=1136378&r1=1136377&r2=1136378&view=diff
==============================================================================
--- incubator/jena/Jena2/ARQ/trunk/src-test/com/hp/hpl/jena/sparql/resultset/TestResultSetFormat2.java
(original)
+++ incubator/jena/Jena2/ARQ/trunk/src-test/com/hp/hpl/jena/sparql/resultset/TestResultSetFormat2.java
Thu Jun 16 11:07:53 2011
@@ -27,6 +27,16 @@ public class TestResultSetFormat2
         ByteArrayInputStream in = new ByteArrayInputStream(b) ;
         ResultSet rs2 = ResultSetFactory.fromTSV(in) ;
     }
+
+    @Test (expected=ARQException.class) public void resultset_11()
+    {
+        // This is illegal
+        // Two vars, row of 1 value only.
+        String x = "?x\t?y\n'a'" ;
+        byte[] b = StrUtils.asUTF8bytes(x) ;
+        ByteArrayInputStream in = new ByteArrayInputStream(b) ;
+        ResultSet rs2 = ResultSetFactory.fromTSV(in) ;
+    }    
     
 }
 

Modified: incubator/jena/Jena2/ARQ/trunk/src/com/hp/hpl/jena/sparql/resultset/TSVInput.java
URL: http://svn.apache.org/viewvc/incubator/jena/Jena2/ARQ/trunk/src/com/hp/hpl/jena/sparql/resultset/TSVInput.java?rev=1136378&r1=1136377&r2=1136378&view=diff
==============================================================================
--- incubator/jena/Jena2/ARQ/trunk/src/com/hp/hpl/jena/sparql/resultset/TSVInput.java (original)
+++ incubator/jena/Jena2/ARQ/trunk/src/com/hp/hpl/jena/sparql/resultset/TSVInput.java Thu
Jun 16 11:07:53 2011
@@ -5,7 +5,7 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.util.ArrayList;
 import java.util.List;
-import java.util.StringTokenizer;
+import java.util.regex.Pattern;
 
 import org.openjena.atlas.io.IO;
 import org.openjena.riot.tokens.Tokenizer;
@@ -27,6 +27,8 @@ import com.hp.hpl.jena.sparql.engine.ite
  */
 public class TSVInput {
 
+	static Pattern pattern = Pattern.compile("\t");
+	
     public static ResultSet fromTSV(InputStream in) {
     	BufferedReader reader = IO.asBufferedUTF8(in);
         List<Var> vars = new ArrayList<Var>();
@@ -39,10 +41,9 @@ public class TSVInput {
         try {
         	while ( ( str = reader.readLine() ) != null ) {
         		line++;
-            	StringTokenizer st = new StringTokenizer(str, "\t");
+        		String[] tokens = pattern.split(str);
         		if ( first ) {
-                	while ( st.hasMoreTokens() ) {
-                		String token = st.nextToken();
+        			for ( String token : tokens ) {
                 		if ( token.startsWith("?") ) 
                 			token = token.substring(1);
                 		Var var = Var.alloc(token);
@@ -51,18 +52,19 @@ public class TSVInput {
                 	}
                 	first = false;
         		} else {
-        	        if ( st.countTokens() > vars.size() ) {
-        	        	throw new ARQException(String.format("Line %d has %d values instead of
%d.", line, st.countTokens(), vars.size()));
+        			int num_tokens = tokens.length;
+        			if ( str.endsWith("\t") )
+        				num_tokens += 1;
+        	        if ( num_tokens != vars.size() ) {
+        	        	 throw new ARQException(String.format("Line %d has %d values instead of
%d.", line, num_tokens, vars.size()));
         	        }
-        	        int i = 0;
         	        Binding binding = BindingFactory.create();
-                	while ( st.hasMoreTokens() ) {
-                		String token = st.nextToken();
+        	        for ( int i = 0; i < tokens.length; i++ ) {
+        	        	String token = tokens[i];
                 		Tokenizer tokenizer = TokenizerFactory.makeTokenizerString(token);
-                		if ( tokenizer.hasNext() ) {
+                		if ( tokenizer.hasNext() && token.length() > 0 ) {
                 			Node node = tokenizer.next().asNode();
                 			binding.add(vars.get(i), node);
-                			i++;
                 		}
                 	}
                 	bindings.add(binding);



Mime
View raw message