<commit_before>/**
 * Copyright (C) 2012 Orbeon, Inc.
 *
 * This program 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 program 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.
 *
 * The full text of the license is available at http://www.gnu.org/copyleft/lesser.html
 */
package org.orbeon.oxf.processor.XQuery;

import org.apache.log4j.Logger;
import org.orbeon.oxf.common.OXFException;
import org.orbeon.oxf.pipeline.api.PipelineContext;
import org.orbeon.oxf.pipeline.api.XMLReceiver;
import org.orbeon.oxf.processor.ProcessorImpl;
import org.orbeon.oxf.processor.ProcessorInputOutputInfo;
import org.orbeon.oxf.processor.ProcessorOutput;
import org.orbeon.oxf.processor.transformer.TransformerURIResolver;
import org.orbeon.oxf.xml.ContentHandlerHelper;
import org.orbeon.oxf.xml.SimpleForwardingXMLReceiver;
import org.orbeon.oxf.xml.XMLUtils;
import org.orbeon.saxon.xqj.SaxonXQDataSource;
import org.xml.sax.SAXException;

import javax.xml.namespace.QName;
import javax.xml.xquery.*;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.sql.*;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Vector;

/**
 * XQuery client/server processor, typically based on XQJ.
 */


public class XQueryProcessor extends ProcessorImpl {


    public static final String XQUERY_NAMESPACE_URI = "http://www.orbeon.org/oxf/xml/xquery";

    private static Logger logger = Logger.getLogger(XQueryProcessor.class);


    private static HashMap<String, String> knownImplementations = initKnownImplementations();

    private static HashMap<String, String> initKnownImplementations() {
        HashMap<String, String> implementations = new HashMap<String, String>();
        implementations.put("exist", "net.xqj.exist.ExistXQDataSource");
        implementations.put("oracle", "oracle.xquery.xqj.OXQDataSource");
        implementations.put("saxon", "org.orbeon.saxon.xqj.SaxonXQDataSource");
        return implementations;
    }

    private static HashMap<String, String> knownJDBCImplementations = initKnownJDBCImplementations();

    private static HashMap<String, String> initKnownJDBCImplementations() {
        HashMap<String, String> implementations = new HashMap<String, String>();
        implementations.put("oracle", "oracle.jdbc.OracleDriver");
        return implementations;
    }


    public XQueryProcessor() {
        addInputInfo(new ProcessorInputOutputInfo(INPUT_CONFIG, XQUERY_NAMESPACE_URI));
        addOutputInfo(new ProcessorInputOutputInfo(OUTPUT_DATA));
    }

    @Override
    public ProcessorOutput createOutput(String name) {
        final ProcessorOutput output = new ProcessorOutputImpl(XQueryProcessor.this, name) {
            public void readImpl(final PipelineContext pipelineContext, XMLReceiver xmlReceiver) {

                class ConfigContainer extends ObjectReceiver {
                    public ConfigContainer() {
                    }

                    public Config config;

                    class Config extends ObjectReceiver {
                        public Config() {
                        }

                        public String vendor;
                        public String implementation;
                        public String info;
                        public String username;
                        public String password;
                        public JDBC jdbc;
                        public Vector<NameValuePair> property = new Vector<NameValuePair>();
                        public String query;
                        public Vector<NameValuePair> parameter = new Vector<NameValuePair>();

                        class NameValuePair extends ObjectReceiver {
                            public NameValuePair() {
                            }

                            public String name;
                            public String value;
                        }

                        class JDBC extends ObjectReceiver {
                            public JDBC() {
                            }

                            public String url;
                            public String implementation;

                        }

                        String getImplementation() {
                            if (vendor != null) {
                                return knownImplementations.get(vendor);
                            }
                            return implementation;
                        }

                        public String getJDBCImplementation() {
                            if (jdbc.implementation != null) {
                                return jdbc.implementation;
                            }
                            return knownJDBCImplementations.get(vendor);
                        }

                    }
                }

                final ConfigContainer container = new ConfigContainer();
                readInputAsSAX(pipelineContext, INPUT_CONFIG, container);
                final ConfigContainer.Config config = container.config;
                ContentHandlerHelper helper = new ContentHandlerHelper(xmlReceiver);
                try {
                    if ("oracle".equals(config.vendor)) {
                        // Use JDBC as a workaround until we find out how to set the connection info in XQJ
                        // (see https://forums.oracle.com/forums/thread.jspa?messageID=10338407#10338407)
                        Driver driver = (Driver) Class.forName(config.getJDBCImplementation()).newInstance();
                        Connection conn = driver.connect(config.jdbc.url, null);
                        StringBuilder xquery = new StringBuilder("SELECT * from XMLTable('" + config.query.replaceAll("'", "''") + "' ");
                        Iterator<ConfigContainer.Config.NameValuePair> iter = config.parameter.iterator();
                        int i = 1;
                        while (iter.hasNext()) {
                            ConfigContainer.Config.NameValuePair parameter = iter.next();
                            xquery.append((i == 1 ? "PASSING " : ", ") + ":" + i + " AS \"" + parameter.name + "\" ");
                            i++;
                        }
                        xquery.append(")");
                        logger.debug("XQuery: " + xquery.toString());
                        PreparedStatement statement = conn.prepareStatement(xquery.toString());
                        i = 1;
                        iter = config.parameter.iterator();
                        while (iter.hasNext()) {
                            ConfigContainer.Config.NameValuePair parameter = iter.next();
                            statement.setString(i, parameter.value);
                            i++;
                        }
                        ResultSet rs = statement.executeQuery();
                        helper.startDocument();
                        helper.startElement("results");

                        while (rs.next()) {
                            for (i = 1; i <= rs.getMetaData().getColumnCount(); i++) {
                                helper.startElement("result");
                                String sqlxml = rs.getString(i);
                                if (sqlxml != null) {
                                    XMLUtils.parseDocumentFragment(sqlxml, xmlReceiver);
                                }
                            }
                            helper.endElement();
                        }

                        helper.endElement();
                        helper.endDocument();
                        rs.close();
                        statement.close();
                        conn.close();
                    } else {
                        // Use XQJ
                        XQDataSource xqs = (XQDataSource) Class.forName(config.getImplementation()).newInstance();
                        if (config.info != null) {
                            helper.startDocument();
                            helper.startElement("info");
                            helper.element("vendor", config.vendor == null ? "" : config.vendor);
                            helper.element("implementation", config.getImplementation());
                            Class[] interfaces = xqs.getClass().getInterfaces();
                            for (int i = 0; i < interfaces.length; i++) {
                                helper.element("implements", interfaces[i].getCanonicalName());
                            }
                            Constructor[] constructors = xqs.getClass().getConstructors();
                            for (int i = 0; i < constructors.length; i++) {
                                helper.element("constructor", constructors[i].toString());<commit_msg>Comment<commit_after>/**
 * Copyright (C) 2012 Orbeon, Inc.
 *
 * This program 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 program 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.
 *
 * The full text of the license is available at http://www.gnu.org/copyleft/lesser.html
 */
package org.orbeon.oxf.processor.XQuery;

import org.apache.log4j.Logger;
import org.orbeon.oxf.common.OXFException;
import org.orbeon.oxf.pipeline.api.PipelineContext;
import org.orbeon.oxf.pipeline.api.XMLReceiver;
import org.orbeon.oxf.processor.ProcessorImpl;
import org.orbeon.oxf.processor.ProcessorInputOutputInfo;
import org.orbeon.oxf.processor.ProcessorOutput;
import org.orbeon.oxf.processor.transformer.TransformerURIResolver;
import org.orbeon.oxf.xml.ContentHandlerHelper;
import org.orbeon.oxf.xml.SimpleForwardingXMLReceiver;
import org.orbeon.oxf.xml.XMLUtils;
import org.orbeon.saxon.xqj.SaxonXQDataSource;
import org.xml.sax.SAXException;

import javax.xml.namespace.QName;
import javax.xml.xquery.*;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.sql.*;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Vector;

/**
 * XQuery client/server processor, typically based on XQJ.
 */


public class XQueryProcessor extends ProcessorImpl {


    public static final String XQUERY_NAMESPACE_URI = "http://www.orbeon.org/oxf/xml/xquery";

    private static Logger logger = Logger.getLogger(XQueryProcessor.class);


    private static HashMap<String, String> knownImplementations = initKnownImplementations();

    private static HashMap<String, String> initKnownImplementations() {
        HashMap<String, String> implementations = new HashMap<String, String>();
        implementations.put("exist", "net.xqj.exist.ExistXQDataSource");
        implementations.put("oracle", "oracle.xquery.xqj.OXQDataSource");
        implementations.put("saxon", "org.orbeon.saxon.xqj.SaxonXQDataSource");
        return implementations;
    }

    private static HashMap<String, String> knownJDBCImplementations = initKnownJDBCImplementations();

    private static HashMap<String, String> initKnownJDBCImplementations() {
        HashMap<String, String> implementations = new HashMap<String, String>();
        implementations.put("oracle", "oracle.jdbc.OracleDriver");
        return implementations;
    }


    public XQueryProcessor() {
        addInputInfo(new ProcessorInputOutputInfo(INPUT_CONFIG, XQUERY_NAMESPACE_URI));
        addOutputInfo(new ProcessorInputOutputInfo(OUTPUT_DATA));
    }

    @Override
    public ProcessorOutput createOutput(String name) {
        final ProcessorOutput output = new ProcessorOutputImpl(XQueryProcessor.this, name) {
            public void readImpl(final PipelineContext pipelineContext, XMLReceiver xmlReceiver) {

                class ConfigContainer extends ObjectReceiver {
                    public ConfigContainer() {
                    }

                    public Config config;

                    class Config extends ObjectReceiver {
                        public Config() {
                        }

                        public String vendor;
                        public String implementation;
                        public String info;
                        public String username;
                        public String password;
                        public JDBC jdbc;
                        public Vector<NameValuePair> property = new Vector<NameValuePair>();
                        public String query;
                        public Vector<NameValuePair> parameter = new Vector<NameValuePair>();

                        class NameValuePair extends ObjectReceiver {
                            public NameValuePair() {
                            }

                            public String name;
                            public String value;
                        }

                        class JDBC extends ObjectReceiver {
                            public JDBC() {
                            }

                            public String url;
                            public String implementation;

                        }

                        String getImplementation() {
                            if (vendor != null) {
                                return knownImplementations.get(vendor);
                            }
                            return implementation;
                        }

                        public String getJDBCImplementation() {
                            if (jdbc.implementation != null) {
                                return jdbc.implementation;
                            }
                            return knownJDBCImplementations.get(vendor);
                        }

                    }
                }

                final ConfigContainer container = new ConfigContainer();
                readInputAsSAX(pipelineContext, INPUT_CONFIG, container);
                final ConfigContainer.Config config = container.config;
                ContentHandlerHelper helper = new ContentHandlerHelper(xmlReceiver);
                try {
                    if ("oracle".equals(config.vendor)) {
                        // Use JDBC as a workaround until we find out how to set the connection info in XQJ
                        // (see https://forums.oracle.com/forums/thread.jspa?messageID=10338407#10338407)
                        Driver driver = (Driver) Class.forName(config.getJDBCImplementation()).newInstance();
                        Connection conn = driver.connect(config.jdbc.url, null);
                        StringBuilder xquery = new StringBuilder("SELECT * from XMLTable('" + config.query.replaceAll("'", "''") + "' ");
                        Iterator<ConfigContainer.Config.NameValuePair> iter = config.parameter.iterator();
                        int i = 1;
                        while (iter.hasNext()) {
                            ConfigContainer.Config.NameValuePair parameter = iter.next();
                            xquery.append((i == 1 ? "PASSING " : ", ") + ":" + i + " AS \"" + parameter.name + "\" ");
                            i++;
                        }
                        xquery.append(")");
                        logger.debug("XQuery: " + xquery.toString());
                        PreparedStatement statement = conn.prepareStatement(xquery.toString());
                        i = 1;
                        iter = config.parameter.iterator();
                        while (iter.hasNext()) {
                            ConfigContainer.Config.NameValuePair parameter = iter.next();
                            statement.setString(i, parameter.value);
                            i++;
                        }
                        ResultSet rs = statement.executeQuery();
                        helper.startDocument();
                        helper.startElement("results");

                        while (rs.next()) {
                            for (i = 1; i <= rs.getMetaData().getColumnCount(); i++) {
                                helper.startElement("result");
                                // This used to use SQLXML.getString() but "The behavior of this method is the same as
                                // ResultSet.getString() when the designated column of the ResultSet has a
                                // type java.sql.Types of SQLXML." For 1.5 compatibility we use getString() instead.
                                String sqlxml = rs.getString(i);
                                if (sqlxml != null) {
                                    XMLUtils.parseDocumentFragment(sqlxml, xmlReceiver);
                                }
                            }
                            helper.endElement();
                        }

                        helper.endElement();
                        helper.endDocument();
                        rs.close();
                        statement.close();
                        conn.close();
                    } else {
                        // Use XQJ
                        XQDataSource xqs = (XQDataSource) Class.forName(config.getImplementation()).newInstance();
                        if (config.info != null) {
                            helper.startDocument();
                            helper.startElement("info");
                            helper.element("vendor", config.vendor == null ? "" : config.vendor);
                            helper.element("implementation", config.getImplementation());
                            Class[] interfaces = xqs.getClass().getInterfaces();
                            for (int i = 0; i < interfaces.length; i++) {
                                helper.element("implements", interfaces[i].getCanonicalName());
                            }
                            Constructor[] constructors = xqs.getClass().getConstructors();
                            for (int i = 0; i < constructors.length; i++) {
                                helper.element("constructor", constructors[i].toString());
{'fraction_non_alphanumeric': 0.08708031157668547, 'fraction_numerical': 0.0032232070910556, 'mean_word_length': 1.5235190456825267, 'pattern_counts': {'":': 2, '<': 35, '<?xml version=': 0, '>': 29, 'https://': 2, 'lorem ipsum': 0, 'www.': 4, 'xml': 26}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5627602', 'n_tokens_mistral': 4356, 'n_tokens_neox': 4071, 'n_words': 1001}