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;
name = name.substring(name.lastIndexOf('/', name.lastIndexOf('/') - 1));
} else {
/* shorten to 5 chars. */
- name = name.substring(4, 9);
+ name = name.substring(4, Math.min(9, name.length()));
}
}
}
if ((linkType == LinkType.SSK) || (linkType == LinkType.USK)) {
try {
new FreenetURI(link);
- fromPostingSone = link.substring(4, 47).equals(postingSone.getId());
+ fromPostingSone = link.substring(4, Math.min(link.length(), 47)).equals(context.getPostingSone().getId());
parts.add(fromPostingSone ? createTrustedFreenetLinkPart(link, name) : createFreenetLinkPart(link, name));
} catch (MalformedURLException mue1) {
/* it’s not a valid link. */