blob: 2bdc76f8dc34a4e7fa5bad2dc73b28166b76be46 [file] [log] [blame]
/*******************************************************************************
* 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 273760 - wrong namespace for functions and data types
* Mukul Gandhi - bug 274805 - improvements to xs:integer data type
* David Carver - bug 282223 - implementation of xs:duration.
* David Carver (STAR) - bug 262765 - fixed expected xs:duration argument
* 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.ArrayList;
import java.util.Collection;
import org.eclipse.wst.xml.xpath2.api.ResultBuffer;
import org.eclipse.wst.xml.xpath2.api.ResultSequence;
import org.eclipse.wst.xml.xpath2.processor.DynamicError;
import org.eclipse.wst.xml.xpath2.processor.internal.SeqType;
import org.eclipse.wst.xml.xpath2.processor.internal.types.QName;
import org.eclipse.wst.xml.xpath2.processor.internal.types.XSDuration;
import org.eclipse.wst.xml.xpath2.processor.internal.types.XSInteger;
/**
* Returns an xs:integer representing the years component in the canonical
* lexical representation of the value of $arg. The result may be negative. If
* $arg is the empty sequence, returns the empty sequence.
*/
public class FnYearsFromDuration extends Function {
private static Collection _expected_args = null;
/**
* Constructor for FnYearsFromDuration.
*/
public FnYearsFromDuration() {
super(new QName("years-from-duration"), 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 years_from_duration(args);
}
/**
* Years-from-Duration operation.
*
* @param args
* Result from the expressions evaluation.
* @throws DynamicError
* Dynamic error.
* @return Result of fn:years-from-duration operation.
*/
public static ResultSequence years_from_duration(Collection args)
throws DynamicError {
Collection cargs = Function.convert_arguments(args, expected_args());
ResultSequence arg1 = (ResultSequence) cargs.iterator().next();
if (arg1.empty()) {
return ResultBuffer.EMPTY;
}
XSDuration ymd = (XSDuration) arg1.first();
int res = ymd.year();
if (ymd.negative())
res *= -1;
return new XSInteger(BigInteger.valueOf(res));
}
/**
* Obtain a list of expected arguments.
*
* @return Result of operation.
*/
public synchronized static Collection expected_args() {
if (_expected_args == null) {
_expected_args = new ArrayList();
_expected_args.add(new SeqType(new XSDuration(),
SeqType.OCC_QMARK));
}
return _expected_args;
}
}