f36b6df81f43cc71465e6b678bf66b2ba6c6efe3
[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("[\u0020\u00a0\u1680\u180e\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u200b\u200c\u200d\u202f\u205f\u2060\u2800\u3000]");
44
45         /**
46          * Enumeration for all recognized link types.
47          *
48          * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
49          */
50         private enum LinkType {
51
52                 /** Link is a KSK. */
53                 KSK,
54
55                 /** Link is a CHK. */
56                 CHK,
57
58                 /** Link is an SSK. */
59                 SSK,
60
61                 /** Link is a USK. */
62                 USK
63
64         }
65
66         /** The template factory. */
67         private final TemplateFactory templateFactory;
68
69         /**
70          * Creates a new freenet link parser.
71          *
72          * @param templateFactory
73          *            The template factory
74          */
75         public FreenetLinkParser(TemplateFactory templateFactory) {
76                 this.templateFactory = templateFactory;
77         }
78
79         //
80         // PART METHODS
81         //
82
83         /**
84          * {@inheritDoc}
85          */
86         @Override
87         public Part parse(Reader source) throws IOException {
88                 PartContainer parts = new PartContainer();
89                 BufferedReader bufferedReader = (source instanceof BufferedReader) ? (BufferedReader) source : new BufferedReader(source);
90                 String line;
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));
100                                         break;
101                                 }
102                                 int next = Integer.MAX_VALUE;
103                                 LinkType linkType = null;
104                                 if ((nextKsk > -1) && (nextKsk < next)) {
105                                         next = nextKsk;
106                                         linkType = LinkType.KSK;
107                                 }
108                                 if ((nextChk > -1) && (nextChk < next)) {
109                                         next = nextChk;
110                                         linkType = LinkType.CHK;
111                                 }
112                                 if ((nextSsk > -1) && (nextSsk < next)) {
113                                         next = nextSsk;
114                                         linkType = LinkType.SSK;
115                                 }
116                                 if ((nextUsk > -1) && (nextUsk < next)) {
117                                         next = nextUsk;
118                                         linkType = LinkType.USK;
119                                 }
120                                 if ((next >= 8) && (line.substring(next - 8, next).equals("freenet:"))) {
121                                         next -= 8;
122                                         line = line.substring(0, next) + line.substring(next + 8);
123                                 }
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);
129                                         String name = link;
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);
134                                         }
135                                         parts.add(createLinkPart(link, name));
136                                         line = line.substring(nextSpace);
137                                 } else {
138                                         parts.add(createPlainTextPart(line.substring(0, next + 4)));
139                                         line = line.substring(next + 4);
140                                 }
141                         }
142                 }
143                 return parts;
144         }
145
146         //
147         // PRIVATE METHODS
148         //
149
150         /**
151          * Creates a new plain text part based on a template.
152          *
153          * @param text
154          *            The text to display
155          * @return The part that displays the given text
156          */
157         private Part createPlainTextPart(String text) {
158                 return new TemplatePart(templateFactory.createTemplate(new StringReader("<% text|html>"))).set("text", text);
159         }
160
161         /**
162          * Creates a new link part based on a template.
163          *
164          * @param link
165          *            The target of the link
166          * @param name
167          *            The name of the link
168          * @return The part that displays the link
169          */
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);
172         }
173
174 }