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.

JavaScriptHelper.java 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * Copyright (c) 2006-2017 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
  5. * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
  7. * permit persons to whom the Software is furnished to do so, subject to the following conditions:
  8. *
  9. * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
  10. * Software.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  13. * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
  14. * OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  15. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  16. */
  17. package com.dmdirc.addons.scriptplugin;
  18. import java.util.HashMap;
  19. import java.util.Locale;
  20. import java.util.Map;
  21. /**
  22. * Used to allow the rhino javascript to do stuff that it otherwise can't, such as setting global
  23. * variables, string trimming and getting a char.
  24. */
  25. public class JavaScriptHelper {
  26. /** Used to identify the JavaScriptHelper. */
  27. private static final String ID = "DMDIRC-JSH";
  28. /**
  29. * Used to allow scripts to know if a specific function they need is available.
  30. */
  31. private static final int VERSION = 2;
  32. /** Hashtable for storing stuff. */
  33. private static final Map<String, Object> SETTINGS = new HashMap<>();
  34. /**
  35. * Method to set Stuff.
  36. *
  37. * @param setting Name of setting
  38. * @param value Value of setting
  39. */
  40. public void setGlobal(final String setting, final Object value) {
  41. if (setting.isEmpty()) {
  42. return;
  43. }
  44. final String key = setting.toLowerCase(Locale.getDefault());
  45. if (SETTINGS.containsKey(key)) {
  46. SETTINGS.remove(key);
  47. }
  48. if (value != null) {
  49. SETTINGS.put(key, value);
  50. }
  51. }
  52. /**
  53. * Method to get Stuff.
  54. *
  55. * @param setting Name of setting
  56. *
  57. * @return Value of setting
  58. */
  59. public Object getGlobal(final String setting) {
  60. if (setting.isEmpty()) {
  61. return "";
  62. }
  63. return SETTINGS.get(setting.toLowerCase(Locale.getDefault()));
  64. }
  65. /**
  66. * Method to trim spaces from strings.
  67. *
  68. * @param str String to trim
  69. *
  70. * @return String without spaces on the ends
  71. */
  72. public String trim(final String str) {
  73. return str.trim();
  74. }
  75. /**
  76. * Method to get a java Character object.
  77. *
  78. * @param str String to make object from
  79. *
  80. * @return Character representing first char in the string
  81. */
  82. public Character toChar(final String str) {
  83. return str.charAt(0);
  84. }
  85. /**
  86. * Get the version of JavaScriptHelper in use.
  87. *
  88. * @return JavaScriptHelper version
  89. */
  90. public int getVersion() {
  91. return VERSION;
  92. }
  93. /**
  94. * Get the ID of this JavaScriptHelper. If you extend or modify this, you should change the
  95. * myIDString variable.
  96. *
  97. * @return JavaScriptHelper ID
  98. */
  99. public String getID() {
  100. return ID;
  101. }
  102. }