<dependency>
<groupId>net.pterodactylus</groupId>
<artifactId>utils</artifactId>
- <version>0.7</version>
+ <version>0.7.1</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<dependency>
<groupId>net.pterodactylus</groupId>
<artifactId>utils.json</artifactId>
- <version>0.1-SNAPSHOT</version>
+ <version>0.1</version>
</dependency>
</dependencies>
+ <repositories>
+ <repository>
+ <id>pterodactylus</id>
+ <url>http://maven.pterodactylus.net/</url>
+ </repository>
+ </repositories>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</plugin>
</plugins>
</build>
+ <reporting>
+ <plugins>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-javadoc-plugin</artifactId>
+ <configuration>
+ <links>
+ <link>http://download.oracle.com/javase/6/docs/api/</link>
+ <link>http://java.pterodactylus.net/utils/apidocs/</link>
+ <link>http://java.pterodactylus.net/utils.json/apidocs/</link>
+ </links>
+ </configuration>
+ </plugin>
+ </plugins>
+ </reporting>
</project>
package net.pterodactylus.sone.template;
+import java.io.IOException;
+import java.io.StringReader;
+
import net.pterodactylus.sone.core.Core;
import net.pterodactylus.sone.data.Post;
import net.pterodactylus.sone.data.Sone;
+import net.pterodactylus.sone.text.FreenetLinkParser;
import net.pterodactylus.util.template.DataProvider;
import net.pterodactylus.util.template.ReflectionAccessor;
+import net.pterodactylus.util.template.TemplateFactory;
/**
* Accessor for {@link Post} objects that adds additional properties:
*/
public class PostAccessor extends ReflectionAccessor {
+ /** Parser for Freenet links. */
+ private final FreenetLinkParser linkParser;
+
/** The core to get the replies from. */
private final Core core;
*
* @param core
* The core to get the replies from
+ * @param templateFactory
+ * The template factory for the text parser
*/
- public PostAccessor(Core core) {
+ public PostAccessor(Core core, TemplateFactory templateFactory) {
this.core = core;
+ linkParser = new FreenetLinkParser(templateFactory);
}
/**
return (currentSone != null) && (currentSone.isLikedPostId(post.getId()));
} else if (member.equals("new")) {
return core.isNewPost(post.getId(), false);
+ } else if (member.equals("text")) {
+ String text = post.getText();
+ try {
+ return linkParser.parse(new StringReader(text));
+ } catch (IOException ioe1) {
+ /* ignore. */
+ }
}
return super.get(dataProvider, object, member);
}
package net.pterodactylus.sone.template;
+import java.io.IOException;
+import java.io.StringReader;
+
import net.pterodactylus.sone.core.Core;
import net.pterodactylus.sone.data.Reply;
import net.pterodactylus.sone.data.Sone;
+import net.pterodactylus.sone.text.FreenetLinkParser;
import net.pterodactylus.util.template.Accessor;
import net.pterodactylus.util.template.DataProvider;
import net.pterodactylus.util.template.ReflectionAccessor;
+import net.pterodactylus.util.template.TemplateFactory;
/**
* {@link Accessor} implementation that adds a couple of properties to
*/
public class ReplyAccessor extends ReflectionAccessor {
+ /** Parser for Freenet links. */
+ private final FreenetLinkParser linkParser;
+
/** The core. */
private final Core core;
*
* @param core
* The core
+ * @param templateFactory
+ * The template factory for the text parser
*/
- public ReplyAccessor(Core core) {
+ public ReplyAccessor(Core core, TemplateFactory templateFactory) {
this.core = core;
+ linkParser = new FreenetLinkParser(templateFactory);
}
/**
return (currentSone != null) && (currentSone.isLikedReplyId(reply.getId()));
} else if (member.equals("new")) {
return core.isNewReply(reply.getId(), false);
+ } else if (member.equals("text")) {
+ String text = reply.getText();
+ try {
+ return linkParser.parse(new StringReader(text));
+ } catch (IOException ioe1) {
+ /* ignore. */
+ }
}
return super.get(dataProvider, object, member);
}
--- /dev/null
+/*
+ * Sone - FreenetLinkParser.java - Copyright © 2010 David Roden
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+package net.pterodactylus.sone.text;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.Reader;
+import java.io.StringReader;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import net.pterodactylus.util.logging.Logging;
+import net.pterodactylus.util.template.TemplateFactory;
+
+/**
+ * {@link Parser} implementation that can recognize Freenet URIs.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public class FreenetLinkParser implements Parser {
+
+ /** The logger. */
+ private static final Logger logger = Logging.getLogger(FreenetLinkParser.class);
+
+ /** Pattern to detect whitespace. */
+ private static final Pattern whitespacePattern = Pattern.compile("[\\p{javaWhitespace}]");
+
+ /** The template factory. */
+ private final TemplateFactory templateFactory;
+
+ /**
+ * Creates a new freenet link parser.
+ *
+ * @param templateFactory
+ * The template factory
+ */
+ public FreenetLinkParser(TemplateFactory templateFactory) {
+ this.templateFactory = templateFactory;
+ }
+
+ //
+ // PART METHODS
+ //
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public Part parse(Reader source) throws IOException {
+ PartContainer parts = new PartContainer();
+ BufferedReader bufferedReader = (source instanceof BufferedReader) ? (BufferedReader) source : new BufferedReader(source);
+ String line;
+ while ((line = bufferedReader.readLine()) != null) {
+ line = line.trim() + "\n";
+ while (line.length() > 0) {
+ int nextKsk = line.indexOf("KSK@");
+ int nextChk = line.indexOf("CHK@");
+ int nextSsk = line.indexOf("SSK@");
+ int nextUsk = line.indexOf("USK@");
+ if ((nextKsk == -1) && (nextChk == -1) && (nextSsk == -1) && (nextUsk == -1)) {
+ parts.add(createPlainTextPart(line));
+ break;
+ }
+ int next = Integer.MAX_VALUE;
+ if ((nextKsk > -1) && (nextKsk < next)) {
+ next = nextKsk;
+ }
+ if ((nextChk > -1) && (nextChk < next)) {
+ next = nextChk;
+ }
+ if ((nextSsk > -1) && (nextSsk < next)) {
+ next = nextSsk;
+ }
+ if ((nextUsk > -1) && (nextUsk < next)) {
+ next = nextUsk;
+ }
+ Matcher matcher = whitespacePattern.matcher(line);
+ int nextSpace = matcher.find(next) ? matcher.start() : line.length();
+ if (nextSpace > (next + 4)) {
+ parts.add(createPlainTextPart(line.substring(0, next)));
+ String link = line.substring(next, nextSpace);
+ String name = link;
+ logger.log(Level.FINER, "Found link: " + link);
+ logger.log(Level.FINEST, "Next: %d, CHK: %d, SSK: %d, USK: %d", new Object[] { next, nextChk, nextSsk, nextUsk });
+ if (((next == nextChk) || (next == nextSsk) || (next == nextUsk)) && (link.length() > 98) && (link.charAt(47) == ',') && (link.charAt(91) == ',') && (link.charAt(99) == '/')) {
+ name = link.substring(0, 47) + "…" + link.substring(99);
+ }
+ parts.add(createLinkPart(link, name));
+ line = line.substring(nextSpace);
+ } else {
+ parts.add(createPlainTextPart(line.substring(0, next + 4)));
+ line = line.substring(next + 4);
+ }
+ }
+ }
+ return parts;
+ }
+
+ //
+ // PRIVATE METHODS
+ //
+
+ /**
+ * Creates a new plain text part based on a template.
+ *
+ * @param text
+ * The text to display
+ * @return The part that displays the given text
+ */
+ private Part createPlainTextPart(String text) {
+ return new TemplatePart(templateFactory.createTemplate(new StringReader("<% text|html>"))).set("text", text);
+ }
+
+ /**
+ * Creates a new link part based on a template.
+ *
+ * @param link
+ * The target of the link
+ * @param name
+ * The name of the link
+ * @return The part that displays the link
+ */
+ private Part createLinkPart(String link, String name) {
+ return new TemplatePart(templateFactory.createTemplate(new StringReader("<a href=\"/<% link|html>\"><% name|html></a>"))).set("link", link).set("name", name);
+ }
+
+}
--- /dev/null
+/*
+ * Sone - Parser.java - Copyright © 2010 David Roden
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+package net.pterodactylus.sone.text;
+
+import java.io.IOException;
+import java.io.Reader;
+
+/**
+ * Interface for parsers that can create {@link Part}s from a text source
+ * (usually a {@link Reader}).
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public interface Parser {
+
+ /**
+ * Create a {@link Part} from the given text source.
+ *
+ * @param source
+ * The text source
+ * @return The parsed part
+ * @throws IOException
+ * if an I/O error occurs
+ */
+ public Part parse(Reader source) throws IOException;
+
+}
--- /dev/null
+/*
+ * Sone - Part.java - Copyright © 2010 David Roden
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+package net.pterodactylus.sone.text;
+
+import net.pterodactylus.util.io.Renderable;
+
+/**
+ * A part is a single piece of information that can be displayed as a single
+ * element.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public interface Part extends Renderable {
+
+ /* all required methods are inherited from {@link Renderable}. */
+
+}
--- /dev/null
+/*
+ * Sone - PartContainer.java - Copyright © 2010 David Roden
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+package net.pterodactylus.sone.text;
+
+import java.io.IOException;
+import java.io.Writer;
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * Part implementation that can contain an arbitrary amount of other parts.
+ * Parts are added using the {@link #add(Part)} method and will be rendered in
+ * the order they are added.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public class PartContainer implements Part {
+
+ /** The parts to render. */
+ private final List<Part> parts = new ArrayList<Part>();
+
+ //
+ // ACCESSORS
+ //
+
+ /**
+ * Adds a part to render.
+ *
+ * @param part
+ * The part to add
+ */
+ public void add(Part part) {
+ parts.add(part);
+ }
+
+ //
+ // PART METHODS
+ //
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public void render(Writer writer) throws IOException {
+ for (Part part : parts) {
+ part.render(writer);
+ }
+ }
+
+}
--- /dev/null
+/*
+ * Sone - TemplatePart.java - Copyright © 2010 David Roden
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+package net.pterodactylus.sone.text;
+
+import java.io.IOException;
+import java.io.Writer;
+
+import net.pterodactylus.util.template.Template;
+
+/**
+ * {@link Part} implementation that is rendered using a {@link Template}.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public class TemplatePart implements Part {
+
+ /** The template to render for this part. */
+ private final Template template;
+
+ /**
+ * Creates a new template part.
+ *
+ * @param template
+ * The template to render
+ */
+ public TemplatePart(Template template) {
+ this.template = template;
+ }
+
+ //
+ // ACTIONS
+ //
+
+ /**
+ * Sets a variable in the template.
+ *
+ * @param key
+ * The key of the variable
+ * @param value
+ * The value of the variable
+ * @return This template part (for method chaining)
+ */
+ public TemplatePart set(String key, String value) {
+ template.set(key, value);
+ return this;
+ }
+
+ //
+ // PART METHODS
+ //
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public void render(Writer writer) throws IOException {
+ template.render(writer);
+ }
+
+}
templateFactory.addAccessor(Object.class, new ReflectionAccessor());
templateFactory.addAccessor(Collection.class, new CollectionAccessor());
templateFactory.addAccessor(Sone.class, new SoneAccessor(getCore()));
- templateFactory.addAccessor(Post.class, new PostAccessor(getCore()));
- templateFactory.addAccessor(Reply.class, new ReplyAccessor(getCore()));
+ templateFactory.addAccessor(Post.class, new PostAccessor(getCore(), templateFactory));
+ templateFactory.addAccessor(Reply.class, new ReplyAccessor(getCore(), templateFactory));
templateFactory.addAccessor(Identity.class, new IdentityAccessor(getCore()));
templateFactory.addAccessor(NotificationManager.class, new NotificationManagerAccessor());
templateFactory.addFilter("date", new DateFilter());
<div class="inner-part">
<div>
<div class="author profile-link"><a href="viewSone.html?sone=<% post.sone.id|html>"><% post.sone.niceName|html></a></div>
- <div class="text"><% post.text|html></div>
+ <div class="text"><% post.text></div>
</div>
<div class="post-status-line status-line">
<div class="time"><a href="viewPost.html?post=<% post.id|html>"><% post.time|date format="MMM d, yyyy, HH:mm:ss"></a></div>
<div class="inner-part">
<div>
<div class="author profile-link"><a href="viewSone.html?sone=<% reply.sone.id|html>"><% reply.sone.niceName|html></a></div>
- <div class="text"><% reply.text|html></div>
+ <div class="text"><% reply.text></div>
</div>
<div class="reply-status-line status-line">
<div class="time"><% reply.time|date format="MMM d, yyyy, HH:mm:ss"></div>