您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

ProcessingManager.java 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. * Copyright (c) 2006-2017 DMDirc Developers
  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. package com.dmdirc.parser.irc;
  23. import com.dmdirc.parser.common.ParserError;
  24. import com.dmdirc.parser.events.NumericEvent;
  25. import com.dmdirc.parser.irc.processors.IRCProcessor;
  26. import java.time.LocalDateTime;
  27. import java.util.HashMap;
  28. import java.util.Map;
  29. import java.util.Set;
  30. import javax.inject.Inject;
  31. /**
  32. * IRC Parser Processing Manager.
  33. * Manages adding/removing/calling processing stuff.
  34. */
  35. public class ProcessingManager {
  36. /** Reference to the parser object that owns this ProcessingManager. */
  37. private final IRCParser parser;
  38. /** Hashtable used to store the different types of IRCProcessor known. */
  39. private final Map<String, IRCProcessor> processHash = new HashMap<>();
  40. /**
  41. * Constructor to create a ProcessingManager.
  42. *
  43. * @param parser IRCParser that owns this Processing Manager
  44. * @param processors The processors to add.
  45. */
  46. @Inject
  47. public ProcessingManager(final IRCParser parser, final Set<IRCProcessor> processors) {
  48. this.parser = parser;
  49. processors.forEach(this::addProcessor);
  50. }
  51. /**
  52. * Debugging Data to the console.
  53. */
  54. private void doDebug(final String line, final Object... args) {
  55. parser.callDebugInfo(IRCParser.DEBUG_PROCESSOR, line, args);
  56. }
  57. /**
  58. * Add new Process type.
  59. *
  60. * @param processor IRCProcessor subclass for the processor.
  61. */
  62. public void addProcessor(final IRCProcessor processor) {
  63. // handles() returns a String array of all the tokens
  64. // that this processor will parse.
  65. addProcessor(processor.handles(), processor);
  66. }
  67. /**
  68. * Add a processor to tokens not-specified in the handles() reply.
  69. *
  70. * @param processor IRCProcessor subclass for the processor.
  71. * @param handles String Array of tokens to add this processor as a hadler for
  72. */
  73. public void addProcessor(final String[] handles, final IRCProcessor processor) {
  74. doDebug("Adding processor: " + processor.getName());
  75. for (String handle : handles) {
  76. if (processHash.containsKey(handle.toLowerCase())) {
  77. // New Processors take priority over old ones
  78. processHash.remove(handle.toLowerCase());
  79. }
  80. doDebug("\t Added handler for: " + handle);
  81. processHash.put(handle.toLowerCase(), processor);
  82. }
  83. }
  84. /**
  85. * Remove a Process type.
  86. *
  87. * @param processor IRCProcessor subclass for the processor.
  88. */
  89. public void delProcessor(final IRCProcessor processor) {
  90. doDebug("Deleting processor: " + processor.getName());
  91. for (String elementName : processHash.keySet()) {
  92. doDebug("\t Checking handler for: " + elementName);
  93. final IRCProcessor testProcessor = processHash.get(elementName);
  94. if (testProcessor.getName().equalsIgnoreCase(processor.getName())) {
  95. doDebug("\t Removed handler for: " + elementName);
  96. processHash.remove(elementName);
  97. }
  98. }
  99. }
  100. /**
  101. * Get the processor used for a specified token.
  102. *
  103. * @param sParam Type of line to process ("005", "PRIVMSG" etc)
  104. * @return IRCProcessor for the given param.
  105. * @throws ProcessorNotFoundException if no processer exists for the param
  106. */
  107. public IRCProcessor getProcessor(final String sParam) throws ProcessorNotFoundException {
  108. if (processHash.containsKey(sParam.toLowerCase())) {
  109. return processHash.get(sParam.toLowerCase());
  110. } else {
  111. throw new ProcessorNotFoundException("No processors will handle " + sParam);
  112. }
  113. }
  114. /**
  115. * Process a Line.
  116. *
  117. * @param date Date of line.
  118. * @param sParam Type of line to process ("005", "PRIVMSG" etc)
  119. * @param token IRCTokenised line to process
  120. * @throws ProcessorNotFoundException exception if no processors exists to handle the line
  121. */
  122. public void process(final LocalDateTime date, final String sParam, final String... token)
  123. throws ProcessorNotFoundException {
  124. IRCProcessor messageProcessor = null;
  125. try {
  126. messageProcessor = getProcessor(sParam);
  127. messageProcessor.process(date, sParam, token);
  128. } catch (ProcessorNotFoundException p) {
  129. throw p;
  130. } catch (Exception e) {
  131. final ParserError ei = new ParserError(ParserError.ERROR_ERROR,
  132. "Exception in Processor. [" + messageProcessor + "]: "
  133. + e.getMessage(), parser.getLastLine());
  134. ei.setException(e);
  135. parser.callErrorInfo(ei);
  136. } finally {
  137. // Try to call callNumeric. We don't want this to work if sParam is a non
  138. // integer param, hence the empty catch
  139. try {
  140. callNumeric(date, Integer.parseInt(sParam), token);
  141. } catch (NumberFormatException e) {
  142. }
  143. }
  144. }
  145. /**
  146. * Callback to all objects implementing the onNumeric Callback.
  147. *
  148. * @param numeric What numeric is this for
  149. * @param token IRC Tokenised line
  150. */
  151. protected void callNumeric(final LocalDateTime time, final int numeric, final String... token) {
  152. parser.getCallbackManager().publish(new NumericEvent(parser, time, numeric,
  153. token));
  154. }
  155. }