incubator-any23-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mosta...@apache.org
Subject svn commit: r1305034 - in /incubator/any23/trunk/core/src: main/java/org/apache/any23/io/nquads/NQuadsParser.java test/java/org/apache/any23/io/nquads/NQuadsParserTest.java
Date Sun, 25 Mar 2012 13:45:05 GMT
Author: mostarda
Date: Sun Mar 25 13:45:05 2012
New Revision: 1305034

URL: http://svn.apache.org/viewvc?rev=1305034&view=rev
Log:
Added support for stopAtFirstError to the NQuadsParser. This commit is related to issue #ANY23-49
.

Modified:
    incubator/any23/trunk/core/src/main/java/org/apache/any23/io/nquads/NQuadsParser.java
    incubator/any23/trunk/core/src/test/java/org/apache/any23/io/nquads/NQuadsParserTest.java

Modified: incubator/any23/trunk/core/src/main/java/org/apache/any23/io/nquads/NQuadsParser.java
URL: http://svn.apache.org/viewvc/incubator/any23/trunk/core/src/main/java/org/apache/any23/io/nquads/NQuadsParser.java?rev=1305034&r1=1305033&r2=1305034&view=diff
==============================================================================
--- incubator/any23/trunk/core/src/main/java/org/apache/any23/io/nquads/NQuadsParser.java
(original)
+++ incubator/any23/trunk/core/src/main/java/org/apache/any23/io/nquads/NQuadsParser.java
Sun Mar 25 13:45:05 2012
@@ -208,6 +208,23 @@ public class NQuadsParser extends RDFPar
     }
 
     /**
+     * Consumes the reader until the next carriage return.
+     *
+     * @param br
+     * @throws IOException
+     */
+    private void consumeBrokenLine(BufferedReader br) throws IOException {
+        char c;
+        while (true) {
+            mark(br);
+            c = readChar(br);
+            if (c == '\n') {
+                return;
+            }
+        }
+    }
+
+    /**
      * Parsers an <i>NQuads</i> line.
      *
      * @param br input stream reader containing NQuads.
@@ -248,6 +265,14 @@ public class NQuadsParser extends RDFPar
         } catch (EOS eos) {
             reportFatalError("Unexpected end of line.", row, col);
             throw new IllegalStateException();
+        } catch (IllegalArgumentException iae) {
+            if(super.stopAtFirstError()) {
+                throw new RDFParseException(iae);
+            } else { // Remove rest of broken line and report error.
+                consumeBrokenLine(br);
+                reportError(iae.getMessage(), row, col);
+                return true;
+            }
         }
 
         notifyStatement(sub, pred, obj, graph);

Modified: incubator/any23/trunk/core/src/test/java/org/apache/any23/io/nquads/NQuadsParserTest.java
URL: http://svn.apache.org/viewvc/incubator/any23/trunk/core/src/test/java/org/apache/any23/io/nquads/NQuadsParserTest.java?rev=1305034&r1=1305033&r2=1305034&view=diff
==============================================================================
--- incubator/any23/trunk/core/src/test/java/org/apache/any23/io/nquads/NQuadsParserTest.java
(original)
+++ incubator/any23/trunk/core/src/test/java/org/apache/any23/io/nquads/NQuadsParserTest.java
Sun Mar 25 13:45:05 2012
@@ -468,6 +468,43 @@ public class NQuadsParserTest {
         verifyStatementWithInvalidDatatype(RDFParser.DatatypeHandling.VERIFY);
     }
 
+    @Test (expected = RDFParseException.class)
+    public void testStopAtFirstErrorStrictParsing() throws RDFHandlerException, IOException,
RDFParseException {
+        final ByteArrayInputStream bais = new ByteArrayInputStream(
+                (
+                    "<http://s0> <http://p0> <http://o0> <http://g0>
.\n" +
+                    "<http://sX>                                     .\n" + // Line
with error.
+                    "<http://s1> <http://p1> <http://o1> <http://g1>
.\n"
+                ).getBytes()
+        );
+        parser.setStopAtFirstError(true);
+        parser.parse(bais, "http://base-uri");
+    }
+
+    @Test
+    public void testStopAtFirstErrorTolerantParsing() throws RDFHandlerException, IOException,
RDFParseException {
+        final ByteArrayInputStream bais = new ByteArrayInputStream(
+                (
+                    "<http://s0> <http://p0> <http://o0> <http://g0>
.\n" +
+                    "<http://sX>                                     .\n" + // Line
with error.
+                    "<http://s1> <http://p1> <http://o1> <http://g1>
.\n"
+                ).getBytes()
+        );
+        final TestRDFHandler rdfHandler = new TestRDFHandler();
+        parser.setRDFHandler(rdfHandler);
+        parser.setStopAtFirstError(false);
+        parser.parse(bais, "http://base-uri");
+        rdfHandler.assertHandler(2);
+        final List<Statement> statements = rdfHandler.getStatements();
+        final int size = statements.size();
+        for(int i = 0; i < size; i++) {
+            Assert.assertEquals("http://s" + i, statements.get(i).getSubject().stringValue()
 );
+            Assert.assertEquals("http://p" + i, statements.get(i).getPredicate().stringValue());
+            Assert.assertEquals("http://o" + i, statements.get(i).getObject().stringValue()
  );
+            Assert.assertEquals("http://g" + i, statements.get(i).getContext().stringValue()
 );
+        }
+    }
+
     private void verifyStatementWithInvalidLiteralContent(RDFParser.DatatypeHandling datatypeHandling)
     throws RDFHandlerException, IOException, RDFParseException {
        final ByteArrayInputStream bais = new ByteArrayInputStream(



Mime
View raw message