You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jena.apache.org by an...@apache.org on 2015/10/02 18:45:18 UTC

jena git commit: JENA-1013: Remove workaround for jsonld-java issue 144 after jsonld-java upgrade.

Repository: jena
Updated Branches:
  refs/heads/master 620c5660a -> a9e1fae23


JENA-1013: Remove workaround for jsonld-java issue 144 after jsonld-java upgrade.


Project: http://git-wip-us.apache.org/repos/asf/jena/repo
Commit: http://git-wip-us.apache.org/repos/asf/jena/commit/a9e1fae2
Tree: http://git-wip-us.apache.org/repos/asf/jena/tree/a9e1fae2
Diff: http://git-wip-us.apache.org/repos/asf/jena/diff/a9e1fae2

Branch: refs/heads/master
Commit: a9e1fae238b4ecd3b821693146d582cee298cd5e
Parents: 620c566
Author: Andy Seaborne <an...@apache.org>
Authored: Fri Oct 2 17:42:40 2015 +0100
Committer: Andy Seaborne <an...@apache.org>
Committed: Fri Oct 2 17:44:22 2015 +0100

----------------------------------------------------------------------
 .../org/apache/jena/riot/lang/JsonLDReader.java | 187 +++++++++----------
 1 file changed, 91 insertions(+), 96 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/jena/blob/a9e1fae2/jena-arq/src/main/java/org/apache/jena/riot/lang/JsonLDReader.java
----------------------------------------------------------------------
diff --git a/jena-arq/src/main/java/org/apache/jena/riot/lang/JsonLDReader.java b/jena-arq/src/main/java/org/apache/jena/riot/lang/JsonLDReader.java
index 09073c6..367e7fd 100644
--- a/jena-arq/src/main/java/org/apache/jena/riot/lang/JsonLDReader.java
+++ b/jena-arq/src/main/java/org/apache/jena/riot/lang/JsonLDReader.java
@@ -23,9 +23,10 @@ import java.io.InputStream ;
 import java.io.Reader ;
 import java.util.List ;
 import java.util.Map ;
-import java.util.Objects;
 import java.util.Map.Entry;
-import java.util.function.Consumer ;
+import java.util.Objects;
+import com.github.jsonldjava.core.* ;
+import com.github.jsonldjava.utils.JsonUtils ;
 
 import org.apache.jena.atlas.io.IO ;
 import org.apache.jena.atlas.lib.InternalErrorException ;
@@ -41,11 +42,6 @@ import org.apache.jena.riot.RiotException ;
 import org.apache.jena.riot.system.* ;
 import org.apache.jena.sparql.core.Quad ;
 import org.apache.jena.sparql.util.Context ;
-import org.apache.jena.util.FileUtils ;
-
-import com.fasterxml.jackson.core.* ;
-import com.fasterxml.jackson.databind.ObjectMapper ;
-import com.github.jsonldjava.core.* ;
 
 public class JsonLDReader implements ReaderRIOT
 {
@@ -58,110 +54,109 @@ public class JsonLDReader implements ReaderRIOT
     @Override public ParserProfile getParserProfile()                   { return parserProfile ; }
     @Override public void setParserProfile(ParserProfile parserProfile) { this.parserProfile = parserProfile ; }
     
-    // pre jsonld-java issue #144 code.
-    // Remove at any point.
-//    @Override
-//    public void read(Reader reader, String baseURI, ContentType ct, StreamRDF output, Context context) {
-//        try {
-//            Object jsonObject = JsonUtils.fromReader(reader) ;
-//            read$(jsonObject, baseURI, ct, output, context) ;
-//        }
-//        catch (IOException e) {
-//            IO.exception(e) ;
-//        }
-//    }
-//
-//    @Override
-//    public void read(InputStream in, String baseURI, ContentType ct, StreamRDF output, Context context) {
-//        try {
-//            Object jsonObject = JsonUtils.fromInputStream(in) ;
-//            read$(jsonObject, baseURI, ct, output, context) ;
-//        }
-//        catch (IOException e) {
-//            IO.exception(e) ;
-//        }
-//    }
-    
-    // This addresses jsonld-java issue #144 so that we get triples/quads out
-    // then there is a parse error. Even if it is fixed in jsonld-java, it would
-    // mean that no triples would be produced - all the JSON parsing is done
-    // before JSON-LD processing. Here we process the first JSON object, whch
-    // causes triples to be generated then decide whether to throw a parse
-    // error.  This is more in the style of other syntaxes and stream parsing.
-    
     @Override
     public void read(Reader reader, String baseURI, ContentType ct, StreamRDF output, Context context) {
         try {
-            readProcess(reader, 
-                       (jsonObject)->read$(jsonObject, baseURI, ct, output, context)) ;
-        }
-        catch (JsonProcessingException ex) {    
-            // includes JsonParseException
-            // The Jackson JSON parser, or addition JSON-level check, throws up something.
-            JsonLocation loc = ex.getLocation() ;
-            errorHandler.error(ex.getOriginalMessage(), loc.getLineNr(), loc.getColumnNr()); 
-            throw new RiotException(ex.getOriginalMessage()) ;
+            Object jsonObject = JsonUtils.fromReader(reader) ;
+            read$(jsonObject, baseURI, ct, output, context) ;
         }
         catch (IOException e) {
-            errorHandler.error(e.getMessage(), -1, -1); 
             IO.exception(e) ;
         }
     }
 
     @Override
     public void read(InputStream in, String baseURI, ContentType ct, StreamRDF output, Context context) {
-        Reader r = FileUtils.asBufferedUTF8(in) ;
-        read(r, baseURI, ct, output, context) ;
-    }
-
-    // From JsonUtils.fromReader in the jsonld-java codebase.
-    // Note that jsonld-java always uses a reader.
-    private static final ObjectMapper JSON_MAPPER = new ObjectMapper();
-    private static final JsonFactory JSON_FACTORY = new JsonFactory(JSON_MAPPER);
-    
-    /** Read a JSON object from the reader, acall the prcoessing function, then
-     * check for trailing content. For Jena, this means tripes/quads are generated
-     * from a valid JSON object, then there is a  parse error for trailing junk.   
-     * @param reader
-     * @param function
-     * @throws IOException
-     */
-    private static void readProcess(Reader reader, Consumer<Object> function) throws IOException {
-        final JsonParser jp = JSON_FACTORY.createParser(reader);
-        Object rval ;
-        final JsonToken initialToken = jp.nextToken();
-
-        if (initialToken == JsonToken.START_ARRAY) {
-            rval = jp.readValueAs(List.class);
-        } else if (initialToken == JsonToken.START_OBJECT) {
-            rval = jp.readValueAs(Map.class);
-        } else if (initialToken == JsonToken.VALUE_STRING) {
-            rval = jp.readValueAs(String.class);
-        } else if (initialToken == JsonToken.VALUE_FALSE || initialToken == JsonToken.VALUE_TRUE) {
-            rval = jp.readValueAs(Boolean.class);
-        } else if (initialToken == JsonToken.VALUE_NUMBER_FLOAT
-                || initialToken == JsonToken.VALUE_NUMBER_INT) {
-            rval = jp.readValueAs(Number.class);
-        } else if (initialToken == JsonToken.VALUE_NULL) {
-            rval = null;
-        } else {
-            throw new JsonParseException("document doesn't start with a valid json element : "
-                    + initialToken, jp.getCurrentLocation());
+        try {
+            Object jsonObject = JsonUtils.fromInputStream(in) ;
+            read$(jsonObject, baseURI, ct, output, context) ;
         }
-        
-        function.accept(rval);
-        
-        JsonToken t ;
-        try { t = jp.nextToken(); }
-        catch (JsonParseException ex) {
-            throw new JsonParseException("Document contains more content after json-ld element - (possible mismatched {}?)",
-                                         jp.getCurrentLocation());
+        catch (IOException e) {
+            IO.exception(e) ;
         }
-        if ( t != null )
-            throw new JsonParseException("Document contains possible json content after the json-ld element - (possible mismatched {}?)",
-                                             jp.getCurrentLocation());
     }
     
+    // This addresses jsonld-java issue #144 pre jsonld-java relase 0.6.0 in
+    // Jena code so that we get triples/quads out then there is a parse error.
+    // Even if it is fixed in jsonld-java, it would mean that no triples would
+    // be produced - all the JSON parsing is done before JSON-LD processing.
+    // Here we process the first JSON object, which causes triples to be
+    // generated then decide whether to throw a parse error. This is more in the
+    // style of other syntaxes and stream parsing.
+    
+//    @Override
+//    public void read(Reader reader, String baseURI, ContentType ct, StreamRDF output, Context context) {
+//        try {
+//            readProcess(reader, 
+//                       (jsonObject)->read$(jsonObject, baseURI, ct, output, context)) ;
+//        }
+//        catch (JsonProcessingException ex) {    
+//            // includes JsonParseException
+//            // The Jackson JSON parser, or addition JSON-level check, throws up something.
+//            JsonLocation loc = ex.getLocation() ;
+//            errorHandler.error(ex.getOriginalMessage(), loc.getLineNr(), loc.getColumnNr()); 
+//            throw new RiotException(ex.getOriginalMessage()) ;
+//        }
+//        catch (IOException e) {
+//            errorHandler.error(e.getMessage(), -1, -1); 
+//            IO.exception(e) ;
+//        }
+//    }
+//
+//    @Override
+//    public void read(InputStream in, String baseURI, ContentType ct, StreamRDF output, Context context) {
+//        Reader r = FileUtils.asBufferedUTF8(in) ;
+//        read(r, baseURI, ct, output, context) ;
+//    }
+//
+//    // From JsonUtils.fromReader in the jsonld-java codebase.
+//    // Note that jsonld-java always uses a reader.
+//    private static final ObjectMapper JSON_MAPPER = new ObjectMapper();
+//    private static final JsonFactory JSON_FACTORY = new JsonFactory(JSON_MAPPER);
+//    
+//    /** Read a JSON object from the reader, acall the prcoessing function, then
+//     * check for trailing content. For Jena, this means tripes/quads are generated
+//     * from a valid JSON object, then there is a  parse error for trailing junk.   
+//     * @param reader
+//     * @param function
+//     * @throws IOException
+//     */
+//    private static void readProcess(Reader reader, Consumer<Object> function) throws IOException {
+//        final JsonParser jp = JSON_FACTORY.createParser(reader);
+//        Object rval ;
+//        final JsonToken initialToken = jp.nextToken();
+//
+//        if (initialToken == JsonToken.START_ARRAY) {
+//            rval = jp.readValueAs(List.class);
+//        } else if (initialToken == JsonToken.START_OBJECT) {
+//            rval = jp.readValueAs(Map.class);
+//        } else if (initialToken == JsonToken.VALUE_STRING) {
+//            rval = jp.readValueAs(String.class);
+//        } else if (initialToken == JsonToken.VALUE_FALSE || initialToken == JsonToken.VALUE_TRUE) {
+//            rval = jp.readValueAs(Boolean.class);
+//        } else if (initialToken == JsonToken.VALUE_NUMBER_FLOAT
+//                || initialToken == JsonToken.VALUE_NUMBER_INT) {
+//            rval = jp.readValueAs(Number.class);
+//        } else if (initialToken == JsonToken.VALUE_NULL) {
+//            rval = null;
+//        } else {
+//            throw new JsonParseException("document doesn't start with a valid json element : "
+//                    + initialToken, jp.getCurrentLocation());
+//        }
+//        
+//        function.accept(rval);
+//        
+//        JsonToken t ;
+//        try { t = jp.nextToken(); }
+//        catch (JsonParseException ex) {
+//            throw new JsonParseException("Document contains more content after json-ld element - (possible mismatched {}?)",
+//                                         jp.getCurrentLocation());
+//        }
+//        if ( t != null )
+//            throw new JsonParseException("Document contains possible json content after the json-ld element - (possible mismatched {}?)",
+//                                             jp.getCurrentLocation());
+//    }
+    
     private void read$(Object jsonObject, String baseURI, ContentType ct, final StreamRDF output, Context context) {
         if ( parserProfile == null )
             parserProfile = RiotLib.profile(RDFLanguages.JSONLD, baseURI, errorHandler) ;