CodeProcessor.java 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package API;
  7. import common.Instruction;
  8. import common.Log;
  9. import java.util.HashMap;
  10. import java.util.LinkedHashMap;
  11. /**
  12. *
  13. * @author Eugenio
  14. */
  15. public class CodeProcessor {
  16. // Todos os processors serão armazenados aqui, e cada evendo possui uma lista de processors separada por virgula
  17. protected static LinkedHashMap<String, API.CodeProcessorInterface> processors = new LinkedHashMap<>();
  18. protected static HashMap<String, HashMap<String, String>> events = new HashMap<>();
  19. public static void _init() {
  20. }
  21. public static void On(String selectors, String event, String processors) {
  22. for (String selector : selectors.split(",")) {
  23. if (!events.containsKey(selector)) {
  24. events.put(selector, new HashMap<String, String>());
  25. }
  26. events.get(selector).put(event, processors);
  27. }
  28. }
  29. public static void Trigger(common.Code c, String selector, String event) throws Exception {
  30. // System.out.println("Try trigger:" + selector);
  31. if (events.containsKey(selector)) {
  32. HashMap<String, String> t = events.get(selector);
  33. if (t.containsKey(event)) {
  34. ProcessorExecute(c, t.get(event));
  35. }
  36. }
  37. }
  38. public static void Add(String id, API.CodeProcessorInterface p) throws Exception {
  39. if (processors.containsKey(id)) {
  40. Log.PrintWarning(
  41. "Code Processor",
  42. new Instruction().S("msg", String.format("Tentativa de sobrescrita do processor %s.", id))
  43. );
  44. }
  45. processors.put(id, p);
  46. }
  47. protected static String[] ProcToList(String events) {
  48. // System.out.println("events:" + events);
  49. return events.replaceAll("\\s+", "").split(",");
  50. }
  51. public static void ProcessorExecute(common.Code c, String eventList) throws Exception {
  52. for (String x : ProcToList(eventList)) {
  53. if (x.equals("")) {
  54. continue;
  55. }
  56. if (!processors.containsKey(x)) {
  57. throw new Exception(String.format("Processador de código '%s' não definido!", x));
  58. }
  59. processors.get(x.trim()).Exec(c, processors);
  60. }
  61. }
  62. }