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.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * Copyright (c) 2006-2013 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.addons.scriptplugin;
  23. import java.util.HashMap;
  24. import java.util.Locale;
  25. import java.util.Map;
  26. /**
  27. * Used to allow the rhino javascript to do stuff that it otherwise can't, such
  28. * as setting global variables, string triming and getting a char.
  29. */
  30. public class JavaScriptHelper {
  31. /** Used to identify the JavaScriptHelper. */
  32. private static final String ID = "DMDIRC-JSH";
  33. /**
  34. * Used to allow scripts to know if a specific function they need is
  35. * available.
  36. */
  37. private static final int VERSION = 2;
  38. /** Hashtable for storing stuff. */
  39. private static final Map<String, Object> SETTINGS
  40. = new HashMap<String, Object>();
  41. /**
  42. * Method to set Stuff.
  43. *
  44. * @param setting Name of setting
  45. * @param value Value of setting
  46. */
  47. public void setGlobal(final String setting, final Object value) {
  48. if (setting.isEmpty()) {
  49. return;
  50. }
  51. final String key = setting.toLowerCase(Locale.getDefault());
  52. if (SETTINGS.containsKey(key)) {
  53. SETTINGS.remove(key);
  54. }
  55. if (value != null) {
  56. SETTINGS.put(key, value);
  57. }
  58. }
  59. /**
  60. * Method to get Stuff.
  61. *
  62. * @param setting Name of setting
  63. *
  64. * @return Value of setting
  65. */
  66. public Object getGlobal(final String setting) {
  67. if (setting.isEmpty()) {
  68. return "";
  69. }
  70. return SETTINGS.get(setting.toLowerCase(Locale.getDefault()));
  71. }
  72. /**
  73. * Method to trim spaces from strings.
  74. *
  75. * @param str String to trim
  76. *
  77. * @return String without spaces on the ends
  78. */
  79. public String trim(final String str) {
  80. return str.trim();
  81. }
  82. /**
  83. * Method to get a java Character object.
  84. *
  85. * @param str String to make object from
  86. *
  87. * @return Character represending first char in the string
  88. */
  89. public Character toChar(final String str) {
  90. return str.charAt(0);
  91. }
  92. /**
  93. * Get the version of JavaScriptHelper in use.
  94. *
  95. * @return JavaScriptHelper version
  96. */
  97. public int getVersion() {
  98. return VERSION;
  99. }
  100. /**
  101. * Get the ID of this JavaScriptHelper.
  102. * If you extend or modify this, you should change the myIDString variable.
  103. *
  104. * @return JavaScriptHelper ID
  105. */
  106. public String getID() {
  107. return ID;
  108. }
  109. }