2 * rhynodge - NewComicTrigger.java - Copyright © 2013 David Roden
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package net.pterodactylus.rhynodge.triggers;
20 import static com.google.common.base.Preconditions.*;
22 import java.util.List;
24 import net.pterodactylus.rhynodge.Reaction;
25 import net.pterodactylus.rhynodge.State;
26 import net.pterodactylus.rhynodge.Trigger;
27 import net.pterodactylus.rhynodge.output.DefaultOutput;
28 import net.pterodactylus.rhynodge.output.Output;
29 import net.pterodactylus.rhynodge.states.ComicState;
30 import net.pterodactylus.rhynodge.states.ComicState.Comic;
31 import net.pterodactylus.rhynodge.states.ComicState.Strip;
33 import com.google.common.collect.Lists;
34 import org.apache.commons.lang3.StringEscapeUtils;
35 import org.apache.commons.lang3.StringUtils;
38 * {@link Trigger} implementation that detects the presence of new {@link
39 * Comic}s in a {@link ComicState}.
41 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
43 public class NewComicTrigger implements Trigger {
45 /** The new comics. */
46 private final List<Comic> newComics = Lists.newArrayList();
48 /** The latest comic state. */
49 private ComicState mergedComicState;
52 public State mergeStates(State previousState, State currentState) {
53 checkArgument(previousState instanceof ComicState, "previous state must be a comic state");
54 checkArgument(currentState instanceof ComicState, "current state must be a comic state");
56 ComicState previousComicState = (ComicState) previousState;
57 ComicState currentComicState = (ComicState) currentState;
59 /* copy old state into new state. */
60 mergedComicState = new ComicState();
61 for (Comic comic : previousComicState) {
62 mergedComicState.add(comic);
66 for (Comic comic : currentComicState) {
67 if (!mergedComicState.comics().contains(comic)) {
69 mergedComicState.add(comic);
73 return mergedComicState;
77 public boolean triggers() {
78 return !newComics.isEmpty();
82 public Output output(Reaction reaction) {
83 DefaultOutput output = new DefaultOutput(String.format("New Comic found for “%s!”", reaction.name()));
85 output.addText("text/plain", generatePlainText());
86 output.addText("text/html", generateHtmlText());
96 * Generates a list of the new comics in plain text format.
98 * @return The list of new comics in plain text format
100 private String generatePlainText() {
101 StringBuilder text = new StringBuilder();
103 for (Comic newComic : newComics) {
104 text.append("Comic Found: ").append(newComic.title()).append("\n\n");
105 for (Strip strip : newComic) {
106 text.append("Image: ").append(strip.imageUrl()).append("\n");
107 if (!StringUtils.isBlank(strip.comment())) {
108 text.append("Comment: ").append(strip.comment()).append("\n");
114 return text.toString();
118 * Generates a list of new comics in HTML format.
120 * @return The list of new comics in HTML format
122 private String generateHtmlText() {
123 StringBuilder html = new StringBuilder();
124 html.append("<body>");
126 for (Comic newComic : newComics) {
127 generateComicHtml(html, newComic);
130 return html.append("</body>").toString();
134 * Generates the HTML for a single comic.
137 * The string builder to append the HTML to
139 * The comic to render
141 private void generateComicHtml(StringBuilder html, Comic comic) {
142 html.append("<h1>").append(StringEscapeUtils.escapeHtml4(comic.title())).append("</h1>\n");
143 for (Strip strip : comic) {
144 html.append("<div><img src=\"").append(StringEscapeUtils.escapeHtml4(strip.imageUrl()));
145 html.append("\" alt=\"").append(StringEscapeUtils.escapeHtml4(strip.comment()));
146 html.append("\" title=\"").append(StringEscapeUtils.escapeHtml4(strip.comment()));
147 html.append("\"></div>\n");