You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ActionSubstitutor.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /*
  2. * Copyright (c) 2006-2010 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. package com.dmdirc.actions;
  23. import com.dmdirc.actions.interfaces.ActionType;
  24. import com.dmdirc.actions.interfaces.ActionComponent;
  25. import com.dmdirc.FrameContainer;
  26. import com.dmdirc.Precondition;
  27. import com.dmdirc.Server;
  28. import com.dmdirc.ServerState;
  29. import com.dmdirc.config.ConfigManager;
  30. import com.dmdirc.config.IdentityManager;
  31. import com.dmdirc.ui.interfaces.Window;
  32. import java.util.HashMap;
  33. import java.util.Map;
  34. import java.util.Set;
  35. /**
  36. * Handles the substitution of variables into action targets and responses.
  37. *
  38. * @author Chris
  39. */
  40. public class ActionSubstitutor {
  41. /** The action type this substitutor is for. */
  42. private final ActionType type;
  43. /**
  44. * Creates a new substitutor for the specified action type.
  45. *
  46. * @param type The action type this substitutor is for
  47. */
  48. public ActionSubstitutor(final ActionType type) {
  49. this.type = type;
  50. }
  51. /**
  52. * Retrieves a list of global config variables that will be substituted.
  53. * Note: does not include initial $.
  54. *
  55. * @return A list of global variable names that will be substituted
  56. */
  57. public Set<String> getConfigSubstitutions() {
  58. return IdentityManager.getGlobalConfig().getOptions("actions").keySet();
  59. }
  60. /**
  61. * Substitutes in config variables into the specified target.
  62. *
  63. * @param config The configuration manager to use for options
  64. * @param target The StringBuilder to modify
  65. * @since 0.6.3m2
  66. */
  67. private void doConfigSubstitutions(final ConfigManager config, final StringBuilder target) {
  68. for (Map.Entry<String, String> option : config.getOptions("actions").entrySet()) {
  69. doReplacement(target, "$" + option.getKey(), option.getValue());
  70. }
  71. }
  72. /**
  73. * Retrieves a list of substitutions derived from argument and component
  74. * combinations, along with a corresponding friendly name for them.
  75. * Note: does not include initial $.
  76. *
  77. * @return A map of component substitution names and their descriptions
  78. */
  79. public Map<String, String> getComponentSubstitutions() {
  80. final Map<String, String> res = new HashMap<String, String>();
  81. int i = 0;
  82. for (Class myClass : type.getType().getArgTypes()) {
  83. for (ActionComponent comp : ActionManager.getCompatibleComponents(myClass)) {
  84. final String key = "{" + i + "." + comp.toString() + "}";
  85. final String desc = type.getType().getArgNames()[i] + "'s " + comp.getName();
  86. res.put(key, desc);
  87. }
  88. i++;
  89. }
  90. return res;
  91. }
  92. /**
  93. * Substitutes in component-style substitutions.
  94. *
  95. * @param target The stringbuilder to be changed
  96. * @param args The arguments passed for this action type
  97. */
  98. private void doComponentSubstitutions(final StringBuilder target, final Object ... args) {
  99. int i = 0;
  100. for (Class myClass : type.getType().getArgTypes()) {
  101. if (args[i] != null) {
  102. for (ActionComponent comp : ActionManager.getCompatibleComponents(myClass)) {
  103. final String needle = "${" + i + "." + comp.toString() + "}";
  104. if (target.indexOf(needle) > -1) {
  105. final Object replacement = comp.get(args[i]);
  106. if (replacement != null) {
  107. doReplacement(target, needle, replacement.toString());
  108. }
  109. }
  110. }
  111. }
  112. i++;
  113. }
  114. }
  115. /**
  116. * Retrieves a list of server substitutions, if this action type supports
  117. * them.
  118. * Note: does not include initial $.
  119. *
  120. * @return A map of server substitution names and their descriptions.
  121. */
  122. public Map<String, String> getServerSubstitutions() {
  123. final Map<String, String> res = new HashMap<String, String>();
  124. if (hasFrameContainer()) {
  125. for (ActionComponent comp : ActionManager.getCompatibleComponents(Server.class)) {
  126. final String key = "{" + comp.toString() + "}";
  127. final String desc = "The connection's " + comp.getName();
  128. res.put(key, desc);
  129. }
  130. }
  131. return res;
  132. }
  133. /**
  134. * Substitutes in server substitutions.
  135. *
  136. * @param target The stringbuilder to be changed
  137. * @param args The arguments passed for this action type
  138. */
  139. private void doServerSubstitutions(final StringBuilder target, final Object ... args) {
  140. if (args.length > 0 && args[0] instanceof FrameContainer) {
  141. final Server server = ((FrameContainer) args[0]).getServer();
  142. if (server != null) {
  143. synchronized (server.getState()) {
  144. if (!server.getState().equals(ServerState.CONNECTED)) {
  145. return;
  146. }
  147. for (ActionComponent comp : ActionManager.getCompatibleComponents(Server.class)) {
  148. final String key = "${" + comp.toString() + "}";
  149. if (target.indexOf(key) > -1) {
  150. final Object res = comp.get(((FrameContainer) args[0]).getServer());
  151. if (res != null) {
  152. doReplacement(target, key, res.toString());
  153. }
  154. }
  155. }
  156. }
  157. }
  158. }
  159. }
  160. /**
  161. * Returns true if this action type's first argument is a frame container,
  162. * or descendent of one.
  163. *
  164. * @return True if this action type's first arg extends or is a FrameContainer
  165. */
  166. private boolean hasFrameContainer() {
  167. Class target = null;
  168. if (type.getType().getArgTypes().length > 0) {
  169. target = type.getType().getArgTypes()[0];
  170. while (target != null && target != FrameContainer.class) {
  171. target = target.getSuperclass();
  172. }
  173. }
  174. return target == FrameContainer.class;
  175. }
  176. /**
  177. * Determines whether or not word substitutions will work for this action
  178. * type. Word substitutions take the form $1, $1-5, $6-, etc.
  179. *
  180. * @return True if word substitutions are supported, false otherwise.
  181. */
  182. public boolean usesWordSubstitutions() {
  183. return type.getType().getArgTypes().length > 2
  184. && type.getType().getArgTypes()[2] == String[].class;
  185. }
  186. /**
  187. * Substitutes in word substitutions.
  188. *
  189. * @param target The stringbuilder to be changed
  190. * @param args The arguments passed for this action type
  191. */
  192. private void doWordSubstitutions(final StringBuilder target, final Object ... args) {
  193. if (args.length > 1) {
  194. String[] words = null;
  195. if (args.length > 2 && args[2] instanceof String[]) {
  196. words = (String[]) args[2];
  197. } else if (args.length > 2 && args[2] instanceof String) {
  198. words = ((String) args[2]).split(" ");
  199. } else if (args[1] instanceof String[]) {
  200. words = (String[]) args[1];
  201. } else if (args[1] instanceof String) {
  202. words = ((String) args[1]).split(" ");
  203. } else {
  204. return;
  205. }
  206. final StringBuffer compound = new StringBuffer();
  207. for (int i = words.length - 1; i >= 0; i--) {
  208. if (compound.length() > 0) {
  209. compound.insert(0, ' ');
  210. }
  211. compound.insert(0, words[i]);
  212. doReplacement(target, "$" + (i + 1) + "-", compound.toString());
  213. doReplacement(target, "$" + (i + 1), words[i]);
  214. }
  215. }
  216. }
  217. /**
  218. * Performs all applicable substitutions on the specified string, with the
  219. * specified arguments.
  220. *
  221. * @param target The string to be altered
  222. * @param args The arguments for the action type
  223. * @return The substituted string
  224. */
  225. @Precondition("Number of arguments given equals the number of arguments " +
  226. "required by this substitutor's type")
  227. public String doSubstitution(final String target, final Object ... args) {
  228. if (type.getType().getArity() != args.length) {
  229. throw new IllegalArgumentException("Invalid number of arguments "
  230. + "for doSubstitution: expected " + type.getType().getArity() + ", got "
  231. + args.length + ". Type: " + type.getName());
  232. }
  233. final StringBuilder res = new StringBuilder(target);
  234. doConfigSubstitutions(getConfigManager(args), res);
  235. doServerSubstitutions(res, args);
  236. doComponentSubstitutions(res, args);
  237. doWordSubstitutions(res, args);
  238. return res.toString();
  239. }
  240. /**
  241. * Tries to retrieve an appropriate configuration manager from the
  242. * specified set of arguments. If any of the arguments is an instance of
  243. * {@link FrameContainer} or {@link Window}, the config manager is
  244. * requested from them. Otherwise, the global config is returned.
  245. *
  246. * @param args The arguments to be tested
  247. * @return The best config manager to use for those arguments
  248. * @since 0.6.3m2
  249. */
  250. protected ConfigManager getConfigManager(final Object ... args) {
  251. for (Object arg : args) {
  252. if (arg instanceof FrameContainer) {
  253. return ((FrameContainer) arg).getConfigManager();
  254. } else if (arg instanceof Window) {
  255. return ((Window) arg).getConfigManager();
  256. }
  257. }
  258. return IdentityManager.getGlobalConfig();
  259. }
  260. /**
  261. * Replaces all occurances of needle in haystack with replacement.
  262. *
  263. * @param haystack The stringbuilder that is to be modified
  264. * @param needle The search string
  265. * @param replacement The string to be substituted in
  266. */
  267. private void doReplacement(final StringBuilder haystack, final String needle,
  268. final String replacement) {
  269. int i = -1;
  270. do {
  271. i = haystack.indexOf(needle);
  272. if (i != -1) {
  273. haystack.replace(i, i + needle.length(), replacement);
  274. }
  275. } while (i != -1);
  276. }
  277. }