incubator-any23-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mosta...@apache.org
Subject svn commit: r1341067 - in /incubator/any23/trunk/core/src: main/java/org/apache/any23/io/nquads/NQuadsParser.java test/java/org/apache/any23/io/nquads/NQuadsParserTest.java test/resources/application/nquads/test1.nq
Date Mon, 21 May 2012 15:22:15 GMT
Author: mostarda
Date: Mon May 21 15:22:15 2012
New Revision: 1341067

URL: http://svn.apache.org/viewvc?rev=1341067&view=rev
Log:
Major improvement in NQuadsParser, added support for optional context. Done NQuadsParserTest
refactorings. This commit is related to issue #ANY23-90.

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
    incubator/any23/trunk/core/src/test/resources/application/nquads/test1.nq

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=1341067&r1=1341066&r2=1341067&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 15:22:15 2012
@@ -253,7 +253,7 @@ public class NQuadsParser extends RDFPar
         final Resource sub;
         final URI      pred;
         final Value    obj;
-        final URI      graph;
+        final URI      context;
         try {
             sub = parseSubject(br);
             consumeSpaces(br);
@@ -261,11 +261,9 @@ public class NQuadsParser extends RDFPar
             consumeSpaces(br);
             obj = parseObject(br);
             consumeSpaces(br);
-            graph = parseGraph(br);
-            consumeSpaces(br);
-            parseDot(br);
+            context = parseContextAndOrDot(br);
         } catch (EOS eos) {
-            reportFatalError("Unexpected end of line.", row, col);
+            reportFatalError("Unexpected end of stream.", row, col);
             throw new IllegalStateException();
         } catch (Exception e) {
             if(super.stopAtFirstError()) {
@@ -280,7 +278,10 @@ public class NQuadsParser extends RDFPar
             }
         }
 
-        notifyStatement(sub, pred, obj, graph);
+        assert sub  != null : "Subject cannot be null.";
+        assert pred != null : "Predicate cannot be null.";
+        assert obj  != null : "Object cannot be null.";
+        notifyStatement(sub, pred, obj, context);
 
         if(!consumeSpacesAndNotEOS(br)) {
             return false;
@@ -350,13 +351,13 @@ public class NQuadsParser extends RDFPar
      * @param sub
      * @param pred
      * @param obj
-     * @param graph
+     * @param context
      * @throws RDFParseException
      * @throws RDFHandlerException
      */
-    private void notifyStatement(Resource sub, URI pred, Value obj, URI graph)
+    private void notifyStatement(Resource sub, URI pred, Value obj, URI context)
     throws RDFParseException, RDFHandlerException {
-        Statement statement = createStatement(sub, pred, obj, graph);
+        Statement statement = super.createStatement(sub, pred, obj, context);
         if (rdfHandler != null) {
             try {
                 rdfHandler.handleStatement(statement);
@@ -424,9 +425,9 @@ public class NQuadsParser extends RDFPar
             String uriStr = NTriplesUtil.unescapeString( sb.toString() );
             URI uri;
             if(uriStr.charAt(0) == '#') {
-                uri = resolveURI(uriStr);
+                uri = super.resolveURI(uriStr);
             } else {
-                uri = createURI(uriStr);
+                uri = super.createURI(uriStr);
             }
             return uri;
         } catch (RDFParseException rdfpe) {
@@ -704,15 +705,26 @@ public class NQuadsParser extends RDFPar
     }
 
     /**
-     * Parses the graph URI.
+     * Parses the context if any.
      *
      * @param br
-     * @return the corresponding URI object.
+     * @return the context URI or null if not found.
      * @throws IOException
      * @throws RDFParseException
      */
-    private URI parseGraph(BufferedReader br) throws IOException, RDFParseException {
-        return parseURI(br);
+    private URI parseContextAndOrDot(BufferedReader br) throws IOException, RDFParseException
{
+        mark(br);
+        final char c = readChar(br);
+        reset(br);
+        if(c == '<') {
+            final URI context = parseURI(br);
+            consumeSpaces(br);
+            parseDot(br);
+            return context;
+        } else {
+            parseDot(br);
+            return null;
+        }
     }
 
     /**

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=1341067&r1=1341066&r2=1341067&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 15:22:15 2012
@@ -399,10 +399,10 @@ public class NQuadsParserTest {
      * @throws RDFHandlerException
      */
     @Test
-    public void testParserWithAllCases()
+    public void testFullParseScenario()
     throws IOException, RDFParseException, RDFHandlerException {
         TestParseLocationListener parseLocationListerner = new TestParseLocationListener();
-        SpecificTestRDFHandler rdfHandler = new SpecificTestRDFHandler();
+        FullParseScenarioRDFHandler rdfHandler = new FullParseScenarioRDFHandler();
         parser.setParseLocationListener(parseLocationListerner);
         parser.setRDFHandler(rdfHandler);
 
@@ -416,8 +416,8 @@ public class NQuadsParserTest {
             "http://test.base.uri"
         );
 
-        rdfHandler.assertHandler(5);
-        parseLocationListerner.assertListener(7, 108);
+        rdfHandler.assertHandler(6);
+        parseLocationListerner.assertListener(8, 71);
     }
 
     /**
@@ -428,7 +428,7 @@ public class NQuadsParserTest {
      * @throws RDFHandlerException
      */
     @Test
-    public void testParserWithRealData()
+    public void testParseRealData()
     throws IOException, RDFParseException, RDFHandlerException {
         TestParseLocationListener parseLocationListener = new TestParseLocationListener();
         TestRDFHandler rdfHandler = new TestRDFHandler();
@@ -520,6 +520,28 @@ public class NQuadsParserTest {
         }
     }
 
+    @Test
+    public void testParseWithNoContext() throws RDFHandlerException, IOException, RDFParseException
{
+        final ByteArrayInputStream bais = new ByteArrayInputStream(
+            ("<http://www.v/dat/4b2-21>" +
+             "<http://www.w3.org/20/ica#dtend>" +
+             "\"2010\"^^<http://www.w3.org/2001/XMLSchema#integer> ."
+            ).getBytes()
+        );
+        final TestRDFHandler rdfHandler = new TestRDFHandler();
+        parser.setRDFHandler(rdfHandler);
+        parser.parse(bais, "http://test.base.uri");
+        final Statement statement = rdfHandler.getStatements().get(0);
+        Assert.assertEquals("http://www.v/dat/4b2-21", statement.getSubject().stringValue());
+        Assert.assertEquals("http://www.w3.org/20/ica#dtend", statement.getPredicate().stringValue());
+        Assert.assertTrue(statement.getObject() instanceof Literal);
+        Literal object = (Literal) statement.getObject();
+        Assert.assertEquals("2010", object.stringValue());
+        Assert.assertNull(object.getLanguage());
+        Assert.assertEquals("http://www.w3.org/2001/XMLSchema#integer", object.getDatatype().toString());
+        Assert.assertNull(statement.getContext());
+    }
+
     private void verifyStatementWithInvalidLiteralContent(RDFParser.DatatypeHandling datatypeHandling)
     throws RDFHandlerException, IOException, RDFParseException {
        final ByteArrayInputStream bais = new ByteArrayInputStream(
@@ -606,23 +628,28 @@ public class NQuadsParserTest {
         }
     }
 
-    private class SpecificTestRDFHandler extends TestRDFHandler {
+    private class FullParseScenarioRDFHandler extends TestRDFHandler {
 
         public void handleStatement(Statement statement) throws RDFHandlerException {
-            int statements = getStatements().size();
-            if(statements == 0){
+            int statementIndex = getStatements().size();
+            if(statementIndex == 0){
                 Assert.assertEquals(new URIImpl("http://example.org/alice/foaf.rdf#me"),
statement.getSubject() );
-
             } else {
                 Assert.assertTrue(statement.getSubject() instanceof BNode);
             }
-            if( statements == 5 ) {
+
+            if( statementIndex == 4) {
                 Assert.assertEquals(new URIImpl("http://test.base.uri#like"), statement.getPredicate()
);
             }
-            Assert.assertEquals(
-                    new URIImpl( String.format("http://example.org/alice/foaf%s.rdf", statements
+ 1) ),
-                    statement.getContext()
-            );
+
+            if(statementIndex == 5) {
+                Assert.assertNull(statement.getContext());
+            } else {
+                Assert.assertEquals(
+                        new URIImpl(String.format("http://example.org/alice/foaf%s.rdf",
statementIndex + 1)),
+                        statement.getContext()
+                );
+            }
 
             super.handleStatement(statement);
         }

Modified: incubator/any23/trunk/core/src/test/resources/application/nquads/test1.nq
URL: http://svn.apache.org/viewvc/incubator/any23/trunk/core/src/test/resources/application/nquads/test1.nq?rev=1341067&r1=1341066&r2=1341067&view=diff
==============================================================================
--- incubator/any23/trunk/core/src/test/resources/application/nquads/test1.nq (original)
+++ incubator/any23/trunk/core/src/test/resources/application/nquads/test1.nq Mon May 21 15:22:15
2012
@@ -4,4 +4,5 @@ _:bnode2 <http://xmlns.com/foaf/0.1/Frie
 
 
 _:bnode3 <http://xmlns.com/foaf/0.1/Friend> "This literal is \"really\" useful"@it
<http://example.org/alice/foaf4.rdf> .
-_:bnode1 <#like> "12345"^^<http://www.w3.org/2001/XMLSchema#integer> <http://example.org/alice/foaf5.rdf>
.
\ No newline at end of file
+_:bnode1 <#like> "12345"^^<http://www.w3.org/2001/XMLSchema#integer> <http://example.org/alice/foaf5.rdf>
.
+_:bnode4 <#type> "triple"^^<http://www.w3.org/2001/XMLSchema#String> .
\ No newline at end of file



Mime
View raw message