Package org.jasen.samples

Source Code of org.jasen.samples.ScanFolder$Command

/*
* @(#)ScanFolder.java  10/11/2004
*
* Copyright (c) 2004, 2005  jASEN.org
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
*   1. Redistributions of source code must retain the above copyright notice,
*      this list of conditions and the following disclaimer.
*
*   2. Redistributions in binary form must reproduce the above copyright
*      notice, this list of conditions and the following disclaimer in
*      the documentation and/or other materials provided with the distribution.
*
*   3. The names of the authors may not be used to endorse or promote products
*      derived from this software without specific prior written permission.
*
*   4. Any modification or additions to the software must be contributed back
*      to the project.
*
*   5. Any investigation or reverse engineering of source code or binary to
*      enable emails to bypass the filters, and hence inflict spam and or viruses
*      onto users who use or do not use jASEN could subject the perpetrator to
*      criminal and or civil liability.
*
* THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JASEN.ORG,
* OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
package org.jasen.samples;

import java.io.File;
import java.io.FileInputStream;
import java.text.DecimalFormat;
import java.text.NumberFormat;

import javax.mail.internet.MimeMessage;

import org.jasen.JasenScanner;
import org.jasen.error.EmptyErrorHandler;
import org.jasen.error.ErrorHandlerBroker;
import org.jasen.interfaces.JasenScanResult;

/**
* <P>
*   Performs a scan on all files in a given folder.
* </P>
* <p>
*   This class is for TESTING ONLY
* </p>
* @author Jason Polites
*/
public class ScanFolder
{
   
    static class Command {
        boolean lspam = false;
        boolean lham = false;
        boolean lborder = false;
        boolean help = false;
        File drop;
        float low = 0.1f;
        float high = 0.9f;       
    }
   
   
   
    public static String showCommandHelp() {
        StringBuffer buffer = new StringBuffer();
        buffer.append("Usage: ScanFolder <folder path>\n\n");
        buffer.append("\tOptional parameters:\n");
        buffer.append("\t---------------------------------------\n");
        buffer.append("\t-H : Look specifically for HAM messages\n");
        buffer.append("\t-S : Look specifically for SPAM messages\n");
        buffer.append("\t-B : Look specifically for BORDERLINE messages\n");
        buffer.append("\t-TL <threshold>: Specify the low score threshold (0.0 < TL <= HL < 1.0)\n");
        buffer.append("\t-TH <threshold>: Specify the low score threshold (0.0 < TL <= HL < 1.0)\n");
        buffer.append("\t-D <folder path>: The path to the folder in which to move matched messages\n");
        buffer.append("\t-? : Display this help topic\n");
       
        return buffer.toString();
    }
   
    private static void drop(File drop, File message) {
        File newMessage = new File(drop.getAbsolutePath() + System.getProperty("file.separator") + message.getName());
        message.renameTo(newMessage);
    }
   
    private static Command parseCommand(String[] args) throws Exception {
       
        Command command = new Command();
        String param = null;
       
        if(args.length > 1) {
            for (int i = 1; i < args.length; i++)
            {
                if(args[i].equalsIgnoreCase("-h")) {
                    command.lham = true;
                }
                else if(args[i].equalsIgnoreCase("-s")) {
                    command.lspam = true;
                }
                else if(args[i].equalsIgnoreCase("-b")) {
                    command.lborder = true;
                }
                else if(args[i].equalsIgnoreCase("-tl")) {
                    // Get the next
                    if(args.length > i + 1) {
                        i++;
                        param = args[i];
                        try
                        {
                            command.low = Float.parseFloat(param);
                           
                            if(command.low <= 0.0f || command.low >= 1.0f) {
                                throw new Exception("Invalid argument TL:  Must be 0.0 < TL <= HL < 0.1");
                            }
                        }
                        catch (NumberFormatException e)
                        {
                            throw new Exception("Invalid argument TL:  Not a number");
                        }
                    }
                    else
                    {
                        throw new Exception("Missing required parameter TL");
                    }
                }
                else if(args[i].equalsIgnoreCase("-th")) {
                    // Get the next
                    if(args.length > i + 1) {
                        i++;
                        param = args[i];
                        try
                        {
                            command.high = Float.parseFloat(param);
                           
                            if(command.high <= 0.0f || command.high >= 1.0f) {
                                throw new Exception("Invalid argument HL:  Must be 0.0 < TL <= HL < 0.1");
                            }
                        }
                        catch (NumberFormatException e)
                        {
                            throw new Exception("Invalid argument HL:  Not a number");
                        }
                    }
                    else
                    {
                        throw new Exception("Missing required parameter HL");
                    }
                }
                else if(args[i].equalsIgnoreCase("-d")) {
                    if(args.length > i + 1) {
                        i++;
                        param = args[i];
                       
                        try
                        {
                            command.drop = new File(param);
                           
                            if(!command.drop.exists() || !command.drop.isDirectory()) {
                                if(!command.drop.mkdirs()) {
                                    throw new Exception("Invalid argument D:  Could not create folder");
                                }
                            }
                        }
                        catch (Exception e)
                        {
                            throw new Exception("Invalid argument D:  Not a folder");
                        }
                    }
                }
                else if(args[i].equalsIgnoreCase("-?")) {
                    command.help = true;
                }
            } 
           
            if(!command.lborder && !command.lspam && !command.lham) {
                command.lborder = command.lspam = command.lham = true;
            }
        }
       
        return command;
       
    }
   
    public static void main(String[] args) {
        try
        {
            if(args.length < 1) {
                System.out.println (showCommandHelp());
            }
            else
            {
                // Valid arguments are...
                // -h|-H : Look for ham
                // -s|-S : Look for spam
                // -b|-B : Look for borderline
                // -tl|-TL : The low threshold
                // -th|TH : The high threshold
                // -d|-D <folder> : The drop folder (to put matches)
                // -? : Display help
               
               
                // Parse the command
               
                File folder = new File(args[0]);

                if(!folder.isDirectory()) {
                    System.err.println ("ERROR! Folder path must be a directory");
                    System.err.println ("");
                    System.err.println(showCommandHelp());
                }
                else
                {
                    Command command = parseCommand(args);
                   
                    if(command.help) {
                        System.out.println (showCommandHelp());
                    }
                    else
                    {
                        int ham = 0;
                        int spam = 0;
                        int borderline = 0;
                        int error = 0;
      
                        System.out.println ("Initialising jASEN ...");

                        JasenScanner.getInstance().init();

                        ErrorHandlerBroker.getInstance().setErrorHandler(new EmptyErrorHandler());

                        System.out.println ("Scanning files in path..." + folder.getAbsolutePath());
                        System.out.println ("");

                        File[] files = folder.listFiles();
                        File file = null;

                        FileInputStream fin = null;
                        MimeMessage mm = null;
                        JasenScanResult result = null;

                        NumberFormat formatter = new DecimalFormat("0.000");

                        for (int i = 0; i < files.length; i++)
                        {
                            file = files[i];

                            if(file.isFile()) {
                                fin = new FileInputStream(file);
                                mm = new MimeMessage(null, fin);
                                fin.close();

                                System.out.println ("Scanning\t:\t" + file.getName());
                                result = JasenScanner.getInstance().scan(mm);

                                if(result.completed()) {
                                    if(result.getProbability() >= command.high) {

                                        if(command.lspam) {
                                            System.out.print ("Result\t\t:\t");
                                            System.out.println(formatter.format(result.getProbability()));
                                            System.out.print ("Judgement\t:\t");
                                            System.out.println ("Most likely SPAM");
                                            System.out.println ("--------------------------------------------------------");
                                            System.out.println ("");
                                           
                                            if(command.drop != null) {
                                                drop(command.drop, file);
                                            }
                                        }

                                        spam++;
                                    }
                                    else if (result.getProbability() <= command.low) {

                                        if(command.lham) {
                                            System.out.print ("Result\t\t:\t");
                                            System.out.println(formatter.format(result.getProbability()));
                                            System.out.print ("Judgement\t:\t");
                                            System.out.println ("Most likely HAM");
                                            System.out.println ("--------------------------------------------------------");
                                            System.out.println ("");
                                           
                                            if(command.drop != null) {
                                                drop(command.drop, file);
                                            }
                                        }

                                        ham++;
                                    }
                                    else {

                                        if(command.lborder) {
                                            System.out.print ("Result\t\t:\t");
                                            System.out.println(formatter.format(result.getProbability()));
                                            System.out.print ("Judgement\t:\t");
                                            System.out.println ("Can't be sure.  Borderline case");
                                            System.out.println ("--------------------------------------------------------");
                                            System.out.println ("");
                                           
                                            if(command.drop != null) {
                                                drop(command.drop, file);
                                            }
                                        }

                                        borderline++;
                                    }
                                }
                                else
                                {
                                    System.out.print ("Result\t\t:\t");
                                    System.out.println (result.getTestResults()[0][0]);
                                    error++;
                                }
                            }
                        }

                        System.out.println ("Summary Results");
                        System.out.println ("--------------------------------------------------------");
                        System.out.println ("Spam\t\t:\t" + spam);
                        System.out.println ("Ham\t\t:\t" + ham);
                        System.out.println ("Borderline\t:\t" + borderline);
                        System.out.println ("Errors\t\t:\t" + error);
                        System.out.println ("Total -no error\t:\t" + (spam+ham+borderline));
                        System.out.println ("Total \t\t:\t" + (spam+ham+borderline+error));
                        System.out.println ("");                       
                    }
                   
                   
                }
            }
        }
        catch (Exception e)
        {
            e.printStackTrace ();
        }
        finally {
            JasenScanner.getInstance().destroy();
        }
    }
}
TOP

Related Classes of org.jasen.samples.ScanFolder$Command

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.