<commit_before>/**
 * Copyright  2002 The JA-SIG Collaborative.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or withoutu
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * 3. Redistributions of any form whatsoever must retain the following
 *    acknowledgment:
 *    "This product includes software developed by the JA-SIG Collaborative
 *    (http://www.jasig.org/)."
 *
 * THIS SOFTWARE IS PROVIDED BY THE JA-SIG COLLABORATIVE "AS IS" AND ANY
 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE JA-SIG COLLABORATIVE OR
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 */



package org.jasig.portal.utils;

import java.io.*;
import java.util.*;

/**
 * <p>Title: CommonUtils class</p>
 * <p>Description: CommonUtils class contains base useful utilities </p>
 * <p>Copyright: Copyright (c) 2002</p>
 * <p>Company: Instructional Media & Magic</p>
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 * @version 1.1
 */


/**
 * CommonUtils class contains base useful utilities
 * @author mike
 * @version 1.1
 */
public class CommonUtils {

   /**
    ** <code>nvl</code> replaces "sourceString" with "replaceString" if sourceString equals to null
        ** @param String sourceString - string to replace
        ** @param String replaceString - replacement
        ** @return String - processed string
    */
   public static String nvl( String sourceStr, String replaceStr ) {
     return (sourceStr != null)?sourceStr:replaceStr;
   }

   /**
    ** <code>nvl</code> replaces "sourceString" with "replaceString" if sourceString equals to null
        ** @param String sourceString - string to replace
        ** @param String replaceString - replacement
        ** @param String prefix - prefix
        ** @return String - processed string
    */
   public static String nvl( String sourceStr, String replaceStr, String prefix ) {
     String nvlStr = nvl(sourceStr,replaceStr);
     if ( !nvlStr.trim().equals("") )
       return new String(prefix+nvlStr);
     else
       return nvlStr;
   }


   /**
    ** <code>nvl</code> replaces "sourceString" with "" if sourceString equals to null
        ** @param String sourceString - string to replace
        ** @return String - processed string
    */
   public static String nvl( String sourceStr ) {
     return nvl(sourceStr,"");
   }


    /**
     * replaces "replacedString" with "newString" in "text"
     * @param text text where to replace
     * @param replacedString string to replace
     * @param newString new string
     * @return new text
     */
    public static String replaceText(String text, String replacedString, String newString) {
            int lastIndex =- newString.length();
            int replacedStringLength = replacedString.length();
            int newStringLength = newString.length();
            while ((lastIndex = text.indexOf(replacedString,
                lastIndex + newStringLength)) != -1) {
                    text = text.substring(0, lastIndex) + newString +
                        text.substring(lastIndex + replacedStringLength);
            }
            return text;
    }

    public static void replaceSubstVariables(Hashtable original, Hashtable subst) {

      for (Enumeration original_keys = original.keys(); original_keys.hasMoreElements(); ) {
        String original_key = (String)original_keys.nextElement();
        String original_value = (String)original.get(original_key);

        for (Enumeration subst_keys = subst.keys(); subst_keys.hasMoreElements(); ) {
          String subst_key = (String)subst_keys.nextElement();
          String subst_value = (String)subst.get(subst_key);

          original_value = replaceText(original_value, subst_key, subst_value);
        }

        original.put(original_key, original_value);

      }

    }

    public static String stackTraceToString(Exception e) {
      StringWriter strwrt = new StringWriter();
      e.printStackTrace(new PrintWriter(strwrt));
      return strwrt.toString();
    }

    /**
  * This method gets a Properties object by fileName parameter.
  * @param fileName - the file name for this properties file
  * @return a Properties object
  **/
 public static Properties getProperties ( Object object, String fileName ) {
     Properties props = new Properties();
     try {
     if ( object != null ) {
        InputStream in = object.getClass().getClassLoader().getResourceAsStream(fileName);
      if ( in != null )
        props.load( in );
      else
        System.err.println( "Util::getProperties: Couldn't load \""+fileName+"\"" );
     } else
        System.err.println( "Util::getProperties: object is null!" );
     } catch ( IOException ioe ) {
        System.err.println( "Util::getProperties: " + ioe );
       }

      return props;
 }


 /**
  * This method gets an array of strings from given string splitted by commas.
  * @param str - a string value
  * @param delim - a delimeter
  * @return an array of strings
  **/
 public static String[] getSplitStringByCommas ( String str, String delim ) {
   if ( str == null ) return null;
   StringTokenizer st = new StringTokenizer(str,delim);
   String[] strArray = new String[st.countTokens()];
   for ( int i = 0; st.hasMoreTokens(); i++ ) {
    strArray[i] = st.nextToken().trim();
   }
   return strArray;
 }

 /**
  * This method gets a properties of strings from given string splitted by commas.
  * @param keys - a string keys for properties<commit_msg>Re-applied patch 1.4 which was to remove the getProperties() method.  It is already in ResourceLoader.<commit_after>/**
 * Copyright  2002 The JA-SIG Collaborative.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or withoutu
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * 3. Redistributions of any form whatsoever must retain the following
 *    acknowledgment:
 *    "This product includes software developed by the JA-SIG Collaborative
 *    (http://www.jasig.org/)."
 *
 * THIS SOFTWARE IS PROVIDED BY THE JA-SIG COLLABORATIVE "AS IS" AND ANY
 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE JA-SIG COLLABORATIVE OR
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 */



package org.jasig.portal.utils;

import java.io.*;
import java.util.*;

/**
 * CommonUtils class contains base useful utilities
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 * @version $Revision$ 
 */
public class CommonUtils {

   /**
    ** <code>nvl</code> replaces "sourceString" with "replaceString" if sourceString equals to null
        ** @param String sourceString - string to replace
        ** @param String replaceString - replacement
        ** @return String - processed string
    */
   public static String nvl( String sourceStr, String replaceStr ) {
     return (sourceStr != null)?sourceStr:replaceStr;
   }

   /**
    ** <code>nvl</code> replaces "sourceString" with "replaceString" if sourceString equals to null
        ** @param String sourceString - string to replace
        ** @param String replaceString - replacement
        ** @param String prefix - prefix
        ** @return String - processed string
    */
   public static String nvl( String sourceStr, String replaceStr, String prefix ) {
     String nvlStr = nvl(sourceStr,replaceStr);
     if ( !nvlStr.trim().equals("") )
       return new String(prefix+nvlStr);
     else
       return nvlStr;
   }


   /**
    ** <code>nvl</code> replaces "sourceString" with "" if sourceString equals to null
        ** @param String sourceString - string to replace
        ** @return String - processed string
    */
   public static String nvl( String sourceStr ) {
     return nvl(sourceStr,"");
   }


    /**
     * replaces "replacedString" with "newString" in "text"
     * @param text text where to replace
     * @param replacedString string to replace
     * @param newString new string
     * @return new text
     */
    public static String replaceText(String text, String replacedString, String newString) {
            int lastIndex =- newString.length();
            int replacedStringLength = replacedString.length();
            int newStringLength = newString.length();
            while ((lastIndex = text.indexOf(replacedString,
                lastIndex + newStringLength)) != -1) {
                    text = text.substring(0, lastIndex) + newString +
                        text.substring(lastIndex + replacedStringLength);
            }
            return text;
    }

    public static void replaceSubstVariables(Hashtable original, Hashtable subst) {

      for (Enumeration original_keys = original.keys(); original_keys.hasMoreElements(); ) {
        String original_key = (String)original_keys.nextElement();
        String original_value = (String)original.get(original_key);

        for (Enumeration subst_keys = subst.keys(); subst_keys.hasMoreElements(); ) {
          String subst_key = (String)subst_keys.nextElement();
          String subst_value = (String)subst.get(subst_key);

          original_value = replaceText(original_value, subst_key, subst_value);
        }

        original.put(original_key, original_value);

      }

    }

    public static String stackTraceToString(Exception e) {
      StringWriter strwrt = new StringWriter();
      e.printStackTrace(new PrintWriter(strwrt));
      return strwrt.toString();
    }


 /**
  * This method gets an array of strings from given string splitted by commas.
  * @param str - a string value
  * @param delim - a delimeter
  * @return an array of strings
  **/
 public static String[] getSplitStringByCommas ( String str, String delim ) {
   if ( str == null ) return null;
   StringTokenizer st = new StringTokenizer(str,delim);
   String[] strArray = new String[st.countTokens()];
   for ( int i = 0; st.hasMoreTokens(); i++ ) {
    strArray[i] = st.nextToken().trim();
   }
   return strArray;
 }

 /**
  * This method gets a properties of strings from given string splitted by commas.
  * @param keys - a string keys for properties
{'fraction_non_alphanumeric': 0.09471751648168239, 'fraction_numerical': 0.00250354669114579, 'mean_word_length': 3.056872037914692, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3129835', 'n_tokens_mistral': 3312, 'n_tokens_neox': 3133, 'n_words': 1156}