john pfeiffer
  • Home
  • Categories
  • Tags
  • Archives

JohnStringUtils

// 2012-10-08 johnpfeiffer
// TODO: ? LevensteinDistance()

package net.kittyandbear.util;

import java.text.CharacterIterator;
import java.text.StringCharacterIterator;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

public class JohnStringUtils
{
    public static final String CLASSVERSION = "0.35";

    /**
     * Returns a string that has web safe replacements for special characters.
     *
     * @param original
     *          string
     * @return html safe string
     * @see https://www.owasp.org/index.php/XSS_(Cross_Site_Scripting)_Prevention_Cheat_Sheet
     */
    // <script>javascript:alert(document.cookie);</script>
    // <form id="injectedform" action="http://kittyandbear.net/john/web" method="post" ><div><input type=\"submit\"/></div></form>

    public static String safeHTML( final String source ) throws IllegalArgumentException
    {
        if( source == null )
        {
            throw new IllegalArgumentException( "ERROR: safeHTML source String cannot be null" );
        }
        final StringBuilder result = new StringBuilder();
        final StringCharacterIterator iterator = new StringCharacterIterator( source );
        char character = iterator.current();
        while( character != CharacterIterator.DONE )
        {
            switch( character )
            {
                case '&':
                    result.append( "&amp;" );
                    break;
                case '<':
                    result.append( "&lt;" );
                    break;
                case '>':
                    result.append( "&gt;" );
                    break;
                case '\"':
                    result.append( "&quot;" );
                    break;
                case '\'':
                    result.append( "&#039;" );
                    break;
                case '/':
                    result.append( "&#047;" );
                    break;
                default:
                    result.append( character );
                    break;
            }
            character = iterator.next();
        }
        return result.toString();
    }

    public static boolean containsWhiteSpace( final String source ) throws IllegalArgumentException
    {
        if( source == null )
        {
            throw new IllegalArgumentException( "ERROR: containsWhiteSpace source String cannot be null" );
        }
        boolean result = false;
        for( int i = 0 ; i < source.length() ; i++ )
        {
            if( Character.isWhitespace( source.charAt( i ) ) )
            {
                result = true;
                break;
            }
        }
        return result;
    }

    public static boolean isDigits( final String source ) throws IllegalArgumentException
    {
        if( source == null )
        {
            throw new IllegalArgumentException( "ERROR: isDigits source String cannot be null" );
        }

        if( source.isEmpty() )
        {
            throw new IllegalArgumentException( "ERROR: isDigits source String cannot be empty" );
        }

        boolean result = true;
        for( int i = 0 ; i < source.length() ; i++ )
        {
            if( !Character.isDigit( source.charAt( i ) ) )
            {
                result = false;
                break;
            }
        }
        return result;
    }

    public static boolean containsIllegalCharacter( final String source , final HashMap <Character , Character> illegalCharacterMap )
            throws IllegalArgumentException
    {
        if( source == null )
        {
            throw new IllegalArgumentException( "ERROR: containsIllegalCharacter source String cannot be null" );
        }
        if( illegalCharacterMap == null )
        {
            throw new IllegalArgumentException( "ERROR: containsIllegalCharacter HashMap parameter cannot be null" );
        }
        boolean result = false;
        for( int i = 0 ; i < source.length() ; i++ )
        {
            if( illegalCharacterMap.containsKey( source.charAt( i ) ) )
            {
                result = true;
            }
        }
        return result;
    }

    // An alternative to the synchronized return new StringBuffer(s).reverse().toString();
    public static String reverse( final String source ) throws IllegalArgumentException
    {
        if( source == null )
        {
            throw new IllegalArgumentException( "ERROR: reverse source String cannot be null" );
        }
        StringBuilder reversed = new StringBuilder();
        for( int i = source.length() - 1 ; i >= 0 ; --i )
        {
            reversed.append( source.charAt( i ) );
        }
        return reversed.toString();
    }

    public static String reverseSpellings( final String source , final String delimiter ) throws IllegalArgumentException
    {
        if( source == null )
        {
            throw new IllegalArgumentException( "ERROR: reverseSpellings String parameter cannot be null" );
        }
        StringBuilder result = new StringBuilder();
        ArrayList <String> tokenList = sourceToDelimitedList( source , " " );
        Iterator <String> it = tokenList.iterator();
        if( it.hasNext() )
        {
            result.append( reverse( it.next() ) );
        }
        while( it.hasNext() )
        {
            result.append( delimiter + reverse( it.next() ) );
        }
        return result.toString();
    }

    public static String replaceTargetWithOneChar( final String source , final String target , final char replacement ) throws IllegalArgumentException
    {
        if( source == null )
        {
            throw new IllegalArgumentException( "ERROR: replaceTargetWithOneChar source String cannot be null" );
        }
        if( target == null )
        {
            throw new IllegalArgumentException( "ERROR: replaceTargetWithOneChar target String cannot be null" );
        }
        if( target.isEmpty() )
        {
            throw new IllegalArgumentException( "ERROR: replaceTargetWithOneChar target String cannot be empty" );
        }
        StringBuilder result = new StringBuilder();
        int current = 0;
        int found = source.indexOf( target , current );
        String token;
        while( found != -1 )
        {
            token = source.substring( current , found );
            result.append( token + replacement );
            current = found + target.length();
            found = source.indexOf( target , current );
        }
        token = source.substring( current , source.length() );
        result.append( token );
        return result.toString();
    }

    public static ArrayList <String> sourceToDelimitedList( final String source , final String delimiter ) throws IllegalArgumentException
    {
        if( source == null )
        {
            throw new IllegalArgumentException( "ERROR: delimitedList source String cannot be null" );
        }
        if( delimiter == null )
        {
            throw new IllegalArgumentException( "ERROR: delimitedList delimiter String cannot be null" );
        }
        if( delimiter.isEmpty() )
        {
            throw new IllegalArgumentException( "ERROR: delimitedList delimiter String cannot be empty" );
        }

        if( delimiter.length() > source.length() )
        {
            throw new IllegalArgumentException( "ERROR: delimiter is longer than source" );
        }

        ArrayList <String> tokenList = new ArrayList <String>();
        if( source.contains( delimiter ) )
        {

            int current = 0;
            int found = source.indexOf( delimiter , current );
            String token;
            while( found != -1 )
            {
                token = source.substring( current , found );
                tokenList.add( token );
                current = found + delimiter.length();
                found = source.indexOf( delimiter , current );
            }
            token = source.substring( current , source.length() );
            tokenList.add( token );
        }
        return tokenList;
    }

    // Returns Empty if not found
    public static String getFirstStringContainsKeywordFromList( final ArrayList <String> source , final String keyword , final String skipStartsWith )
    {
        if( source == null )
        {
            throw new IllegalArgumentException( "ERROR: source cannot be null" );
        }
        if( keyword == null )
        {
            throw new IllegalArgumentException( "ERROR: keyword cannot be null" );
        }
        String result = "";
        for( String line : source )
        {
            if( line.contains( keyword ) && !line.startsWith( skipStartsWith ) ) // skips comments
            {
                result = line;
                break;
            }
        }
        return result;
    }

    public static String getSubstringAfterDelimiter( final String source , final String delimiter )
    {
        if( source == null )
        {
            throw new IllegalArgumentException( "ERROR: source cannot be null" );
        }
        if( source.isEmpty() )
        {
            throw new IllegalArgumentException( "ERROR: source cannot be empty" );
        }
        if( delimiter == null )
        {
            throw new IllegalArgumentException( "ERROR: delimiter cannot be null" );
        }
        if( delimiter.isEmpty() )
        {
            throw new IllegalArgumentException( "ERROR: delimiter cannot be empty" );
        }
        int found = source.indexOf( delimiter );
        String result = source.substring( found + delimiter.length() );
        return result;
    }

} // end class

  • « BAT watch timed loop command as parameter
  • JohnStringUtilsTest »

Published

Oct 8, 2012

Category

java-classes

~698 words

Tags

  • classes 92
  • java 252
  • johnstringutils 2