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

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