jena-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From a...@apache.org
Subject svn commit: r1362068 - in /jena/Scratch/AFS/Dev/trunk/src/main/java/projects/riot_reader: Langs.java PROCESS.java RDFReaderRIOT.java RDFReaderRIOT_ARP.java TestJenaReaderRIOT.java WebReader2.java
Date Mon, 16 Jul 2012 14:43:47 GMT
Author: andy
Date: Mon Jul 16 14:43:46 2012
New Revision: 1362068

URL: http://svn.apache.org/viewvc?rev=1362068&view=rev
Log: (empty)

Modified:
    jena/Scratch/AFS/Dev/trunk/src/main/java/projects/riot_reader/Langs.java
    jena/Scratch/AFS/Dev/trunk/src/main/java/projects/riot_reader/PROCESS.java
    jena/Scratch/AFS/Dev/trunk/src/main/java/projects/riot_reader/RDFReaderRIOT.java
    jena/Scratch/AFS/Dev/trunk/src/main/java/projects/riot_reader/RDFReaderRIOT_ARP.java
    jena/Scratch/AFS/Dev/trunk/src/main/java/projects/riot_reader/TestJenaReaderRIOT.java
    jena/Scratch/AFS/Dev/trunk/src/main/java/projects/riot_reader/WebReader2.java

Modified: jena/Scratch/AFS/Dev/trunk/src/main/java/projects/riot_reader/Langs.java
URL: http://svn.apache.org/viewvc/jena/Scratch/AFS/Dev/trunk/src/main/java/projects/riot_reader/Langs.java?rev=1362068&r1=1362067&r2=1362068&view=diff
==============================================================================
--- jena/Scratch/AFS/Dev/trunk/src/main/java/projects/riot_reader/Langs.java (original)
+++ jena/Scratch/AFS/Dev/trunk/src/main/java/projects/riot_reader/Langs.java Mon Jul 16 14:43:46
2012
@@ -223,7 +223,7 @@ public class Langs
     private static void addContentTypeLang(ContentType contentType, Lang2 lang)
     { mapContentTypeToLang.put(contentType.getContentType(), lang) ; }
     
-    private static Lang2 contentTypeToLang(String contentType)
+    static Lang2 contentTypeToLang(String contentType)
     { return mapContentTypeToLang.get(contentType) ; }
     
     static Lang2 contentTypeToLang(ContentType contentType)
@@ -282,7 +282,7 @@ public class Langs
         return lang.getContentType().getContentType() ;
     }
 
-    private static Lang convert(Lang2 language)
+    static Lang convert(Lang2 language)
     {
         if ( language.equals(langRDFXML))     return Lang.RDFXML ;
         if ( language.equals(langTurtle))     return Lang.TURTLE ;

Modified: jena/Scratch/AFS/Dev/trunk/src/main/java/projects/riot_reader/PROCESS.java
URL: http://svn.apache.org/viewvc/jena/Scratch/AFS/Dev/trunk/src/main/java/projects/riot_reader/PROCESS.java?rev=1362068&r1=1362067&r2=1362068&view=diff
==============================================================================
--- jena/Scratch/AFS/Dev/trunk/src/main/java/projects/riot_reader/PROCESS.java (original)
+++ jena/Scratch/AFS/Dev/trunk/src/main/java/projects/riot_reader/PROCESS.java Mon Jul 16
14:43:46 2012
@@ -28,20 +28,22 @@ public class PROCESS
     //   => does not use hint lang because hint lang not passed down to .read.
     // 2 - Webreader2 - does not have base override operations.
     
+    // Quad tests
+    
     // Lang/Lang2
     // Use of Lang as an enum in switch statements.
     
-    // webreader2.prcoessTriples - 
+    // webreader2.processTriples - 
     // if hint is different from extensions file type?
     //   Currently, extension wins.  Opps.
     
+    // java.io.Reader as input.
+    
     // Update documentation
     //  http://jena.apache.org/documentation/io/index.html
     //  http://jena.apache.org/documentation/io/iohowto.html
     //  http://jena.apache.org/documentation/io/riot.html
     
-    //  RDFReaderRIOT - input stream and reader versions
-    
     // Webreader2 - ops with a base.
     
     /*

Modified: jena/Scratch/AFS/Dev/trunk/src/main/java/projects/riot_reader/RDFReaderRIOT.java
URL: http://svn.apache.org/viewvc/jena/Scratch/AFS/Dev/trunk/src/main/java/projects/riot_reader/RDFReaderRIOT.java?rev=1362068&r1=1362067&r2=1362068&view=diff
==============================================================================
--- jena/Scratch/AFS/Dev/trunk/src/main/java/projects/riot_reader/RDFReaderRIOT.java (original)
+++ jena/Scratch/AFS/Dev/trunk/src/main/java/projects/riot_reader/RDFReaderRIOT.java Mon Jul
16 14:43:46 2012
@@ -22,8 +22,6 @@ import java.io.InputStream ;
 import java.io.Reader ;
 import java.util.Locale ;
 
-import org.openjena.atlas.lib.NotImplemented ;
-
 import com.hp.hpl.jena.rdf.model.Model ;
 import com.hp.hpl.jena.rdf.model.RDFErrorHandler ;
 import com.hp.hpl.jena.rdf.model.RDFReader ;
@@ -56,17 +54,15 @@ public class RDFReaderRIOT implements RD
 
     @Override
     public void read(Model model, Reader r, String base)
-    { throw new NotImplemented() ; }
+    { WebReader2.read(model, r, base, hintlang) ; }
 
     @Override
     public void read(Model model, InputStream r, String base)
-    { throw new NotImplemented() ; }
+    { WebReader2.read(model, r, base, hintlang) ; }
     
     @Override
     public void read(Model model, String url)
-    {
-        WebReader2.read(model, url, hintlang) ;
-    }
+    { WebReader2.read(model, url, hintlang) ; }
 
     @Override
     public Object setProperty(String propName, Object propValue)

Modified: jena/Scratch/AFS/Dev/trunk/src/main/java/projects/riot_reader/RDFReaderRIOT_ARP.java
URL: http://svn.apache.org/viewvc/jena/Scratch/AFS/Dev/trunk/src/main/java/projects/riot_reader/RDFReaderRIOT_ARP.java?rev=1362068&r1=1362067&r2=1362068&view=diff
==============================================================================
--- jena/Scratch/AFS/Dev/trunk/src/main/java/projects/riot_reader/RDFReaderRIOT_ARP.java (original)
+++ jena/Scratch/AFS/Dev/trunk/src/main/java/projects/riot_reader/RDFReaderRIOT_ARP.java Mon
Jul 16 14:43:46 2012
@@ -54,11 +54,6 @@ public class RDFReaderRIOT_ARP implement
     public void read(Model model, String url)
     {
         fileMgr.readModel(model, url) ;
-//        int i = url.indexOf(':') ;
-//        
-//        if ( i < 0 || i == 1 )  // 1 => Windows drive.
-//            url = "file:"+url ;
-//        reader.read(model, url) ;
     }
 
     @Override

Modified: jena/Scratch/AFS/Dev/trunk/src/main/java/projects/riot_reader/TestJenaReaderRIOT.java
URL: http://svn.apache.org/viewvc/jena/Scratch/AFS/Dev/trunk/src/main/java/projects/riot_reader/TestJenaReaderRIOT.java?rev=1362068&r1=1362067&r2=1362068&view=diff
==============================================================================
--- jena/Scratch/AFS/Dev/trunk/src/main/java/projects/riot_reader/TestJenaReaderRIOT.java
(original)
+++ jena/Scratch/AFS/Dev/trunk/src/main/java/projects/riot_reader/TestJenaReaderRIOT.java
Mon Jul 16 14:43:46 2012
@@ -18,6 +18,10 @@
 
 package projects.riot_reader;
 
+import java.io.FileInputStream ;
+import java.io.IOException ;
+import java.io.StringReader ;
+
 import org.junit.AfterClass ;
 import org.junit.BeforeClass ;
 import org.junit.Test ;
@@ -27,6 +31,7 @@ import com.hp.hpl.jena.rdf.model.Model ;
 import com.hp.hpl.jena.rdf.model.ModelFactory ;
 import com.hp.hpl.jena.rdf.model.Resource ;
 import com.hp.hpl.jena.sparql.util.Context ;
+import com.hp.hpl.jena.util.FileUtils ;
 
 public class TestJenaReaderRIOT extends BaseTest
 {
@@ -71,9 +76,46 @@ public class TestJenaReaderRIOT extends 
     @Test public void read_30() { jenaread("D-not-TTL.ttl", "RDF/XML") ; } // Override extension
     
     @Test public void read_base_1() { jenaread("D-no-base.ttl", "http://baseuri/", "TTL")
; }
-
-    // Base jumping.
     
+    @Test public void read_input_1() throws IOException
+    { jenaread_stream("D.ttl", "TTL") ; }
+        
+    @Test public void read_input_2() throws IOException
+    { jenaread_stream("D.rdf", "RDF/XML") ; }
+    
+    private static void jenaread_stream(String filename, String lang) throws IOException
+    {
+        filename = directory+"/"+filename ;
+        
+        // Read with a base
+        FileInputStream in0 = new FileInputStream(filename) ;
+        Model m0 = ModelFactory.createDefaultModel() ;
+        WebReader2.read(m0, in0, "http://example/base2", lang) ;
+        in0.close() ;
+
+        // Read again, but without base
+        FileInputStream in1 = new FileInputStream(filename) ;
+        Model m1 = ModelFactory.createDefaultModel() ;
+        WebReader2.read(m1, in1, lang) ;
+        in1.close() ;
+        
+        // Fail because Jena core does a look up of lang with ModelCom builtin in RDFReaderF,
then calls RIOReader().
+        // 1/ Fix Jena - remove RDFReaderF
+        // 2/ Change RDFReaderF to pass in the language name. 
+        
+        if ( true ) return ;
+        
+        // Read via Jena API.
+        Model m2 = ModelFactory.createDefaultModel() ;
+        FileInputStream in2 = new FileInputStream(filename) ;
+        m2.read(in2, "http://example/base3", lang) ;
+        in2.close() ;
+        
+        String x = FileUtils.readWholeFileAsUTF8(filename) ;
+        Model m3 = ModelFactory.createDefaultModel() ;
+        m2.read(new StringReader(x), "http://example/base4", lang) ;
+    }
+
     // Web.
     // TriG
     // NQ

Modified: jena/Scratch/AFS/Dev/trunk/src/main/java/projects/riot_reader/WebReader2.java
URL: http://svn.apache.org/viewvc/jena/Scratch/AFS/Dev/trunk/src/main/java/projects/riot_reader/WebReader2.java?rev=1362068&r1=1362067&r2=1362068&view=diff
==============================================================================
--- jena/Scratch/AFS/Dev/trunk/src/main/java/projects/riot_reader/WebReader2.java (original)
+++ jena/Scratch/AFS/Dev/trunk/src/main/java/projects/riot_reader/WebReader2.java Mon Jul
16 14:43:46 2012
@@ -18,11 +18,17 @@
 
 package projects.riot_reader;
 
+import java.io.InputStream ;
+import java.io.Reader ;
+
 import org.openjena.atlas.lib.Sink ;
 import org.openjena.atlas.web.ContentType ;
 import org.openjena.riot.* ;
+import org.openjena.riot.lang.LangRIOT ;
 import org.openjena.riot.lang.SinkQuadsToDataset ;
 import org.openjena.riot.lang.SinkTriplesToGraph ;
+import org.openjena.riot.tokens.Tokenizer ;
+import org.openjena.riot.tokens.TokenizerFactory ;
 import org.slf4j.Logger ;
 import org.slf4j.LoggerFactory ;
 import projects.riot_reader.fm2.FileManager2 ;
@@ -64,7 +70,6 @@ public class WebReader2
     private static String riotBase = "http://jena.apache.org/riot/" ; 
     private static String fileManagerSymbolStr = riotBase+"filemanager" ; 
     public static Symbol fileManagerSymbol = Symbol.create(fileManagerSymbolStr) ; 
-    
 
     public static void wireIntoJena()
     {
@@ -90,33 +95,68 @@ public class WebReader2
     /** Read triples into a Model from the given location. 
      * @see #read(Model,String,String,Context) 
      */
-    public static void read(Model m, String uri)                    { read(m, uri, null,
null) ; }
+    public static void read(Model model, String uri)                    { read(model, uri,
null, null) ; }
     
     /** Read triples into a Model from the given location, with a hint of the language (MIME
type) 
      *  @see #read(Model,String,String,Context) 
      */
-    public static void read(Model m, String uri, String hintLang)   { read(m, uri, hintLang,
null) ; }
+    public static void read(Model model, String uri, String hintLang)   { read(model, uri,
hintLang, null) ; }
     
     /** Read triples into a Model from the given location, with some parameters for the reader
      *  @see #read(Model,String,String,Context) 
      */ 
-    public static void read(Model m, String uri, Context context)   { read(m, uri, null,
context) ; }
+    public static void read(Model model, String uri, Context context)   { read(model, uri,
null, context) ; }
     
     /** Read triples into a Model from the given location, with hint of langauge and the
with some parameters for the reader 
     *  Throws {@link RiotNotFoundException} if the location is not found - the model is unchanged.
-    *  Throws parse errors depending on the language and reader; the model maybe partially
updated.
+    *  Throws parse errors depending on the language and reader; the Model modelaybe partially
updated.
     *  The hint language is   
     */
-    public static void read(Model m, String uri, String hintLang, Context context)
+    public static void read(Model model, String uri, String hintLang, Context context)
     {
-        Graph g = m.getGraph() ;
+        Graph g = model.getGraph() ;
         Sink<Triple> sink = new SinkTriplesToGraph(g) ;
         readTriples(sink, uri, hintLang, context) ;
     }
+    
+    /** Read triples into a Model with bytes from an InputStream.
+     *  A base URI and a syntax can be provided.
+     *  The base URI defualts to "no base" in which case the data should have no relative
URIs.
+     *  The lang gives the syntax of the stream. 
+     */
+
+    public static void read(Model model, InputStream in, String lang)
+    {
+        read(model, in, null, lang) ;
+    }
+        
+    /** Read triples into a Model with bytes from an InputStream.
+     *  A base URI and a syntax can be provided.
+     *  The base URI defualts to "no base" in which case the data should have no relative
URIs.
+     *  The lang gives the syntax of the stream. 
+     */
+
+    public static void read(Model model, InputStream in, String base, String lang)
+    {
+        Graph g = model.getGraph() ;
+        Sink<Triple> sink = new SinkTriplesToGraph(g) ;
+        processTriples(sink, base, new TypedInputStream2(in), lang, null) ;
+    }
+
+    /** Read triples into a model with chars from an Reader.
+     * Use java.io.Readers is not encouraged - use with a StringReader is the primary use
case.
+     * For files, open a {@link java.io.FileInputStream} to ensure correct character set
handling.
+     */
+    public static void read(Model model, Reader in, String base, String hintlang)
+    {
+        Graph g = model.getGraph() ;
+        Sink<Triple> sink = new SinkTriplesToGraph(g) ;
+        processTriples(sink, base, in, hintlang, null) ;
+    }
 
     /** Read quads into a Dataset from the given location, with hint of langauge and the
with some parameters for the reader 
      *  Throws {@link RiotNotFoundException} if the location is not found - the model is
unchanged.
-     *  Throws parse errors depending on the language and reader; the model maybe partially
updated. 
+     *  Throws parse errors depending on the language and reader; the Model modelaybe partially
updated. 
      */
     public static void read(Dataset dataset, String uri, String hintLang, Context context)
     {
@@ -124,9 +164,18 @@ public class WebReader2
         Sink<Quad> sink = new SinkQuadsToDataset(dsg) ;
         readQuads(sink, uri, hintLang, context) ;
     }
-    
-    // Lang2 lang = Langs.contentTypeToLang(ct) ;
-    // ReaderRIOTFactory<Triple> r = Langs.langToTriples.get(lang) ; 
+
+    /** Read triples into a model with chars from an Reader.
+     * Use java.io.Readers is not encouraged - use with a StringReader is the primary use
case.
+     * For files, open a {@link java.io.FileInputStream} to ensure correct character set
handling.
+     */
+    public static void read(Dataset dataset, Reader in, String base, String hintlang)
+    {
+        DatasetGraph dsg = dataset.asDatasetGraph() ;
+        Sink<Quad> sink = new SinkQuadsToDataset(dsg) ;
+        processQuads(sink, base, in, hintlang, null) ;
+    }
+
     
     public static void addTripleSyntax(Lang2 language, ContentType contentType, ReaderRIOTFactory<Triple>
factory, String ... fileExt )
     { 
@@ -202,7 +251,7 @@ public class WebReader2
     // We could have had two step design - ReaderFactory-ReaderInstance
     // no - put the bruden on complicated readers, not everyone. 
     
-    private static void processTriples(Sink<Triple> sink, String uri, TypedInputStream2
in, String hintLang, Context context)
+    private static void processTriples(Sink<Triple> sink, String base, TypedInputStream2
in, String hintLang, Context context)
     {
         boolean isTextPlain = WebContent.contentTypeTextPlain.equals(in.getContentType())
;
         
@@ -222,7 +271,7 @@ public class WebReader2
         if ( reader == null )
             throw new RiotException("No reader for content type: "+_ct) ;
         
-        reader.read(in.getInput(), uri, ct, sink, context) ;
+        reader.read(in.getInput(), base, ct, sink, context) ;
     }
 
     private static ReaderRIOT<Triple> getReaderTriples(ContentType ct)
@@ -233,6 +282,17 @@ public class WebReader2
             return null ;
         return r.create(lang) ;
     }
+
+    // java.io.Readers are NOT preferred.
+    private static void processTriples(Sink<Triple> sink, String base, Reader in, String
hintLang, Context context)
+    {
+        Tokenizer tokenizer = TokenizerFactory.makeTokenizer(in) ;
+        String ct = Langs.jenaNameToContentType(hintLang) ;
+        Lang2 lang2 = Langs.contentTypeToLang(ct) ;
+        Lang lang = Langs.convert(lang2) ;
+        LangRIOT parser = RiotReader.createParserTriples(tokenizer, lang, base, sink) ;
+        parser.parse() ;
+    }
     
     private static void processQuads(Sink<Quad> sink, String uri, TypedInputStream2
in, String hintLang, Context context)
     {
@@ -264,15 +324,15 @@ public class WebReader2
         return r.create(lang) ;
     }
     
-//    static HttpResponseHandler turtleHandler = new HttpResponseHandler(){
-//        @Override
-//        public void handle(String contentType, String baseIRI, HttpResponse response) throws
IOException
-//        {}} ;
-//    
-//    static Map<String, HttpResponseHandler> handlers = new HashMap<>() ;
-//    static {
-//        handlers.put(WebContent.contentTypeTurtle, null ) ;
-//    }
-    
+    // java.io.Readers are NOT preferred.
+    private static void processQuads(Sink<Quad> sink, String base, Reader in, String
hintLang, Context context)
+    {
+        Tokenizer tokenizer = TokenizerFactory.makeTokenizer(in) ;
+        String ct = Langs.jenaNameToContentType(hintLang) ;
+        Lang2 lang2 = Langs.contentTypeToLang(ct) ;
+        Lang lang = Langs.convert(lang2) ;
+        LangRIOT parser = RiotReader.createParserQuads(tokenizer, lang, base, sink) ;
+        parser.parse() ;
+    }
 }
 



Mime
View raw message