<commit_before>/*
 *  eXist Open Source Native XML Database
 *  Copyright (C) 2001-03 <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;

import org.exist.dom.NodeProxy;
import org.exist.dom.QName;
import org.exist.memtree.NodeImpl;
import org.exist.xquery.Cardinality;
import org.exist.xquery.Function;
import org.exist.xquery.FunctionSignature;
import org.exist.xquery.Module;
import org.exist.xquery.XQueryContext;
import org.exist.xquery.XPathException;
import org.exist.xquery.value.Item;
import org.exist.xquery.value.Sequence;
import org.exist.xquery.value.SequenceType;
import org.exist.xquery.value.Type;
import org.w3c.dom.Node;

/**
 * @author <NAME> (<EMAIL>)
 */
public class FunRoot extends Function {

	public final static FunctionSignature signatures[] = {
		new FunctionSignature(
			new QName("root", Module.BUILTIN_FUNCTION_NS),
			"Returns the root of the tree to which the context node belongs. This will usually, "
				+ "but not necessarily, be a document node.",
			new SequenceType[0],
			new SequenceType(Type.NODE, Cardinality.EXACTLY_ONE)
		),
		new FunctionSignature(
			new QName("root", Module.BUILTIN_FUNCTION_NS),
			"Returns the root of the tree to which $arg belongs. This will usually, "
				+ "but not necessarily, be a document node.",
			new SequenceType[] { new SequenceType(Type.NODE, Cardinality.ZERO_OR_ONE)},
			new SequenceType(Type.NODE, Cardinality.ZERO_OR_ONE)
		)
	};

	/**
	 * @param context
	 * @param signature
	 */
	public FunRoot(XQueryContext context, FunctionSignature signature) {
		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 eval(Sequence contextSequence, Item contextItem)
		throws XPathException {
		Item item = contextItem;
		if (getSignature().getArgumentCount() > 0) {
			Sequence seq = getArgument(0).eval(contextSequence, contextItem);
			if (seq.getLength() == 0)
				return Sequence.EMPTY_SEQUENCE;
			item = seq.itemAt(0);
		}
		if (!Type.subTypeOf(item.getType(), Type.NODE))
			throw new XPathException("Context item is not a node; got " + Type.getTypeName(item.getType()));
		if (item instanceof NodeProxy) {
		    return new NodeProxy(((NodeProxy) item).getDocument(), 1, Node.ELEMENT_NODE);
		} else
			return (NodeImpl) ((NodeImpl) item).getOwnerDocument().getDocumentElement();
	}

}<commit_msg>fn:root should return the document node, not the root element.<commit_after>/*
 *  eXist Open Source Native XML Database
 *  Copyright (C) 2001-03 <NAME>
 *  w<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;

import org.exist.dom.NodeProxy;
import org.exist.dom.QName;
import org.exist.memtree.DocumentImpl;
import org.exist.memtree.NodeImpl;
import org.exist.xquery.Cardinality;
import org.exist.xquery.Function;
import org.exist.xquery.FunctionSignature;
import org.exist.xquery.Module;
import org.exist.xquery.XQueryContext;
import org.exist.xquery.XPathException;
import org.exist.xquery.value.Item;
import org.exist.xquery.value.Sequence;
import org.exist.xquery.value.SequenceType;
import org.exist.xquery.value.Type;
import org.w3c.dom.Node;

/**
 * @author <NAME> (<EMAIL>)
 */
public class FunRoot extends Function {

	public final static FunctionSignature signatures[] = {
		new FunctionSignature(
			new QName("root", Module.BUILTIN_FUNCTION_NS),
			"Returns the root of the tree to which the context node belongs. This will usually, "
				+ "but not necessarily, be a document node.",
			new SequenceType[0],
			new SequenceType(Type.NODE, Cardinality.EXACTLY_ONE)
		),
		new FunctionSignature(
			new QName("root", Module.BUILTIN_FUNCTION_NS),
			"Returns the root of the tree to which $arg belongs. This will usually, "
				+ "but not necessarily, be a document node.",
			new SequenceType[] { new SequenceType(Type.NODE, Cardinality.ZERO_OR_ONE)},
			new SequenceType(Type.NODE, Cardinality.ZERO_OR_ONE)
		)
	};

	/**
	 * @param context
	 * @param signature
	 */
	public FunRoot(XQueryContext context, FunctionSignature signature) {
		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 eval(Sequence contextSequence, Item contextItem)
		throws XPathException {
		Item item = contextItem;
		if (getSignature().getArgumentCount() > 0) {
			Sequence seq = getArgument(0).eval(contextSequence, contextItem);
			if (seq.getLength() == 0)
				return Sequence.EMPTY_SEQUENCE;
			item = seq.itemAt(0);
		}
		if (!Type.subTypeOf(item.getType(), Type.NODE))
			throw new XPathException("Context item is not a node; got " + Type.getTypeName(item.getType()));
		if (item instanceof NodeProxy) {
		    return new NodeProxy(((NodeProxy) item).getDocument(), -1, Node.DOCUMENT_NODE);
		} else
			return (DocumentImpl) ((NodeImpl) item).getOwnerDocument();
	}

}
{'fraction_non_alphanumeric': 0.10754832770788586, 'fraction_numerical': 0.006750536974532065, 'mean_word_length': 4.157436708860759, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '4142471', 'n_tokens_mistral': 2123, 'n_tokens_neox': 1963, 'n_words': 621}