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

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