/*
 * eXist-db Open Source Native XML Database
 * Copyright (C) 2001 The eXist-db Authors
 *
 * info@exist-db.org
 * http://www.exist-db.org
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */
package org.exist.xquery.modules.xslfo;

import java.io.OutputStream;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.Properties;
import javax.xml.transform.Source;
import javax.xml.transform.dom.DOMSource;
import org.exist.storage.DBBroker;
import org.exist.xquery.XPathException;
import org.exist.xquery.value.NodeValue;
import org.w3c.dom.Node;
import org.xml.sax.ContentHandler;
import org.xml.sax.SAXException;

/**
 *
 * @author <a href="mailto:adam@exist-db.org">Adam Retter</a>
 */
public class RenderXXepProcessorAdapter implements ProcessorAdapter {

    private Object formatter = null;


    @Override
    public ContentHandler getContentHandler(DBBroker broker, NodeValue configFile, Properties parameters, String mimeType, OutputStream os) throws XPathException, SAXException {

        if(configFile == null) {
            throw new XPathException("XEP requires a configuration file");
        }

        try {

            Class formatterImplClazz = Class.forName("com.renderx.xep.FormatterImpl");

            if(parameters == null) {
                Constructor formatterImplCstr = formatterImplClazz.getConstructor(Source.class);
                formatter = formatterImplCstr.newInstance(new DOMSource(configFile.getNode()));
            } else {
                Constructor formatterImplCstr = formatterImplClazz.getConstructor(Source.class, Properties.class);
                formatter = formatterImplCstr.newInstance(new DOMSource(configFile.getNode()), parameters);
            }
            String backendType = mimeType.substring(mimeType.indexOf("/")+1).toUpperCase();

            Class foTargetClazz = Class.forName("com.renderx.xep.FOTarget");
            Constructor foTargetCstr = foTargetClazz.getConstructor(OutputStream.class, String.class);
            Object foTarget = foTargetCstr.newInstance(os, backendType);

            Method createContentHandlerMethod = formatterImplClazz.getMethod("createContentHandler", String.class, foTargetClazz);

            return (ContentHandler) createContentHandlerMethod.invoke(formatter, null, foTarget);
        } catch (Exception e) {
            throw new SAXException(e.getMessage(), e);
        }
    }

    @Override
    public void cleanup() {
        if(formatter != null) {
            try{
                Class formatterImplClazz = Class.forName("com.renderx.xep.FormatterImpl");
                Method cleanupMethod = formatterImplClazz.getMethod("cleanup");
                cleanupMethod.invoke(formatter);
            } catch(Exception e) {
                // do nothing
            }
        }
     }
}

@@ -1118,54 +1118,263 @@
 orm.
-dom.DOMSource;%0Aimport org.exist.storage.DBBrok
+TransformerException;%0Aimport javax.xml.transform.URIResolver;%0Aimport javax.xml.transform.dom.DOMSource;%0A%0Aimport org.exist.EXistException;%0Aimport org.exist.storage.BrokerPool;%0Aimport org.exist.storage.DBBroker;%0Aimport org.exist.util.EXistURISchemeURIResolv
 er;%0A
@@ -1469,20 +1469,35 @@
 org.
-w3c.dom.Node
+exist.xslt.EXistURIResolver
 ;%0Aim
@@ -1745,16 +1745,1263 @@
  null;%0A%0A
+    static %7B%0A        System.setProperty(%22com.renderx.jaxp.uriresolver%22,%22org.exist.xquery.modules.xslfo.RenderXXepProcessorAdapter.EXistURISchemeAndURIResolver%22);%0A    %7D%0A%0A    public static class EXistURISchemeAndURIResolver implements URIResolver %7B%0A        private static final String DEFAULT_BASE_URI = %22exist://localhost/db/%22;%0A%0A        private static URIResolver existUriSchemeAndURIResolver = null;%0A%0A        @Override%0A        public Source resolve(final String href, final String base) throws TransformerException %7B%0A            synchronized (EXistURISchemeAndURIResolver.class) %7B%0A                if (existUriSchemeAndURIResolver == null) %7B%0A                    try %7B%0A                        existUriSchemeAndURIResolver = init();%0A                    %7D catch (final EXistException e) %7B%0A                        throw new TransformerException(e.getMessage(), e);%0A                    %7D%0A                %7D%0A            %7D%0A            return existUriSchemeAndURIResolver.resolve(href, base);%0A        %7D%0A%0A        private static URIResolver init() throws EXistException %7B%0A            final BrokerPool brokerPool = BrokerPool.getInstance();%0A            return new EXistURISchemeURIResolver(new EXistURIResolver(brokerPool, DEFAULT_BASE_URI));%0A        %7D%0A    %7D%0A%0A
 %0A    @Ov

Add eXist-db URI Resolved for RenderX XSL:FO processor