/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.xpath.functions.array;

import org.apache.xpath.Expression;
import org.apache.xpath.XPathContext;
import org.apache.xpath.axes.SelfIteratorNoPredicate;
import org.apache.xpath.functions.FunctionOneArg;
import org.apache.xpath.objects.ResultSequence;
import org.apache.xpath.objects.XMLNodeCursorImpl;
import org.apache.xpath.objects.XObject;
import org.apache.xpath.objects.XPathArray;
import org.apache.xpath.operations.Variable;

import xml.xpath31.processor.types.XSInteger;

/**
 * Implementation of the array:size function.
 * 
 * @author Mukul Gandhi <mukulg@apache.org>
 * 
 * @xsl.usage advanced
 */
public class FuncArraySize extends FunctionOneArg {

	private static final long serialVersionUID = 5600653040311629451L;
	
	/**
	 * Class constructor.
	 */
	public FuncArraySize() {
		m_defined_arity = new Short[] { 1 };
	}
	
	public XObject execute(XPathContext xctxt) throws javax.xml.transform.TransformerException
	{
	    XObject result = null;
	       
	    Expression arg0 = getArg0();
	    
	    XObject xObject = null;
	    
	    if (arg0 instanceof Variable) {
	       xObject = ((Variable)arg0).execute(xctxt);
	       if (xObject instanceof XPathArray) {
	    	   result = new XSInteger(((XPathArray)xObject).size() + ""); 
	       }
	       else if (xObject instanceof XMLNodeCursorImpl) {
	    	  result = new XSInteger(((XMLNodeCursorImpl)xObject).getLength() + "");  
	       }
	       else if (xObject instanceof ResultSequence) {
	    	  result = new XSInteger(((ResultSequence)xObject).size() + "");  
	       }
	       else {
	    	  result = new XSInteger("1");  
	       }
	    }
	    else if ((arg0 instanceof SelfIteratorNoPredicate) && (xctxt.getXPath3ContextItem() != null)) {
	       XObject xpath3CtxtItem = xctxt.getXPath3ContextItem();
	       if (xpath3CtxtItem instanceof XPathArray) {
	          XPathArray xpathArr = (XPathArray)xpath3CtxtItem;
	          
	          result = new XSInteger("" + xpathArr.size());
	       }
	       else {
	    	  result = new XSInteger("0"); 
	       }
	    }
	    else {
	       xObject = arg0.execute(xctxt);
	       if (xObject instanceof XPathArray) {
	    	   result = new XSInteger(((XPathArray)xObject).size() + ""); 
	       }
	       else if (xObject instanceof XMLNodeCursorImpl) {
		   	  result = new XSInteger(((XMLNodeCursorImpl)xObject).getLength() + "");  
		   }
		   else if (xObject instanceof ResultSequence) {
		      result = new XSInteger(((ResultSequence)xObject).size() + "");  
		   }
		   else {
		      result = new XSInteger("1");  
		   }
	    }
	    
	    return result;
	}

}
