/*
 * Copyright
 *   2007 axYus - www.axyus.com
 *   2005 C.Marchand - christophe.marchand@axyus.com
 *
 * This file is part of XEMELIOS.
 *
 * XEMELIOS 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 3 of the License, or
 * (at your option) any later version.
 *
 * XEMELIOS 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 XEMELIOS; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package fr.gouv.finances.dgfip.utils;

import java.text.ParseException;

/**
 * @author chm
 */
public class StringUtilities {

	/**
	 * Removes the suffix from the passed file name.
	 * @param	fileName	File name to remove suffix from.
	 * @return	<tt>fileName</tt> without a suffix.
	 * @throws	IllegalArgumentException	if <tt>null</tt> file name passed.
	 */
	public static String removeFileNameSuffix(String fileName) {
		if (fileName == null) {
			throw new IllegalArgumentException("null file name");
		}
		int pos = fileName.lastIndexOf('.');
		if (pos > 0 && pos < fileName.length() - 1) {
			return fileName.substring(0, pos);
		}
		return fileName;
	}
	public static String getFileNameSuffix(String fileName) {
		int pos = fileName.lastIndexOf('.');
		if(pos>0 && pos<fileName.length()-1) {
			return fileName.substring(pos+1);
		}
		return fileName;
	}

	/**
	 * Change the passed file name to its corresponding class name. E.G. change &quot;Utilities.class&quot; to &quot;Utilities&quot;.
	 * @param	name	Class name to be changed. If this does not represent a Java class then <TT>null</TT> is returned.
	 * @throws IllegalArgumentException	If a null <tt>name</tt> passed.
	 */
	public static String changeFileNameToClassName(String name) {
		if (name == null) {
			throw new IllegalArgumentException("null File Name");
		}
		String className = null;
		if (name.toLowerCase().endsWith(".class")) {
			className = name.replace('/', '.');
			className = className.replace('\\', '.');
			className = className.substring(0, className.length() - 6);
		}
		return className;
	}
        /**
         * Returns the position of closing parenthesis at <tt>openningPos</tt>.
         * If char at <tt>openningPos</tt> is not an openning parenthesis, it is ignored.
         * If <tt>source</tt> expression contains nested '(...)' blocks, they are ignored.
         * If closing parenthesis can not be found (unbalanced parenthesis), an {@link ParseException} is thrown.
         * @param source The expression to search in
         * @param openningPos The position where the '(' should be
         * @return The position where the closing ')' is
         * @throw ParseException if closing parenthesis can not be found
         */
        public static int getClosingParenthesisPos(String source,int openningPos) throws ParseException {
            int count=0;
            int length=source.length();
            int current = openningPos;
            boolean found = false;
            while(!found && current<length) {
                char c = source.charAt(current);
                if(c=='(') count++;
                else if(c==')') {
                    count--;
                    if(count==0) found = true;
                }
                current++;
            }
            if(!found) throw new ParseException("closing ')' not found for openning '(' at "+openningPos,openningPos);
            return current-1;
        }
}
