34edc481e724265453df72d22ca4d698ac866204
[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         /**
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                                 Matcher matcher = whitespacePattern.matcher(line);
121                                 int nextSpace = matcher.find(next) ? matcher.start() : line.length();
122                                 if (nextSpace > (next + 4)) {
123                                         parts.add(createPlainTextPart(line.substring(0, next)));
124                                         String link = line.substring(next, nextSpace);
125                                         String name = link;
126                                         logger.log(Level.FINER, "Found link: " + link);
127                                         logger.log(Level.FINEST, "Next: %d, CHK: %d, SSK: %d, USK: %d", new Object[] { next, nextChk, nextSsk, nextUsk });
128                                         if (((linkType == LinkType.CHK) || (linkType == LinkType.SSK) || (linkType == LinkType.USK)) && (link.length() > 98) && (link.charAt(47) == ',') && (link.charAt(91) == ',') && (link.charAt(99) == '/')) {
129                                                 name = link.substring(0, 47) + "…" + link.substring(99);
130                                         }
131                                         parts.add(createLinkPart(link, name));
132                                         line = line.substring(nextSpace);
133                                 } else {
134                                         parts.add(createPlainTextPart(line.substring(0, next + 4)));
135                                         line = line.substring(next + 4);
136                                 }
137                         }
138                 }
139                 return parts;
140         }
141
142         //
143         // PRIVATE METHODS
144         //
145
146         /**
147          * Creates a new plain text part based on a template.
148          *
149          * @param text
150          *            The text to display
151          * @return The part that displays the given text
152          */
153         private Part createPlainTextPart(String text) {
154                 return new TemplatePart(templateFactory.createTemplate(new StringReader("<% text|html>"))).set("text", text);
155         }
156
157         /**
158          * Creates a new link part based on a template.
159          *
160          * @param link
161          *            The target of the link
162          * @param name
163          *            The name of the link
164          * @return The part that displays the link
165          */
166         private Part createLinkPart(String link, String name) {
167                 return new TemplatePart(templateFactory.createTemplate(new StringReader("<a href=\"/<% link|html>\"><% name|html></a>"))).set("link", link).set("name", name);
168         }
169
170 }