| /******************************************************************************* |
| * 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.util.ArrayList; |
| import java.util.Collection; |
| import java.util.Iterator; |
| |
| import org.eclipse.wst.xml.xpath2.processor.DynamicError; |
| import org.eclipse.wst.xml.xpath2.processor.ResultSequence; |
| import org.eclipse.wst.xml.xpath2.processor.ResultSequenceFactory; |
| 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.XSBoolean; |
| import org.eclipse.wst.xml.xpath2.processor.internal.types.XSString; |
| |
| /** |
| * Returns an xs:boolean indicating whether or not the value of $arg1 ends with |
| * a sequence of collation units that provides a minimal match to the collation |
| * units of M$arg2 according to the collation that is used. |
| */ |
| public class FnEndsWith extends Function { |
| private static Collection _expected_args = null; |
| |
| /** |
| * Constructor for FnEndsWith. |
| */ |
| public FnEndsWith() { |
| super(new QName("ends-with"), 2); |
| } |
| |
| /** |
| * Evaluate arguments. |
| * |
| * @param args |
| * argument expressions. |
| * @throws DynamicError |
| * Dynamic error. |
| * @return Result of evaluation. |
| */ |
| public ResultSequence evaluate(Collection args) throws DynamicError { |
| return ends_with(args); |
| } |
| |
| /** |
| * Ends-with operation. |
| * |
| * @param args |
| * Result from the expressions evaluation. |
| * @throws DynamicError |
| * Dynamic error. |
| * @return Result of fn:ends-with operation. |
| */ |
| public static ResultSequence ends_with(Collection args) throws DynamicError { |
| Collection cargs = Function.convert_arguments(args, expected_args()); |
| |
| ResultSequence rs = ResultSequenceFactory.create_new(); |
| |
| // get args |
| Iterator argiter = cargs.iterator(); |
| ResultSequence arg1 = (ResultSequence) argiter.next(); |
| String str1 = ""; |
| String str2 = ""; |
| if (!arg1.empty()) |
| str1 = ((XSString) arg1.first()).value(); |
| |
| ResultSequence arg2 = (ResultSequence) argiter.next(); |
| if (!arg2.empty()) |
| str2 = ((XSString) arg2.first()).value(); |
| |
| int str1len = str1.length(); |
| int str2len = str2.length(); |
| |
| if (str1len == 0 && str2len != 0) { |
| rs.add(new XSBoolean(false)); |
| return rs; |
| } |
| if (str2len == 0) { |
| rs.add(new XSBoolean(true)); |
| return rs; |
| } |
| |
| if (str1.endsWith(str2)) |
| rs.add(new XSBoolean(true)); |
| else |
| rs.add(new XSBoolean(false)); |
| |
| return rs; |
| } |
| |
| /** |
| * Obtain a list of expected arguments. |
| * |
| * @return Result of operation. |
| */ |
| public synchronized static Collection expected_args() { |
| if (_expected_args == null) { |
| _expected_args = new ArrayList(); |
| SeqType arg = new SeqType(new XSString(), SeqType.OCC_QMARK); |
| _expected_args.add(arg); |
| _expected_args.add(arg); |
| } |
| |
| return _expected_args; |
| } |
| } |