[ https://issues.apache.org/jira/browse/JENA-1454?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16302141#comment-16302141 ]
ASF GitHub Bot commented on JENA-1454: -------------------------------------- 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, > Configurable ResultSet reading and writing > ------------------------------------------ > > Key: JENA-1454 > URL: https://issues.apache.org/jira/browse/JENA-1454 > Project: Apache Jena > Issue Type: Improvement > Reporter: Andy Seaborne > Assignee: Andy Seaborne > Priority: Minor > > Same patterns as {{RDFParser}}, {{RDFWriter}}. -- This message was sent by Atlassian JIRA (v6.4.14#64029)