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/02/09 17:36:35 UTC
svn commit: r1242385 - in
/incubator/jena/Scratch/AFS/Dev/trunk/src/main/java/riot/reader: Langs.java
ReaderFactory.java WebReader2.java
Author: andy
Date: Thu Feb 9 16:36:34 2012
New Revision: 1242385
URL: http://svn.apache.org/viewvc?rev=1242385&view=rev
Log: (empty)
Added:
incubator/jena/Scratch/AFS/Dev/trunk/src/main/java/riot/reader/ReaderFactory.java
Modified:
incubator/jena/Scratch/AFS/Dev/trunk/src/main/java/riot/reader/Langs.java
incubator/jena/Scratch/AFS/Dev/trunk/src/main/java/riot/reader/WebReader2.java
Modified: incubator/jena/Scratch/AFS/Dev/trunk/src/main/java/riot/reader/Langs.java
URL: http://svn.apache.org/viewvc/incubator/jena/Scratch/AFS/Dev/trunk/src/main/java/riot/reader/Langs.java?rev=1242385&r1=1242384&r2=1242385&view=diff
==============================================================================
--- incubator/jena/Scratch/AFS/Dev/trunk/src/main/java/riot/reader/Langs.java (original)
+++ incubator/jena/Scratch/AFS/Dev/trunk/src/main/java/riot/reader/Langs.java Thu Feb 9 16:36:34 2012
@@ -34,6 +34,7 @@ import org.openjena.riot.lang.LangRIOT ;
import com.hp.hpl.jena.graph.Triple ;
import com.hp.hpl.jena.sparql.core.Quad ;
import com.hp.hpl.jena.sparql.engine.binding.Binding ;
+import com.hp.hpl.jena.sparql.util.Context ;
public class Langs
{
@@ -54,16 +55,10 @@ public class Langs
// See Reader interface and combine.
- // Formally one-shot or many?
- // Accumulator model.??
- static interface Parser { void parse(InputStream in, String baseURI) ; }
-
- // The destination has to go in the constructor as it's <T>
- // Uck. abstract class?
- static interface ParserFactory<T> { Parser create(Lang2 language, Sink<T> sink) ; }
- // --------
+// // Formally one-shot or many?
+// // Accumulator model.??
+// static interface Parser { void parse(InputStream in, String baseURI) ; }
-
// 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) ;
@@ -78,40 +73,65 @@ public class Langs
// This code understands different types fo things it can read:
// triples, quads, result sets, unknown
- // These form (disjoint) value spaces static ParserFactory<Triple> pfTriples = new ParserFactory<Triple>() {
+ // 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.
// The decision is driven by the Content-Type.
// Up until the response header, the code does not need to know but given the content type,
// it needs to determine the class of parser that's going to be needed.
// (We won't need to do this if we could pass in the type information at runtime.)
-// private static String BASE = "http://jena.apastatic ParserFactory<Triple> pfTriples = new ParserFactory<Triple>() {che.org/parserType/" ;
+// private static String BASE = "http://jena.apastatic ReaderFactory<Triple> pfTriples = new ReaderFactory<Triple>() {che.org/parserType/" ;
// // Open enum. Allows extensibility but what the use case?
// private Symbol TRIPLES = Symbol.create(BASE+"triples") ;
// private Symbol QUADS = Symbol.create(BASE+"quads") ;
// private Symbol RESULTSET = Symbol.create(BASE+"table") ;
// private Symbol OTHER = Symbol.create(BASE+"unknown") ;
- enum DestClass { TRIPLES, QUADS, RESULTSET } ;
+ enum DataClass { TRIPLES, QUADS, RESULTSET } ;
+ static Map<Lang2, DataClass> langToDataClass = new HashMap<Lang2, DataClass>() ;
+ static {
+ // See WebContent.mapContentTypeToLang
+ register(langRDFXML, DataClass.TRIPLES) ;
+ register(langTurtle, DataClass.TRIPLES) ;
+ register(langNTriples, DataClass.TRIPLES) ;
+ register(langN3, DataClass.TRIPLES) ;
+ register(langRDFJSON, DataClass.TRIPLES) ;
+
+ register(langNQuads, DataClass.QUADS) ;
+ register(langTriG, DataClass.QUADS) ;
+ }
+
+ static DataClass dataClass(Lang2 lang)
+ {
+ return langToDataClass.get(lang) ;
+ }
+
+ private static void register(Lang2 lang, DataClass dataClass)
+ {
+
+ }
// Triples
// Generic parser factory.
- static ParserFactory<Triple> pfTriples = new ParserFactory<Triple>() {
+ static ReaderFactory<Triple> pfTriples = new ReaderFactory<Triple>() {
@Override
- public Parser create(final Lang2 language, final Sink<Triple> sink)
+ public Reader<Triple> create(final Lang2 language)
{
- return new Parser() {
+ return new Reader<Triple>() {
// Needs sorting out
@Override
- public void parse(InputStream in, String baseURI)
+ public void read(InputStream in, String baseURI, ContentType ct, Sink<Triple> sink, Context context)
{
Lang lang = convert(language) ;
LangRIOT parser = RiotReader.createParserTriples(in, lang, baseURI, sink) ;
parser.parse() ;
- }} ;
+ }
+
+
+ {}} ;
}} ;
- static Map<Lang2, ParserFactory<Triple>> langToTriples = new HashMap<Lang2, ParserFactory<Triple>>() ;
+ static Map<Lang2, ReaderFactory<Triple>> langToTriples = new HashMap<Lang2, ReaderFactory<Triple>>() ;
static {
langToTriples.put(langRDFXML, pfTriples) ;
langToTriples.put(langTurtle, pfTriples) ;
@@ -124,14 +144,14 @@ public class Langs
// Triples
// Generic parser factory.
- static ParserFactory<Quad> pfQuads = new ParserFactory<Quad>() {
+ static ReaderFactory<Quad> pfQuads = new ReaderFactory<Quad>() {
@Override
- public Parser create(final Lang2 language, final Sink<Quad> sink)
+ public Reader<Quad> create(final Lang2 language)
{
- return new Parser() {
+ return new Reader<Quad>() {
// Needs sorting out
@Override
- public void parse(InputStream in, String baseURI)
+ public void read(InputStream in, String baseURI, ContentType ct, Sink<Quad> sink, Context context)
{
Lang lang = convert(language) ;
LangRIOT parser = RiotReader.createParserQuads(in, lang, baseURI, sink) ;
@@ -139,35 +159,35 @@ public class Langs
}} ;
}} ;
- static Map<Lang2, ParserFactory<Quad>> langToQuads = new HashMap<Lang2, ParserFactory<Quad>>() ;
+ static Map<Lang2, ReaderFactory<Quad>> langToQuads = new HashMap<Lang2, ReaderFactory<Quad>>() ;
static {
langToQuads.put(langNQuads, pfQuads) ;
langToQuads.put(langTriG, pfQuads) ;
}
- static ParserFactory<Binding> pfBindings = new ParserFactory<Binding>() {
+ static ReaderFactory<Binding> pfBindings = new ReaderFactory<Binding>() {
@Override
- public Parser create(Lang2 language, Sink<Binding> sink)
+ public Reader<Binding> create(Lang2 language)
{
throw new NotImplemented() ;
}} ;
- static Map<Lang2, ParserFactory<Binding>> langToBindings = new HashMap<Lang2, ParserFactory<Binding>>() ;
+ static Map<Lang2, ReaderFactory<Binding>> langToBindings = new HashMap<Lang2, ReaderFactory<Binding>>() ;
static {
//langToBindings.put(????, pfBindings) ;
}
// ---- THE WORKERS
- public static void readTriples(InputStream in, String contentType, Sink<Triple> dest)
+ public static void readTriples(TypedStream2 in, Sink<Triple> dest)
{
- Lang2 lang = contentTypeToLang(contentType) ;
+ Lang2 lang = contentTypeToLang(in.getContentType()) ;
// if ( ! lang.isTriples() )
// throw new RiotException() ;
- ParserFactory<Triple> pf = langToTriples.get(lang) ;
- if ( pf == null )
+ ReaderFactory<Triple> rf = langToTriples.get(lang) ;
+ if ( rf == null )
{}
- Parser parser = pf.create(lang, dest) ;
- parser.parse(in, contentType) ;
+ Reader<Triple> reader = rf.create(lang) ;
+ reader.read(in.getInput(), in.getBaseURI(), in.getMediaType(), dest, null) ;
}
static Lang2 contentTypeToLang(String contentType)
@@ -217,27 +237,6 @@ public class Langs
- static {
- // See WebContent.mapContentTypeToLang
- register(langRDFXML) ;
- register(langTurtle) ;
- register(langTurtle) ;
- register(langTurtle) ;
-
- register(langNTriples) ;
-
- register(langN3) ;
-
- register(langRDFJSON) ;
- register(langNQuads) ;
- register(langTriG) ;
- }
-
- private static void register(Lang2 lang)
- {
-
- }
-
// MIME specific.
// Check for uniqueness.
Added: incubator/jena/Scratch/AFS/Dev/trunk/src/main/java/riot/reader/ReaderFactory.java
URL: http://svn.apache.org/viewvc/incubator/jena/Scratch/AFS/Dev/trunk/src/main/java/riot/reader/ReaderFactory.java?rev=1242385&view=auto
==============================================================================
--- incubator/jena/Scratch/AFS/Dev/trunk/src/main/java/riot/reader/ReaderFactory.java (added)
+++ incubator/jena/Scratch/AFS/Dev/trunk/src/main/java/riot/reader/ReaderFactory.java Thu Feb 9 16:36:34 2012
@@ -0,0 +1,24 @@
+/*
+ * 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 riot.reader;
+
+public interface ReaderFactory<T>
+{
+ public Reader<T> create(Lang2 language) ;
+}
Modified: incubator/jena/Scratch/AFS/Dev/trunk/src/main/java/riot/reader/WebReader2.java
URL: http://svn.apache.org/viewvc/incubator/jena/Scratch/AFS/Dev/trunk/src/main/java/riot/reader/WebReader2.java?rev=1242385&r1=1242384&r2=1242385&view=diff
==============================================================================
--- incubator/jena/Scratch/AFS/Dev/trunk/src/main/java/riot/reader/WebReader2.java (original)
+++ incubator/jena/Scratch/AFS/Dev/trunk/src/main/java/riot/reader/WebReader2.java Thu Feb 9 16:36:34 2012
@@ -89,28 +89,10 @@ public class WebReader2
System.out.println("triples = "+m.size()) ;
}
- // interface ReaderFactory<T> { Reader<T> create() ; }
- // -----
- static public Registry2<Reader<Triple>> registryGraphReaders = new Registry2<Reader<Triple>>() ;
-
// >>>> **** Setup
- // Lang is an enum and hence closed.
- // lang -> open-ended?
- // Constants - all needs sorting out!
- // Order dependent choices? Chains of "try me"?
- static {
- register(WebContent.contentTypeTurtle, new ReaderLangTriple(Lang.TURTLE)) ;
- register(WebContent.contentTypeNTriples, new ReaderLangTriple(Lang.NTRIPLES)) ;
- register(WebContent.contentTypeRDFXML, new ReaderLangTriple(Lang.RDFXML)) ;
- register(WebContent.contentTypeRDFJSON, new ReaderLangTriple(Lang.RDFJSON)) ;
- //register("*", null) ;
- }
-
- public static void register(String contentType, Reader<Triple> reader)
- {
- // Check name?
- registryGraphReaders.put(contentType, reader) ;
- }
+ // in Langs.
+ // CTstring -> Lang -> reader?
+ // Yes - provides a constant for a language
// <<<< **** Setup
@@ -192,38 +174,32 @@ public class WebReader2
private static void process(Sink<Triple> sink, String uri, TypedStream in, String hintLang, Context context)
{
- if ( false )
- {
- // Assumes Lang is extensible.
- boolean isTextPlain = WebContent.contentTypeTextPlain.equals(in.getContentType()) ;
- Lang lang1 = Lang.guess(uri) ;
- Lang lang2 = WebContent.contentTypeToLang(in.getContentType()) ;
-
- // Tune
- Lang lang = lang2 ;
- if ( lang == null || isTextPlain )
- {
- lang = lang1 ;
- }
-
- RiotLoader.readTriples(in.getInput(), lang, uri, sink) ;
- }
- else
- {
- // Extensible(?)
- // Hint?
- String _ct = null ;
- if ( in.getMediaType() != null )
- _ct = WebContent.contentTypeCanonical(in.getContentType()) ;
- if ( _ct == null )
- _ct = hintLang ;
- // Error? ....
- ContentType ct = ContentType.parse(_ct) ;
- Reader<Triple> reader = registryGraphReaders.get(_ct) ;
- reader.read(in.getInput(), uri, ct, sink, context) ;
- }
+ boolean isTextPlain = WebContent.contentTypeTextPlain.equals(in.getContentType()) ;
+
+ String _ct = null ;
+ if ( in.getMediaType() != null )
+ _ct = WebContent.contentTypeCanonical(in.getContentType()) ;
+ if ( _ct == null || isTextPlain )
+ _ct = hintLang ;
+ // Error? ....
+ ContentType ct = ContentType.parse(_ct) ;
+ if ( ct == null )
+ {}
+ Reader<Triple> reader = getReaderTriples(ct) ;
+ if ( reader == null )
+ {}
+ reader.read(in.getInput(), uri, ct, sink, context) ;
+ }
+
+ private static Reader<Triple> getReaderTriples(ContentType ct)
+ {
+ Lang2 lang = Langs.contentTypeToLang(ct) ;
+ ReaderFactory<Triple> r = Langs.langToTriples.get(lang) ;
+ if ( r == null )
+ return null ;
+ return r.create(lang) ;
}
-
+
/** General reader-of-triples for a fixed language */
static class ReaderLangTriple implements Reader<Triple>
{