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.util.logging.Level;
25 import java.util.logging.Logger;
26 import java.util.regex.Matcher;
27 import java.util.regex.Pattern;
29 import net.pterodactylus.util.logging.Logging;
30 import net.pterodactylus.util.template.TemplateFactory;
33 * {@link Parser} implementation that can recognize Freenet URIs.
35 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
37 public class FreenetLinkParser implements Parser {
40 private static final Logger logger = Logging.getLogger(FreenetLinkParser.class);
42 /** Pattern to detect whitespace. */
43 private static final Pattern whitespacePattern = Pattern.compile("[\\p{javaWhitespace}]");
46 * Enumeration for all recognized link types.
48 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
50 private enum LinkType {
58 /** Link is an SSK. */
66 /** The template factory. */
67 private final TemplateFactory templateFactory;
70 * Creates a new freenet link parser.
72 * @param templateFactory
73 * The template factory
75 public FreenetLinkParser(TemplateFactory templateFactory) {
76 this.templateFactory = templateFactory;
87 public Part parse(Reader source) throws IOException {
88 PartContainer parts = new PartContainer();
89 BufferedReader bufferedReader = (source instanceof BufferedReader) ? (BufferedReader) source : new BufferedReader(source);
91 while ((line = bufferedReader.readLine()) != null) {
92 line = line.trim() + "\n";
93 while (line.length() > 0) {
94 int nextKsk = line.indexOf("KSK@");
95 int nextChk = line.indexOf("CHK@");
96 int nextSsk = line.indexOf("SSK@");
97 int nextUsk = line.indexOf("USK@");
98 if ((nextKsk == -1) && (nextChk == -1) && (nextSsk == -1) && (nextUsk == -1)) {
99 parts.add(createPlainTextPart(line));
102 int next = Integer.MAX_VALUE;
103 LinkType linkType = null;
104 if ((nextKsk > -1) && (nextKsk < next)) {
106 linkType = LinkType.KSK;
108 if ((nextChk > -1) && (nextChk < next)) {
110 linkType = LinkType.CHK;
112 if ((nextSsk > -1) && (nextSsk < next)) {
114 linkType = LinkType.SSK;
116 if ((nextUsk > -1) && (nextUsk < next)) {
118 linkType = LinkType.USK;
120 if ((next >= 8) && (line.substring(next - 8, next).equals("freenet:"))) {
122 line = line.substring(0, next) + line.substring(next + 8);
124 Matcher matcher = whitespacePattern.matcher(line);
125 int nextSpace = matcher.find(next) ? matcher.start() : line.length();
126 if (nextSpace > (next + 4)) {
127 parts.add(createPlainTextPart(line.substring(0, next)));
128 String link = line.substring(next, nextSpace);
130 logger.log(Level.FINER, "Found link: " + link);
131 logger.log(Level.FINEST, "Next: %d, CHK: %d, SSK: %d, USK: %d", new Object[] { next, nextChk, nextSsk, nextUsk });
132 if (((linkType == LinkType.CHK) || (linkType == LinkType.SSK) || (linkType == LinkType.USK)) && (link.length() > 98) && (link.charAt(47) == ',') && (link.charAt(91) == ',') && (link.charAt(99) == '/')) {
133 name = link.substring(0, 47) + "…" + link.substring(99);
135 parts.add(createLinkPart(link, name));
136 line = line.substring(nextSpace);
138 parts.add(createPlainTextPart(line.substring(0, next + 4)));
139 line = line.substring(next + 4);
151 * Creates a new plain text part based on a template.
154 * The text to display
155 * @return The part that displays the given text
157 private Part createPlainTextPart(String text) {
158 return new TemplatePart(templateFactory.createTemplate(new StringReader("<% text|html>"))).set("text", text);
162 * Creates a new link part based on a template.
165 * The target of the link
167 * The name of the link
168 * @return The part that displays the link
170 private Part createLinkPart(String link, String name) {
171 return new TemplatePart(templateFactory.createTemplate(new StringReader("<a href=\"/<% link|html>\"><% name|html></a>"))).set("link", link).set("name", name);