Expression.java 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. * Copyright (C) 2004-2018 L2J Server
  3. *
  4. * This file is part of L2J Server.
  5. *
  6. * L2J Server is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * L2J Server is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package com.l2jserver.gameserver.script;
  20. import java.util.logging.Level;
  21. import java.util.logging.Logger;
  22. import javax.script.ScriptContext;
  23. import com.l2jserver.gameserver.scripting.L2ScriptEngineManager;
  24. public class Expression
  25. {
  26. protected static final Logger _log = Logger.getLogger(Expression.class.getName());
  27. private final ScriptContext _context;
  28. @SuppressWarnings("unused")
  29. private final String _lang;
  30. @SuppressWarnings("unused")
  31. private final String _code;
  32. public static Object eval(String lang, String code)
  33. {
  34. try
  35. {
  36. return L2ScriptEngineManager.getInstance().eval(lang, code);
  37. }
  38. catch (Exception e)
  39. {
  40. _log.log(Level.WARNING, "", e);
  41. return null;
  42. }
  43. }
  44. public static Object eval(ScriptContext context, String lang, String code)
  45. {
  46. try
  47. {
  48. return L2ScriptEngineManager.getInstance().eval(lang, code, context);
  49. }
  50. catch (Exception e)
  51. {
  52. _log.log(Level.WARNING, "", e);
  53. return null;
  54. }
  55. }
  56. public static Expression create(ScriptContext context, String lang, String code)
  57. {
  58. try
  59. {
  60. return new Expression(context, lang, code);
  61. }
  62. catch (Exception e)
  63. {
  64. _log.log(Level.WARNING, "", e);
  65. return null;
  66. }
  67. }
  68. private Expression(ScriptContext pContext, String pLang, String pCode)
  69. {
  70. _context = pContext;
  71. _lang = pLang;
  72. _code = pCode;
  73. }
  74. public <T> void addDynamicVariable(String name, T value)
  75. {
  76. try
  77. {
  78. _context.setAttribute(name, value, ScriptContext.ENGINE_SCOPE);
  79. }
  80. catch (Exception e)
  81. {
  82. _log.log(Level.WARNING, "", e);
  83. }
  84. }
  85. public void removeDynamicVariable(String name)
  86. {
  87. try
  88. {
  89. _context.removeAttribute(name, ScriptContext.ENGINE_SCOPE);
  90. }
  91. catch (Exception e)
  92. {
  93. _log.log(Level.WARNING, "", e);
  94. }
  95. }
  96. }