/*
 * JOnAS: Java(TM) Open Application Server
 * Copyright (C) 1999 Bull S.A.
 * Contact: jonas-team@objectweb.org
 * 
 * This library 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.1 of the License, or any later version.
 * 
 * This library 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 this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
 * USA
 *
 * Initial developer(s): Helene Joanin
 * Contributor(s): ______________________________________.
 *
 * --------------------------------------------------------------------------
 * $Id$
 * --------------------------------------------------------------------------
 */


package org.objectweb.jonas_ejb.genic;

import java.lang.String;


/**
 * This class implements useful methods not already implemented
 * in the java.lang.String class.
 */

public class JString {

    private String value;

    public JString(String s) {
	value = new String(s);
    }

    /**
     * Returns a new string resulting from replacing the first occurence, or all occurrences,
     * of sOld string in this string with sNew string. 
     * If the string sOld does not occur in the character sequence represented by this object,
     * then this string is returned. 
     * @param  sOld      the old string
     * @param  sNew      the new string
     * @param  all=true  all occurrences of the sOld string are replaced
     * @param  all=false only the first occurrence of the sOld string is replaced
     * @return a string derived from this string by replacing the first ocurrence,
     *         or every occurrence of sOld with sNew.
     */
    public String replace(String sOld, String sNew, boolean all) {
	String valReturn = "";
	int l  = value.length();
	int lo = sOld.length();
	int ln = sNew.length();
	int i = 0; int j;
	while (i <= l) {
	    j = value.indexOf(sOld, i);
	    if (j == -1) {
		valReturn = valReturn.concat(value.substring(i, l));
		i = l + 1;  // Stop, no more occurrence.
	    } else {
		valReturn = valReturn.concat(value.substring(i, j));
		valReturn = valReturn.concat(sNew);
		i = j + lo;
		if (!all) { // Stop, replace the first occurrence only.
		    valReturn = valReturn.concat(value.substring(i, l));
		    i = l + 1; // Stop, replace the first occurrence only.
		}
	    }
	}
	return(valReturn);
    }

    /**
     * Returns a new string resulting from remplacing all occurrences,
     * of sOld string in this string with sNew string. 
     * If the string sOld does not occur in the character sequence represented by this object,
     * then this string is returned. 
     * @param  sOld      the old string
     * @param  sNew      the new string
     * @return a string derived from this string by replacing every occurrence of sOld with sNew.
     */
    public String replace(String sOld, String sNew) {
	return(replace(sOld, sNew, true));
    }

    /**
     * Returns a new string resulting from remplacing the end of this String
     * from oldSuffix to newSuffix.
     * If the value of JString is return in case of it ends not by oldSuffix
     * @param  oldSuffix     the old suffix
     * @param  newSuffix     the new suffix
     * @return a string derived from this string by replacing the end oldSuffix by newSuffix
     */
    public String replaceEndWith(String oldSuffix, String newSuffix) {
	if (value.endsWith(oldSuffix)) {
	    String st = value.substring(0, value.length() - oldSuffix.length());
	    return(st.concat(newSuffix));
	} else {
	    return(value);
	}
    }

    /**
     * Returns a String object representing this JString's value
     */
    public String toString() {
	if (value == null) {
	    return("null");
	} else {
	    return(value);
	}
    }
}
