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.

EventPropertyManager.java 3.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. * Copyright (c) 2006-2015 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.ui.messages;
  23. import com.google.common.base.Strings;
  24. import java.lang.reflect.Method;
  25. import java.util.HashMap;
  26. import java.util.Map;
  27. import java.util.Optional;
  28. import java.util.function.Function;
  29. import javax.inject.Inject;
  30. import javax.inject.Singleton;
  31. import org.slf4j.Logger;
  32. import org.slf4j.LoggerFactory;
  33. import static com.dmdirc.util.LogUtils.USER_ERROR;
  34. /**
  35. * Provides properties and functions used by an {@link EventFormatter}.
  36. *
  37. * <p>Properties are dyanamically supplied based on get methods within the class. For example, if
  38. * channel objects have a 'getName()' method, then the name can be accessed as {{channel.name}}.
  39. *
  40. * <p>Functions are implemented as string transformations, and are defined in
  41. * {@link #EventPropertyManager()}.
  42. */
  43. @Singleton
  44. public class EventPropertyManager {
  45. private static final Logger LOG = LoggerFactory.getLogger(EventPropertyManager.class);
  46. private final Map<String, Function<String, String>> functions = new HashMap<>();
  47. @Inject
  48. public EventPropertyManager() {
  49. functions.put("uppercase", String::toUpperCase);
  50. functions.put("lowercase", String::toLowerCase);
  51. functions.put("trim", String::trim);
  52. functions.put("bracketed", s -> Strings.isNullOrEmpty(s) ? "" : " (" + s + ')');
  53. }
  54. public <S> Optional<Object> getProperty(final S object, final Class<? extends S> type,
  55. final String property) {
  56. final String methodName = "get" + property.substring(0, 1).toUpperCase()
  57. + property.substring(1);
  58. try {
  59. final Method method = type.getMethod(methodName);
  60. // TODO: This is needed for AutoValues, should probably get return types not real types
  61. method.setAccessible(true);
  62. final Object result = method.invoke(object);
  63. if (result instanceof Optional<?>) {
  64. return Optional.ofNullable(((Optional<?>) result).orElse(null));
  65. }
  66. return Optional.ofNullable(result);
  67. } catch (ReflectiveOperationException ex) {
  68. LOG.warn(USER_ERROR, "Unable to format event: could not retrieve property {}",
  69. ex.getMessage(), ex);
  70. }
  71. return Optional.empty();
  72. }
  73. public String applyFunction(final String input, final String function) {
  74. if (functions.containsKey(function)) {
  75. return functions.get(function).apply(input);
  76. }
  77. LOG.info(USER_ERROR, "unable to format event: no such function {}", function);
  78. return input;
  79. }
  80. }