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.net.MalformedURLException;
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.sone.core.PostProvider;
30 import net.pterodactylus.sone.core.SoneProvider;
31 import net.pterodactylus.sone.data.Post;
32 import net.pterodactylus.sone.data.Sone;
33 import net.pterodactylus.util.logging.Logging;
34 import freenet.keys.FreenetURI;
37 * {@link Parser} implementation that can recognize Freenet URIs.
39 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
41 public class SoneTextParser implements Parser<SoneTextParserContext> {
44 private static final Logger logger = Logging.getLogger(SoneTextParser.class);
46 /** Pattern to detect whitespace. */
47 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]");
50 * Enumeration for all recognized link types.
52 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
54 private enum LinkType {
62 /** Link is an SSK. */
74 /** Link is a Sone. */
77 /** Link is a post. */
82 /** The Sone provider. */
83 private final SoneProvider soneProvider;
85 /** The post provider. */
86 private final PostProvider postProvider;
89 * Creates a new freenet link parser.
96 public SoneTextParser(SoneProvider soneProvider, PostProvider postProvider) {
97 this.soneProvider = soneProvider;
98 this.postProvider = postProvider;
109 public Iterable<Part> parse(SoneTextParserContext context, Reader source) throws IOException {
110 PartContainer parts = new PartContainer();
111 BufferedReader bufferedReader = (source instanceof BufferedReader) ? (BufferedReader) source : new BufferedReader(source);
113 boolean lastLineEmpty = true;
115 while ((line = bufferedReader.readLine()) != null) {
116 if (line.trim().length() == 0) {
120 parts.add(new PlainTextPart("\n"));
122 lastLineEmpty = emptyLines == 2;
126 boolean lineComplete = true;
127 while (line.length() > 0) {
128 int nextKsk = line.indexOf("KSK@");
129 int nextChk = line.indexOf("CHK@");
130 int nextSsk = line.indexOf("SSK@");
131 int nextUsk = line.indexOf("USK@");
132 int nextHttp = line.indexOf("http://");
133 int nextHttps = line.indexOf("https://");
134 int nextSone = line.indexOf("sone://");
135 int nextPost = line.indexOf("post://");
136 if ((nextKsk == -1) && (nextChk == -1) && (nextSsk == -1) && (nextUsk == -1) && (nextHttp == -1) && (nextHttps == -1) && (nextSone == -1) && (nextPost == -1)) {
137 if (lineComplete && !lastLineEmpty) {
138 parts.add(new PlainTextPart("\n" + line));
140 parts.add(new PlainTextPart(line));
144 int next = Integer.MAX_VALUE;
145 LinkType linkType = null;
146 if ((nextKsk > -1) && (nextKsk < next)) {
148 linkType = LinkType.KSK;
150 if ((nextChk > -1) && (nextChk < next)) {
152 linkType = LinkType.CHK;
154 if ((nextSsk > -1) && (nextSsk < next)) {
156 linkType = LinkType.SSK;
158 if ((nextUsk > -1) && (nextUsk < next)) {
160 linkType = LinkType.USK;
162 if ((nextHttp > -1) && (nextHttp < next)) {
164 linkType = LinkType.HTTP;
166 if ((nextHttps > -1) && (nextHttps < next)) {
168 linkType = LinkType.HTTPS;
170 if ((nextSone > -1) && (nextSone < next)) {
172 linkType = LinkType.SONE;
174 if ((nextPost > -1) && (nextPost < next)) {
176 linkType = LinkType.POST;
178 if (linkType == LinkType.SONE) {
180 parts.add(new PlainTextPart(line.substring(0, next)));
182 if (line.length() >= (next + 7 + 43)) {
183 String soneId = line.substring(next + 7, next + 50);
184 Sone sone = soneProvider.getSone(soneId, false);
186 parts.add(new SonePart(sone));
188 parts.add(new PlainTextPart(line.substring(next, next + 50)));
190 line = line.substring(next + 50);
192 parts.add(new PlainTextPart(line.substring(next)));
197 if (linkType == LinkType.POST) {
199 parts.add(new PlainTextPart(line.substring(0, next)));
201 if (line.length() >= (next + 7 + 36)) {
202 String postId = line.substring(next + 7, next + 43);
203 Post post = postProvider.getPost(postId, false);
204 if ((post != null) && (post.getSone() != null)) {
205 String postText = post.getText();
206 postText = postText.substring(0, Math.min(postText.length(), 20)) + "…";
207 parts.add(new PostPart(post));
209 parts.add(new PlainTextPart(line.substring(next, next + 43)));
211 line = line.substring(next + 43);
213 parts.add(new PlainTextPart(line.substring(next)));
218 if ((next >= 8) && (line.substring(next - 8, next).equals("freenet:"))) {
220 line = line.substring(0, next) + line.substring(next + 8);
222 Matcher matcher = whitespacePattern.matcher(line);
223 int nextSpace = matcher.find(next) ? matcher.start() : line.length();
224 if (nextSpace > (next + 4)) {
225 if (!lastLineEmpty && lineComplete) {
226 parts.add(new PlainTextPart("\n" + line.substring(0, next)));
229 parts.add(new PlainTextPart(line.substring(0, next)));
232 String link = line.substring(next, nextSpace);
234 logger.log(Level.FINER, "Found link: %s", link);
235 logger.log(Level.FINEST, "Next: %d, CHK: %d, SSK: %d, USK: %d", new Object[] { next, nextChk, nextSsk, nextUsk });
237 if ((linkType == LinkType.KSK) || (linkType == LinkType.CHK) || (linkType == LinkType.SSK) || (linkType == LinkType.USK)) {
239 if (name.indexOf('?') > -1) {
240 name = name.substring(0, name.indexOf('?'));
242 if (name.endsWith("/")) {
243 name = name.substring(0, name.length() - 1);
246 uri = new FreenetURI(name);
247 name = uri.lastMetaString();
249 name = uri.getDocName();
252 name = link.substring(0, Math.min(9, link.length()));
254 boolean fromPostingSone = ((linkType == LinkType.SSK) || (linkType == LinkType.USK)) && (context != null) && (context.getPostingSone() != null) && link.substring(4, Math.min(link.length(), 47)).equals(context.getPostingSone().getId());
255 parts.add(new FreenetLinkPart(link, name, fromPostingSone));
256 } catch (MalformedURLException mue1) {
257 /* not a valid link, insert as plain text. */
258 parts.add(new PlainTextPart(link));
259 } catch (NullPointerException npe1) {
260 /* FreenetURI sometimes throws these, too. */
261 parts.add(new PlainTextPart(link));
262 } catch (ArrayIndexOutOfBoundsException aioobe1) {
263 /* oh, and these, too. */
264 parts.add(new PlainTextPart(link));
266 } else if ((linkType == LinkType.HTTP) || (linkType == LinkType.HTTPS)) {
267 name = link.substring(linkType == LinkType.HTTP ? 7 : 8);
268 int firstSlash = name.indexOf('/');
269 int lastSlash = name.lastIndexOf('/');
270 if ((lastSlash - firstSlash) > 3) {
271 name = name.substring(0, firstSlash + 1) + "…" + name.substring(lastSlash);
273 if (name.endsWith("/")) {
274 name = name.substring(0, name.length() - 1);
276 if (((name.indexOf('/') > -1) && (name.indexOf('.') < name.lastIndexOf('.', name.indexOf('/'))) || ((name.indexOf('/') == -1) && (name.indexOf('.') < name.lastIndexOf('.')))) && name.startsWith("www.")) {
277 name = name.substring(4);
279 if (name.indexOf('?') > -1) {
280 name = name.substring(0, name.indexOf('?'));
282 parts.add(new LinkPart(link, name));
284 line = line.substring(nextSpace);
286 if (!lastLineEmpty && lineComplete) {
287 parts.add(new PlainTextPart("\n" + line.substring(0, next + 4)));
289 parts.add(new PlainTextPart(line.substring(0, next + 4)));
291 line = line.substring(next + 4);
293 lineComplete = false;
295 lastLineEmpty = false;
297 for (int partIndex = parts.size() - 1; partIndex >= 0; --partIndex) {
298 Part part = parts.getPart(partIndex);
299 if (!(part instanceof PlainTextPart) || !"\n".equals(((PlainTextPart) part).getText())) {
302 parts.removePart(partIndex);