package org.deri.any23.extractor.rdfa;

import java.io.InputStream;
import java.io.Writer;

import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

import org.w3c.dom.Document;

/**
 * An XSLT stylesheet loaded from an InputStream, can be applied
 * to DOM trees and writes the result to a {@link Writer}. 
 * 
 * TODO: XSLTStylesheet should have better error handling
 * 
 * @author Gabriele Renzi
 * @author Richard Cyganiak (richard@cyganiak.de)
 */
public class XSLTStylesheet {
	private final Transformer transformer;

	public XSLTStylesheet(InputStream xsltFile) {
		try {
			transformer = TransformerFactory.newInstance().newTransformer(new StreamSource(xsltFile));
		} catch (TransformerConfigurationException e) {
			throw new RuntimeException("Should not happen, we use the default configuration", e);
		}
	}

	public void applyTo(Document document, Writer output) {
		try {
			transformer.transform(new DOMSource(document, document.getBaseURI()), new StreamResult(output));
		} catch (TransformerException e) {	// TODO: Figure out when this can be thrown
			throw new RuntimeException("Exception occured during XSLT transformation", e);
		}
	}
}
@@ -422,16 +422,73 @@
 ource;%0A%0A
+import org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;%0A
 import o
@@ -508,16 +508,16 @@
 cument;%0A
-
 %0A/**%0A * 
@@ -814,16 +814,100 @@
 sheet %7B%0A
+%09private final static Logger log = LoggerFactory.getLogger(XSLTStylesheet.class);%0A%09%0A
 %09private
@@ -1243,16 +1243,29 @@
 %09public 
+synchronized 
 void app
@@ -1409,24 +1409,24 @@
 t(output));%0A
-
 %09%09%7D catch (T
@@ -1494,16 +1494,235 @@
  thrown%0A
+%09%09%09log.info(%22Exception in XSLTStylesheet.applyTo; details follow%22, e);%0A%09%09%09log.info(%22Input DOM node:%22, document);%0A%09%09%09log.info(%22Input DOM node getBaseURI:%22 , document.getBaseURI());%0A%09%09%09log.info(%22Output writer:%22, output);%0A
 %09%09%09throw

make XSLT tranformation synchronized to avoid concurrency troubles, and better logging