/*
 * This file is part of Mapyrus, software for plotting maps.
 * Copyright (C) 2003 - 2010 Simon Chenery.
 *
 * Mapyrus 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.
 *
 * Mapyrus 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 Mapyrus; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

/*
 * @(#) $Id$
 */
package org.mapyrus.function;

import java.io.File;
import java.util.ArrayList;

import org.mapyrus.Argument;
import org.mapyrus.ContextStack;
import org.mapyrus.MapyrusException;

/**
 * Function checking if a file exists and is readable.
 * For example, readable("/etc/passwd") returns 1 on a UNIX machine.
 */
public class Readable implements Function
{
	/**
	 * @see org.mapyrus.function.Function#evaluate(org.mapyrus.ContextStack, ArrayList)
	 */
	public Argument evaluate(ContextStack context, ArrayList args)
		throws MapyrusException
	{
		Argument arg1 = (Argument)args.get(0);
		String filename = arg1.getStringValue();
		File f = new File(filename);
		
		Argument retval;
		if (f.isFile() && f.canRead())
			retval = Argument.numericOne;
		else
			retval = Argument.numericZero;
		return(retval);
	}

	/**
	 * @see org.mapyrus.function.Function#getMaxArgumentCount()
	 */
	public int getMaxArgumentCount()
	{
		return(1);
	}

	/**
	 * @see org.mapyrus.function.Function#getMinArgumentCount()
	 */
	public int getMinArgumentCount()
	{
		return(1);
	}

	/**
	 * @see org.mapyrus.function.Function#getName()
	 */
	public String getName()
	{
		return("readable");
	}
}



@@ -1526,18 +1526,52 @@
 t retval
-;%0A
+ = Argument.numericZero;%0A%09%09try%0A%09%09%7B%0A%09
 %09%09if (f.
@@ -1595,16 +1595,17 @@
 Read())%0A
+%09
 %09%09%09retva
@@ -1635,46 +1635,108 @@
 ;%0A%09%09
-else%0A%09%09%09retval = Argument.numericZero;
+%7D%0A%09%09catch (SecurityException e)%0A%09%09%7B%0A%09%09%09/*%0A%09%09%09 * No access to file so it is not readable.%0A%09%09%09 */%0A%09%09%7D%0A
 %0A%09%09r

Return false if file is not accessable due to SecurityException.