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 2014/08/08 22:16:15 UTC
svn commit: r1616868 - in /jena/trunk/jena-arq/src:
main/java/org/apache/jena/riot/ main/java/org/apache/jena/riot/resultset/
test/java/org/apache/jena/riot/ test/java/org/apache/jena/riot/resultset/
Author: andy
Date: Fri Aug 8 20:16:15 2014
New Revision: 1616868
URL: http://svn.apache.org/r1616868
Log:
JENA-762: Initial implementations (InputStream and OutputStream only)
Added:
jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/resultset/
jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/resultset/ResultSetLang.java
jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/resultset/ResultSetMgr.java
jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/resultset/ResultSetReader.java
jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/resultset/ResultSetReaderFactory.java
jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/resultset/ResultSetReaderRegistry.java
jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/resultset/ResultSetWriter.java
jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/resultset/ResultSetWriterFactory.java
jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/resultset/ResultSetWriterRegistry.java
jena/trunk/jena-arq/src/test/java/org/apache/jena/riot/resultset/
jena/trunk/jena-arq/src/test/java/org/apache/jena/riot/resultset/TS_ResultSetRIOT.java
jena/trunk/jena-arq/src/test/java/org/apache/jena/riot/resultset/TestResultSetIO.java
Modified:
jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/RDFLanguages.java
jena/trunk/jena-arq/src/test/java/org/apache/jena/riot/TC_Riot.java
Modified: jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/RDFLanguages.java
URL: http://svn.apache.org/viewvc/jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/RDFLanguages.java?rev=1616868&r1=1616867&r2=1616868&view=diff
==============================================================================
--- jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/RDFLanguages.java (original)
+++ jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/RDFLanguages.java Fri Aug 8 20:16:15 2014
@@ -124,6 +124,12 @@ public class RDFLanguages
.addFileExtensions("csv")
.build() ;
+ /** Text */
+ public static final Lang TEXT = LangBuilder.create("text", contentTypeTextPlain)
+ .addAltNames("TEXT")
+ .addFileExtensions("txt")
+ .build() ;
+
/** The "null" language */
public static final Lang RDFNULL = LangBuilder.create("rdf/null", "null/rdf")
.addAltNames("NULL", "null")
Added: jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/resultset/ResultSetLang.java
URL: http://svn.apache.org/viewvc/jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/resultset/ResultSetLang.java?rev=1616868&view=auto
==============================================================================
--- jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/resultset/ResultSetLang.java (added)
+++ jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/resultset/ResultSetLang.java Fri Aug 8 20:16:15 2014
@@ -0,0 +1,67 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.jena.riot.resultset;
+
+import org.apache.jena.riot.Lang ;
+import org.apache.jena.riot.LangBuilder ;
+import org.apache.jena.riot.RDFLanguages ;
+import org.apache.jena.riot.WebContent ;
+
+public class ResultSetLang {
+ public static final Lang SPARQLResultSetXML
+ = LangBuilder.create("SPARQL-Results-XML", WebContent.contentTypeResultsXML)
+ .addAltNames("SRX")
+ .addFileExtensions("srx")
+ .build() ;
+
+ public static final Lang SPARQLResultSetJSON
+ = LangBuilder.create("SPARQL-Results-JSON", WebContent.contentTypeResultsJSON)
+ .addAltNames("SRJ")
+ .addFileExtensions("srj")
+ .build() ;
+
+ public static final Lang SPARQLResultSetCSV = Lang.CSV ;
+// = LangBuilder.create("CSV", WebContent.contentTypeResultsXML)
+// .addAltNames("SRX")
+// .addFileExtensions("srx")
+// .build() ;
+ public static final Lang SPARQLResultSetTSV
+ = LangBuilder.create("TSV", WebContent.contentTypeTextTSV)
+ .addFileExtensions("tsv")
+ .build() ;
+
+ public static final Lang SPARQLResultSetThrift
+ = LangBuilder.create("SPARQL-Results-Thrift", "application/sparql-results+thrift")
+ .addAltNames("SRT")
+ .addFileExtensions("srt")
+ .build() ;
+
+ public static final Lang SPARQLResultSetText
+ = LangBuilder.create("SPARQL-Results-Text", WebContent.contentTypeTextPlain)
+ .addFileExtensions("txt")
+ .build() ;
+
+ public static void init() {
+ RDFLanguages.register(SPARQLResultSetXML) ;
+ RDFLanguages.register(SPARQLResultSetJSON) ;
+ RDFLanguages.register(SPARQLResultSetCSV) ;
+ RDFLanguages.register(SPARQLResultSetTSV) ;
+ RDFLanguages.register(SPARQLResultSetThrift) ;
+ }
+}
Added: jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/resultset/ResultSetMgr.java
URL: http://svn.apache.org/viewvc/jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/resultset/ResultSetMgr.java?rev=1616868&view=auto
==============================================================================
--- jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/resultset/ResultSetMgr.java (added)
+++ jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/resultset/ResultSetMgr.java Fri Aug 8 20:16:15 2014
@@ -0,0 +1,108 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.jena.riot.resultset;
+
+import java.io.InputStream ;
+import java.io.OutputStream ;
+
+import org.apache.jena.atlas.web.TypedInputStream ;
+import org.apache.jena.riot.Lang ;
+import org.apache.jena.riot.RDFDataMgr ;
+import org.apache.jena.riot.RDFLanguages ;
+import org.apache.jena.riot.RiotException ;
+
+import com.hp.hpl.jena.query.ARQ ;
+import com.hp.hpl.jena.query.ResultSet ;
+import com.hp.hpl.jena.query.ResultSetFactory ;
+import com.hp.hpl.jena.sparql.util.Context ;
+
+/**
+ * Reading and writing of Result Sets
+ */
+public class ResultSetMgr {
+ static {
+
+ ResultSetReaderRegistry.init() ;
+ ResultSetWriterRegistry.init() ;
+ }
+
+ // Register with RDFLanguages.
+
+ /**
+ * Read from an {@code InputStream} and produce a {@linkplain 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 {@linkplain ResultSetFactory#copyResults(ResultSet)}
+ * for a ResultSet that is detached from the {@code InputStream}.
+ *
+ * @param in
+ * @param lang
+ * @return ResultSet
+ */
+ public static ResultSet read(InputStream in, Lang lang) {
+ ResultSetReaderFactory f = ResultSetReaderRegistry.lookup(lang) ;
+ if ( f == null )
+ throw new RiotException("No result set reader for "+lang) ;
+ ResultSetReader rsr = f.create(lang) ;
+ return rsr.read(in, ARQ.getContext()) ;
+ }
+
+ public static ResultSet read(String uri) {
+ return read(uri, null) ;
+ }
+
+ public static ResultSet read(String uri, Lang lang) {
+ return parse(uri, lang, null) ;
+ }
+
+ /** 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)
+ {
+ 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) ;
+ //ct -> lang
+ return process(in, hintLang) ;
+ }
+ // Read from URL.
+
+ private static ResultSet process(InputStream in, Lang lang) {
+ ResultSetReaderFactory f = ResultSetReaderRegistry.lookup(lang) ;
+ if ( f == null )
+ throw new RiotException("No result set reader for "+lang) ;
+ ResultSetReader rsr = f.create(lang) ;
+ return rsr.read(in, ARQ.getContext()) ;
+ }
+
+ // ----
+
+ public static void write(OutputStream out, ResultSet resultSet, Lang lang) {
+ ResultSetWriterFactory f = ResultSetWriterRegistry.lookup(lang) ;
+ f.create(lang).write(out, resultSet, null) ;
+ }
+}
+
Added: jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/resultset/ResultSetReader.java
URL: http://svn.apache.org/viewvc/jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/resultset/ResultSetReader.java?rev=1616868&view=auto
==============================================================================
--- jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/resultset/ResultSetReader.java (added)
+++ jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/resultset/ResultSetReader.java Fri Aug 8 20:16:15 2014
@@ -0,0 +1,53 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.jena.riot.resultset;
+
+import java.io.InputStream ;
+import java.io.Reader ;
+
+import com.hp.hpl.jena.query.ResultSet ;
+import com.hp.hpl.jena.query.ResultSetFactory ;
+import com.hp.hpl.jena.sparql.util.Context ;
+
+public interface ResultSetReader {
+
+ /**
+ * Read from an {@code InputStream} and produce a {@linkplain ResultSet}.
+ * Note that return result may stream and so the input stream be read
+ * while the ResultSet is used.
+ * See {@linkplain ResultSetFactory#copyResults(ResultSet)} for a ResultSet that is detached from the {@code InputStream}.
+ * @param in InputStream to read from.
+ * @param context
+ * @return ResultSet
+ */
+ public ResultSet read(InputStream in, Context context) ;
+
+ /**
+ * Using {@link #read(InputStream, Context)} is preferred.
+ * Read from an {@code Reader} and produce a {@linkplain ResultSet}.
+ * Note that return result may stream and so the reader be read
+ * while the ResultSet is used.
+ * See {@linkplain ResultSetFactory#copyResults(ResultSet)} for a ResultSet that is detached from the {@code InputStream}.
+ * @param in Reader
+ * @param context
+ * @return ResultSet
+ */
+ public ResultSet read(Reader in, Context context) ;
+}
+
Added: jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/resultset/ResultSetReaderFactory.java
URL: http://svn.apache.org/viewvc/jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/resultset/ResultSetReaderFactory.java?rev=1616868&view=auto
==============================================================================
--- jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/resultset/ResultSetReaderFactory.java (added)
+++ jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/resultset/ResultSetReaderFactory.java Fri Aug 8 20:16:15 2014
@@ -0,0 +1,26 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.jena.riot.resultset;
+
+import org.apache.jena.riot.Lang ;
+
+/* Create a one-time use ResultSetReader */
+public interface ResultSetReaderFactory {
+ public ResultSetReader create(Lang lang) ;
+}
Added: jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/resultset/ResultSetReaderRegistry.java
URL: http://svn.apache.org/viewvc/jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/resultset/ResultSetReaderRegistry.java?rev=1616868&view=auto
==============================================================================
--- jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/resultset/ResultSetReaderRegistry.java (added)
+++ jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/resultset/ResultSetReaderRegistry.java Fri Aug 8 20:16:15 2014
@@ -0,0 +1,108 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.jena.riot.resultset;
+
+import static org.apache.jena.riot.resultset.ResultSetLang.SPARQLResultSetCSV ;
+import static org.apache.jena.riot.resultset.ResultSetLang.SPARQLResultSetJSON ;
+import static org.apache.jena.riot.resultset.ResultSetLang.SPARQLResultSetTSV ;
+import static org.apache.jena.riot.resultset.ResultSetLang.SPARQLResultSetThrift ;
+import static org.apache.jena.riot.resultset.ResultSetLang.SPARQLResultSetXML ;
+
+import java.io.InputStream ;
+import java.io.Reader ;
+import java.util.HashMap ;
+import java.util.Map ;
+import java.util.Objects ;
+
+import org.apache.jena.riot.Lang ;
+import org.apache.jena.riot.RiotException ;
+
+import com.hp.hpl.jena.query.ResultSet ;
+import com.hp.hpl.jena.query.ResultSetFactory ;
+import com.hp.hpl.jena.sparql.resultset.CSVInput ;
+import com.hp.hpl.jena.sparql.resultset.JSONInput ;
+import com.hp.hpl.jena.sparql.resultset.XMLInput ;
+import com.hp.hpl.jena.sparql.util.Context ;
+
+public class ResultSetReaderRegistry {
+
+ /** Lookup a {@linkplain Lang} to get the registered {@linkplain ResultSetReaderFactory} (or null) */
+ public static ResultSetReaderFactory lookup(Lang lang) {
+ Objects.requireNonNull(lang) ;
+ return registry.get(lang) ;
+ }
+
+ /** Register a {@linkplain ResultSetReaderFactory} for a {@linkplain Lang} */
+ public static void register(Lang lang, ResultSetReaderFactory factory) {
+ Objects.requireNonNull(lang) ;
+ Objects.requireNonNull(factory) ;
+ registry.put(lang, factory) ;
+ }
+
+ private static Map<Lang, ResultSetReaderFactory> registry = new HashMap<>() ;
+ static { init(); }
+
+ /*package*/ static void init() {
+ ResultSetReaderFactory factory = new ResultSetReaderFactoryStd() ;
+ register(SPARQLResultSetXML, factory) ;
+ register(SPARQLResultSetJSON, factory) ;
+ register(SPARQLResultSetCSV, factory) ;
+ register(SPARQLResultSetTSV, factory) ;
+ register(SPARQLResultSetThrift, factory) ;
+ }
+
+ static { init(); }
+
+ private static class ResultSetReaderFactoryStd implements ResultSetReaderFactory {
+ @Override
+ public ResultSetReader create(Lang lang) {
+ lang = Objects.requireNonNull(lang, "Language must not be null") ;
+ if ( lang.equals(SPARQLResultSetXML) ) return readerXML ;
+ if ( lang.equals(SPARQLResultSetJSON) ) return readerJSON ;
+ if ( lang.equals(SPARQLResultSetCSV) ) return readerCSV ;
+ if ( lang.equals(SPARQLResultSetTSV) ) return readerTSV ;
+ throw new RiotException("Lang not registered (ResultSet reader)") ;
+ }
+ }
+
+ private static ResultSetReader readerXML = new ResultSetReader() {
+ @Override public ResultSet read(InputStream in, Context context) { return XMLInput.fromXML(in); }
+ @Override public ResultSet read(Reader in, Context context) { return null ; }
+ } ;
+
+ private static ResultSetReader readerJSON = new ResultSetReader() {
+ @Override public ResultSet read(InputStream in, Context context) { return JSONInput.fromJSON(in) ; }
+ @Override public ResultSet read(Reader in, Context context) { return null ; }
+ } ;
+
+ private static ResultSetReader readerCSV = new ResultSetReader() {
+ @Override public ResultSet read(InputStream in, Context context) { return CSVInput.fromCSV(in) ; }
+ @Override public ResultSet read(Reader in, Context context) { return null ; }
+ } ;
+
+ private static ResultSetReader readerTSV = new ResultSetReader() {
+ @Override public ResultSet read(InputStream in, Context context) { return ResultSetFactory.fromTSV(in) ; }
+ @Override public ResultSet read(Reader in, Context context) { return null ; }
+ } ;
+
+ private static ResultSetReader readerNo = new ResultSetReader() {
+ @Override public ResultSet read(InputStream in, Context context) { return null ; }
+ @Override public ResultSet read(Reader in, Context context) { return null ; }
+ } ;
+}
Added: jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/resultset/ResultSetWriter.java
URL: http://svn.apache.org/viewvc/jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/resultset/ResultSetWriter.java?rev=1616868&view=auto
==============================================================================
--- jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/resultset/ResultSetWriter.java (added)
+++ jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/resultset/ResultSetWriter.java Fri Aug 8 20:16:15 2014
@@ -0,0 +1,37 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.jena.riot.resultset;
+
+import java.io.OutputStream ;
+import java.io.Writer ;
+
+import com.hp.hpl.jena.query.ResultSet ;
+import com.hp.hpl.jena.sparql.util.Context ;
+
+public interface ResultSetWriter {
+ /** Write the ResultSet to the OutputStream */
+ public void write(OutputStream out, ResultSet resultSet, Context context) ;
+
+ /**
+ * Using {@linkplain #write(OutputStream, ResultSet, Context)} is preferred.
+ * Write the ResultSet to the Writer
+ */
+ public void write(Writer out, ResultSet resultSet, Context context) ;
+}
+
Added: jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/resultset/ResultSetWriterFactory.java
URL: http://svn.apache.org/viewvc/jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/resultset/ResultSetWriterFactory.java?rev=1616868&view=auto
==============================================================================
--- jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/resultset/ResultSetWriterFactory.java (added)
+++ jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/resultset/ResultSetWriterFactory.java Fri Aug 8 20:16:15 2014
@@ -0,0 +1,26 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.jena.riot.resultset;
+
+import org.apache.jena.riot.Lang ;
+
+/* Create a one-time use ResultSetReader */
+public interface ResultSetWriterFactory {
+ public ResultSetWriter create(Lang lang) ;
+}
Added: jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/resultset/ResultSetWriterRegistry.java
URL: http://svn.apache.org/viewvc/jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/resultset/ResultSetWriterRegistry.java?rev=1616868&view=auto
==============================================================================
--- jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/resultset/ResultSetWriterRegistry.java (added)
+++ jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/resultset/ResultSetWriterRegistry.java Fri Aug 8 20:16:15 2014
@@ -0,0 +1,139 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.jena.riot.resultset;
+
+import static org.apache.jena.riot.resultset.ResultSetLang.SPARQLResultSetCSV ;
+import static org.apache.jena.riot.resultset.ResultSetLang.SPARQLResultSetJSON ;
+import static org.apache.jena.riot.resultset.ResultSetLang.SPARQLResultSetTSV ;
+import static org.apache.jena.riot.resultset.ResultSetLang.SPARQLResultSetText ;
+import static org.apache.jena.riot.resultset.ResultSetLang.SPARQLResultSetThrift ;
+import static org.apache.jena.riot.resultset.ResultSetLang.SPARQLResultSetXML ;
+
+import java.io.OutputStream ;
+import java.io.Writer ;
+import java.util.HashMap ;
+import java.util.Map ;
+import java.util.Objects ;
+
+import org.apache.jena.riot.Lang ;
+import org.apache.jena.riot.RiotException ;
+
+import com.hp.hpl.jena.query.ResultSet ;
+import com.hp.hpl.jena.sparql.core.Prologue ;
+import com.hp.hpl.jena.sparql.resultset.* ;
+import com.hp.hpl.jena.sparql.serializer.SerializationContext ;
+import com.hp.hpl.jena.sparql.util.Context ;
+
+public class ResultSetWriterRegistry {
+
+ private static Map<Lang, ResultSetWriterFactory> registry = new HashMap<>() ;
+
+ /** Lookup a {@linkplain Lang} to get the registered {@linkplain ResultSetReaderFactory} (or null) */
+ public static ResultSetWriterFactory lookup(Lang lang) {
+ Objects.requireNonNull(lang) ;
+ return registry.get(lang) ;
+ }
+
+ /** Register a {@linkplain ResultSetReaderFactory} for a {@linkplain Lang} */
+ public static void register(Lang lang, ResultSetWriterFactory factory) {
+ Objects.requireNonNull(lang) ;
+ Objects.requireNonNull(factory) ;
+ registry.put(lang, factory) ;
+ }
+
+ static { init(); }
+
+ /*package*/ static void init() {
+// RDFLanguages.register(SPARQLResultSetXML) ;
+// RDFLanguages.register(SPARQLResultSetJSON) ;
+// RDFLanguages.register(SPARQLResultSetCSV) ;
+// RDFLanguages.register(SPARQLResultSetTSV) ;
+// RDFLanguages.register(SPARQLResultSetThrift) ;
+
+ ResultSetWriterFactory factory = new ResultSetWriterFactoryStd() ;
+ register(SPARQLResultSetXML, factory) ;
+ register(SPARQLResultSetJSON, factory) ;
+ register(SPARQLResultSetCSV, factory) ;
+ register(SPARQLResultSetTSV, factory) ;
+ register(SPARQLResultSetThrift, factory) ;
+ register(SPARQLResultSetText, factory) ;
+ }
+
+
+ private static ResultSetWriter writerXML = new ResultSetWriter() {
+ @Override public void write(OutputStream out, ResultSet resultSet, Context context) {
+ XMLOutput xOut = new XMLOutput(null) ;
+ xOut.format(out, resultSet) ;
+ }
+ @Override public void write(Writer out, ResultSet resultSet, Context context) {}
+ } ;
+
+ private static ResultSetWriter writerJSON = new ResultSetWriter() {
+ @Override public void write(OutputStream out, ResultSet resultSet, Context context) {
+ JSONOutput jOut = new JSONOutput() ;
+ jOut.format(out, resultSet) ;
+ }
+ @Override public void write(Writer out, ResultSet resultSet, Context context) {}
+ } ;
+
+ private static ResultSetWriter writerCSV = new ResultSetWriter() {
+ @Override public void write(OutputStream out, ResultSet resultSet, Context context) {
+ CSVOutput fmt = new CSVOutput() ;
+ fmt.format(out, resultSet) ;
+ }
+ @Override public void write(Writer out, ResultSet resultSet, Context context) {}
+ } ;
+
+ private static ResultSetWriter writerTSV = new ResultSetWriter() {
+ @Override public void write(OutputStream out, ResultSet resultSet, Context context) {
+ TSVOutput fmt = new TSVOutput() ;
+ fmt.format(out, resultSet) ;
+ }
+ @Override public void write(Writer out, ResultSet resultSet, Context context) {}
+ } ;
+
+ private static ResultSetWriter writerNo = new ResultSetWriter() {
+ @Override public void write(OutputStream out, ResultSet resultSet, Context context) {}
+ @Override public void write(Writer out, ResultSet resultSet, Context context) {}
+ } ;
+
+ private static ResultSetWriter writerText = new ResultSetWriter() {
+ @Override public void write(OutputStream out, ResultSet resultSet, Context context) {
+ // Prefix mapp
+ TextOutput tFmt = new TextOutput(new SerializationContext((Prologue)null)) ;
+ tFmt.format(out, resultSet) ;
+ }
+ @Override public void write(Writer out, ResultSet resultSet, Context context) {}
+ } ;
+
+ private static class ResultSetWriterFactoryStd implements ResultSetWriterFactory {
+ @Override
+ public ResultSetWriter create(Lang lang) {
+ lang = Objects.requireNonNull(lang, "Language must not be null") ;
+ if ( lang.equals(SPARQLResultSetXML) ) return writerXML ;
+ if ( lang.equals(SPARQLResultSetJSON) ) return writerJSON ;
+ if ( lang.equals(SPARQLResultSetCSV) ) return writerCSV ;
+ if ( lang.equals(SPARQLResultSetTSV) ) return writerTSV ;
+ if ( lang.equals(SPARQLResultSetText) ) return writerText ;
+ throw new RiotException("Lang not registered (ResultSet writer)") ;
+ }
+
+ }
+}
+
Modified: jena/trunk/jena-arq/src/test/java/org/apache/jena/riot/TC_Riot.java
URL: http://svn.apache.org/viewvc/jena/trunk/jena-arq/src/test/java/org/apache/jena/riot/TC_Riot.java?rev=1616868&r1=1616867&r2=1616868&view=diff
==============================================================================
--- jena/trunk/jena-arq/src/test/java/org/apache/jena/riot/TC_Riot.java (original)
+++ jena/trunk/jena-arq/src/test/java/org/apache/jena/riot/TC_Riot.java Fri Aug 8 20:16:15 2014
@@ -22,6 +22,7 @@ import org.apache.jena.riot.adapters.TS_
import org.apache.jena.riot.lang.TS_Lang ;
import org.apache.jena.riot.out.TS_Out ;
import org.apache.jena.riot.process.TS_Process ;
+import org.apache.jena.riot.resultset.TS_ResultSetRIOT ;
import org.apache.jena.riot.stream.TS_IO2 ;
import org.apache.jena.riot.system.TS_RiotSystem ;
import org.apache.jena.riot.web.TS_RiotWeb ;
@@ -42,6 +43,7 @@ import org.junit.runners.Suite ;
, TS_RiotWriter.class
, TS_RiotSystem.class
, TS_RiotWeb.class
+ , TS_ResultSetRIOT.class
})
Added: jena/trunk/jena-arq/src/test/java/org/apache/jena/riot/resultset/TS_ResultSetRIOT.java
URL: http://svn.apache.org/viewvc/jena/trunk/jena-arq/src/test/java/org/apache/jena/riot/resultset/TS_ResultSetRIOT.java?rev=1616868&view=auto
==============================================================================
--- jena/trunk/jena-arq/src/test/java/org/apache/jena/riot/resultset/TS_ResultSetRIOT.java (added)
+++ jena/trunk/jena-arq/src/test/java/org/apache/jena/riot/resultset/TS_ResultSetRIOT.java Fri Aug 8 20:16:15 2014
@@ -0,0 +1,30 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.jena.riot.resultset;
+
+import org.junit.runner.RunWith ;
+import org.junit.runners.Suite ;
+
+@RunWith(Suite.class)
+@Suite.SuiteClasses( {
+ TestResultSetIO.class
+})
+
+public class TS_ResultSetRIOT { }
+
Added: jena/trunk/jena-arq/src/test/java/org/apache/jena/riot/resultset/TestResultSetIO.java
URL: http://svn.apache.org/viewvc/jena/trunk/jena-arq/src/test/java/org/apache/jena/riot/resultset/TestResultSetIO.java?rev=1616868&view=auto
==============================================================================
--- jena/trunk/jena-arq/src/test/java/org/apache/jena/riot/resultset/TestResultSetIO.java (added)
+++ jena/trunk/jena-arq/src/test/java/org/apache/jena/riot/resultset/TestResultSetIO.java Fri Aug 8 20:16:15 2014
@@ -0,0 +1,95 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.jena.riot.resultset;
+
+import java.io.ByteArrayInputStream ;
+import java.io.ByteArrayOutputStream ;
+import java.util.ArrayList ;
+import java.util.Collection ;
+import java.util.List ;
+
+import org.apache.jena.atlas.lib.StrUtils ;
+import org.apache.jena.riot.Lang ;
+import org.junit.Before ;
+import org.junit.Test ;
+import org.junit.runner.RunWith ;
+import org.junit.runners.Parameterized ;
+import org.junit.runners.Parameterized.Parameters ;
+
+import com.hp.hpl.jena.query.ResultSet ;
+import com.hp.hpl.jena.query.ResultSetFactory ;
+import com.hp.hpl.jena.query.ResultSetRewindable ;
+import com.hp.hpl.jena.sparql.resultset.ResultSetCompare ;
+import com.hp.hpl.jena.sparql.sse.SSE ;
+import com.hp.hpl.jena.sparql.sse.builders.BuilderResultSet ;
+
+@RunWith(Parameterized.class)
+public class TestResultSetIO {
+ @Parameters(name = "{index}: {0}") public static Collection<Object[]> data()
+ {
+ Lang[] langs = { ResultSetLang.SPARQLResultSetXML
+ , ResultSetLang.SPARQLResultSetJSON
+ , ResultSetLang.SPARQLResultSetCSV
+ , ResultSetLang.SPARQLResultSetTSV
+ } ;
+
+ List<Object[]> x = new ArrayList<>() ;
+ for ( Lang lang : langs ) {
+ x.add(new Object[]{ "test:"+lang.getName(), lang } ) ;
+ }
+ return x ;
+ }
+
+ static String rsStr = StrUtils.strjoinNL
+ ("(resultset (?x ?y)"
+ ," (row (?x _:b0) (?y _:b1))"
+ ," (row (?x _:b2) (?y _:b3))"
+ ," (row (?x _:b1) (?y _:b0))"
+ ," (row (?x 1) )"
+ ," (row (?y 2) )"
+ ," (row )"
+ ,")"
+ ) ;
+
+ static ResultSetRewindable test_rs = ResultSetFactory.makeRewindable(BuilderResultSet.build(SSE.parse(rsStr))) ;
+
+ private final Lang lang ;
+ @Before public void beforetest() { test_rs.reset() ; }
+
+ public TestResultSetIO(String name, Lang lang) {
+ this.lang = lang ;
+ }
+
+ @Test public void test_resultset_01() {
+ ByteArrayOutputStream out = new ByteArrayOutputStream() ;
+ ResultSetMgr.write(out, test_rs, lang) ;
+ test_rs.reset();
+ ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()) ;
+ ResultSet rs = ResultSetMgr.read(in, lang) ;
+ ResultSetCompare.equalsByTerm(test_rs, rs) ;
+
+ out.reset();
+ ResultSetMgr.write(out, rs, lang) ;
+ test_rs.reset();
+ in = new ByteArrayInputStream(out.toByteArray()) ;
+ ResultSet rs2 = ResultSetMgr.read(in, lang) ;
+ ResultSetCompare.equalsByTerm(test_rs, rs2) ;
+ }
+}
+