2 * Sone - FreenetLinkParser.java - Copyright © 2010 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.sone.text;
20 import java.io.BufferedReader;
21 import java.io.IOException;
22 import java.io.Reader;
23 import java.io.StringReader;
24 import java.net.MalformedURLException;
25 import java.util.logging.Level;
26 import java.util.logging.Logger;
27 import java.util.regex.Matcher;
28 import java.util.regex.Pattern;
30 import net.pterodactylus.sone.data.Sone;
31 import net.pterodactylus.util.logging.Logging;
32 import net.pterodactylus.util.template.TemplateFactory;
33 import freenet.keys.FreenetURI;
36 * {@link Parser} implementation that can recognize Freenet URIs.
38 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
40 public class FreenetLinkParser implements Parser {
43 private static final Logger logger = Logging.getLogger(FreenetLinkParser.class);
45 /** Pattern to detect whitespace. */
46 private static final Pattern whitespacePattern = Pattern.compile("[\\u000a\u0020\u00a0\u1680\u180e\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u200b\u200c\u200d\u202f\u205f\u2060\u2800\u3000]");
49 * Enumeration for all recognized link types.
51 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
53 private enum LinkType {
61 /** Link is an SSK. */
75 /** The template factory. */
76 private final TemplateFactory templateFactory;
78 /** The Sone that posted the currently parsed text. */
79 private Sone postingSone;
82 * Creates a new freenet link parser.
84 * @param templateFactory
85 * The template factory
87 public FreenetLinkParser(TemplateFactory templateFactory) {
88 this.templateFactory = templateFactory;
96 * Sets the Sone that posted the text that will be parsed in the next call
97 * to {@link #parse(Reader)}. You need to synchronize calling this method
98 * and {@link #parse(Reader)}!
101 * The Sone that posted the text
103 public void setPostingSone(Sone sone) {
115 public Part parse(Reader source) throws IOException {
116 PartContainer parts = new PartContainer();
117 BufferedReader bufferedReader = (source instanceof BufferedReader) ? (BufferedReader) source : new BufferedReader(source);
119 while ((line = bufferedReader.readLine()) != null) {
120 line = line.trim() + "\n";
121 while (line.length() > 0) {
122 int nextKsk = line.indexOf("KSK@");
123 int nextChk = line.indexOf("CHK@");
124 int nextSsk = line.indexOf("SSK@");
125 int nextUsk = line.indexOf("USK@");
126 int nextHttp = line.indexOf("http://");
127 int nextHttps = line.indexOf("https://");
128 if ((nextKsk == -1) && (nextChk == -1) && (nextSsk == -1) && (nextUsk == -1) && (nextHttp == -1) && (nextHttps == -1)) {
129 parts.add(createPlainTextPart(line));
132 int next = Integer.MAX_VALUE;
133 LinkType linkType = null;
134 if ((nextKsk > -1) && (nextKsk < next)) {
136 linkType = LinkType.KSK;
138 if ((nextChk > -1) && (nextChk < next)) {
140 linkType = LinkType.CHK;
142 if ((nextSsk > -1) && (nextSsk < next)) {
144 linkType = LinkType.SSK;
146 if ((nextUsk > -1) && (nextUsk < next)) {
148 linkType = LinkType.USK;
150 if ((nextHttp > -1) && (nextHttp < next)) {
152 linkType = LinkType.HTTP;
154 if ((nextHttps > -1) && (nextHttps < next)) {
156 linkType = LinkType.HTTPS;
158 if ((next >= 8) && (line.substring(next - 8, next).equals("freenet:"))) {
160 line = line.substring(0, next) + line.substring(next + 8);
162 Matcher matcher = whitespacePattern.matcher(line);
163 int nextSpace = matcher.find(next) ? matcher.start() : line.length();
164 if (nextSpace > (next + 4)) {
165 parts.add(createPlainTextPart(line.substring(0, next)));
166 String link = line.substring(next, nextSpace);
168 logger.log(Level.FINER, "Found link: %s", link);
169 logger.log(Level.FINEST, "Next: %d, CHK: %d, SSK: %d, USK: %d", new Object[] { next, nextChk, nextSsk, nextUsk });
170 if (linkType == LinkType.KSK) {
171 name = link.substring(4);
172 } else if ((linkType == LinkType.CHK) || (linkType == LinkType.SSK) || (linkType == LinkType.USK)) {
173 if (name.indexOf('/') > -1) {
174 if (!name.endsWith("/")) {
175 name = name.substring(name.lastIndexOf('/') + 1);
177 if (name.indexOf('/') != name.lastIndexOf('/')) {
178 name = name.substring(name.lastIndexOf('/', name.lastIndexOf('/') - 1));
180 /* shorten to 5 chars. */
181 name = name.substring(4, 9);
185 if (name.indexOf('?') > -1) {
186 name = name.substring(0, name.indexOf('?'));
188 boolean fromPostingSone = false;
189 if ((linkType == LinkType.SSK) || (linkType == LinkType.USK)) {
191 new FreenetURI(link);
192 fromPostingSone = link.substring(4, 47).equals(postingSone.getId());
193 parts.add(fromPostingSone ? createTrustedFreenetLinkPart(link, name) : createFreenetLinkPart(link, name));
194 } catch (MalformedURLException mue1) {
195 /* it’s not a valid link. */
196 parts.add(createPlainTextPart(link));
199 parts.add(fromPostingSone ? createTrustedFreenetLinkPart(link, name) : createFreenetLinkPart(link, name));
201 } else if ((linkType == LinkType.HTTP) || (linkType == LinkType.HTTPS)) {
202 name = link.substring(linkType == LinkType.HTTP ? 7 : 8);
203 int firstSlash = name.indexOf('/');
204 int lastSlash = name.lastIndexOf('/');
205 if ((lastSlash - firstSlash) > 3) {
206 name = name.substring(0, firstSlash + 1) + "…" + name.substring(lastSlash);
208 if (name.endsWith("/")) {
209 name = name.substring(0, name.length() - 1);
211 if (((name.indexOf('/') > -1) && (name.indexOf('.') < name.lastIndexOf('.', name.indexOf('/'))) || ((name.indexOf('/') == -1) && (name.indexOf('.') < name.lastIndexOf('.')))) && name.startsWith("www.")) {
212 name = name.substring(4);
214 if (name.indexOf('?') > -1) {
215 name = name.substring(0, name.indexOf('?'));
217 link = "?_CHECKED_HTTP_=" + link;
218 parts.add(createInternetLinkPart(link, name));
220 line = line.substring(nextSpace);
222 parts.add(createPlainTextPart(line.substring(0, next + 4)));
223 line = line.substring(next + 4);
235 * Creates a new plain text part based on a template.
238 * The text to display
239 * @return The part that displays the given text
241 private Part createPlainTextPart(String text) {
242 return new TemplatePart(templateFactory.createTemplate(new StringReader("<% text|html>"))).set("text", text);
246 * Creates a new part based on a template that links to a site within the
250 * The target of the link
252 * The name of the link
253 * @return The part that displays the link
255 private Part createInternetLinkPart(String link, String name) {
256 return new TemplatePart(templateFactory.createTemplate(new StringReader("<a class=\"internet\" href=\"/<% link|html>\" title=\"<% link|html>\"><% name|html></a>"))).set("link", link).set("name", name);
260 * Creates a new part based on a template that links to a site within
264 * The target of the link
266 * The name of the link
267 * @return The part that displays the link
269 private Part createFreenetLinkPart(String link, String name) {
270 return new TemplatePart(templateFactory.createTemplate(new StringReader("<a class=\"freenet\" href=\"/<% link|html>\" title=\"<% link|html>\"><% name|html></a>"))).set("link", link).set("name", name);
274 * Creates a new part based on a template that links to a page in the
278 * The target of the link
280 * The name of the link
281 * @return The part that displays the link
283 private Part createTrustedFreenetLinkPart(String link, String name) {
284 return new TemplatePart(templateFactory.createTemplate(new StringReader("<a class=\"freenet-trusted\" href=\"/<% link|html>\" title=\"<% link|html>\"><% name|html></a>"))).set("link", link).set("name", name);