<commit_before>/*
 *  eXist Open Source Native XML Database
 *  Copyright (C) 2001-06 <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; either version 2
 *  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.
 *  
 *  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.xquery.functions.xmldb;

import org.exist.dom.QName;
import org.exist.xquery.Cardinality;
import org.exist.xquery.FunctionSignature;
import org.exist.xquery.XPathException;
import org.exist.xquery.XQueryContext;
import org.exist.xquery.value.Sequence;
import org.exist.xquery.value.SequenceType;
import org.exist.xquery.value.StringValue;
import org.exist.xquery.value.Type;
import org.xmldb.api.base.Collection;
import org.xmldb.api.base.XMLDBException;

/**
 * @author wolf
 */
public class XMLDBCreateCollection extends XMLDBAbstractCollectionManipulator {

	public final static FunctionSignature signature = new FunctionSignature(
			new QName("create-collection", XMLDBModule.NAMESPACE_URI,
					XMLDBModule.PREFIX),
					"Create a new collection as a child of the collection specified in the "
					+ "first argument. The collection can be passed as a simple collection "
					+ "path or an XMLDB URI."
					+ "The second argument specifies the name of the new "
					+ "collection. The function returns the path to the new collection "
					+ "as an xs:string or - if the collection could not be created - the empty sequence.",
			new SequenceType[]{
					new SequenceType(Type.STRING, Cardinality.EXACTLY_ONE),
					new SequenceType(Type.STRING, Cardinality.EXACTLY_ONE)},
			new SequenceType(Type.STRING, Cardinality.ZERO_OR_ONE));

	/**
	 * @param context
	 */
	public XMLDBCreateCollection(XQueryContext context) {
		super(context, signature);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.exist.xquery.Expression#eval(org.exist.dom.DocumentSet,
	 *         org.exist.xquery.value.Sequence, org.exist.xquery.value.Item)
	 */
	public Sequence evalWithCollection(Collection collection, Sequence args[], Sequence contextSequence) throws XPathException
	{
		
		String collectionName = args[1].getStringValue();
		
		try
		{
			Collection newCollection = createCollectionPath(collection, collectionName);
			
			if (newCollection == null)
				return Sequence.EMPTY_SEQUENCE;
			else
				return new StringValue(newCollection.getName());
		}
		catch(XMLDBException e)
		{
			throw new XPathException(this, "failed to create new collection " + collectionName + ": " + e.getMessage(), e);
		}
	}
}<commit_msg>[documentation] Function documentation changes and adding logging support for the xmldb module. More to come.<commit_after>/*
 * eXist Open Source Native XML Database
 * Copyright (C) 2001-2009 The eXist Project
 * http://exist-db.org
 *
 * 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
 * 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.
 * 
 * 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *  
 *  $Id$
 */
package org.exist.xquery.functions.xmldb;

import org.apache.log4j.Logger;

import org.exist.dom.QName;
import org.exist.xquery.Cardinality;
import org.exist.xquery.FunctionSignature;
import org.exist.xquery.XPathException;
import org.exist.xquery.XQueryContext;
import org.exist.xquery.value.FunctionParameterSequenceType;
import org.exist.xquery.value.Sequence;
import org.exist.xquery.value.SequenceType;
import org.exist.xquery.value.StringValue;
import org.exist.xquery.value.Type;
import org.xmldb.api.base.Collection;
import org.xmldb.api.base.XMLDBException;

/**
 * Implements eXist's xmldb:create-collection() function.
 *
 * @author wolf
 */
public class XMLDBCreateCollection extends XMLDBAbstractCollectionManipulator {
    private static final Logger logger = Logger.getLogger(XMLDBCreateCollection.class);
	public final static FunctionSignature signature = new FunctionSignature(
			new QName("create-collection", XMLDBModule.NAMESPACE_URI,
					XMLDBModule.PREFIX),
					"Create a new collection as a child of the collection specified in "
					+ "$target-collection-uri. The collection can be passed as a simple collection "
					+ "path or an XMLDB URI."
					+ "The second argument, $new-collection, specifies the name of the new "
					+ "collection. The function returns the path to the new collection "
					+ "as an xs:string or - if the collection could not be created - the empty sequence.",
			new SequenceType[]{
			    new FunctionParameterSequenceType("target-collection-uri", Type.STRING, Cardinality.EXACTLY_ONE, "the target collection uri"),
			    new FunctionParameterSequenceType("new-collection", Type.STRING, Cardinality.EXACTLY_ONE, "the new collection to create")},
			new SequenceType(Type.STRING, Cardinality.ZERO_OR_ONE));

    /**
     * @param context
     */
    public XMLDBCreateCollection(XQueryContext context) {
	super(context, signature);
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.exist.xquery.Expression#eval(org.exist.dom.DocumentSet,
     *         org.exist.xquery.value.Sequence, org.exist.xquery.value.Item)
     */
    public Sequence evalWithCollection(Collection collection, Sequence args[], Sequence contextSequence)
	throws XPathException {
	logger.info("Entering " + XMLDBModule.PREFIX + ":" + getName().getLocalName());

	String collectionName = args[1].getStringValue();
		
	try {
	    Collection newCollection = createCollectionPath(collection, collectionName);

	    logger.info("Exiting " + XMLDBModule.PREFIX + ":" + getName().getLocalName());
	    if (newCollection == null)
		return Sequence.EMPTY_SEQUENCE;
	    else
		return new StringValue(newCollection.getName());

	} catch (XMLDBException e) {
	    logger.error("Unable to create new collection " + collectionName, e);
	    logger.info("Exiting " + XMLDBModule.PREFIX + ":" + getName().getLocalName());
	    throw new XPathException(this, "failed to create new collection " + collectionName + ": " + e.getMessage(), e);
	}
    }
}
{'fraction_non_alphanumeric': 0.09545967287084038, 'fraction_numerical': 0.00535815002820079, 'mean_word_length': 3.9985905567300914, 'pattern_counts': {'":': 5, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 8}, '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': '5216644', 'n_tokens_mistral': 2165, 'n_tokens_neox': 1978, 'n_words': 648}