//# 0 errors, 65 messages
//#
/*
    //#SanitisedFilenameFilter.java:1:1: class: com.dmdirc.addons.ui_swing.components.SanitisedFilenameFilter
    //#SanitisedFilenameFilter.java:1:1: method: com.dmdirc.addons.ui_swing.components.SanitisedFilenameFilter.com.dmdirc.addons.ui_swing.components.SanitisedFilenameFilter__static_init
 * Copyright (c) 2006-2009 Chris Smith, Shane Mc Cormack, Gregory Holmes
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package com.dmdirc.addons.ui_swing.components;

import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.DocumentFilter;

/**
 * Filters input to create valid filenames.
 */
public class SanitisedFilenameFilter extends DocumentFilter {
    
    /** Invalid filename characters. */
    private static final String INVALID_CHARS = "^[^\\w\\.\\s\\-\\#\\&\\_]";
    
    /** Creates a new instance of SanitisedFilenameFilter. */
    public SanitisedFilenameFilter() {
        super();
    //#SanitisedFilenameFilter.java:39: method: void com.dmdirc.addons.ui_swing.components.SanitisedFilenameFilter.com.dmdirc.addons.ui_swing.components.SanitisedFilenameFilter()
    //#input(void com.dmdirc.addons.ui_swing.components.SanitisedFilenameFilter()): this
    }
    //#SanitisedFilenameFilter.java:40: end of method: void com.dmdirc.addons.ui_swing.components.SanitisedFilenameFilter.com.dmdirc.addons.ui_swing.components.SanitisedFilenameFilter()
    
    /** {@inheritDoc} */
    @Override
    public void insertString(final DocumentFilter.FilterBypass fb,
            final int offset, final String string, final AttributeSet attr)
            throws BadLocationException {
        
        if (string == null || string.isEmpty()) {
    //#SanitisedFilenameFilter.java:48: method: void com.dmdirc.addons.ui_swing.components.SanitisedFilenameFilter.insertString(DocumentFilter$FilterBypass, int, String, AttributeSet)
    //#input(void insertString(DocumentFilter$FilterBypass, int, String, AttributeSet)): __Descendant_Table[com/dmdirc/addons/ui_swing/components/SanitisedFilenameFilter]
    //#input(void insertString(DocumentFilter$FilterBypass, int, String, AttributeSet)): __Descendant_Table[others]
    //#input(void insertString(DocumentFilter$FilterBypass, int, String, AttributeSet)): __Dispatch_Table.replace(Ljavax/swing/text/DocumentFilter$FilterBypass;IILjava/lang/String;Ljavax/swing/text/AttributeSet;)V
    //#input(void insertString(DocumentFilter$FilterBypass, int, String, AttributeSet)): attr
    //#input(void insertString(DocumentFilter$FilterBypass, int, String, AttributeSet)): fb
    //#input(void insertString(DocumentFilter$FilterBypass, int, String, AttributeSet)): offset
    //#input(void insertString(DocumentFilter$FilterBypass, int, String, AttributeSet)): string
    //#input(void insertString(DocumentFilter$FilterBypass, int, String, AttributeSet)): this
    //#input(void insertString(DocumentFilter$FilterBypass, int, String, AttributeSet)): this.__Tag
    //#pre[2] (void insertString(DocumentFilter$FilterBypass, int, String, AttributeSet)): (soft) fb != null
    //#pre[6] (void insertString(DocumentFilter$FilterBypass, int, String, AttributeSet)): (soft) this.__Tag == com/dmdirc/addons/ui_swing/components/SanitisedFilenameFilter
    //#unanalyzed(void insertString(DocumentFilter$FilterBypass, int, String, AttributeSet)): Effects-of-calling:java.lang.String:replaceAll
    //#unanalyzed(void insertString(DocumentFilter$FilterBypass, int, String, AttributeSet)): Effects-of-calling:javax.swing.text.DocumentFilter$FilterBypass:replace
    //#test_vector(void insertString(DocumentFilter$FilterBypass, int, String, AttributeSet)): string: Addr_Set{null}, Inverse{null}
    //#test_vector(void insertString(DocumentFilter$FilterBypass, int, String, AttributeSet)): java.lang.String:isEmpty(...)@48: {0}, {1}
            return;
        } else {
            replace(fb, offset, string.length(), string, attr);
        }
    }
    //#SanitisedFilenameFilter.java:53: end of method: void com.dmdirc.addons.ui_swing.components.SanitisedFilenameFilter.insertString(DocumentFilter$FilterBypass, int, String, AttributeSet)
    
    /** {@inheritDoc} */
    @Override
    public void remove(final DocumentFilter.FilterBypass fb, final int offset,
            final int length) throws BadLocationException {
        
        replace(fb, offset, length, "", null);
    //#SanitisedFilenameFilter.java:60: method: void com.dmdirc.addons.ui_swing.components.SanitisedFilenameFilter.remove(DocumentFilter$FilterBypass, int, int)
    //#input(void remove(DocumentFilter$FilterBypass, int, int)): __Descendant_Table[com/dmdirc/addons/ui_swing/components/SanitisedFilenameFilter]
    //#input(void remove(DocumentFilter$FilterBypass, int, int)): __Descendant_Table[others]
    //#input(void remove(DocumentFilter$FilterBypass, int, int)): __Dispatch_Table.replace(Ljavax/swing/text/DocumentFilter$FilterBypass;IILjava/lang/String;Ljavax/swing/text/AttributeSet;)V
    //#input(void remove(DocumentFilter$FilterBypass, int, int)): fb
    //#input(void remove(DocumentFilter$FilterBypass, int, int)): length
    //#input(void remove(DocumentFilter$FilterBypass, int, int)): offset
    //#input(void remove(DocumentFilter$FilterBypass, int, int)): this
    //#input(void remove(DocumentFilter$FilterBypass, int, int)): this.__Tag
    //#pre[1] (void remove(DocumentFilter$FilterBypass, int, int)): fb != null
    //#pre[5] (void remove(DocumentFilter$FilterBypass, int, int)): this.__Tag == com/dmdirc/addons/ui_swing/components/SanitisedFilenameFilter
    //#unanalyzed(void remove(DocumentFilter$FilterBypass, int, int)): Effects-of-calling:java.lang.String:replaceAll
    //#unanalyzed(void remove(DocumentFilter$FilterBypass, int, int)): Effects-of-calling:javax.swing.text.DocumentFilter$FilterBypass:replace
    }
    //#SanitisedFilenameFilter.java:61: end of method: void com.dmdirc.addons.ui_swing.components.SanitisedFilenameFilter.remove(DocumentFilter$FilterBypass, int, int)
    
    /** {@inheritDoc} */
    @Override
    public void replace(final DocumentFilter.FilterBypass fb, final int offset,
            final int length, final String text, final AttributeSet attrs)
            throws BadLocationException {
        
        fb.replace(offset, length, sanitise(text), attrs);
    //#SanitisedFilenameFilter.java:69: method: void com.dmdirc.addons.ui_swing.components.SanitisedFilenameFilter.replace(DocumentFilter$FilterBypass, int, int, String, AttributeSet)
    //#input(void replace(DocumentFilter$FilterBypass, int, int, String, AttributeSet)): attrs
    //#input(void replace(DocumentFilter$FilterBypass, int, int, String, AttributeSet)): fb
    //#input(void replace(DocumentFilter$FilterBypass, int, int, String, AttributeSet)): length
    //#input(void replace(DocumentFilter$FilterBypass, int, int, String, AttributeSet)): offset
    //#input(void replace(DocumentFilter$FilterBypass, int, int, String, AttributeSet)): text
    //#input(void replace(DocumentFilter$FilterBypass, int, int, String, AttributeSet)): this
    //#pre[2] (void replace(DocumentFilter$FilterBypass, int, int, String, AttributeSet)): fb != null
    //#pre[5] (void replace(DocumentFilter$FilterBypass, int, int, String, AttributeSet)): text != null
    //#unanalyzed(void replace(DocumentFilter$FilterBypass, int, int, String, AttributeSet)): Effects-of-calling:java.lang.String:replaceAll
    }
    //#SanitisedFilenameFilter.java:70: end of method: void com.dmdirc.addons.ui_swing.components.SanitisedFilenameFilter.replace(DocumentFilter$FilterBypass, int, int, String, AttributeSet)
    
    /** Sanitises the proposed value. */
    private String sanitise(final String proposedValue) {
        return proposedValue.replaceAll(INVALID_CHARS, "");
    //#SanitisedFilenameFilter.java:74: method: String com.dmdirc.addons.ui_swing.components.SanitisedFilenameFilter.sanitise(String)
    //#input(String sanitise(String)): proposedValue
    //#output(String sanitise(String)): return_value
    //#pre[1] (String sanitise(String)): proposedValue != null
    //#post(String sanitise(String)): return_value != null
    //#SanitisedFilenameFilter.java:74: end of method: String com.dmdirc.addons.ui_swing.components.SanitisedFilenameFilter.sanitise(String)
    }
    
}
    //#output(com.dmdirc.addons.ui_swing.components.SanitisedFilenameFilter__static_init): __Descendant_Table[com/dmdirc/addons/ui_swing/components/SanitisedFilenameFilter]
    //#output(com.dmdirc.addons.ui_swing.components.SanitisedFilenameFilter__static_init): __Dispatch_Table.insertString(Ljavax/swing/text/DocumentFilter$FilterBypass;ILjava/lang/String;Ljavax/swing/text/AttributeSet;)V
    //#output(com.dmdirc.addons.ui_swing.components.SanitisedFilenameFilter__static_init): __Dispatch_Table.remove(Ljavax/swing/text/DocumentFilter$FilterBypass;II)V
    //#output(com.dmdirc.addons.ui_swing.components.SanitisedFilenameFilter__static_init): __Dispatch_Table.replace(Ljavax/swing/text/DocumentFilter$FilterBypass;IILjava/lang/String;Ljavax/swing/text/AttributeSet;)V
    //#output(com.dmdirc.addons.ui_swing.components.SanitisedFilenameFilter__static_init): __Dispatch_Table.sanitise(Ljava/lang/String;)Ljava/lang/String;
    //#post(com.dmdirc.addons.ui_swing.components.SanitisedFilenameFilter__static_init): __Descendant_Table[com/dmdirc/addons/ui_swing/components/SanitisedFilenameFilter] == &__Dispatch_Table
    //#post(com.dmdirc.addons.ui_swing.components.SanitisedFilenameFilter__static_init): __Dispatch_Table.insertString(Ljavax/swing/text/DocumentFilter$FilterBypass;ILjava/lang/String;Ljavax/swing/text/AttributeSet;)V == &insertString
    //#post(com.dmdirc.addons.ui_swing.components.SanitisedFilenameFilter__static_init): __Dispatch_Table.remove(Ljavax/swing/text/DocumentFilter$FilterBypass;II)V == &remove
    //#post(com.dmdirc.addons.ui_swing.components.SanitisedFilenameFilter__static_init): __Dispatch_Table.replace(Ljavax/swing/text/DocumentFilter$FilterBypass;IILjava/lang/String;Ljavax/swing/text/AttributeSet;)V == &replace
    //#post(com.dmdirc.addons.ui_swing.components.SanitisedFilenameFilter__static_init): __Dispatch_Table.sanitise(Ljava/lang/String;)Ljava/lang/String; == &sanitise
    //#SanitisedFilenameFilter.java:: end of method: com.dmdirc.addons.ui_swing.components.SanitisedFilenameFilter.com.dmdirc.addons.ui_swing.components.SanitisedFilenameFilter__static_init
    //#SanitisedFilenameFilter.java:: end of class: com.dmdirc.addons.ui_swing.components.SanitisedFilenameFilter
