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 2012/06/03 19:13:52 UTC

svn commit: r1345715 - in /jena/Scratch/AFS/Dev/trunk/src/main/java/projects/riot_reader: Lang2.java Langs.java WebReader2.java

Author: andy
Date: Sun Jun  3 17:13:52 2012
New Revision: 1345715

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

Modified:
    jena/Scratch/AFS/Dev/trunk/src/main/java/projects/riot_reader/Lang2.java
    jena/Scratch/AFS/Dev/trunk/src/main/java/projects/riot_reader/Langs.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/Lang2.java
URL: http://svn.apache.org/viewvc/jena/Scratch/AFS/Dev/trunk/src/main/java/projects/riot_reader/Lang2.java?rev=1345715&r1=1345714&r2=1345715&view=diff
==============================================================================
--- jena/Scratch/AFS/Dev/trunk/src/main/java/projects/riot_reader/Lang2.java (original)
+++ jena/Scratch/AFS/Dev/trunk/src/main/java/projects/riot_reader/Lang2.java Sun Jun  3 17:13:52 2012
@@ -21,14 +21,12 @@ package projects.riot_reader;
 import java.util.UUID ;
 
 
-// Should this generalize to result set formats?
-// DataFormat.
 public class Lang2 
 {
     private final String label ;
 
     /** Create a language with a well-known name,
-     * All languages with the same name wil be treated as the same language.
+     * All languages with the same name will be treated as the same language.
      */
     static public Lang2 create(String label)
     {
@@ -44,7 +42,7 @@ public class Lang2 
     protected Lang2(String label)
     { 
         if ( label == null )
-            throw new IllegalArgumentException("Nul not allowed for language name") ;
+            throw new IllegalArgumentException("Null not allowed for language name") ;
         else
             label = label.intern();
         this.label = label ;

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=1345715&r1=1345714&r2=1345715&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 Sun Jun  3 17:13:52 2012
@@ -19,7 +19,6 @@
 package projects.riot_reader;
 
 import java.io.InputStream ;
-import java.util.HashMap ;
 import java.util.Map ;
 
 import org.openjena.atlas.lib.DS ;
@@ -37,27 +36,8 @@ import com.hp.hpl.jena.sparql.util.Conte
 
 public class Langs
 {
-    // Constants and wiring.  Work-in-progress. 
-    
-    // Wiring:
-    //    WebReader2
-    //    
- 
-    // A parser encapsulates a process
-    // A parser can be called more than once (internally it must cope with this)
-    // A parser has exclusive access the input stream (no concurrency at this level). 
-    
-    // A parser factory generates parsers.
-    // 
-
-    // --------
-    
-    // See Reader interface and combine.
-    
-//    // Formally one-shot or many?
-//    //   Accumulator model.??
-//    static interface Parser { void parse(InputStream in, String baseURI) ; }
-    
+    // Constants and wiring. 
+
     // Languages as constants.  Lang is an "open enum" (you can add your own),
     // registered with their MIME type or official name,
     static Lang2 langRDFXML     = Lang2.create(WebContent.contentTypeRDFXML) ;
@@ -70,14 +50,14 @@ public class Langs
     static Lang2 langNQuads     = Lang2.create(WebContent.contentTypeNQuads) ;
     static Lang2 langTriG       = Lang2.create(WebContent.contentTypeTriG) ;
 
-    private static Map<Lang2, ReaderRIOTFactory<Triple>> langToTriples = DS.map() ;
-    private static Map<Lang2, ReaderRIOTFactory<Quad>> langToQuads = DS.map() ;
-    private static Map<String, Lang2> mapContentTypeToLang = new HashMap<String, Lang2>() ;
-    private static MultiMap<Lang2, String> langToExt = MultiMap.createMapList() ;
-    private static Map<String, Lang2> extToLang = DS.map() ;
-    private static Map<String, Lang2> jenaName = DS.map() ;
+    private static Map<Lang2, ReaderRIOTFactory<Triple>> langToTriples  = DS.map() ;
+    private static Map<Lang2, ReaderRIOTFactory<Quad>> langToQuads      = DS.map() ;
+    private static Map<String, Lang2> mapContentTypeToLang              = DS.map() ;
+    private static MultiMap<Lang2, String> langToExt                    = MultiMap.createMapList() ;
+    private static Map<String, Lang2> extToLang                         = DS.map() ;
+    private static Map<String, Lang2> jenaName                          = DS.map() ;
    
-    // This code understands different types fo things it can read:
+    // This code understands different types of things it can read:
     //   triples, quads, result sets, unknown
     // These form (disjoint) value spaces static ReaderFactory<Triple> pfTriples = new ReaderFactory<Triple>() {
     // This is needed because we need to determine the output type of the reading process.
@@ -259,6 +239,22 @@ public class Langs
     {
         return convert(Lang.guess(resourceIRI)) ;
     }
+    
+    /** Attempt to guess the content type string from a language name */   
+    public static String jenaNameToContentType(String jenaShortName)
+    {
+        return jenaNameToContentType(jenaShortName, null) ;
+    }
+    
+    /** Attempt to guess the content type string from a language name */   
+    public static String jenaNameToContentType(String jenaShortName, String dft)
+    {
+        Lang2 lang = Lang2.create(jenaShortName) ;
+        String str = jenaName.get(lang).getName() ;
+        if ( str == null )
+            return dft ;
+        return str ;
+    }
 
     // Temporary.
     private static Lang2 convert(Lang lang)

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=1345715&r1=1345714&r2=1345715&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 Sun Jun  3 17:13:52 2012
@@ -18,8 +18,6 @@
 
 package projects.riot_reader;
 
-import java.io.InputStream ;
-
 import org.openjena.atlas.lib.Sink ;
 import org.openjena.riot.* ;
 import org.openjena.riot.lang.SinkQuadsToDataset ;
@@ -113,23 +111,24 @@ public class WebReader2
     }
     
     /** Read triples into a Model from the given location. 
-     * @see #read(Model,String,Context) 
+     * @see #read(Model,String,String,Context) 
      */
     public static void read(Model m, String uri)                    { read(m, uri, null, null) ; }
     
     /** Read triples into a Model from the given location, with a hint of the language (MIME type) 
-     *  @see #read(Model,String,Context) 
+     *  @see #read(Model,String,String,Context) 
      */
     public static void read(Model m, String uri, String hintLang)   { read(m, uri, hintLang, null) ; }
     
     /** Read triples into a Model from the given location, with some parameters for the reader
-     *  @see #read(Model,String,Context) 
+     *  @see #read(Model,String,String,Context) 
      */ 
     public static void read(Model m, String uri, Context context)   { read(m, 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 maybe partially updated.
+    *  The hint language is   
     */
     public static void read(Model m, String uri, String hintLang, Context context)
     {
@@ -234,14 +233,18 @@ public class WebReader2
         if ( in.getMediaType() != null )
             _ct = WebContent.contentTypeCanonical(in.getContentType()) ;
         if ( ( _ct == null || isTextPlain ) && hintLang != null )
-            _ct = hintLang ;
-        // Error? ....
-        ContentType ct = ContentType.parse(_ct) ;  // TODO Error
+            _ct = Langs.jenaNameToContentType(_ct, _ct) ;
+
+        if ( _ct == null )
+            throw new RiotException("Failed to find the content type") ;
+        
+        ContentType ct = ContentType.parse(_ct) ;
         if ( ct == null )
-        {} // TODO Error
+            throw new RiotException("Failed to parse content type: "+_ct) ;
         ReaderRIOT<Triple> reader = getReaderTriples(ct) ;
         if ( reader == null )
-        {}  // TODO Error
+            throw new RiotException("No reader for content type: "+_ct) ;
+        
         reader.read(in.getInput(), uri, ct, sink, context) ;
     }
 
@@ -260,14 +263,18 @@ public class WebReader2
         if ( in.getMediaType() != null )
             _ct = WebContent.contentTypeCanonical(in.getContentType()) ;
         if ( _ct == null && hintLang != null )
-            _ct = hintLang ;
-        // Error? ....
-        ContentType ct = ContentType.parse(_ct) ;  // TODO Error
+            _ct = Langs.jenaNameToContentType(_ct, _ct) ;
+
+        if ( _ct == null )
+            throw new RiotException("Failed to find the content type") ;
+
+        ContentType ct = ContentType.parse(_ct) ;
         if ( ct == null )
-        {} // TODO Error
+            throw new RiotException("Failed to parse content type: "+_ct) ;
         ReaderRIOT<Quad> reader = getReaderQuads(ct) ;
         if ( reader == null )
-        {}  // TODO Error
+            throw new RiotException("No reader for content type: "+_ct) ;
+        
         reader.read(in.getInput(), uri, ct, sink, context) ;
     }
 
@@ -280,21 +287,6 @@ public class WebReader2
         return r.create(lang) ;
     }
     
-    /** General reader-of-triples for a fixed language */
-    static class ReaderLangTriple implements ReaderRIOT<Triple>
-    {
-        private final Lang lang ;
-
-        ReaderLangTriple(Lang lang) { this.lang = lang ; }
-        
-        @Override
-        public void read(InputStream in, String baseURI, ContentType ct, Sink<Triple> sink, Context context)
-        {
-            RiotLoader.readTriples(in, lang, baseURI, sink) ;
-        }
-    } 
-
-    
 //    static HttpResponseHandler turtleHandler = new HttpResponseHandler(){
 //        @Override
 //        public void handle(String contentType, String baseIRI, HttpResponse response) throws IOException