/*
 * Copyright (c) 2011 Miguel Ceriani
 * miguel.ceriani@gmail.com

 * This file is part of Semantic Web Open datatafloW System (SWOWS).

 * SWOWS is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of
 * the License, or (at your option) any later version.

 * SWOWS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.

 * You should have received a copy of the GNU Affero General
 * Public License along with SWOWS.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.swows.reader.query;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringWriter;
import java.util.Iterator;

import org.apache.jena.atlas.web.ContentType;
import org.apache.jena.riot.Lang;
import org.apache.jena.riot.LangBuilder;
import org.apache.jena.riot.RDFLanguages;
import org.apache.jena.riot.RDFParserRegistry;
import org.apache.jena.riot.ReaderRIOT;
import org.apache.jena.riot.ReaderRIOTFactory;
import org.apache.jena.riot.RiotException;
import org.apache.jena.riot.system.StreamRDF;
import org.swows.reader.HtmlReaderRIOTFactory;
import org.swows.spinx.SpinxFactory;
import org.swows.vocabulary.SWI;
import org.swows.xmlinrdf.DomEncoder;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLReaderFactory;

import com.hp.hpl.jena.graph.NodeFactory;
import com.hp.hpl.jena.n3.JenaReaderBase;
import com.hp.hpl.jena.query.QueryFactory;
import com.hp.hpl.jena.query.Syntax;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.impl.RDFReaderFImpl;
import com.hp.hpl.jena.sparql.util.Context;

public class QueryReaderRIOTFactory implements ReaderRIOTFactory {
		
//		public static final String XML_SYNTAX_URI = "http://www.swows.org/syntaxes/XML";

//		static boolean initialized = false;
		
	static {

		Lang lang =
        		LangBuilder
        		.create("SPARQL", "application/sparql-query")
        		.addFileExtensions("rq","sparql").build() ;
        // This just registers the name, not the parser.
        RDFLanguages.register(lang) ;
        
        // Register the parser factory.
        ReaderRIOTFactory factory = new QueryReaderRIOTFactory() ;
        RDFParserRegistry.registerLangTriples(lang, factory) ;
		
	}

	public static void initialize() {}

	public ReaderRIOT create(Lang language) {
		return new ReaderRIOT() {
			
			public void read(
					InputStream in,
					String baseURI,
					ContentType ct,
					StreamRDF output,
					Context context) {
				
				StringWriter sw = new StringWriter();
				Reader reader = null;
				try {
					reader = (ct.getCharset() == null) ? new InputStreamReader(in) : new InputStreamReader(in, ct.getCharset());
					for( int currChar = reader.read(); currChar > 0; currChar = reader.read() )
						sw.write(currChar);
					reader.close();
					sw.flush();
					sw.close();
				} catch(IOException e) {
					throw new RiotException(e);
				} finally {
					if (reader != null)
						try {reader.close();} catch(IOException e) {}
				}

				com.hp.hpl.jena.query.Query query = QueryFactory.create(sw.toString(),baseURI);
				SpinxFactory.fromQuery(query, output, NodeFactory.createURI(baseURI)/*SWI.GraphRoot.asNode()*/);
			}
		};
	}

}

@@ -956,35 +956,8 @@
 ter;
-%0Aimport java.util.Iterator;
 %0A%0Aim
@@ -1361,543 +1361,112 @@
 ows.
-reader.HtmlReaderRIOTFactory;%0Aimport org.swows.spinx.SpinxFactory;%0Aimport org.swows.vocabulary.SWI;%0Aimport org.swows.xmlinrdf.DomEncoder;%0Aimport org.xml.sax.InputSource;%0Aimport org.xml.sax.SAXException;%0Aimport org.xml.sax.XMLReader;%0Aimport org.xml.sax.helpers.XMLReaderFactory;%0A%0Aimport com.hp.hpl.jena.graph.NodeFactory;%0Aimport com.hp.hpl.jena.n3.JenaReaderBase;%0Aimport com.hp.hpl.jena.query.QueryFactory;%0Aimport com.hp.hpl.jena.query.Syntax;%0Aimport com.hp.hpl.jena.rdf.model.Model;%0Aimport com.hp.hpl.jena.rdf.model.impl.RDFReaderFImpl
+spinx.SpinxFactory;%0A%0Aimport com.hp.hpl.jena.graph.NodeFactory;%0Aimport com.hp.hpl.jena.query.QueryFactory
 ;%0Aim

Clean up