You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@any23.apache.org by mo...@apache.org on 2012/05/21 16:11:54 UTC

svn commit: r1341039 - in /incubator/any23/trunk/core/src: main/java/org/apache/any23/io/nquads/NQuadsParser.java test/java/org/apache/any23/io/nquads/NQuadsParserTest.java

Author: mostarda
Date: Mon May 21 14:11:54 2012
New Revision: 1341039

URL: http://svn.apache.org/viewvc?rev=1341039&view=rev
Log:
Improved literal type parsing. Related to issue #ANY23-91.

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=1341039&r1=1341038&r2=1341039&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 Mon May 21 14:11:54 2012
@@ -199,10 +199,12 @@ public class NQuadsParser extends RDFPar
      * @param c
      * @throws IOException
      */
-    private void assertChar(BufferedReader br, char c) throws IOException {
-        if( readChar(br) != c) {
-            throw new IllegalArgumentException(
-                    String.format("Unexpected char at location %s %s, expected '%s'", row, col, c)
+    private void assertChar(BufferedReader br, char c) throws IOException, RDFParseException {
+        final char read = readChar(br);
+        if(read != c) {
+            throw new RDFParseException(
+                    String.format("Unexpected char '%s', expected '%s'", read, c),
+                    row, col
             );
         }
     }
@@ -265,12 +267,15 @@ public class NQuadsParser extends RDFPar
         } catch (EOS eos) {
             reportFatalError("Unexpected end of line.", row, col);
             throw new IllegalStateException();
-        } catch (IllegalArgumentException iae) {
+        } catch (Exception e) {
             if(super.stopAtFirstError()) {
-                throw new RDFParseException(iae);
+                if(e instanceof RDFParseException)
+                    throw (RDFParseException) e;
+                else
+                    throw new RDFParseException(e, row, col);
             } else { // Remove rest of broken line and report error.
                 consumeBrokenLine(br);
-                reportError(iae.getMessage(), row, col);
+                reportError(e.getMessage(), row, col);
                 return true;
             }
         }
@@ -388,7 +393,7 @@ public class NQuadsParser extends RDFPar
      * @param br
      * @throws IOException
      */
-    private void parseDot(BufferedReader br) throws IOException {
+    private void parseDot(BufferedReader br) throws IOException, RDFParseException {
         assertChar(br, '.');
     }
 
@@ -470,7 +475,7 @@ public class NQuadsParser extends RDFPar
      * @return the literal attribute.
      * @throws IOException
      */
-    private LiteralAttribute parseLiteralAttribute(BufferedReader br) throws IOException {
+    private LiteralAttribute parseLiteralAttribute(BufferedReader br) throws IOException, RDFParseException {
         char c = readChar(br);
         if(c != '^' && c != '@') {
             reset(br);
@@ -483,29 +488,25 @@ public class NQuadsParser extends RDFPar
             assertChar(br, '^');
         }
 
-        // Consuming eventual open URI.
-        mark(br);
-        c = readChar(br);
-        if(c != '<') {
-            reset(br);
-        }
-
-        StringBuilder sb = new StringBuilder();
-        while(true) {
-            c = readChar(br);
-            if(c == '>') {
-                mark(br);
-                continue;
-            }
-            if(c != ' ' && c != '<') {
-                mark(br);
-                sb.append(c);
-            } else {
-                break;
+        final String attribute;
+        if (isLang) { // Read until space or context begin.
+            final StringBuilder sb = new StringBuilder();
+            while (true) {
+                c = readChar(br);
+                if (c != ' ' && c != '<') {
+                    mark(br);
+                    sb.append(c);
+                } else {
+                    reset(br);
+                    break;
+                }
             }
+            attribute = sb.toString();
+        }  else {
+            attribute = parseURI(br).stringValue();
         }
-        reset(br);
-        return new LiteralAttribute( isLang, sb.toString() );
+
+        return new LiteralAttribute(isLang, attribute);
     }
 
     /**

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=1341039&r1=1341038&r2=1341039&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 Mon May 21 14:11:54 2012
@@ -505,6 +505,21 @@ public class NQuadsParserTest {
         }
     }
 
+    @Test
+    public void testReportInvalidLiteralAttribute() throws RDFHandlerException, IOException, RDFParseException {
+        final ByteArrayInputStream bais = new ByteArrayInputStream(
+                "<http://a> <http://b> \"literal\"^^xsd:datetime <http://c> .".getBytes()
+        );
+        try {
+            parser.parse(bais, "http://base-uri");
+            Assert.fail("Expected failure here.");
+        } catch (RDFParseException e) {
+            Assert.assertTrue(e.getMessage().contains("expected '<'"));
+            Assert.assertEquals(1 , e.getLineNumber());
+            Assert.assertEquals(35, e.getColumnNumber());
+        }
+    }
+
     private void verifyStatementWithInvalidLiteralContent(RDFParser.DatatypeHandling datatypeHandling)
     throws RDFHandlerException, IOException, RDFParseException {
        final ByteArrayInputStream bais = new ByteArrayInputStream(