X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;ds=inline;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Ftext%2FFreenetLinkParser.java;h=704792caa11a5b9cbc0278b241d9ac465c2ff6ef;hb=eece10daa3b076ea97a99f810c4c5350094d8bf9;hp=fbf87c8eb6f2f26b90fbf8900927b952eb15cdb1;hpb=a5a01001dea42270864ba51345f323d710602f0b;p=Sone.git
diff --git a/src/main/java/net/pterodactylus/sone/text/FreenetLinkParser.java b/src/main/java/net/pterodactylus/sone/text/FreenetLinkParser.java
index fbf87c8..704792c 100644
--- a/src/main/java/net/pterodactylus/sone/text/FreenetLinkParser.java
+++ b/src/main/java/net/pterodactylus/sone/text/FreenetLinkParser.java
@@ -27,9 +27,9 @@ import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
-import net.pterodactylus.sone.data.Sone;
import net.pterodactylus.util.logging.Logging;
-import net.pterodactylus.util.template.TemplateFactory;
+import net.pterodactylus.util.template.TemplateContextFactory;
+import net.pterodactylus.util.template.TemplateParser;
import freenet.keys.FreenetURI;
/**
@@ -37,7 +37,7 @@ import freenet.keys.FreenetURI;
*
* @author David âBombeâ Roden
*/
-public class FreenetLinkParser implements Parser {
+public class FreenetLinkParser implements Parser {
/** The logger. */
private static final Logger logger = Logging.getLogger(FreenetLinkParser.class);
@@ -73,35 +73,16 @@ public class FreenetLinkParser implements Parser {
}
/** The template factory. */
- private final TemplateFactory templateFactory;
-
- /** The Sone that posted the currently parsed text. */
- private Sone postingSone;
+ private final TemplateContextFactory templateContextFactory;
/**
* Creates a new freenet link parser.
*
- * @param templateFactory
- * The template factory
- */
- public FreenetLinkParser(TemplateFactory templateFactory) {
- this.templateFactory = templateFactory;
- }
-
- //
- // ACCESSORS
- //
-
- /**
- * Sets the Sone that posted the text that will be parsed in the next call
- * to {@link #parse(Reader)}. You need to synchronize calling this method
- * and {@link #parse(Reader)}!
- *
- * @param sone
- * The Sone that posted the text
+ * @param templateContextFactory
+ * The template context factory
*/
- public void setPostingSone(Sone sone) {
- postingSone = sone;
+ public FreenetLinkParser(TemplateContextFactory templateContextFactory) {
+ this.templateContextFactory = templateContextFactory;
}
//
@@ -112,7 +93,7 @@ public class FreenetLinkParser implements Parser {
* {@inheritDoc}
*/
@Override
- public Part parse(Reader source) throws IOException {
+ public Part parse(FreenetLinkParserContext context, Reader source) throws IOException {
PartContainer parts = new PartContainer();
BufferedReader bufferedReader = (source instanceof BufferedReader) ? (BufferedReader) source : new BufferedReader(source);
String line;
@@ -167,36 +148,35 @@ public class FreenetLinkParser implements Parser {
String name = link;
logger.log(Level.FINER, "Found link: %s", link);
logger.log(Level.FINEST, "Next: %d, CHK: %d, SSK: %d, USK: %d", new Object[] { next, nextChk, nextSsk, nextUsk });
- if (linkType == LinkType.KSK) {
- name = link.substring(4);
- } else if ((linkType == LinkType.CHK) || (linkType == LinkType.SSK) || (linkType == LinkType.USK)) {
- if (name.indexOf('/') > -1) {
- if (!name.endsWith("/")) {
- name = name.substring(name.lastIndexOf('/') + 1);
- } else {
- if (name.indexOf('/') != name.lastIndexOf('/')) {
- name = name.substring(name.lastIndexOf('/', name.lastIndexOf('/') - 1));
- } else {
- /* shorten to 5 chars. */
- name = name.substring(4, 9);
- }
- }
- }
+
+ if ((linkType == LinkType.KSK) || (linkType == LinkType.CHK) || (linkType == LinkType.SSK) || (linkType == LinkType.USK)) {
+ FreenetURI uri;
if (name.indexOf('?') > -1) {
name = name.substring(0, name.indexOf('?'));
}
- boolean fromPostingSone = false;
- if ((linkType == LinkType.SSK) || (linkType == LinkType.USK)) {
- try {
- new FreenetURI(link);
- fromPostingSone = link.substring(4, 47).equals(postingSone.getId());
- parts.add(fromPostingSone ? createTrustedFreenetLinkPart(link, name) : createFreenetLinkPart(link, name));
- } catch (MalformedURLException mue1) {
- /* itâs not a valid link. */
- parts.add(createPlainTextPart(link));
+ if (name.endsWith("/")) {
+ name = name.substring(0, name.length() - 1);
+ }
+ try {
+ uri = new FreenetURI(name);
+ name = uri.lastMetaString();
+ if (name == null) {
+ name = uri.getDocName();
+ }
+ if (name == null) {
+ name = link.substring(0, Math.min(9, link.length()));
}
- } else {
+ boolean fromPostingSone = ((linkType == LinkType.SSK) || (linkType == LinkType.USK)) && link.substring(4, Math.min(link.length(), 47)).equals(context.getPostingSone().getId());
parts.add(fromPostingSone ? createTrustedFreenetLinkPart(link, name) : createFreenetLinkPart(link, name));
+ } catch (MalformedURLException mue1) {
+ /* not a valid link, insert as plain text. */
+ parts.add(createPlainTextPart(link));
+ } catch (NullPointerException npe1) {
+ /* FreenetURI sometimes throws these, too. */
+ parts.add(createPlainTextPart(link));
+ } catch (ArrayIndexOutOfBoundsException aioobe1) {
+ /* oh, and these, too. */
+ parts.add(createPlainTextPart(link));
}
} else if ((linkType == LinkType.HTTP) || (linkType == LinkType.HTTPS)) {
name = link.substring(linkType == LinkType.HTTP ? 7 : 8);
@@ -239,7 +219,7 @@ public class FreenetLinkParser implements Parser {
* @return The part that displays the given text
*/
private Part createPlainTextPart(String text) {
- return new TemplatePart(templateFactory.createTemplate(new StringReader("<% text|html>"))).set("text", text);
+ return new TemplatePart(templateContextFactory, TemplateParser.parse(new StringReader("<% text|html>"))).set("text", text);
}
/**
@@ -253,7 +233,7 @@ public class FreenetLinkParser implements Parser {
* @return The part that displays the link
*/
private Part createInternetLinkPart(String link, String name) {
- return new TemplatePart(templateFactory.createTemplate(new StringReader("\" title=\"<% link|html>\"><% name|html>"))).set("link", link).set("name", name);
+ return new TemplatePart(templateContextFactory, TemplateParser.parse(new StringReader("\" title=\"<% link|html>\"><% name|html>"))).set("link", link).set("name", name);
}
/**
@@ -267,7 +247,7 @@ public class FreenetLinkParser implements Parser {
* @return The part that displays the link
*/
private Part createFreenetLinkPart(String link, String name) {
- return new TemplatePart(templateFactory.createTemplate(new StringReader("\" title=\"<% link|html>\"><% name|html>"))).set("link", link).set("name", name);
+ return new TemplatePart(templateContextFactory, TemplateParser.parse(new StringReader("\" title=\"<% link|html>\"><% name|html>"))).set("link", link).set("name", name);
}
/**
@@ -281,7 +261,7 @@ public class FreenetLinkParser implements Parser {
* @return The part that displays the link
*/
private Part createTrustedFreenetLinkPart(String link, String name) {
- return new TemplatePart(templateFactory.createTemplate(new StringReader("\" title=\"<% link|html>\"><% name|html>"))).set("link", link).set("name", name);
+ return new TemplatePart(templateContextFactory, TemplateParser.parse(new StringReader("\" title=\"<% link|html>\"><% name|html>"))).set("link", link).set("name", name);
}
}