001    package net.sourceforge.mp3dings.filter;
002    
003    /* Mp3dings - manage mp3 meta-information
004     * Copyright (C) 2003 Moritz Ringler
005     * $Id: AbstractStringFilter.java 13 2007-07-27 09:52:39Z ringler $
006     *
007     * This program is free software; you can redistribute it and/or
008     * modify it under the terms of the GNU General Public License
009     * as published by the Free Software Foundation; either version 2
010     * of the License, or (at your option) any later version.
011     *
012     * This program is distributed in the hope that it will be useful,
013     * but WITHOUT ANY WARRANTY; without even the implied warranty of
014     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
015     * GNU General Public License for more details.
016     *
017     * You should have received a copy of the GNU General Public License
018     * along with this program; if not, write to the Free Software
019     * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
020     */
021    
022    import de.mospace.lang.FullTextString;
023    
024    /** A filter that affects only strings and returns other objects unchanged.
025     * FullTextStrings are converted to strings and backwards
026     * as needed.
027     * @author Moritz Ringler
028     * @version $Revision: 13 $ ($Date: 2007-07-27 11:52:39 +0200 (Fr, 27 Jul 2007) $)
029     */
030    public abstract class AbstractStringFilter extends FilterBean{
031            static final long serialVersionUID = -8169225812665596453L;
032    
033            /** Handles non-String objects and calls filterString() for Strings.
034             */
035            public Object filter(Object val){
036                Object o = myFilter(val);
037                assert (val == null || o == null || val.getClass().isInstance(o)): "" + val + ";"+o;
038                return o;
039            }
040    
041            private Object myFilter(Object val){
042                /* don't change null */
043                if (val == null){
044                    return val;
045                }
046    
047                boolean isFullTextString = val instanceof FullTextString;
048                boolean isString = val instanceof String;
049                /* don't change if not a String or FullTextString */
050                if (! (isString || isFullTextString)){
051                    return val;
052                }
053                /* turn into String */
054                String str = val.toString();
055                /* do String filtering */
056                str = filterString(str);
057                /* turn back into FullTextString if necessary */
058                if(isFullTextString && str != null){
059                    return new FullTextString(str);
060                }
061                return str;
062            }
063    
064            abstract public String filterString(String str);
065    }