<commit_before>/*
 *  eXist Open Source Native XML Database
 *  Copyright (C) 2001 <NAME>
 *  <EMAIL>
 *  http://exist.sourceforge.net
 *
 *  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; er 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.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *  
 *  $Id:
 */
package org.exist.xmldb;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.StringTokenizer;

import org.apache.xmlrpc.XmlRpc;
import org.apache.xmlrpc.XmlRpcClient;
import org.exist.EXistException;
import org.exist.security.User;
import org.exist.storage.BrokerPool;
import org.exist.util.Configuration;
import org.xmldb.api.base.Collection;
import org.xmldb.api.base.Database;
import org.xmldb.api.base.ErrorCodes;
import org.xmldb.api.base.XMLDBException;

/**
 * The XMLDB driver class for eXist. This driver manages two different
 * internal implementations. The first communicates with a remote 
 * database using the XMLRPC protocol. The second has direct access
 * to an embedded database instance running in the same virtual machine.
 * The driver chooses an implementation depending on the XML:DB URI passed
 * to getCollection().
 * 
 * When running in embedded mode, the driver can create a new database
 * instance if none is available yet. It will do so if the property
 * "create-database" is set to "true" or if there is a system property
 * "exist.initdb" with value "true".
 * 
 * You may optionally provide the location of an alternate configuration
 * file through the "configuration" property. The driver is also able to
 * address different database instances - which may have been installed at
 * different places.
 * 
 * @author <NAME>
 */
public class DatabaseImpl implements Database {

	//TODO : discuss about other possible values
	protected final static String LOCAL_HOSTNAME = "";    
    
    protected final static int UNKNOWN_CONNECTION = -1;
    protected final static int LOCAL_CONNECTION = 0;
    protected final static int REMOTE_CONNECTION = 1;
    
    protected boolean autoCreate = false;
    protected String configuration = null;
    protected String currentInstanceName = null;
   
    private HashMap rpcClients = new HashMap();
    protected ShutdownListener shutdown = null;
	protected int mode = UNKNOWN_CONNECTION;	
	
    public DatabaseImpl() {
        try {
            XmlRpc.setEncoding( "UTF-8" );
        } catch ( Exception e ) {
        }
    	String initdb = System.getProperty( "exist.initdb" );
    	if(initdb != null)
    		autoCreate = initdb.equalsIgnoreCase("true");
    }

    public static Collection readCollection(String c, XmlRpcClient rpcClient) 
    		throws XMLDBException {
    	//TODO : refactor
    	//TODO : use dedicated function in XmldbURI
        StringTokenizer tok = new StringTokenizer( c, "/" );
        String temp = tok.nextToken();
        if(temp.equals("db"))
        	temp = '/' + temp;
        Collection current =
            new RemoteCollection( rpcClient, null, temp );
        while ( current != null && tok.hasMoreTokens() ) {
            temp = tok.nextToken();
            current =
                current.getChildCollection( ( (RemoteCollection) current ).getPath() + '/' + temp );
        }
        return current;
    }

    public boolean acceptsURI(String xmldbURI) throws XMLDBException {
    	XmldbURI uri;
    	try {    		
    		uri = new XmldbURI(XmldbURI.XMLDB_URI_PREFIX + xmldbURI);
    	} catch (Exception e) {    		
    		throw new XMLDBException(ErrorCodes.INVALID_DATABASE, "xmldb URI is not well formed:" + xmldbURI);   
    	}    	
        return (uri.getURI() != null);
    }

    /**
     *  In embedded mode: configure the database instance
     *
     *@exception  XMLDBException  Description of the Exception
     */    
    private void configure(String instanceName) throws XMLDBException {        
    	String home, file = "conf.xml";    	
        if(configuration == null) {
        	home = findExistHomeFromProperties();
        } else {
        	File f = new File(configuration);
        	if (!f.isAbsolute())
        		f = new File(new File(findExistHomeFromProperties()), configuration).getAbsoluteFile();
    		file = f.getName();
			home = f.getParentFile().getPath();
        }
		System.out.println("configuring '" + instanceName + "' using " + home + File.pathSeparator + file);
        try {
            Configuration config = new Configuration(file, home);
            BrokerPool.configure(instanceName, 1, 5, config);            
            if (shutdown != null)
            	BrokerPool.getInstance(instanceName).registerShutdownListener(shutdown);<commit_msg>Fixed System.out message.<commit_after>/*
 *  eXist Open Source Native XML Database
 *  Copyright (C) 2001 <NAME>
 *  <EMAIL>
 *  http://exist.sourceforge.net
 *
 *  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; er 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.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *  
 *  $Id:
 */
package org.exist.xmldb;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.StringTokenizer;

import org.apache.xmlrpc.XmlRpc;
import org.apache.xmlrpc.XmlRpcClient;
import org.exist.EXistException;
import org.exist.security.User;
import org.exist.storage.BrokerPool;
import org.exist.util.Configuration;
import org.xmldb.api.base.Collection;
import org.xmldb.api.base.Database;
import org.xmldb.api.base.ErrorCodes;
import org.xmldb.api.base.XMLDBException;

/**
 * The XMLDB driver class for eXist. This driver manages two different
 * internal implementations. The first communicates with a remote 
 * database using the XMLRPC protocol. The second has direct access
 * to an embedded database instance running in the same virtual machine.
 * The driver chooses an implementation depending on the XML:DB URI passed
 * to getCollection().
 * 
 * When running in embedded mode, the driver can create a new database
 * instance if none is available yet. It will do so if the property
 * "create-database" is set to "true" or if there is a system property
 * "exist.initdb" with value "true".
 * 
 * You may optionally provide the location of an alternate configuration
 * file through the "configuration" property. The driver is also able to
 * address different database instances - which may have been installed at
 * different places.
 * 
 * @author <NAME>
 */
public class DatabaseImpl implements Database {

	//TODO : discuss about other possible values
	protected final static String LOCAL_HOSTNAME = "";    
    
    protected final static int UNKNOWN_CONNECTION = -1;
    protected final static int LOCAL_CONNECTION = 0;
    protected final static int REMOTE_CONNECTION = 1;
    
    protected boolean autoCreate = false;
    protected String configuration = null;
    protected String currentInstanceName = null;
   
    private HashMap rpcClients = new HashMap();
    protected ShutdownListener shutdown = null;
	protected int mode = UNKNOWN_CONNECTION;	
	
    public DatabaseImpl() {
        try {
            XmlRpc.setEncoding( "UTF-8" );
        } catch ( Exception e ) {
        }
    	String initdb = System.getProperty( "exist.initdb" );
    	if(initdb != null)
    		autoCreate = initdb.equalsIgnoreCase("true");
    }

    public static Collection readCollection(String c, XmlRpcClient rpcClient) 
    		throws XMLDBException {
    	//TODO : refactor
    	//TODO : use dedicated function in XmldbURI
        StringTokenizer tok = new StringTokenizer( c, "/" );
        String temp = tok.nextToken();
        if(temp.equals("db"))
        	temp = '/' + temp;
        Collection current =
            new RemoteCollection( rpcClient, null, temp );
        while ( current != null && tok.hasMoreTokens() ) {
            temp = tok.nextToken();
            current =
                current.getChildCollection( ( (RemoteCollection) current ).getPath() + '/' + temp );
        }
        return current;
    }

    public boolean acceptsURI(String xmldbURI) throws XMLDBException {
    	XmldbURI uri;
    	try {    		
    		uri = new XmldbURI(XmldbURI.XMLDB_URI_PREFIX + xmldbURI);
    	} catch (Exception e) {    		
    		throw new XMLDBException(ErrorCodes.INVALID_DATABASE, "xmldb URI is not well formed:" + xmldbURI);   
    	}    	
        return (uri.getURI() != null);
    }

    /**
     *  In embedded mode: configure the database instance
     *
     *@exception  XMLDBException  Description of the Exception
     */    
    private void configure(String instanceName) throws XMLDBException {        
    	String home, file = "conf.xml";    	
        if(configuration == null) {
        	home = findExistHomeFromProperties();
        } else {
        	File f = new File(configuration);
        	if (!f.isAbsolute())
        		f = new File(new File(findExistHomeFromProperties()), configuration).getAbsoluteFile();
    		file = f.getName();
			home = f.getParentFile().getPath();
        }
		System.out.println("Configuring '" + instanceName + "' using " + home + File.separatorChar + file);
        try {
            Configuration config = new Configuration(file, home);
            BrokerPool.configure(instanceName, 1, 5, config);            
            if (shutdown != null)
            	BrokerPool.getInstance(instanceName).registerShutdownListener(shutdown);
{'fraction_non_alphanumeric': 0.08711538461538462, 'fraction_numerical': 0.0034615384615384616, 'mean_word_length': 2.932325141776938, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 24}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3000398', 'n_tokens_mistral': 2936, 'n_tokens_neox': 2861, 'n_words': 989}