Package wzhybridbots.security

Source Code of wzhybridbots.security.BotOperatorLineNumberReader

/**
* This source file is part of WZ Hybrid Bots
* For the latest information, see http://sourceforge.net/projects/wzhybridbots
*
* Copyright (c) 2006 - WzCtf
* For more information about WzCtf, see http://www.wzctf.net
* Also see the license in license.txt
*/
package wzhybridbots.security;


/** The imported classes/interfaces */
import java.io.IOException;
import java.io.LineNumberReader;
import java.io.Reader;

import wzhybridbots.security.BotOperator;
import wzhybridbots.security.BotOperatorSet;
import wzhybridbots.security.HashBotOperatorSet;


/**
* This class reads a set of bot operators from a text plain file where
* each line contains the name of a bot operator.  The access level is known
* from the file read.  Each line starting with # or // will be considered as
* comment line.
*
* @author Witlospock
* @version 1.0
*/
public class BotOperatorLineNumberReader implements BotOperatorReader {
  /** The data source to read the bot operators from. */
  private Reader reader;
  /** The {@link wzhybridbots.consts.AccessLevel access level} of every bot operator. */
  private int accessLevel;
 
 
  /**
   * The class constructor.
   *
   * @param reader The data source to read the bot operators from.
   * @param accessLevel The access level of every bot operators in that data source.
   */
  public BotOperatorLineNumberReader ( Reader reader,
                     int accessLevel ) {
    if ( reader == null ) { throw new NullPointerException(); }
   
    // Initialize the class
    this.reader = reader;
    this.accessLevel = accessLevel;
  }
 
  /**
   * Reads the bot operators from the data source.
   *
   * @return The set of the bot operators.
   */
  public BotOperatorSet readBotOperators() {
    // Create the bot operator container
    BotOperatorSet set = new HashBotOperatorSet();
   
    return readBotOperators( set );
  }
 
  /**
   * Reads a set of bot operators from a data source and insert
   * them in a specified container.
   *
   * @param set The container to put the bot operators in.
   * @return The original set passed has parameter with an altered content.
   */
  public BotOperatorSet readBotOperators ( BotOperatorSet set ) {
    if ( set == null ) { throw new NullPointerException(); }
   
    // Create the appropriate reader
    LineNumberReader rd = new LineNumberReader( this.reader );
   
    try {
      String line = rd.readLine();
      // Loop through the data source
      while ( line != null ) {
        // Checks if it is a comment
        if ( line.startsWith( "#" ) ||
           line.startsWith( "//" ) ) {
          // It is a comment line
        }
        else {
          // There is a bot operator name
          line = line.trim();
         
          if ( line.length() > 0 ) {
            // Create and add the bot operator to the container
            BotOperator op = new BotOperator( line, this.accessLevel );
            set.add( op );
          }
        }
       
        line = rd.readLine();
      }
    }
    catch ( IOException ioe ) {
      // We catch an io exception
      String msg = ioe.getMessage();
     
      // Print debugging information
      if ( msg != null ) {
        System.out.println( msg );
      }
      ioe.printStackTrace();
    }
   
    return set;
  }
}
TOP

Related Classes of wzhybridbots.security.BotOperatorLineNumberReader

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.