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}]");
45 /** The template factory. */
46 private final TemplateFactory templateFactory;
49 * Creates a new freenet link parser.
51 * @param templateFactory
52 * The template factory
54 public FreenetLinkParser(TemplateFactory templateFactory) {
55 this.templateFactory = templateFactory;
66 public Part parse(Reader source) throws IOException {
67 PartContainer parts = new PartContainer();
68 BufferedReader bufferedReader = (source instanceof BufferedReader) ? (BufferedReader) source : new BufferedReader(source);
70 while ((line = bufferedReader.readLine()) != null) {
71 line = line.trim() + "\n";
72 while (line.length() > 0) {
73 int nextKsk = line.indexOf("KSK@");
74 int nextChk = line.indexOf("CHK@");
75 int nextSsk = line.indexOf("SSK@");
76 int nextUsk = line.indexOf("USK@");
77 if ((nextKsk == -1) && (nextChk == -1) && (nextSsk == -1) && (nextUsk == -1)) {
78 parts.add(createPlainTextPart(line));
81 int next = Integer.MAX_VALUE;
82 if ((nextKsk > -1) && (nextKsk < next)) {
85 if ((nextChk > -1) && (nextChk < next)) {
88 if ((nextSsk > -1) && (nextSsk < next)) {
91 if ((nextUsk > -1) && (nextUsk < next)) {
94 Matcher matcher = whitespacePattern.matcher(line);
95 int nextSpace = matcher.find(next) ? matcher.start() : line.length();
96 if (nextSpace > (next + 4)) {
97 parts.add(createPlainTextPart(line.substring(0, next)));
98 String link = line.substring(next, nextSpace);
100 logger.log(Level.FINER, "Found link: " + link);
101 logger.log(Level.FINEST, "Next: %d, CHK: %d, SSK: %d, USK: %d", new Object[] { next, nextChk, nextSsk, nextUsk });
102 if (((next == nextChk) || (next == nextSsk) || (next == nextUsk)) && (link.length() > 98) && (link.charAt(47) == ',') && (link.charAt(91) == ',') && (link.charAt(99) == '/')) {
103 name = link.substring(0, 47) + "…" + link.substring(99);
105 parts.add(createLinkPart(link, name));
106 line = line.substring(nextSpace);
108 parts.add(createPlainTextPart(line.substring(0, next + 4)));
109 line = line.substring(next + 4);
121 * Creates a new plain text part based on a template.
124 * The text to display
125 * @return The part that displays the given text
127 private Part createPlainTextPart(String text) {
128 return new TemplatePart(templateFactory.createTemplate(new StringReader("<% text|html>"))).set("text", text);
132 * Creates a new link part based on a template.
135 * The target of the link
137 * The name of the link
138 * @return The part that displays the link
140 private Part createLinkPart(String link, String name) {
141 return new TemplatePart(templateFactory.createTemplate(new StringReader("<a href=\"/<% link|html>\"><% name|html></a>"))).set("link", link).set("name", name);