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 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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.dmdirc.events.DisplayProperty;
  19. import com.dmdirc.events.DisplayPropertyMap;
  20. import com.dmdirc.events.DisplayableEvent;
  21. import com.dmdirc.interfaces.Displayable;
  22. import com.dmdirc.util.colours.ColourUtils;
  23. import java.util.Optional;
  24. import javax.inject.Inject;
  25. import javax.inject.Singleton;
  26. /**
  27. * Formats an event into a text string, based on a user-defined template.
  28. *
  29. * <p>Template tags start with <code>{{</code> and end with <code>}}</code>. The start of the
  30. * tag should be the property of the event that will be displayed. Properties can be chained using
  31. * a <code>.</code> character, e.g. <code>{{user.hostname}}</code>. One or more functions can
  32. * be applied to the result of this, to change the appearance of the output. Functions are
  33. * separated from their argument with a <code>|</code> character,
  34. * e.g. <code>{{user.hostname|uppercase}}</code>.
  35. *
  36. * <p>Properties and functions are case-insensitive.
  37. */
  38. @Singleton
  39. public class EventFormatter {
  40. private static final String ERROR_STRING = "<FormatError>";
  41. private final EventPropertyManager propertyManager;
  42. private final EventFormatProvider formatProvider;
  43. @Inject
  44. public EventFormatter(final EventPropertyManager propertyManager,
  45. final EventFormatProvider formatProvider) {
  46. this.propertyManager = propertyManager;
  47. this.formatProvider = formatProvider;
  48. }
  49. public Optional<String> format(final DisplayableEvent event) {
  50. final Optional<EventFormat> format = formatProvider.getFormat(event.getClass());
  51. format.map(EventFormat::getDisplayProperties)
  52. .ifPresent(event.getDisplayProperties()::putAll);
  53. return format.map(f -> format(f, event));
  54. }
  55. private String format(final EventFormat format, final DisplayableEvent event) {
  56. final StringBuilder builder = new StringBuilder();
  57. format.getBeforeTemplate().ifPresent(
  58. before -> builder.append(doSubstitutions(event, before)).append('\n'));
  59. builder.append(
  60. format.getIterateProperty()
  61. .map(iterate -> formatIterable(event, iterate, format.getTemplate()))
  62. .orElseGet(() -> doSubstitutions(event, format.getTemplate())));
  63. format.getAfterTemplate().ifPresent(
  64. after -> builder.append('\n').append(doSubstitutions(event, after)));
  65. return builder.toString();
  66. }
  67. private String doSubstitutions(final Object dataSource, final String line) {
  68. final StringBuilder builder = new StringBuilder(line);
  69. int tagStart = builder.indexOf("{{");
  70. while (tagStart > -1) {
  71. final int tagEnd = builder.indexOf("}}", tagStart);
  72. final String tag = builder.substring(tagStart + 2, tagEnd);
  73. final String replacement = getReplacement(dataSource, tag);
  74. builder.replace(tagStart, tagEnd + 2, replacement);
  75. tagStart = builder.indexOf("{{", tagStart + replacement.length());
  76. }
  77. return builder.toString();
  78. }
  79. private String formatIterable(final DisplayableEvent event, final String property,
  80. final String template) {
  81. final Optional<Object> value
  82. = propertyManager.getProperty(event, event.getClass(), property);
  83. if (!value.isPresent() || !(value.get() instanceof Iterable<?>)) {
  84. return ERROR_STRING;
  85. }
  86. @SuppressWarnings("unchecked")
  87. final Iterable<Object> collection = (Iterable<Object>) value.get();
  88. final StringBuilder res = new StringBuilder();
  89. for (Object line : collection) {
  90. if (res.length() > 0) {
  91. res.append('\n');
  92. }
  93. res.append(doSubstitutions(line, template));
  94. }
  95. return res.toString();
  96. }
  97. private String getReplacement(final Object dataSource, final String tag) {
  98. final String[] functionParts = tag.split("\\|");
  99. final String[] dataParts = functionParts[0].split("\\.");
  100. final DisplayPropertyMap displayProperties = new DisplayPropertyMap();
  101. Object target = dataSource;
  102. for (String part : dataParts) {
  103. final Optional<Object> result = propertyManager.getProperty(target, target.getClass(), part);
  104. if (result.isPresent()) {
  105. target = result.get();
  106. // Collate all the display properties for objects as we traverse. More specific ones will
  107. // override earlier ones.
  108. if (target instanceof Displayable) {
  109. displayProperties.putAll(((Displayable) target).getDisplayProperties());
  110. }
  111. } else {
  112. return ERROR_STRING;
  113. }
  114. }
  115. String value = applyDisplayProperties(displayProperties, target.toString());
  116. for (int i = 1; i < functionParts.length; i++) {
  117. value = propertyManager.applyFunction(value, functionParts[i]);
  118. }
  119. return value;
  120. }
  121. // TODO: It should be possible for plugins etc to add new ways of applying properties.
  122. private String applyDisplayProperties(final DisplayPropertyMap displayProperties, final String value) {
  123. final StringBuilder res = new StringBuilder(value);
  124. displayProperties.get(DisplayProperty.LINK_USER).ifPresent(user -> res
  125. .insert(0, StyleApplier.CODE_NICKNAME)
  126. .insert(0, user.getNickname())
  127. .insert(0, StyleApplier.CODE_NICKNAME)
  128. .append(StyleApplier.CODE_NICKNAME));
  129. displayProperties.get(DisplayProperty.FOREGROUND_COLOUR).ifPresent(colour -> res
  130. .insert(0, ColourUtils.getHex(colour))
  131. .insert(0, IRCControlCodes.COLOUR_HEX)
  132. .append(IRCControlCodes.COLOUR_HEX));
  133. return res.toString();
  134. }
  135. }