7749b0091e8c567f08e445f8d0853d19d81bc6af
[Sone.git] / src / main / java / net / pterodactylus / sone / text / FreenetLinkParser.java
1 /*
2  * Sone - FreenetLinkParser.java - Copyright © 2010 David Roden
3  *
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.
8  *
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.
13  *
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/>.
16  */
17
18 package net.pterodactylus.sone.text;
19
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;
28
29 import net.pterodactylus.util.logging.Logging;
30 import net.pterodactylus.util.template.TemplateFactory;
31
32 /**
33  * {@link Parser} implementation that can recognize Freenet URIs.
34  *
35  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
36  */
37 public class FreenetLinkParser implements Parser {
38
39         /** The logger. */
40         private static final Logger logger = Logging.getLogger(FreenetLinkParser.class);
41
42         /** Pattern to detect whitespace. */
43         private static final Pattern whitespacePattern = Pattern.compile("[\\p{javaWhitespace}]");
44
45         /** The template factory. */
46         private final TemplateFactory templateFactory;
47
48         /**
49          * Creates a new freenet link parser.
50          *
51          * @param templateFactory
52          *            The template factory
53          */
54         public FreenetLinkParser(TemplateFactory templateFactory) {
55                 this.templateFactory = templateFactory;
56         }
57
58         //
59         // PART METHODS
60         //
61
62         /**
63          * {@inheritDoc}
64          */
65         @Override
66         public Part parse(Reader source) throws IOException {
67                 PartContainer parts = new PartContainer();
68                 BufferedReader bufferedReader = (source instanceof BufferedReader) ? (BufferedReader) source : new BufferedReader(source);
69                 String line;
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));
79                                         break;
80                                 }
81                                 int next = Integer.MAX_VALUE;
82                                 if ((nextKsk > -1) && (nextKsk < next)) {
83                                         next = nextKsk;
84                                 }
85                                 if ((nextChk > -1) && (nextChk < next)) {
86                                         next = nextChk;
87                                 }
88                                 if ((nextSsk > -1) && (nextSsk < next)) {
89                                         next = nextSsk;
90                                 }
91                                 if ((nextUsk > -1) && (nextUsk < next)) {
92                                         next = nextUsk;
93                                 }
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);
99                                         String name = link;
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);
104                                         }
105                                         parts.add(createLinkPart(link, name));
106                                         line = line.substring(nextSpace);
107                                 } else {
108                                         parts.add(createPlainTextPart(line.substring(0, next + 4)));
109                                         line = line.substring(next + 4);
110                                 }
111                         }
112                 }
113                 return parts;
114         }
115
116         //
117         // PRIVATE METHODS
118         //
119
120         /**
121          * Creates a new plain text part based on a template.
122          *
123          * @param text
124          *            The text to display
125          * @return The part that displays the given text
126          */
127         private Part createPlainTextPart(String text) {
128                 return new TemplatePart(templateFactory.createTemplate(new StringReader("<% text|html>"))).set("text", text);
129         }
130
131         /**
132          * Creates a new link part based on a template.
133          *
134          * @param link
135          *            The target of the link
136          * @param name
137          *            The name of the link
138          * @return The part that displays the link
139          */
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);
142         }
143
144 }