File Source: ProcessingManager.java

         /* 
    P/P   *  Method: com.dmdirc.parser.irc.ProcessingManager__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.parser.irc;
    24  
    25  import java.util.Hashtable;
    26  
    27  /**
    28   * IRC Parser Processing Manager.
    29   * Manages adding/removing/calling processing stuff.
    30   *
    31   * @author Shane Mc Cormack
    32   */
    33  public class ProcessingManager {
    34  	/** Reference to the parser object that owns this ProcessingManager */
    35  	IRCParser myParser;
    36  
    37  	/** Hashtable used to store the different types of IRCProcessor known. */
    38  	private final Hashtable<String,IRCProcessor> processHash = new Hashtable<String,IRCProcessor>();
    39  
    40  	/**
    41  	 * Debugging Data to the console.
    42  	 */
    43  	private void doDebug(final String line, final Object... args) {
        		 /* 
    P/P 		  *  Method: void doDebug(String, Object[])
        		  * 
        		  *  Preconditions:
        		  *    this.myParser != null
        		  *    this.myParser.myCallbackManager != null
        		  *    this.myParser.myCallbackManager.callbackHash != null
        		  */
    44  		myParser.callDebugInfo(IRCParser.DEBUG_PROCESSOR, line, args);
    45  	}
    46  
    47  	/**
    48  	 * Constructor to create a ProcessingManager.
    49  	 *
    50  	 * @param parser IRCParser that owns this Processing Manager
    51  	 */
        	 /* 
    P/P 	  *  Method: void com.dmdirc.parser.irc.ProcessingManager(IRCParser)
        	  * 
        	  *  Preconditions:
        	  *    parser != null
        	  *    parser.myCallbackManager != null
        	  *    parser.myCallbackManager.callbackHash != null
        	  * 
        	  *  Postconditions:
        	  *    this.myParser == parser
        	  *    this.myParser != null
        	  *    this.processHash == &amp;new Hashtable(ProcessingManager#1)
        	  *    new Hashtable(ProcessingManager#1) num objects == 1
        	  */
    52  	public ProcessingManager(IRCParser parser) {
    53  		myParser = parser;
    54  		//------------------------------------------------
    55  		// Add processors
    56  		//------------------------------------------------
    57  		// NOTICE AUTH
    58  		addProcessor(new ProcessNoticeAuth(myParser, this));
    59  		// 001
    60  		addProcessor(new Process001(myParser, this));
    61  		// 004
    62  		// 005
    63  		addProcessor(new Process004005(myParser, this));
    64  		// 464
    65  		addProcessor(new Process464(myParser, this));
    66  		// 301
    67  		// 305
    68  		// 306
    69  		addProcessor(new ProcessAway(myParser, this));
    70  		// 352
    71  		addProcessor(new ProcessWho(myParser, this));
    72  		// INVITE
    73  		addProcessor(new ProcessInvite(myParser, this));
    74  		// JOIN
    75  		addProcessor(new ProcessJoin(myParser, this));
    76  		// KICK
    77  		addProcessor(new ProcessKick(myParser, this));
    78  		// PRIVMSG
    79  		// NOTICE
    80  		addProcessor(new ProcessMessage(myParser, this));
    81  		// MODE
    82  		// 324
    83  		addProcessor(new ProcessMode(myParser, this));
    84  		// 372
    85  		// 375
    86  		// 376
    87  		// 422
    88  		addProcessor(new ProcessMOTD(myParser, this));
    89  		// 353
    90  		// 366
    91  		addProcessor(new ProcessNames(myParser, this));
    92  		// 433
    93  		addProcessor(new ProcessNickInUse(myParser, this));
    94  		// NICK
    95  		addProcessor(new ProcessNick(myParser, this));
    96  		// PART
    97  		addProcessor(new ProcessPart(myParser, this));
    98  		// QUIT
    99  		addProcessor(new ProcessQuit(myParser, this));
   100  		// TOPIC
   101  		// 332
   102  		// 333
   103  		addProcessor(new ProcessTopic(myParser, this));
   104  		// 344
   105  		// 345
   106  		// 346
   107  		// 347
   108  		// 348
   109  		// 349
   110  		// 367
   111  		// 368
   112  		addProcessor(new ProcessListModes(myParser, this));
   113  		// WALLOPS
   114  		addProcessor(new ProcessWallops(myParser, this));
   115  	}
   116  
   117  	/**
   118  	 * Add new Process type.
   119  	 *
   120  	 * @param processor IRCProcessor subclass for the processor.
   121  	 */
   122  	public void addProcessor(final IRCProcessor processor) {
   123  		// handles() returns a String array of all the tokens
   124  		// that this processor will parse.
        		 /* 
    P/P 		  *  Method: void addProcessor(IRCProcessor)
        		  * 
        		  *  Preconditions:
        		  *    processor != null
        		  *    this.myParser != null
        		  *    this.myParser.myCallbackManager != null
        		  *    this.myParser.myCallbackManager.callbackHash != null
        		  *    (soft) this.processHash != null
        		  * 
        		  *  Presumptions:
        		  *    handles(...).length@125 <= 232-1
        		  *    handles(...)@125 != null
        		  *    handles(...)[...]@125 != null
        		  */
   125  		addProcessor(processor.handles(), processor);
   126  	}
   127  
   128  	/**
   129  	 * Add a processor to tokens not-specified in the handles() reply.
   130  	 *
   131  	 * @param processor IRCProcessor subclass for the processor.
   132  	 * @param handles String Array of tokens to add this processor as a hadler for
   133  	 */
   134  	public void addProcessor(final String[] handles, final IRCProcessor processor) {
        		 /* 
    P/P 		  *  Method: void addProcessor(String[], IRCProcessor)
        		  * 
        		  *  Preconditions:
        		  *    handles != null
        		  *    handles.length <= 232-1
        		  *    processor != null
        		  *    this.myParser != null
        		  *    this.myParser.myCallbackManager != null
        		  *    this.myParser.myCallbackManager.callbackHash != null
        		  *    (soft) handles[...] != null
        		  *    (soft) this.processHash != null
        		  * 
        		  *  Test Vectors:
        		  *    java.util.Hashtable:containsKey(...)@138: {0}, {1}
        		  */
   135  		doDebug("Adding processor: "+processor.getName());
   136  
   137  		for (int i = 0; i < handles.length; ++i) {
   138  			if (processHash.containsKey(handles[i].toLowerCase())) {
   139  				// New Processors take priority over old ones
   140  				processHash.remove(handles[i].toLowerCase());
   141  			}
   142  			doDebug("\t Added handler for: "+handles[i]);
   143  			processHash.put(handles[i].toLowerCase(), processor);
   144  		}
   145  	}
   146  
   147  	/**
   148  	 * Remove a Process type.
   149  	 *
   150  	 * @param processor IRCProcessor subclass for the processor.
   151  	 */
   152  	public void delProcessor(final IRCProcessor processor) {
   153  		IRCProcessor testProcessor;
        		 /* 
    P/P 		  *  Method: void delProcessor(IRCProcessor)
        		  * 
        		  *  Preconditions:
        		  *    processor != null
        		  *    this.myParser != null
        		  *    this.myParser.myCallbackManager != null
        		  *    this.myParser.myCallbackManager.callbackHash != null
        		  *    this.processHash != null
        		  * 
        		  *  Presumptions:
        		  *    java.util.Hashtable:get(...)@157 != null
        		  * 
        		  *  Test Vectors:
        		  *    java.lang.String:equalsIgnoreCase(...)@158: {0}, {1}
        		  *    java.util.Iterator:hasNext(...)@155: {0}, {1}
        		  */
   154  		doDebug("Deleting processor: "+processor.getName());
   155  		for (String elementName : processHash.keySet()) {
   156  			doDebug("\t Checking handler for: "+elementName);
   157  			testProcessor = processHash.get(elementName);
   158  			if (testProcessor.getName().equalsIgnoreCase(processor.getName())) {
   159  				doDebug("\t Removed handler for: "+elementName);
   160  				processHash.remove(elementName);
   161  			}
   162  		}
   163  	}
   164  
   165  	/**
   166  	 * Get the processor used for a specified token.
   167  	 *
   168  	 * @param sParam Type of line to process ("005", "PRIVMSG" etc)
   169  	 * @return IRCProcessor for the given param.
   170  	 * @throws ProcessorNotFoundException if no processer exists for the param
   171  	 */
   172  	public IRCProcessor getProcessor(final String sParam) throws ProcessorNotFoundException {
        		 /* 
    P/P 		  *  Method: IRCProcessor getProcessor(String)
        		  * 
        		  *  Preconditions:
        		  *    sParam != null
        		  *    this.processHash != null
        		  * 
        		  *  Presumptions:
        		  *    java.util.Hashtable:containsKey(...)@173 == 1
        		  * 
        		  *  Postconditions:
        		  *    init'ed(return_value)
        		  */
   173  		if (processHash.containsKey(sParam.toLowerCase())) {
   174  			return processHash.get(sParam.toLowerCase());
   175  		} else {
   176  			throw new ProcessorNotFoundException("No processors will handle "+sParam);
   177  		}
   178  	}
   179  
   180  	/**
   181  	 * Process a Line.
   182  	 *
   183  	 * @param sParam Type of line to process ("005", "PRIVMSG" etc)
   184  	 * @param token IRCTokenised line to process
   185  	 * @throws ProcessorNotFoundException exception if no processors exists to handle the line
   186  	 */
   187  	public void process(final String sParam, final String[] token) throws ProcessorNotFoundException {
        		 /* 
    P/P 		  *  Method: void process(String, String[])
        		  * 
        		  *  Preconditions:
        		  *    (soft) sParam != null
        		  *    (soft) this.myParser != null
        		  *    (soft) init'ed(this.myParser.lastLine)
        		  *    (soft) this.myParser.myCallbackManager != null
        		  *    (soft) this.myParser.myCallbackManager.callbackHash != null
        		  *    (soft) this.processHash != null
        		  */
   188  		IRCProcessor messageProcessor = null;
   189  		try {
   190  			messageProcessor = getProcessor(sParam);
   191  			messageProcessor.process(sParam, token);
   192  		} catch (ProcessorNotFoundException p) {
   193  			throw p;
   194  		} catch (Exception e) {
   195  			final ParserError ei = new ParserError(ParserError.ERROR_ERROR,"Exception in Processor. ["+messageProcessor+"]: "+e.getMessage(), myParser.getLastLine());
   196  			ei.setException(e);
   197  			myParser.callErrorInfo(ei);
   198  		} finally {
   199  			// Try to call callNumeric. We don't want this to work if sParam is a non
   200  			// integer param, hense the empty catch
   201  			try {
   202  				callNumeric(Integer.parseInt(sParam), token);
   203  			} catch (NumberFormatException e) { }
   204  		}
   205  	}
   206  
   207  	/**
   208  	 * Callback to all objects implementing the onNumeric Callback.
   209  	 *
   210  	 * @see INumeric
   211  	 * @param numeric What numeric is this for
   212  	 * @param token IRC Tokenised line
   213  	 * @return true if a method was called, false otherwise
   214  	 */
   215  	protected boolean callNumeric(final int numeric, final String[] token) {
        		 /* 
    P/P 		  *  Method: bool callNumeric(int, String[])
        		  * 
        		  *  Preconditions:
        		  *    this.myParser != null
        		  *    this.myParser.myCallbackManager != null
        		  *    this.myParser.myCallbackManager.callbackHash != null
        		  * 
        		  *  Postconditions:
        		  *    init'ed(return_value)
        		  */
   216  		return myParser.getCallbackManager().getCallbackType("OnNumeric").call(numeric, token);
   217  	}
   218  
   219  }
   220  








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