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.

EventFormatter.java 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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.dmdirc.events.DisplayProperty;
  24. import com.dmdirc.events.DisplayableEvent;
  25. import java.util.Optional;
  26. import javax.inject.Inject;
  27. import javax.inject.Singleton;
  28. /**
  29. * Formats an event into a text string, based on a user-defined template.
  30. *
  31. * <p>Template tags start with <code>{{</code> and end with <code>}}</code>. The start of the
  32. * tag should be the property of the event that will be displayed. Properties can be chained using
  33. * a <code>.</code> character, e.g. <code>{{user.hostname}}</code>. One or more functions can
  34. * be applied to the result of this, to change the appearance of the output. Functions are
  35. * separated from their argument with a <code>|</code> character,
  36. * e.g. <code>{{user.hostname|uppercase}}</code>.
  37. *
  38. * <p>Properties and functions are case-insensitive.
  39. */
  40. @Singleton
  41. public class EventFormatter {
  42. private static final String ERROR_STRING = "<FormatError>";
  43. private final EventPropertyManager propertyManager;
  44. private final EventFormatProvider formatProvider;
  45. @Inject
  46. public EventFormatter(final EventPropertyManager propertyManager,
  47. final EventFormatProvider formatProvider) {
  48. this.propertyManager = propertyManager;
  49. this.formatProvider = formatProvider;
  50. }
  51. public Optional<String> format(final DisplayableEvent event) {
  52. final Optional<EventFormat> format = formatProvider.getFormat(event.getClass());
  53. if (!event.hasDisplayProperty(DisplayProperty.FOREGROUND_COLOUR)) {
  54. format.flatMap(EventFormat::getDefaultForegroundColour)
  55. .ifPresent(c -> event.setDisplayProperty(DisplayProperty.FOREGROUND_COLOUR, c));
  56. }
  57. return format.map(f -> {
  58. final StringBuilder builder = getTemplate(f, event);
  59. int tagStart = builder.indexOf("{{");
  60. while (tagStart > -1) {
  61. final int tagEnd = builder.indexOf("}}", tagStart);
  62. final String tag = builder.substring(tagStart + 2, tagEnd);
  63. final String replacement = getReplacement(event, tag);
  64. builder.replace(tagStart, tagEnd + 2, replacement);
  65. tagStart = builder.indexOf("{{", tagStart + replacement.length());
  66. }
  67. return builder.toString();
  68. });
  69. }
  70. private StringBuilder getTemplate(final EventFormat format, final DisplayableEvent event) {
  71. final StringBuilder builder = new StringBuilder();
  72. format.getBeforeTemplate().ifPresent(before -> builder.append(before).append('\n'));
  73. builder.append(format.getTemplate());
  74. format.getAfterTemplate().ifPresent(after -> builder.append('\n').append(after));
  75. return builder;
  76. }
  77. private String getReplacement(final DisplayableEvent event, final String tag) {
  78. final String[] functionParts = tag.split("\\|");
  79. final String[] dataParts = functionParts[0].split("\\.");
  80. Object target = event;
  81. for (String part : dataParts) {
  82. final Optional<Object> result =
  83. propertyManager.getProperty(target, target.getClass(), part);
  84. if (result.isPresent()) {
  85. target = result.get();
  86. } else {
  87. return ERROR_STRING;
  88. }
  89. }
  90. String value = target.toString();
  91. for (int i = 1; i < functionParts.length; i++) {
  92. value = propertyManager.applyFunction(value, functionParts[i]);
  93. }
  94. return value;
  95. }
  96. }