/*
 * Portions Copyright (c) 2009-10 Zepheira LLC and James Leigh, Some Rights Reserved
 * Portions Copyright (c) 2010-11 Talis Inc, Some Rights Reserved
 *
 * Licensed 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.callimachusproject.behaviours;

import java.io.IOException;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Map;

import javax.tools.FileObject;
import javax.xml.stream.XMLEventReader;

import org.callimachusproject.annotations.method;
import org.callimachusproject.annotations.query;
import org.callimachusproject.annotations.type;
import org.callimachusproject.concepts.Page;
import org.callimachusproject.engine.RDFEventReader;
import org.callimachusproject.engine.RDFaReader;
import org.callimachusproject.engine.Template;
import org.callimachusproject.engine.TemplateEngine;
import org.callimachusproject.engine.TemplateEngineFactory;
import org.callimachusproject.engine.TemplateException;
import org.callimachusproject.engine.helpers.RDFXMLEventReader;
import org.openrdf.repository.object.RDFObject;

/**
 * Implements a few {@link Page} methods to read an RDFa document. This class is
 * responsible for applying XSL transformations, extracting x-pointer elements,
 * and parsing RDFa into sparql.
 * 
 * @author James Leigh
 * @author Steve Battle
 * 
 */
public abstract class RDFaSupport implements Page, RDFObject,
		FileObject {
	private static final TemplateEngineFactory tef = TemplateEngineFactory.newInstance();

	@method("GET")
	@query("xslt")
	@type("application/xml")
	public XMLEventReader xslt(@query("query") String query,
			@query("element") String element) throws IOException, TemplateException {
		String base = toUri().toASCIIString();
		TemplateEngine engine = tef.createTemplateEngine(getObjectConnection());
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("query", query);
		param.put("template", true);
		Template temp = engine.getTemplate(base, param).getElement(element);
		return temp.openSourceReader();
	}

	@method("GET")
	@query("triples")
	@type("application/rdf+xml")
	public XMLEventReader triples(@query("query") String query,
			@query("element") String element) throws Exception {
		String base = toUri().toASCIIString();
		TemplateEngine engine = tef.createTemplateEngine(getObjectConnection());
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("query", query);
		param.put("template", false);
		Template temp = engine.getTemplate(base, param).getElement(element);
		XMLEventReader doc = temp.openSourceReader();
		return new RDFXMLEventReader(new RDFaReader(base, doc, toString()));
	}

	@method("GET")
	@query("sparql")
	@type("application/sparql-query")
	public byte[] sparql(@query("about") String about,
			@query("query") String query, @query("element") String element)
			throws Exception {
		String base = toUri().toASCIIString();
		TemplateEngine engine = tef.createTemplateEngine(getObjectConnection());
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("query", query);
		param.put("template", false);
		Template temp = engine.getTemplate(base, param).getElement(element);
		return temp.getQuery().getBytes(Charset.forName("UTF-8"));
	}

	public RDFEventReader openPatternReader(String about, String query,
			String element) throws IOException, TemplateException {
		String base = toUri().toASCIIString();
		TemplateEngine engine = tef.createTemplateEngine(getObjectConnection());
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("query", query);
		param.put("template", true);
		Template temp = engine.getTemplate(base, param).getElement(element);
		return temp.openQueryReader();
	}

}

@@ -870,39 +870,8 @@
 p;%0A%0A
-import javax.tools.FileObject;%0A
 impo
@@ -906,16 +906,16 @@
 Reader;%0A
+
 %0Aimport 
@@ -1413,24 +1413,139 @@
 eException;%0A
+import org.callimachusproject.engine.events.Base;%0Aimport org.callimachusproject.engine.helpers.OverrideBaseReader;%0A
 import org.c
@@ -1966,22 +1966,8 @@
 ject
-,%0A%09%09FileObject
  %7B%0A%09
@@ -2262,37 +2262,41 @@
  base = 
-toUri().toASCIIString
+getResource().stringValue
 ();%0A%09%09Te
@@ -2794,37 +2794,41 @@
  base = 
-toUri().toASCIIString
+getResource().stringValue
 ();%0A%09%09Te
@@ -3440,37 +3440,41 @@
  base = 
-toUri().toASCIIString
+getResource().stringValue
 ();%0A%09%09Te
@@ -3950,29 +3950,33 @@
 e = 
-toUri().toASCIIString
+getResource().stringValue
 ();%0A
@@ -4248,37 +4248,313 @@
 ;%0A%09%09
-return temp.openQueryReader()
+RDFEventReader reader = temp.openQueryReader();%0A%09%09Base resolver = new Base(base);%0A%09%09if (about == null) %7B%0A%09%09%09reader = new OverrideBaseReader(resolver, null, reader);%0A%09%09%7D else %7B%0A%09%09%09String uri = resolver.resolve(about);%0A%09%09%09reader = new OverrideBaseReader(resolver, new Base(uri), reader);%0A%09%09%7D%0A%09%09return reader
 ;%0A%09%7D

Fix create validation