/*
 * @(#)CaseActionFilter.java    1.0  9 February 2001
 *
 * Copyright 2004
 * College of Computer and Information Science
 * Northeastern University
 * Boston, MA  02115
 *
 * The Java Power Tools software may be used for educational
 * purposes as long as this copyright notice is retained intact
 * at the top of all source files.
 *
 * To discuss possible commercial use of this software, 
 * contact Richard Rasala at Northeastern University, 
 * College of Computer and Information Science,
 * 617-373-2462 or rasala@ccs.neu.edu.
 *
 * The Java Power Tools software has been designed and built
 * in collaboration with Viera Proulx and Jeff Raab.
 *
 * Should this software be modified, the words "Modified from 
 * Original" must be included as a comment below this notice.
 *
 * All publication rights are retained.  This software or its 
 * documentation may not be published in any media either
 * in whole or in part without explicit permission.
 *
 * This software was created with support from Northeastern 
 * University and from NSF grant DUE-9950829.
 */

package edu.neu.ccs.filter;

import edu.neu.ccs.*;
import java.io.Serializable;
import java.text.ParseException;

/**
 * <P>Filter used for modification of 
 * <CODE>XString</CODE> objects, in order to change case,
 * restrict particular character codes, &c.</P>
 *
 * @author  Jeff Raab
 * @version 2.2
 * @since   1.0
 */
public class CaseActionFilter 
    implements StringableFilter, Cloneable, Serializable 
{

    /** 
     * Case action filter that transforms 
     * <CODE>XString</CODE> data
     * to all upper case characters. 
     */
    public static final CaseActionFilter UPPER_CASE =
        new CaseActionFilter() {
            public Stringable filterStringable(Stringable obj)
                throws FilterException
            {
                // superclass throws exception if not an XString
                super.filterStringable(obj);
                
                // change to upper case
                XString x = (XString)obj;
                String input = x.getValue();
                x.setValue(input.toUpperCase());
                return x;
            }
        };

    /** 
     * Case action filter that transforms 
     * <CODE>XString</CODE> data
     * to all lower case characters. 
     */
    public static final CaseActionFilter LOWER_CASE =
        new CaseActionFilter() {
            public Stringable filterStringable(Stringable obj)
                throws FilterException
            {
                // superclass throws exception if not an XString
                super.filterStringable(obj);
                
                // change to upper case
                XString x = (XString)obj;
                String input = x.getValue();
                x.setValue(input.toLowerCase());
                return x;
            }
        };

    /** 
     * Case action filter that transforms 
     * <CODE>XString</CODE> data
     * to all title case characters. 
     *
     * This case action changes all words to title case;
     * words like "the", "a", and "and" are changed as well.
     */
    public static final CaseActionFilter TITLE_CASE =
        new CaseActionFilter() {
            public Stringable filterStringable(Stringable obj)
                throws FilterException
            {
                // superclass throws exception if not an XString
                super.filterStringable(obj);
                
                // change to title case
                XString x = (XString)obj;
                StringBuffer buffer = new StringBuffer(x.getValue());
                boolean capitalize = true;
                char c;

                // for each character in the input
                for (int i = 0; i < buffer.length(); i++) {
                    c = buffer.charAt(i);
                    
                    // capitalize first character of word
                    if (Character.isLetter(c)) {
                        buffer.setCharAt(
                            i, 
                            capitalize ?
                                Character.toUpperCase(c) :
                                Character.toLowerCase(c));
                        capitalize = false;
                    }

                    // reset capitalization when word ends
                    else if (Character.isWhitespace(c))
                        capitalize = true;
                }
                
                // return the filtered string
                x.setValue(buffer.toString());
                return x;
            }
        };

    //////////////////////
    // StringableFilter //
    //////////////////////
    
    /**
     * Filters the given <CODE>XString</CODE> object
     * based on this case action scheme.
     *
     * @param obj the object to be filtered
     * @throws FilterException if the object is not
     *      an <CODE>XString</CODE> object
     */
    public Stringable filterStringable(Stringable obj) 
        throws FilterException 
    {
        if (obj instanceof XString)
            return obj;
        
        throw new FilterException(
            obj, "Not a string: " + obj.toString());
    }
}
