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 freenet.keys.FreenetURI;
*
* @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
*/
-public class FreenetLinkParser implements Parser {
+public class FreenetLinkParser implements Parser<FreenetLinkParserContext> {
/** The logger. */
private static final Logger logger = Logging.getLogger(FreenetLinkParser.class);
/** The template factory. */
private final TemplateFactory templateFactory;
- /** The Sone that posted the currently parsed text. */
- private Sone postingSone;
-
/**
* Creates a new freenet link parser.
*
}
//
- // 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
- */
- public void setPostingSone(Sone sone) {
- postingSone = sone;
- }
-
- //
// PART METHODS
//
* {@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;
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);