| /******************************************************************************* |
| * Copyright (c) 2005, 2010 Andrea Bittau, University College London, and others |
| * All rights reserved. This program and the accompanying materials |
| * are made available under the terms of the Eclipse Public License v1.0 |
| * which accompanies this distribution, and is available at |
| * http://www.eclipse.org/legal/epl-v10.html |
| * |
| * Contributors: |
| * Andrea Bittau - initial API and implementation from the PsychoPath XPath 2.0 |
| * Mukul Gandhi - bug 280798 - PsychoPath support for JDK 1.4 |
| *******************************************************************************/ |
| |
| package org.eclipse.wst.xml.xpath2.processor.internal.function; |
| |
| import java.math.BigInteger; |
| import java.util.Collection; |
| import java.util.Iterator; |
| |
| import org.eclipse.wst.xml.xpath2.api.ResultSequence; |
| import org.eclipse.wst.xml.xpath2.processor.DynamicError; |
| import org.eclipse.wst.xml.xpath2.processor.ResultSequenceFactory; |
| import org.eclipse.wst.xml.xpath2.processor.internal.types.QName; |
| import org.eclipse.wst.xml.xpath2.processor.internal.types.XSInteger; |
| |
| /** |
| * Returns the number of items in the value of $arg. Returns 0 if $arg is the |
| * empty sequence. |
| */ |
| public class FnCount extends Function { |
| /** |
| * Constructor for FnCount. |
| */ |
| public FnCount() { |
| super(new QName("count"), 1); |
| } |
| |
| /** |
| * Evaluate arguments. |
| * |
| * @param args |
| * argument expressions. |
| * @throws DynamicError |
| * Dynamic error. |
| * @return Result of evaluation. |
| */ |
| public ResultSequence evaluate(Collection args, org.eclipse.wst.xml.xpath2.api.EvaluationContext ec) throws DynamicError { |
| return count(args); |
| } |
| |
| /** |
| * Count operation. |
| * |
| * @param args |
| * Result from the expressions evaluation. |
| * @throws DynamicError |
| * Dynamic error. |
| * @return Result of fn:count operation. |
| */ |
| public static ResultSequence count(Collection args) throws DynamicError { |
| |
| assert args.size() == 1; |
| |
| // get args |
| Iterator citer = args.iterator(); |
| ResultSequence arg = (ResultSequence) citer.next(); |
| |
| return ResultSequenceFactory.create_new(new XSInteger(BigInteger.valueOf(arg.size()))); |
| } |
| } |