/*
 * 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.any23.extractor.rdfa;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Document;

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 java.io.InputStream;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map;

/**
 * An XSLT stylesheet loaded from an InputStream, can be applied to DOM trees and writes the result to a {@link Writer}.
 *
 * @author Gabriele Renzi
 * @author Richard Cyganiak (richard@cyganiak.de)
 */
public class XSLTStylesheet {

    private final static Logger log = LoggerFactory.getLogger(XSLTStylesheet.class);

    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);
        }
    }

    /**
     * Applies the XSLT transformation
     * 
     * @param document
     *            where apply the transformation
     * @param output
     *            the {@link java.io.Writer} where write on
     * 
     * @throws XSLTStylesheetException
     *             if there is an error applying the transformation
     */
    public synchronized void applyTo(Document document, Writer output) throws XSLTStylesheetException {
        this.applyTo(document, output, new HashMap<String, String>());
    }

    /**
     * Applies the XSLT transformation
     * 
     * @param document
     *            where apply the transformation
     * @param output
     *            the {@link java.io.Writer} where write on
     * @param parameters
     *            the parameters to be passed to {@link Transformer}. Pass an empty {@link Map} if no parameters are
     *            foreseen.
     * 
     * @throws XSLTStylesheetException
     *             if there is an error applying the transformation
     */
    public synchronized void applyTo(Document document, Writer output, Map<String, String> parameters)
            throws XSLTStylesheetException {
        for (String parameterKey : parameters.keySet()) {
            transformer.setParameter(parameterKey, parameters.get(parameterKey));
        }
        try {
            transformer.transform(new DOMSource(document, document.getBaseURI()), new StreamResult(output));
        } catch (TransformerException te) {
            log.error("------ BEGIN XSLT Transformer Exception ------");
            log.error("Exception in XSLT Stylesheet transformation.", te);
            log.error("Input DOM node:", document);
            log.error("Input DOM node getBaseIRI:", document.getBaseURI());
            log.error("Output writer:", output);
            log.error("------ END XSLT Transformer Exception ------");
            throw new XSLTStylesheetException(" An error occurred during the XSLT transformation", te);
        }
    }

}
@@ -922,16 +922,47 @@
 ument;%0A%0A
+import javax.xml.XMLConstants;%0A
 import j
@@ -1826,33 +1826,33 @@
 y %7B%0A            
-t
+T
 ransformer = Tra
@@ -1845,16 +1845,26 @@
 nsformer
+Factory tf
  = Trans
@@ -1890,16 +1890,192 @@
 stance()
+;%0A            tf.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, %22%22);%0A            tf.setAttribute(XMLConstants.ACCESS_EXTERNAL_STYLESHEET, %22%22);%0A            this.transformer = tf
 .newTran
@@ -2162,16 +2162,18 @@
 ception 
+tc
 e) %7B%0A   
@@ -2263,16 +2263,18 @@
 ation%22, 
+tc
 e);%0A    

Address comments from liontree0110