You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@jena.apache.org by afs <gi...@git.apache.org> on 2017/12/22 21:45:28 UTC

[GitHub] jena pull request #334: JENA-1454: Introduce builder pattern for result set ...

GitHub user afs opened a pull request:

    https://github.com/apache/jena/pull/334

    JENA-1454: Introduce builder pattern for result set reading and writing.

    Rework the machinery for ResultSet reading and writing to use a builder pattern (in ResultsReader and ResultsWriter).  This is styled after RDFParse/RDFParserBuilder and RDFWriter/RDFWriterBuilder.

You can merge this pull request into a Git repository by running:

    $ git pull https://github.com/afs/jena resultset-bnode

Alternatively you can review and apply these changes as the patch at:

    https://github.com/apache/jena/pull/334.patch

To close this pull request, make a commit to your master/trunk branch
with (at least) the following in the commit message:

    This closes #334
    
----
commit 716b86cfa300e8829dbc9238b7c67b711cdef1e2
Author: Andy Seaborne <an...@...>
Date:   2017-12-18T14:49:40Z

    JENA-1454: Introduce builder pattern for result set reading and writing.

----


---

[GitHub] jena pull request #334: JENA-1454: Introduce builder pattern for result set ...

Posted by kinow <gi...@git.apache.org>.
Github user kinow commented on a diff in the pull request:

    https://github.com/apache/jena/pull/334#discussion_r158567055
  
    --- Diff: jena-arq/src/main/java/org/apache/jena/riot/ResultSetMgr.java ---
    @@ -36,123 +41,216 @@
      * @see ResultSetFormatter 
      */
     public class ResultSetMgr {
    -    
    +    /**
    +     * Read from a {@code URL} (including filenames) and produce a {@link ResultSet}.
    +     * Note that returned result set may stream and so the input stream be read
    +     * while the ResultSet is used.
    +     * <p>
    +     * See {@link ResultSetFactory#copyResults(ResultSet)}
    +     * for a ResultSet that is detached from the {@code InputStream}.
    +     * 
    +     * @param urlOrFilename
    +     * @return ResultSet
    +     */
    +    public static ResultSet read(String urlOrFilename) {
    +        ResultSet rs = readAny(urlOrFilename).getResultSet();
    +        if ( rs == null )
    +            throw new ResultSetException("Not a result set"); 
    +        return rs;
    +    }
    +
    +
    +    /**
    +     * Read from a {@code URL} (including filenames) and produce a {@link ResultSet};
    +     * the stream is expect to use syntax {@code lang}.  Note that returned
    +     * result set may stream and so the input stream be read while the ResultSet is used.
    +     * See {@link ResultSetFactory#copyResults(ResultSet)}
    +     * for a ResultSet that is detached from the {@code InputStream}.
    +     * 
    +     * @param urlOrFilename
    +     * @param lang
    +     * @return ResultSet
    +     */
    +    public static ResultSet read(String urlOrFilename, Lang lang) {
    +        ResultSet rs = readAny(urlOrFilename, lang).getResultSet();
    +        if ( rs == null )
    +            throw new ResultSetException("Not a result set"); 
    +        return rs;
    +    }
    +
    +    /**
    +     * Read from a {@code URL} (including filenames) and produce a {@link ResultSet}.
    +     * Note that returned result set may stream and so the input stream be read
    +     * while the ResultSet is used.
    +     * <p>
    +     * See {@link ResultSetFactory#copyResults(ResultSet)}
    +     * for a ResultSet that is detached from the {@code InputStream}.
    +     * 
    +     * @param input
    +     * @return ResultSet
    +     */
    +    public static ResultSet read(InputStream input) {
    +        ResultSet rs = readAny(input).getResultSet();
    +        if ( rs == null )
    +            throw new ResultSetException("Not a result set"); 
    +        return rs;
    +    }
    +
         /**
          * Read from an {@code InputStream} and produce a {@link ResultSet};
          * the stream is expect to use syntax {@code lang}.  Note that returned
          * result set may stream and so the input stream be read while the ResultSet is used.
          * See {@link ResultSetFactory#copyResults(ResultSet)}
          * for a ResultSet that is detached from the {@code InputStream}.
          * 
    -     * @param in
    +     * @param input
          * @param lang
          * @return ResultSet
          */
    -    public static ResultSet read(InputStream in, Lang lang) {
    -        return process(TypedInputStream.wrap(in), null, lang, null) ;
    +    public static ResultSet read(InputStream input, Lang lang) {
    +        ResultSet rs = readAny(input, lang).getResultSet();
    +        if ( rs == null )
    +            throw new ResultSetException("Not a result set"); 
    +        return rs;
    +    }
    +
    +    private static void checkLang(Lang lang) {
    +        Objects.requireNonNull(lang);
    +        if ( ! ResultSetReaderRegistry.isRegistered(lang) ) {
    +            throw new ResultSetException("Not a result set syntax: "+lang);
    +        }
         }
         
    -    /** Read a result set from the URI */
    -    public static ResultSet read(String uri) {
    -        return read(uri, null) ;
    +    /** Read a boolean result from the URI
    +     * 
    +     * @param urlOrFilename
    +     * @return boolean
    +     */
    +    public static boolean readBoolean(String urlOrFilename) {
    +        Boolean b = readAny(urlOrFilename).getBooleanResult();
    +        return b;
         }
         
    -    /** Read a result set from the URI, in the specified syntax */ 
    -    public static ResultSet read(String uri, Lang lang) {
    -        return parse(uri, lang, null) ;
    +    /** Read a boolean result from the URI;
    +     * the input is expect to use syntax {@code lang}
    +     * 
    +     * @param urlOrFilename
    +     * @param lang
    +     * @return boolean
    +     */
    +    public static boolean readBoolean(String urlOrFilename, Lang lang) {
    +        Boolean b = readAny(urlOrFilename, lang).getBooleanResult();
    +        return b;
    +    }
    +    
    +    /** Read a boolean result from the URI
    +     * 
    +     * @param input
    +     * @return boolean
    +     */
    +    public static boolean readBoolean(InputStream input) {
    +        Boolean b = readAny(input).getBooleanResult();
    +        return b;
    +    }
    +    
    +    /** Read a boolean result from the URI;
    +     * the input is expect to use syntax {@code lang}
    +     * 
    +     * @param input
    +     * @param lang
    +     * @return boolean
    +     */
    +    public static boolean readBoolean(InputStream input, Lang lang) {
    +        Boolean b = readAny(input, lang).getBooleanResult();
    +        return b;
         }
     
    +    private static SPARQLResult readAny(String url) {
    +        return ResultsReader.create().build().readAny(url);
    +    }
    +
    +    private static SPARQLResult readAny(String url, Lang lang) {
    +        checkLang(lang);
    +        return ResultsReader.create()
    +            .lang(lang)
    +            .build()
    +            .readAny(url);
    +    }
    +    
    +    private static SPARQLResult readAny(InputStream input) {
    +        return ResultsReader.create().build().readAny(input);
    +    }
    +
    +    private static SPARQLResult readAny(InputStream input, Lang lang) {
    +        checkLang(lang);
    +        return ResultsReader.create()
    +            .lang(lang)
    +            .build()
    +            .readAny(input);
    +    }
    +    // -------------------------------
    +    
         /** Read ResultSet.
          * @param uri       URI to read from (includes file: and a plain file name).
          * @param hintLang  Hint for the syntax
          * @param context   Content object to control reading process.
          */
    -    public static ResultSet parse(String uri, Lang hintLang, Context context)
    -    {
    -        // Conneg
    -        if ( uri == null )
    -            throw new IllegalArgumentException("URI to read from is null") ;
    -        if ( hintLang == null )
    -            hintLang = RDFLanguages.filenameToLang(uri) ;
    -        TypedInputStream in = RDFDataMgr.open(uri, context) ;
    -        if ( in == null )
    -            throw new RiotException("Not found: "+uri) ;
    -        return process(in, uri, hintLang, context) ;
    -    }
    -        
    -    private static ResultSet process(TypedInputStream in, String srcURI, Lang hintLang, Context context) {
    -        ContentType ct = WebContent.determineCT(in.getContentType(), hintLang, srcURI) ;
    -        if ( ct == null )
    -            throw new RiotException("Failed to determine the content type: (URI="+srcURI+" : stream="+in.getContentType()+" : hint="+hintLang+")") ;
    -        ResultSetReader reader = getReader(ct) ;
    -        if ( reader == null )
    -            throw new RiotException("No parser registered for content type: "+ct.getContentType()) ;
    -        return reader.read(in, context) ;
    +    public static ResultSet parse(String uri, Lang hintLang, Context context) {
    +        ResultSet rs = ResultsReader.create().lang(hintLang).context(context).read(uri);
    --- End diff --
    
    In the `#readAny` methods, there's a `checkLang` call to verify whether the language is not null and supported. Do we need this check in all other public methods that use a `Lang` object? Or just in the `#readAny` methods? 


---

[GitHub] jena issue #334: JENA-1454: Introduce builder pattern for result set reading...

Posted by afs <gi...@git.apache.org>.
Github user afs commented on the issue:

    https://github.com/apache/jena/pull/334
  
    Needs some further cleanup before its ready to merge.
    
    The main syntax forms, XML, JSON support read/write with a `Context` for passing in configuration. Thrift does anyway.
    
    TSV, CSV, Text work in this framework but have not been deeply integrated.
    
    All old mechanisms to access features specific to a syntax should still work.


---

[GitHub] jena pull request #334: JENA-1454: Introduce builder pattern for result set ...

Posted by afs <gi...@git.apache.org>.
Github user afs commented on a diff in the pull request:

    https://github.com/apache/jena/pull/334#discussion_r158602513
  
    --- Diff: jena-arq/src/main/java/org/apache/jena/riot/resultset/rw/ResultsWriter.java ---
    @@ -77,19 +79,33 @@ private ResultsWriter(Lang lang, Context context) {
             this.context = context;
         }
     
    -    public void write(String url, ResultSet resultSet) {
    -        throw new NotImplemented();
    +    /** Write a result set, using the configurartion of the {@code ResultWriter}, to a file */ 
    +    public void write(String filename, ResultSet resultSet) {
    +        Objects.requireNonNull(filename);
    +        Objects.requireNonNull(resultSet);
    +        try ( OutputStream out = openURL(filename) ) {
    +            write(out, resultSet);
    +        } catch (IOException ex) { IO.exception(ex); }
         }
         
    +    /** Write a result set, using the configurartion of the {@code ResultWriter}, to an {@code OutputStream}. */ 
         public void write(OutputStream output, ResultSet resultSet) {
    +        Objects.requireNonNull(output);
    +        Objects.requireNonNull(resultSet);
             write(output, resultSet, null, lang);
         }
         
    -    public void write(String url, boolean booleanResult) {
    -        throw new NotImplemented();
    +    /** Write a boolean result, using the configurartion of the {@code ResultWriter}, to a file */ 
    +    public void write(String filename, boolean booleanResult) {
    +        Objects.requireNonNull(booleanResult);
    --- End diff --
    
    Thanks - fixed. Testing the wrong argument; `Objects.requireNonNull(booleanResult)` is never null!


---

[GitHub] jena pull request #334: JENA-1454: Introduce builder pattern for result set ...

Posted by kinow <gi...@git.apache.org>.
Github user kinow commented on a diff in the pull request:

    https://github.com/apache/jena/pull/334#discussion_r158590480
  
    --- Diff: jena-arq/src/main/java/org/apache/jena/riot/resultset/rw/ResultsWriter.java ---
    @@ -77,19 +79,33 @@ private ResultsWriter(Lang lang, Context context) {
             this.context = context;
         }
     
    -    public void write(String url, ResultSet resultSet) {
    -        throw new NotImplemented();
    +    /** Write a result set, using the configurartion of the {@code ResultWriter}, to a file */ 
    +    public void write(String filename, ResultSet resultSet) {
    +        Objects.requireNonNull(filename);
    +        Objects.requireNonNull(resultSet);
    +        try ( OutputStream out = openURL(filename) ) {
    +            write(out, resultSet);
    +        } catch (IOException ex) { IO.exception(ex); }
         }
         
    +    /** Write a result set, using the configurartion of the {@code ResultWriter}, to an {@code OutputStream}. */ 
         public void write(OutputStream output, ResultSet resultSet) {
    +        Objects.requireNonNull(output);
    +        Objects.requireNonNull(resultSet);
             write(output, resultSet, null, lang);
         }
         
    -    public void write(String url, boolean booleanResult) {
    -        throw new NotImplemented();
    +    /** Write a boolean result, using the configurartion of the {@code ResultWriter}, to a file */ 
    +    public void write(String filename, boolean booleanResult) {
    +        Objects.requireNonNull(booleanResult);
    --- End diff --
    
    Only minor nit-pick I found reviewing the commits added after my previous comment was here.
    
    `write(String filename, ResultSet resultSet)` has `requireNonNull(filename)`, and `requireNonNull(resultSet)`. And in this method the `filename` is not checked for null. Not sure if it is intentional or not.


---

[GitHub] jena pull request #334: JENA-1454: Introduce builder pattern for result set ...

Posted by afs <gi...@git.apache.org>.
Github user afs commented on a diff in the pull request:

    https://github.com/apache/jena/pull/334#discussion_r158571855
  
    --- Diff: jena-arq/src/main/java/org/apache/jena/riot/ResultSetMgr.java ---
    @@ -36,123 +41,216 @@
      * @see ResultSetFormatter 
      */
     public class ResultSetMgr {
    -    
    +    /**
    +     * Read from a {@code URL} (including filenames) and produce a {@link ResultSet}.
    +     * Note that returned result set may stream and so the input stream be read
    +     * while the ResultSet is used.
    +     * <p>
    +     * See {@link ResultSetFactory#copyResults(ResultSet)}
    +     * for a ResultSet that is detached from the {@code InputStream}.
    +     * 
    +     * @param urlOrFilename
    +     * @return ResultSet
    +     */
    +    public static ResultSet read(String urlOrFilename) {
    +        ResultSet rs = readAny(urlOrFilename).getResultSet();
    +        if ( rs == null )
    +            throw new ResultSetException("Not a result set"); 
    +        return rs;
    +    }
    +
    +
    +    /**
    +     * Read from a {@code URL} (including filenames) and produce a {@link ResultSet};
    +     * the stream is expect to use syntax {@code lang}.  Note that returned
    +     * result set may stream and so the input stream be read while the ResultSet is used.
    +     * See {@link ResultSetFactory#copyResults(ResultSet)}
    +     * for a ResultSet that is detached from the {@code InputStream}.
    +     * 
    +     * @param urlOrFilename
    +     * @param lang
    +     * @return ResultSet
    +     */
    +    public static ResultSet read(String urlOrFilename, Lang lang) {
    +        ResultSet rs = readAny(urlOrFilename, lang).getResultSet();
    +        if ( rs == null )
    +            throw new ResultSetException("Not a result set"); 
    +        return rs;
    +    }
    +
    +    /**
    +     * Read from a {@code URL} (including filenames) and produce a {@link ResultSet}.
    +     * Note that returned result set may stream and so the input stream be read
    +     * while the ResultSet is used.
    +     * <p>
    +     * See {@link ResultSetFactory#copyResults(ResultSet)}
    +     * for a ResultSet that is detached from the {@code InputStream}.
    +     * 
    +     * @param input
    +     * @return ResultSet
    +     */
    +    public static ResultSet read(InputStream input) {
    +        ResultSet rs = readAny(input).getResultSet();
    +        if ( rs == null )
    +            throw new ResultSetException("Not a result set"); 
    +        return rs;
    +    }
    +
         /**
          * Read from an {@code InputStream} and produce a {@link ResultSet};
          * the stream is expect to use syntax {@code lang}.  Note that returned
          * result set may stream and so the input stream be read while the ResultSet is used.
          * See {@link ResultSetFactory#copyResults(ResultSet)}
          * for a ResultSet that is detached from the {@code InputStream}.
          * 
    -     * @param in
    +     * @param input
          * @param lang
          * @return ResultSet
          */
    -    public static ResultSet read(InputStream in, Lang lang) {
    -        return process(TypedInputStream.wrap(in), null, lang, null) ;
    +    public static ResultSet read(InputStream input, Lang lang) {
    +        ResultSet rs = readAny(input, lang).getResultSet();
    +        if ( rs == null )
    +            throw new ResultSetException("Not a result set"); 
    +        return rs;
    +    }
    +
    +    private static void checkLang(Lang lang) {
    +        Objects.requireNonNull(lang);
    +        if ( ! ResultSetReaderRegistry.isRegistered(lang) ) {
    +            throw new ResultSetException("Not a result set syntax: "+lang);
    +        }
         }
         
    -    /** Read a result set from the URI */
    -    public static ResultSet read(String uri) {
    -        return read(uri, null) ;
    +    /** Read a boolean result from the URI
    +     * 
    +     * @param urlOrFilename
    +     * @return boolean
    +     */
    +    public static boolean readBoolean(String urlOrFilename) {
    +        Boolean b = readAny(urlOrFilename).getBooleanResult();
    +        return b;
         }
         
    -    /** Read a result set from the URI, in the specified syntax */ 
    -    public static ResultSet read(String uri, Lang lang) {
    -        return parse(uri, lang, null) ;
    +    /** Read a boolean result from the URI;
    +     * the input is expect to use syntax {@code lang}
    +     * 
    +     * @param urlOrFilename
    +     * @param lang
    +     * @return boolean
    +     */
    +    public static boolean readBoolean(String urlOrFilename, Lang lang) {
    +        Boolean b = readAny(urlOrFilename, lang).getBooleanResult();
    +        return b;
    +    }
    +    
    +    /** Read a boolean result from the URI
    +     * 
    +     * @param input
    +     * @return boolean
    +     */
    +    public static boolean readBoolean(InputStream input) {
    +        Boolean b = readAny(input).getBooleanResult();
    +        return b;
    +    }
    +    
    +    /** Read a boolean result from the URI;
    +     * the input is expect to use syntax {@code lang}
    +     * 
    +     * @param input
    +     * @param lang
    +     * @return boolean
    +     */
    +    public static boolean readBoolean(InputStream input, Lang lang) {
    +        Boolean b = readAny(input, lang).getBooleanResult();
    +        return b;
         }
     
    +    private static SPARQLResult readAny(String url) {
    +        return ResultsReader.create().build().readAny(url);
    +    }
    +
    +    private static SPARQLResult readAny(String url, Lang lang) {
    +        checkLang(lang);
    +        return ResultsReader.create()
    +            .lang(lang)
    +            .build()
    +            .readAny(url);
    +    }
    +    
    +    private static SPARQLResult readAny(InputStream input) {
    +        return ResultsReader.create().build().readAny(input);
    +    }
    +
    +    private static SPARQLResult readAny(InputStream input, Lang lang) {
    +        checkLang(lang);
    +        return ResultsReader.create()
    +            .lang(lang)
    +            .build()
    +            .readAny(input);
    +    }
    +    // -------------------------------
    +    
         /** Read ResultSet.
          * @param uri       URI to read from (includes file: and a plain file name).
          * @param hintLang  Hint for the syntax
          * @param context   Content object to control reading process.
          */
    -    public static ResultSet parse(String uri, Lang hintLang, Context context)
    -    {
    -        // Conneg
    -        if ( uri == null )
    -            throw new IllegalArgumentException("URI to read from is null") ;
    -        if ( hintLang == null )
    -            hintLang = RDFLanguages.filenameToLang(uri) ;
    -        TypedInputStream in = RDFDataMgr.open(uri, context) ;
    -        if ( in == null )
    -            throw new RiotException("Not found: "+uri) ;
    -        return process(in, uri, hintLang, context) ;
    -    }
    -        
    -    private static ResultSet process(TypedInputStream in, String srcURI, Lang hintLang, Context context) {
    -        ContentType ct = WebContent.determineCT(in.getContentType(), hintLang, srcURI) ;
    -        if ( ct == null )
    -            throw new RiotException("Failed to determine the content type: (URI="+srcURI+" : stream="+in.getContentType()+" : hint="+hintLang+")") ;
    -        ResultSetReader reader = getReader(ct) ;
    -        if ( reader == null )
    -            throw new RiotException("No parser registered for content type: "+ct.getContentType()) ;
    -        return reader.read(in, context) ;
    +    public static ResultSet parse(String uri, Lang hintLang, Context context) {
    +        ResultSet rs = ResultsReader.create().lang(hintLang).context(context).read(uri);
    --- End diff --
    
    I'll add checks to `ResultReader` and `ResultWriter` and use try-resource,


---

[GitHub] jena pull request #334: JENA-1454: Introduce builder pattern for result set ...

Posted by asfgit <gi...@git.apache.org>.
Github user asfgit closed the pull request at:

    https://github.com/apache/jena/pull/334


---

[GitHub] jena pull request #334: JENA-1454: Introduce builder pattern for result set ...

Posted by kinow <gi...@git.apache.org>.
Github user kinow commented on a diff in the pull request:

    https://github.com/apache/jena/pull/334#discussion_r158571105
  
    --- Diff: jena-arq/src/main/java/org/apache/jena/riot/ResultSetMgr.java ---
    @@ -36,123 +41,216 @@
      * @see ResultSetFormatter 
      */
     public class ResultSetMgr {
    -    
    +    /**
    +     * Read from a {@code URL} (including filenames) and produce a {@link ResultSet}.
    +     * Note that returned result set may stream and so the input stream be read
    +     * while the ResultSet is used.
    +     * <p>
    +     * See {@link ResultSetFactory#copyResults(ResultSet)}
    +     * for a ResultSet that is detached from the {@code InputStream}.
    +     * 
    +     * @param urlOrFilename
    +     * @return ResultSet
    +     */
    +    public static ResultSet read(String urlOrFilename) {
    +        ResultSet rs = readAny(urlOrFilename).getResultSet();
    +        if ( rs == null )
    +            throw new ResultSetException("Not a result set"); 
    +        return rs;
    +    }
    +
    +
    +    /**
    +     * Read from a {@code URL} (including filenames) and produce a {@link ResultSet};
    +     * the stream is expect to use syntax {@code lang}.  Note that returned
    +     * result set may stream and so the input stream be read while the ResultSet is used.
    +     * See {@link ResultSetFactory#copyResults(ResultSet)}
    +     * for a ResultSet that is detached from the {@code InputStream}.
    +     * 
    +     * @param urlOrFilename
    +     * @param lang
    +     * @return ResultSet
    +     */
    +    public static ResultSet read(String urlOrFilename, Lang lang) {
    +        ResultSet rs = readAny(urlOrFilename, lang).getResultSet();
    +        if ( rs == null )
    +            throw new ResultSetException("Not a result set"); 
    +        return rs;
    +    }
    +
    +    /**
    +     * Read from a {@code URL} (including filenames) and produce a {@link ResultSet}.
    +     * Note that returned result set may stream and so the input stream be read
    +     * while the ResultSet is used.
    +     * <p>
    +     * See {@link ResultSetFactory#copyResults(ResultSet)}
    +     * for a ResultSet that is detached from the {@code InputStream}.
    +     * 
    +     * @param input
    +     * @return ResultSet
    +     */
    +    public static ResultSet read(InputStream input) {
    +        ResultSet rs = readAny(input).getResultSet();
    +        if ( rs == null )
    +            throw new ResultSetException("Not a result set"); 
    +        return rs;
    +    }
    +
         /**
          * Read from an {@code InputStream} and produce a {@link ResultSet};
          * the stream is expect to use syntax {@code lang}.  Note that returned
          * result set may stream and so the input stream be read while the ResultSet is used.
          * See {@link ResultSetFactory#copyResults(ResultSet)}
          * for a ResultSet that is detached from the {@code InputStream}.
          * 
    -     * @param in
    +     * @param input
          * @param lang
          * @return ResultSet
          */
    -    public static ResultSet read(InputStream in, Lang lang) {
    -        return process(TypedInputStream.wrap(in), null, lang, null) ;
    +    public static ResultSet read(InputStream input, Lang lang) {
    +        ResultSet rs = readAny(input, lang).getResultSet();
    +        if ( rs == null )
    +            throw new ResultSetException("Not a result set"); 
    +        return rs;
    +    }
    +
    +    private static void checkLang(Lang lang) {
    +        Objects.requireNonNull(lang);
    +        if ( ! ResultSetReaderRegistry.isRegistered(lang) ) {
    +            throw new ResultSetException("Not a result set syntax: "+lang);
    +        }
         }
         
    -    /** Read a result set from the URI */
    -    public static ResultSet read(String uri) {
    -        return read(uri, null) ;
    +    /** Read a boolean result from the URI
    +     * 
    +     * @param urlOrFilename
    +     * @return boolean
    +     */
    +    public static boolean readBoolean(String urlOrFilename) {
    +        Boolean b = readAny(urlOrFilename).getBooleanResult();
    +        return b;
         }
         
    -    /** Read a result set from the URI, in the specified syntax */ 
    -    public static ResultSet read(String uri, Lang lang) {
    -        return parse(uri, lang, null) ;
    +    /** Read a boolean result from the URI;
    +     * the input is expect to use syntax {@code lang}
    +     * 
    +     * @param urlOrFilename
    +     * @param lang
    +     * @return boolean
    +     */
    +    public static boolean readBoolean(String urlOrFilename, Lang lang) {
    +        Boolean b = readAny(urlOrFilename, lang).getBooleanResult();
    +        return b;
    +    }
    +    
    +    /** Read a boolean result from the URI
    +     * 
    +     * @param input
    +     * @return boolean
    +     */
    +    public static boolean readBoolean(InputStream input) {
    +        Boolean b = readAny(input).getBooleanResult();
    +        return b;
    +    }
    +    
    +    /** Read a boolean result from the URI;
    +     * the input is expect to use syntax {@code lang}
    +     * 
    +     * @param input
    +     * @param lang
    +     * @return boolean
    +     */
    +    public static boolean readBoolean(InputStream input, Lang lang) {
    +        Boolean b = readAny(input, lang).getBooleanResult();
    +        return b;
         }
     
    +    private static SPARQLResult readAny(String url) {
    +        return ResultsReader.create().build().readAny(url);
    +    }
    +
    +    private static SPARQLResult readAny(String url, Lang lang) {
    +        checkLang(lang);
    +        return ResultsReader.create()
    +            .lang(lang)
    +            .build()
    +            .readAny(url);
    +    }
    +    
    +    private static SPARQLResult readAny(InputStream input) {
    +        return ResultsReader.create().build().readAny(input);
    +    }
    +
    +    private static SPARQLResult readAny(InputStream input, Lang lang) {
    +        checkLang(lang);
    +        return ResultsReader.create()
    +            .lang(lang)
    +            .build()
    +            .readAny(input);
    +    }
    +    // -------------------------------
    +    
         /** Read ResultSet.
          * @param uri       URI to read from (includes file: and a plain file name).
          * @param hintLang  Hint for the syntax
          * @param context   Content object to control reading process.
          */
    -    public static ResultSet parse(String uri, Lang hintLang, Context context)
    -    {
    -        // Conneg
    -        if ( uri == null )
    -            throw new IllegalArgumentException("URI to read from is null") ;
    -        if ( hintLang == null )
    -            hintLang = RDFLanguages.filenameToLang(uri) ;
    -        TypedInputStream in = RDFDataMgr.open(uri, context) ;
    -        if ( in == null )
    -            throw new RiotException("Not found: "+uri) ;
    -        return process(in, uri, hintLang, context) ;
    -    }
    -        
    -    private static ResultSet process(TypedInputStream in, String srcURI, Lang hintLang, Context context) {
    -        ContentType ct = WebContent.determineCT(in.getContentType(), hintLang, srcURI) ;
    -        if ( ct == null )
    -            throw new RiotException("Failed to determine the content type: (URI="+srcURI+" : stream="+in.getContentType()+" : hint="+hintLang+")") ;
    -        ResultSetReader reader = getReader(ct) ;
    -        if ( reader == null )
    -            throw new RiotException("No parser registered for content type: "+ct.getContentType()) ;
    -        return reader.read(in, context) ;
    +    public static ResultSet parse(String uri, Lang hintLang, Context context) {
    +        ResultSet rs = ResultsReader.create().lang(hintLang).context(context).read(uri);
    --- End diff --
    
    :+1: makes sense. Happy to take another look once you say it's complete and ready to be merged. Thanks for the quick reply.


---

[GitHub] jena pull request #334: JENA-1454: Introduce builder pattern for result set ...

Posted by afs <gi...@git.apache.org>.
Github user afs commented on a diff in the pull request:

    https://github.com/apache/jena/pull/334#discussion_r158571022
  
    --- Diff: jena-arq/src/main/java/org/apache/jena/riot/ResultSetMgr.java ---
    @@ -36,123 +41,216 @@
      * @see ResultSetFormatter 
      */
     public class ResultSetMgr {
    -    
    +    /**
    +     * Read from a {@code URL} (including filenames) and produce a {@link ResultSet}.
    +     * Note that returned result set may stream and so the input stream be read
    +     * while the ResultSet is used.
    +     * <p>
    +     * See {@link ResultSetFactory#copyResults(ResultSet)}
    +     * for a ResultSet that is detached from the {@code InputStream}.
    +     * 
    +     * @param urlOrFilename
    +     * @return ResultSet
    +     */
    +    public static ResultSet read(String urlOrFilename) {
    +        ResultSet rs = readAny(urlOrFilename).getResultSet();
    +        if ( rs == null )
    +            throw new ResultSetException("Not a result set"); 
    +        return rs;
    +    }
    +
    +
    +    /**
    +     * Read from a {@code URL} (including filenames) and produce a {@link ResultSet};
    +     * the stream is expect to use syntax {@code lang}.  Note that returned
    +     * result set may stream and so the input stream be read while the ResultSet is used.
    +     * See {@link ResultSetFactory#copyResults(ResultSet)}
    +     * for a ResultSet that is detached from the {@code InputStream}.
    +     * 
    +     * @param urlOrFilename
    +     * @param lang
    +     * @return ResultSet
    +     */
    +    public static ResultSet read(String urlOrFilename, Lang lang) {
    +        ResultSet rs = readAny(urlOrFilename, lang).getResultSet();
    +        if ( rs == null )
    +            throw new ResultSetException("Not a result set"); 
    +        return rs;
    +    }
    +
    +    /**
    +     * Read from a {@code URL} (including filenames) and produce a {@link ResultSet}.
    +     * Note that returned result set may stream and so the input stream be read
    +     * while the ResultSet is used.
    +     * <p>
    +     * See {@link ResultSetFactory#copyResults(ResultSet)}
    +     * for a ResultSet that is detached from the {@code InputStream}.
    +     * 
    +     * @param input
    +     * @return ResultSet
    +     */
    +    public static ResultSet read(InputStream input) {
    +        ResultSet rs = readAny(input).getResultSet();
    +        if ( rs == null )
    +            throw new ResultSetException("Not a result set"); 
    +        return rs;
    +    }
    +
         /**
          * Read from an {@code InputStream} and produce a {@link ResultSet};
          * the stream is expect to use syntax {@code lang}.  Note that returned
          * result set may stream and so the input stream be read while the ResultSet is used.
          * See {@link ResultSetFactory#copyResults(ResultSet)}
          * for a ResultSet that is detached from the {@code InputStream}.
          * 
    -     * @param in
    +     * @param input
          * @param lang
          * @return ResultSet
          */
    -    public static ResultSet read(InputStream in, Lang lang) {
    -        return process(TypedInputStream.wrap(in), null, lang, null) ;
    +    public static ResultSet read(InputStream input, Lang lang) {
    +        ResultSet rs = readAny(input, lang).getResultSet();
    +        if ( rs == null )
    +            throw new ResultSetException("Not a result set"); 
    +        return rs;
    +    }
    +
    +    private static void checkLang(Lang lang) {
    +        Objects.requireNonNull(lang);
    +        if ( ! ResultSetReaderRegistry.isRegistered(lang) ) {
    +            throw new ResultSetException("Not a result set syntax: "+lang);
    +        }
         }
         
    -    /** Read a result set from the URI */
    -    public static ResultSet read(String uri) {
    -        return read(uri, null) ;
    +    /** Read a boolean result from the URI
    +     * 
    +     * @param urlOrFilename
    +     * @return boolean
    +     */
    +    public static boolean readBoolean(String urlOrFilename) {
    +        Boolean b = readAny(urlOrFilename).getBooleanResult();
    +        return b;
         }
         
    -    /** Read a result set from the URI, in the specified syntax */ 
    -    public static ResultSet read(String uri, Lang lang) {
    -        return parse(uri, lang, null) ;
    +    /** Read a boolean result from the URI;
    +     * the input is expect to use syntax {@code lang}
    +     * 
    +     * @param urlOrFilename
    +     * @param lang
    +     * @return boolean
    +     */
    +    public static boolean readBoolean(String urlOrFilename, Lang lang) {
    +        Boolean b = readAny(urlOrFilename, lang).getBooleanResult();
    +        return b;
    +    }
    +    
    +    /** Read a boolean result from the URI
    +     * 
    +     * @param input
    +     * @return boolean
    +     */
    +    public static boolean readBoolean(InputStream input) {
    +        Boolean b = readAny(input).getBooleanResult();
    +        return b;
    +    }
    +    
    +    /** Read a boolean result from the URI;
    +     * the input is expect to use syntax {@code lang}
    +     * 
    +     * @param input
    +     * @param lang
    +     * @return boolean
    +     */
    +    public static boolean readBoolean(InputStream input, Lang lang) {
    +        Boolean b = readAny(input, lang).getBooleanResult();
    +        return b;
         }
     
    +    private static SPARQLResult readAny(String url) {
    +        return ResultsReader.create().build().readAny(url);
    +    }
    +
    +    private static SPARQLResult readAny(String url, Lang lang) {
    +        checkLang(lang);
    +        return ResultsReader.create()
    +            .lang(lang)
    +            .build()
    +            .readAny(url);
    +    }
    +    
    +    private static SPARQLResult readAny(InputStream input) {
    +        return ResultsReader.create().build().readAny(input);
    +    }
    +
    +    private static SPARQLResult readAny(InputStream input, Lang lang) {
    +        checkLang(lang);
    +        return ResultsReader.create()
    +            .lang(lang)
    +            .build()
    +            .readAny(input);
    +    }
    +    // -------------------------------
    +    
         /** Read ResultSet.
          * @param uri       URI to read from (includes file: and a plain file name).
          * @param hintLang  Hint for the syntax
          * @param context   Content object to control reading process.
          */
    -    public static ResultSet parse(String uri, Lang hintLang, Context context)
    -    {
    -        // Conneg
    -        if ( uri == null )
    -            throw new IllegalArgumentException("URI to read from is null") ;
    -        if ( hintLang == null )
    -            hintLang = RDFLanguages.filenameToLang(uri) ;
    -        TypedInputStream in = RDFDataMgr.open(uri, context) ;
    -        if ( in == null )
    -            throw new RiotException("Not found: "+uri) ;
    -        return process(in, uri, hintLang, context) ;
    -    }
    -        
    -    private static ResultSet process(TypedInputStream in, String srcURI, Lang hintLang, Context context) {
    -        ContentType ct = WebContent.determineCT(in.getContentType(), hintLang, srcURI) ;
    -        if ( ct == null )
    -            throw new RiotException("Failed to determine the content type: (URI="+srcURI+" : stream="+in.getContentType()+" : hint="+hintLang+")") ;
    -        ResultSetReader reader = getReader(ct) ;
    -        if ( reader == null )
    -            throw new RiotException("No parser registered for content type: "+ct.getContentType()) ;
    -        return reader.read(in, context) ;
    +    public static ResultSet parse(String uri, Lang hintLang, Context context) {
    +        ResultSet rs = ResultsReader.create().lang(hintLang).context(context).read(uri);
    --- End diff --
    
    All read public methods of `ResultSetMgr` that take a `Lang` use `readAny` so `lang` gets checked.
    
    Adding a check to the write methods makes sense (now done in local working copy) because it's necessary (no default included, no legacy requirement).
    
    `ResultSetMgr`isn't the main API (`ResultSetFormatter` is, albeit older and clunkier).
    
    For `parse`, the method is only there because there was one before this upgrade. There isn't a "no lang" version. lang==null makes sense because the `uri` is used (conneg or file extension).
    
    Some of the `if ( rs == null )` checks are actually unnecessary because `SPARQLResult` does not return null, it throws an exception, but have been left in place in case that changes.



---

[GitHub] jena issue #334: JENA-1454: Introduce builder pattern for result set reading...

Posted by kinow <gi...@git.apache.org>.
Github user kinow commented on the issue:

    https://github.com/apache/jena/pull/334
  
    +1


---

[GitHub] jena issue #334: JENA-1454: Introduce builder pattern for result set reading...

Posted by afs <gi...@git.apache.org>.
Github user afs commented on the issue:

    https://github.com/apache/jena/pull/334
  
    Cleaning up and switching to new reader/writer code now done.
    
    This PR is now complete.



---