File Source: SanitisedFilenameFilter.java

         /* 
    P/P   *  Method: com.dmdirc.addons.ui_swing.components.SanitisedFilenameFilter__static_init
          */
     1  /*
     2   * Copyright (c) 2006-2009 Chris Smith, Shane Mc Cormack, Gregory Holmes
     3   *
     4   * Permission is hereby granted, free of charge, to any person obtaining a copy
     5   * of this software and associated documentation files (the "Software"), to deal
     6   * in the Software without restriction, including without limitation the rights
     7   * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     8   * copies of the Software, and to permit persons to whom the Software is
     9   * furnished to do so, subject to the following conditions:
    10   *
    11   * The above copyright notice and this permission notice shall be included in
    12   * all copies or substantial portions of the Software.
    13   *
    14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
    20   * SOFTWARE.
    21   */
    22  
    23  package com.dmdirc.addons.ui_swing.components;
    24  
    25  import javax.swing.text.AttributeSet;
    26  import javax.swing.text.BadLocationException;
    27  import javax.swing.text.DocumentFilter;
    28  
    29  /**
    30   * Filters input to create valid filenames.
    31   */
    32  public class SanitisedFilenameFilter extends DocumentFilter {
    33      
    34      /** Invalid filename characters. */
    35      private static final String INVALID_CHARS = "^[^\\w\\.\\s\\-\\#\\&\\_]";
    36      
    37      /** Creates a new instance of SanitisedFilenameFilter. */
    38      public SanitisedFilenameFilter() {
                 /* 
    P/P           *  Method: void com.dmdirc.addons.ui_swing.components.SanitisedFilenameFilter()
                  */
    39          super();
    40      }
    41      
    42      /** {@inheritDoc} */
    43      @Override
    44      public void insertString(final DocumentFilter.FilterBypass fb,
    45              final int offset, final String string, final AttributeSet attr)
    46              throws BadLocationException {
    47          
                 /* 
    P/P           *  Method: void insertString(DocumentFilter$FilterBypass, int, String, AttributeSet)
                  * 
                  *  Preconditions:
                  *    (soft) fb != null
                  * 
                  *  Test Vectors:
                  *    string: Addr_Set{null}, Inverse{null}
                  *    java.lang.String:isEmpty(...)@48: {0}, {1}
                  */
    48          if (string == null || string.isEmpty()) {
    49              return;
    50          } else {
    51              replace(fb, offset, string.length(), string, attr);
    52          }
    53      }
    54      
    55      /** {@inheritDoc} */
    56      @Override
    57      public void remove(final DocumentFilter.FilterBypass fb, final int offset,
    58              final int length) throws BadLocationException {
    59          
                 /* 
    P/P           *  Method: void remove(DocumentFilter$FilterBypass, int, int)
                  * 
                  *  Preconditions:
                  *    fb != null
                  */
    60          replace(fb, offset, length, "", null);
    61      }
    62      
    63      /** {@inheritDoc} */
    64      @Override
    65      public void replace(final DocumentFilter.FilterBypass fb, final int offset,
    66              final int length, final String text, final AttributeSet attrs)
    67              throws BadLocationException {
    68          
                 /* 
    P/P           *  Method: void replace(DocumentFilter$FilterBypass, int, int, String, AttributeSet)
                  * 
                  *  Preconditions:
                  *    fb != null
                  *    text != null
                  */
    69          fb.replace(offset, length, sanitise(text), attrs);
    70      }
    71      
    72      /** Sanitises the proposed value. */
    73      private String sanitise(final String proposedValue) {
                 /* 
    P/P           *  Method: String sanitise(String)
                  * 
                  *  Preconditions:
                  *    proposedValue != null
                  * 
                  *  Postconditions:
                  *    return_value != null
                  */
    74          return proposedValue.replaceAll(INVALID_CHARS, "");
    75      }
    76      
    77  }








SofCheck Inspector Build Version : 2.17854
SanitisedFilenameFilter.java 2009-Jun-25 01:54:24
SanitisedFilenameFilter.class 2009-Sep-02 17:04:15