All pastes #415842 Raw Edit

censor

public text v1 · immutable
#415842 ·published 2007-03-29 21:42 UTC
rendered paste body
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

/** Text censoring application:

   A text field to hold the undesirable text;
   a text area to hold a the message to be censored;
   and two buttons: "Check" and "Censor now".

   When Check is clicked various checks are carried out: If either the
   undesirable text or the message are blank then this is reported.
   If there are no occurrences of the undesirable text in the message
   then this is reported, and if there is at least one occurrence then 
   a count of the number of occurrences is displayed.

   When Censor now is clicked, each occurrence in the message
   of the undesirable text is replaced by *BLEEP*.

   Warning: Doesn't actually check that it is replacing "words" with "words"!

   SBJ April 2006
*/
public class Censor extends JFrame
                    implements ActionListener {

    /** For entry of the undesirable text. */
    private JTextField undesirable = new JTextField(10);

    /** For entry and display of the text to be censored. */
    private JTextArea text = new JTextArea(10,30);
    
    /** Click to check whether censoring is required. */
    private JButton check = new JButton("Check");
    
    /** Click to censor the text. */
    private JButton censor = new JButton("Censor now");
    
    /** To display various reports. */
    private JTextField report = new JTextField(20);
 
    /** The censoring replacement string. */
    private final String bleep = "*BLEEP*";

    /**
     *  The main program launcher for the Censor class.
     *
     * @param  args  The command line arguments (ignored here).
     */
    public static void main( String[] args ) {

        Censor frame = new Censor();
        frame.setSize( 400, 350 );
        frame.createGUI();
        frame.setVisible( true );

    } // End of main

    /** Sets up the graphical user interface. */
    private void createGUI() {

        setDefaultCloseOperation( EXIT_ON_CLOSE );
        Container window = getContentPane();
        window.setLayout( new FlowLayout() );

        window.add(new JLabel("Enter undesirable text:"));
        window.add(undesirable);
        window.add(new JLabel("Enter message to be censored:"));
        window.add(text);
        window.add(check);
        check.addActionListener(this);
        window.add(censor);
        censor.addActionListener(this);
        window.add(report);
        report.setEditable(false);

    } // End of createGUI

    /** Count the number of occurrences of badText in text and return it as the result. 
     *  Note: This should only be called with badText a non-empty string.
     *
     * @param  badText  Text to search for.
     * @param  text     The text to search within.
     * @return          The number of occurrences of badText in text.
     */
    private int countOccurrences(String badText, String text) {
        
        int badTextLength = badText.length();                          // Compute its length        
        int occurrences = 0;                                           // To count the number of occurrences - none yet
        int place = 0;                                                 // Keeps the current search position, 0 initially
        boolean finished = false;                                      // Records whether the search has finished, initially not
        // Repeatedly scan for badText until none is found
        do {
            place = text.indexOf(badText, place);                      // Search for next occurrence of badText
            finished = place == -1;                                    // The search has finished if there was not another occurrence
            if (!finished)
                occurrences++;                                         // Found an ocurrence, so count it
            place = place + badTextLength;                             // Set next scan to start after this occurrence
        } while (!finished);

        return occurrences;
        
    } // End of countOccurrecnes
    
    /** Depending on the button clicked, check whether censoring is required,
     *  or censor the text.
     *  
     *  @param  event   Details of the event.
     */
    public void actionPerformed(ActionEvent event) {

        if (event.getSource() == check)
            checkMessage();
        
        if (event.getSource() == censor)
            censorMessage();

    } // End of actionPerformed

    /** Check whether the message needs censoring and report the outcome
     *  in the report text field.
     */
    private void checkMessage() {
        
        String badText = undesirable.getText();                        // Get the undesirable text
        String theMessage = text.getText();                            // Get the text to be censored

        // Check whether there is any text for censoring,
        // and if so whether it needs censoring
        if (badText.length() == 0)
            report.setText("No undesirable text entered");
        else if (theMessage.length() == 0)
            report.setText("There is no text to censor");
        else if (theMessage.contains(badText)) {
            int occurrences = countOccurrences(badText, theMessage);
            report.setText("Censoring is required ("+occurrences+" occurrences)");
        }
        else
            report.setText("Censoring is not required");
            
    } // End of checkMessage
    
    /** Carry out the censoring action. Replaces the contents of text area
     *  by censored version.
     */
    private void censorMessage() {

        String badText = undesirable.getText();                        // Get the undesirable text
        String theMessage = text.getText();                            // Get the text to be censored
        theMessage = theMessage.replace(badText, bleep);               // Carry out the replacement
        text.setText(theMessage);                                      // Display final censored text
        report.setText("Censoring complete");

    } // End of censorMessage

} // End of Censor program