com.ing.connector.util.WStringUtil.java Source code

Java tutorial

Introduction

Here is the source code for com.ing.connector.util.WStringUtil.java

Source

//******************************************************************************
// THE SOFTWARE COMPRISING THIS SYSTEM IS THE PROPERTY OF INSURANCE SOFTWARE
// SOLUTIONS CORP. OR ITS LICENSORS.
//
// ALL COPYRIGHT, PATENT, TRADE SECRET, AND OTHER INTELLECTUAL PROPERTY RIGHTS
// IN THE SOFTWARE COMPRISING THIS SYSTEM ARE, AND SHALL REMAIN, THE VALUABLE
// PROPERTY OF INSURANCE SOFTWARE SOLUTIONS CORP. OR ITS LICENSORS.
//
// USE, DISCLOSURE, OR REPRODUCTION OF THIS SOFTWARE IS STRICTLY PROHIBITED,
// EXCEPT UNDER WRITTEN LICENSE FROM INSURANCE SOFTWARE SOLUTIONS CORP. OR ITS
// LICENSORS.
//
// (C) COPYRIGHT 1998 INSURANCE SOFTWARE SOLUTIONS CORP.  ALL RIGHTS RESERVED
//******************************************************************************
// $Log:
//
//   Rev 1.1   Apr 2001   A?O
// Added formatCapitalization and helper methods
//******************************************************************************

package com.ing.connector.util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.ing.connector.Registrar;

/**
 * WStringUtil provides class methods for string manipulation.
 *
 */

public class WStringUtil {
    /**
     *  Pad to string to the right to "length" using character "c".
     *  Warning: if string.length() > length then "string" is returned, not truncated
     */
    public static String padStringRight(String string, int length, char c) {
        StringBuffer sb = new StringBuffer(length);
        sb.append(string);
        while (sb.length() < sb.capacity())
            sb.append(c);
        return sb.toString();
    }

    /**
     *  Pad to string to the left to "length" using character "c".
     *  Warning: if string.length() > length then "string" is returned, not truncated
     */
    public static String padStringLeft(String string, int length, char c) {
        StringBuffer sb = new StringBuffer(length);
        int difference = length - string.length();
        while (difference > 0) {
            sb.append(c);
            difference--;
        }
        sb.append(string);
        return sb.toString();
    }

    /**
     * Trim the leading and trailing whitespace from a string.  If a null
    * reference is passed in then return an empty string.
     */
    public static String trim(String string) {
        if (string == null) {
            return "";
        }

        return string.trim();
    }

    //A?O1.1 changes starts here

    /**
     *  formatCapitalization --> formats the input stream to the
     *                      proper capitalization [this is the
     *                      public method that calls all other private
     *                      capitalization methods within this class]
     */
    public String formatCapitalization(String aString) {
        String lString = null;

        if (aString != null) {
            lString = aString;

            lString = generalFormating(lString);
            lString = checkForZip(lString);
            lString = checkForSpecialWords(lString);
            lString = checkForSpecialPrefixes(lString);

            return lString;
        } else {
            return "";
        }
    }

    /**
     *  checkForSpecialPrefixes --> processes the known prefix:
     *                        "McX" so that the letter "X"
     *                        proceeding the prefix is
     *                        capitalized
     */
    private String checkForSpecialPrefixes(String aString) {
        String lString = aString;
        int lLength = lString.length();

        if (lLength >= 3) {
            String[] lArray = new String[(lLength - 2)];

            for (int i = 0; i < lLength - 2; i++) {
                lArray[i] = lString.substring(i, i + 3);

                if (lArray[i].substring(0, 2).equalsIgnoreCase("Mc") && Character.isLetter(lString.charAt(i + 2))) {
                    char[] lChar = lString.toCharArray();

                    lChar[i + 2] = Character.toUpperCase(lChar[i + 2]);

                    lString = new String(lChar);
                }
            }
        }

        return lString;
    }

    /**
     *  generalFormating --> makes the first letter of any character
     *                       string uppercase; otherwise, makes all
     *                       characters lowercase
     */
    private String generalFormating(String aString) {
        String lString = aString;
        char lPreviousChar = '\u0000';

        lString = lString.toUpperCase();

        char[] lCharArray = lString.toCharArray();

        for (int i = 0; i < lString.length(); i++) {
            if (Character.isLetterOrDigit(lPreviousChar)) {
                lCharArray[i] = Character.toLowerCase(lCharArray[i]);
            }
            lPreviousChar = lCharArray[i];
        }

        lString = new String(lCharArray);

        return lString;
    }

    /**
     *  checkForSpecialWords --> uppercases any appropriate
     *                           word phrases
     */
    private String checkForSpecialWords(String aString) {
        String lString = aString;
        int lLength = lString.length();

        //************************************************************
        //the following logic checks for word phrases 2 char in length
        //************************************************************
        if (lLength >= 2) {
            String[] lArrayFor2 = new String[(lLength - 1)];

            for (int i = 0; i < lLength - 1; i++) {
                lArrayFor2[i] = lString.substring(i, i + 2);

                if (lArrayFor2[i].equalsIgnoreCase("MD") || lArrayFor2[i].equalsIgnoreCase("II")
                        || lArrayFor2[i].equalsIgnoreCase("IV")) {
                    char[] lChar2 = lString.toCharArray();

                    lChar2[i] = Character.toUpperCase(lChar2[i]);
                    lChar2[i + 1] = Character.toUpperCase(lChar2[i + 1]);

                    lString = new String(lChar2);
                }
            }
        }

        //************************************************************
        //the following logic checks for word phrases 3 char in length
        //************************************************************
        if (lLength >= 3) {
            String[] lArrayFor3 = new String[(lLength - 2)];

            for (int i = 0; i < lLength - 2; i++) {
                lArrayFor3[i] = lString.substring(i, i + 3);

                if (lArrayFor3[i].equalsIgnoreCase("III") || lArrayFor3[i].equalsIgnoreCase("CPA")) {
                    char[] lChar3 = lString.toCharArray();

                    lChar3[i] = Character.toUpperCase(lChar3[i]);
                    lChar3[i + 1] = Character.toUpperCase(lChar3[i + 1]);
                    lChar3[i + 2] = Character.toUpperCase(lChar3[i + 2]);

                    lString = new String(lChar3);
                }
            }
        }

        //************************************************************
        //the following logic checks for word phrases 4 char in length
        //************************************************************
        if (lLength >= 4) {
            String[] lArrayFor4 = new String[(lLength - 3)];

            for (int i = 0; i < lLength - 3; i++) {
                lArrayFor4[i] = lString.substring(i, i + 4);

                //*** make uppercase
                if (lArrayFor4[i].equalsIgnoreCase("FLMI") || lArrayFor4[i].equalsIgnoreCase("CLCU")) {
                    char[] lChar4 = lString.toCharArray();

                    lChar4[i] = Character.toUpperCase(lChar4[i]);
                    lChar4[i + 1] = Character.toUpperCase(lChar4[i + 1]);
                    lChar4[i + 2] = Character.toUpperCase(lChar4[i + 2]);
                    lChar4[i + 3] = Character.toUpperCase(lChar4[i + 3]);

                    lString = new String(lChar4);
                }

                //*** make lowercase
                if (lArrayFor4[i].equalsIgnoreCase(" OR ") || lArrayFor4[i].equalsIgnoreCase(" OF ")) {
                    char[] lChar4 = lString.toCharArray();

                    lChar4[i + 1] = Character.toLowerCase(lChar4[i + 1]);
                    lChar4[i + 2] = Character.toLowerCase(lChar4[i + 2]);

                    lString = new String(lChar4);
                }
            }
        }

        //************************************************************
        //the following logic checks for word phrases 5 char in length
        //************************************************************
        if (lLength >= 5) {
            String[] lArrayFor5 = new String[(lLength - 4)];

            for (int i = 0; i < lLength - 4; i++) {
                lArrayFor5[i] = lString.substring(i, i + 5);

                //*** make lowercase
                if (lArrayFor5[i].equalsIgnoreCase(" AND ")) {
                    char[] lChar5 = lString.toCharArray();

                    lChar5[i + 1] = Character.toLowerCase(lChar5[i + 1]);
                    lChar5[i + 2] = Character.toLowerCase(lChar5[i + 2]);
                    lChar5[i + 3] = Character.toLowerCase(lChar5[i + 3]);

                    lString = new String(lChar5);
                }
            }
        }

        return lString;
    }

    /**
     *  checkForZip --> uppercases the state code where appropriate
     */
    private String checkForZip(String aString) {
        String lString = aString;
        String lStateSubString = null;
        int lLength = lString.length();
        int lStateIndex = 0;
        boolean lHasStateCode = true;
        boolean lState = false;

        if (lLength >= 5) {
            for (int i = 0; i < lLength - 4; i++) {
                //various 5 char strings to test for zip codes
                String lZipSubString = lString.substring(i, (i + 5));

                char[] lZipSubArray = lZipSubString.toCharArray();

                boolean lZip = true;

                for (int j = 0; j < 5; j++) {
                    if (!Character.isDigit(lZipSubArray[j])) {
                        lZip = false;
                    }
                }

                if (lZip) {
                    if (i > 0) {
                        String lPreZipSubString = lString.substring(0, i);

                        if (lPreZipSubString.length() >= 2) {
                            for (int k = lPreZipSubString.length() - 2; k >= 0; k--) {
                                //various 2 char strings to test for state codes
                                lStateSubString = lPreZipSubString.substring(k, (k + 2));

                                char[] lStateSubArray = lStateSubString.toCharArray();

                                lState = true;

                                for (int m = 0; m < 2; m++) {
                                    if (!Character.isLetter(lStateSubArray[m])) {
                                        lState = false;
                                    }
                                }

                                if (lState) {
                                    //we have found a state code!
                                    lStateIndex = k;
                                    break;
                                }
                            }
                        }

                        if (lState) {
                            if (lStateSubString.equalsIgnoreCase("AL") || lStateSubString.equalsIgnoreCase("AK")
                                    || lStateSubString.equalsIgnoreCase("AZ")
                                    || lStateSubString.equalsIgnoreCase("AR")
                                    || lStateSubString.equalsIgnoreCase("CA")
                                    || lStateSubString.equalsIgnoreCase("CO")
                                    || lStateSubString.equalsIgnoreCase("CT")
                                    || lStateSubString.equalsIgnoreCase("DE")
                                    || lStateSubString.equalsIgnoreCase("DC")
                                    || lStateSubString.equalsIgnoreCase("FL")
                                    || lStateSubString.equalsIgnoreCase("GA")
                                    || lStateSubString.equalsIgnoreCase("HI")
                                    || lStateSubString.equalsIgnoreCase("ID")
                                    || lStateSubString.equalsIgnoreCase("IL")
                                    || lStateSubString.equalsIgnoreCase("IN")
                                    || lStateSubString.equalsIgnoreCase("IA")
                                    || lStateSubString.equalsIgnoreCase("KS")
                                    || lStateSubString.equalsIgnoreCase("KY")
                                    || lStateSubString.equalsIgnoreCase("LA")
                                    || lStateSubString.equalsIgnoreCase("ME")
                                    || lStateSubString.equalsIgnoreCase("MD")
                                    || lStateSubString.equalsIgnoreCase("MA")
                                    || lStateSubString.equalsIgnoreCase("MI")
                                    || lStateSubString.equalsIgnoreCase("MN")
                                    || lStateSubString.equalsIgnoreCase("MS")
                                    || lStateSubString.equalsIgnoreCase("MO")
                                    || lStateSubString.equalsIgnoreCase("MT")
                                    || lStateSubString.equalsIgnoreCase("NE")
                                    || lStateSubString.equalsIgnoreCase("NV")
                                    || lStateSubString.equalsIgnoreCase("NH")
                                    || lStateSubString.equalsIgnoreCase("NJ")
                                    || lStateSubString.equalsIgnoreCase("NM")
                                    || lStateSubString.equalsIgnoreCase("NY")
                                    || lStateSubString.equalsIgnoreCase("NC")
                                    || lStateSubString.equalsIgnoreCase("ND")
                                    || lStateSubString.equalsIgnoreCase("OH")
                                    || lStateSubString.equalsIgnoreCase("OK")
                                    || lStateSubString.equalsIgnoreCase("OR")
                                    || lStateSubString.equalsIgnoreCase("PA")
                                    || lStateSubString.equalsIgnoreCase("PR")
                                    || lStateSubString.equalsIgnoreCase("RI")
                                    || lStateSubString.equalsIgnoreCase("SC")
                                    || lStateSubString.equalsIgnoreCase("SD")
                                    || lStateSubString.equalsIgnoreCase("TN")
                                    || lStateSubString.equalsIgnoreCase("TX")
                                    || lStateSubString.equalsIgnoreCase("UT")
                                    || lStateSubString.equalsIgnoreCase("VT")
                                    || lStateSubString.equalsIgnoreCase("VA")
                                    || lStateSubString.equalsIgnoreCase("WA")
                                    || lStateSubString.equalsIgnoreCase("WV")
                                    || lStateSubString.equalsIgnoreCase("WI")
                                    || lStateSubString.equalsIgnoreCase("WY")) {
                                char[] lCharArray = lString.toCharArray();

                                lCharArray[lStateIndex] = Character.toUpperCase(lCharArray[lStateIndex]);
                                lCharArray[lStateIndex + 1] = Character.toUpperCase(lCharArray[lStateIndex + 1]);

                                lString = new String(lCharArray);
                            } // if (lStateSubString.equalsIgnoreCase("AL") || ...
                        } // if(lState)
                    } // if(i > 0)
                } // if (lZip)
            } // for (int i=0; i < lLength - 4; i++)
        } // if(lLength >= 5)

        return lString;
    }

    //A?O1.1 Changes end here

    /* RL003918 May 19 2008
      To Create the filename based on the user id to create Individual
      Autoextract Pending Log
    */

    public static String getAgentLogfileName(String autoExtractLog, String agentId) {
        //This method is added for Multithreading Batch Downloads
        String logFileName = "";

        if (autoExtractLog != null) {
            int endIndex = autoExtractLog.lastIndexOf(".txt");
            logFileName = autoExtractLog.substring(0, endIndex);
            logFileName = logFileName + "_" + agentId + ".txt";
        }
        return logFileName;
    }

    /* RL003918 May 19 2008
      To Append all the files generated for each user to Autoextract Pending Log
    */
    public static void appendFile(String source, String filetoappend) {

        try {
            boolean append = true;
            File filesrc = new File(source);
            BufferedWriter output = new BufferedWriter(new FileWriter(filesrc, append));

            File ftoappend = new File(filetoappend);
            BufferedReader br = new BufferedReader(new FileReader(ftoappend));
            String line = br.readLine();

            while (line != null) {
                output.write(line);
                output.newLine();
                line = br.readLine();
            }
            output.close();
            br.close();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    public static String[] extractFromSecurityToken(String input) {
        if (input == null) {
            return new String[] { null, null, null, null };
        }
        String[] extractedValues = new String[4];
        String ekey = extractValue(input, "ACTASEKEY=");
        String actAsLogin = extractValue(input, "ACTASLOGIN=");
        String sfdcPolicyNumber = extractValue(input, "policyNumber=");
        String status = extractValue(input, "status="); // Connector Pro Dashboard
        extractedValues[0] = actAsLogin;
        extractedValues[1] = ekey;
        extractedValues[2] = sfdcPolicyNumber;
        extractedValues[3] = status; // Connector Pro Dashboard
        return extractedValues;
    }

    private static String extractValue(String input, String key) {
        int beginIndex = input.indexOf(key);
        if (beginIndex < 0) {
            return null;
        }

        int endIndex = input.indexOf(";", beginIndex);
        if (endIndex > 0) {
            return input.substring(beginIndex + key.length(), endIndex);
        } else {
            return input.substring(beginIndex + key.length());
        }
    }

    //   GDB2NY 10/31/08 SXS
    public static String getSubcompanyFromPolNo(String policyNo) {
        String s = "";
        if (policyNo != null)
            s = FormatUtil.mid(policyNo, 7, 2);
        return s;
    }
    //   GDB2NY 10/31/08 SXS

    //RL-009109 - SXL - START
    public static String formatAgentName(String agentName) {

        int commaCount = StringUtils.countMatches(agentName, ",");
        String formattedAgentName = null;
        if (commaCount == 2) {
            String lastName = agentName.substring(0, agentName.indexOf(",")).trim();
            String firstName = (agentName.substring(agentName.indexOf(",") + 1, agentName.lastIndexOf(","))).trim();
            String middleName = (agentName.substring(agentName.lastIndexOf(",") + 1)).trim();

            formattedAgentName = lastName;
            if (firstName.length() > 0) {
                if (formattedAgentName.length() > 0) {
                    formattedAgentName = formattedAgentName + ", " + firstName;
                } else {
                    formattedAgentName = firstName;
                }
            }

            if (middleName.length() > 0) {
                if (formattedAgentName.length() == 0) {
                    formattedAgentName = middleName;
                } else if (firstName.length() > 0) {
                    formattedAgentName = formattedAgentName + " " + middleName;
                } else {
                    formattedAgentName = formattedAgentName + ", " + middleName;
                }
            }
            return formattedAgentName;

        } else {
            return agentName;
        }

    }
    //RL-009109 - SXL - END

    //RL-011284 - RXK - START
    public static String formatRequirementName(String reqmtName) {
        if (reqmtName.contains("'")) {
            //reqmtName = reqmtName.replaceAll("'", "&#39;");
            reqmtName = reqmtName.replaceAll("'", "\\\\'");
        }
        return reqmtName;

    }
    //RL-011284 - RXK - END

    public static String replaceSpecialChars(String input) {
        if (input == null) {
            return input;
        }
        StringBuilder filteredText = new StringBuilder();
        for (int i = 0; i < input.length(); i++) {
            char nextChar = input.charAt(i);
            if (nextChar > 31 && nextChar < 127) {
                filteredText.append(nextChar);
            } else if (nextChar == '\r' || nextChar == '\n') {
                filteredText.append(nextChar);
            } else {
                filteredText.append(" ");
            }
        }
        return filteredText.toString();
    }

    public static List<String> splitLine(String input, int splitPosition) {
        List<String> lines = new ArrayList<String>();

        StringBuilder stringBuilder = new StringBuilder(input.trim());
        boolean linesRemaining = true;
        while (linesRemaining) {
            if (stringBuilder.length() <= splitPosition || stringBuilder.lastIndexOf(" ") < splitPosition) {
                lines.add(stringBuilder.toString());
                linesRemaining = false;
            } else {
                int indexOfSpace = stringBuilder.indexOf(" ", splitPosition);
                String nextLine = stringBuilder.substring(0, indexOfSpace + 1);
                lines.add(nextLine);
                stringBuilder.delete(0, indexOfSpace + 1);
            }
        }

        return lines;
    }
}