From 169711a171fc44506ef3e41efcc17331c1791e8f Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Fri, 7 Jan 2011 22:33:33 +0100 Subject: [PATCH] Enhance Parser with a ParserContext. --- .../pterodactylus/sone/template/PostAccessor.java | 6 +-- .../pterodactylus/sone/template/ReplyAccessor.java | 6 +-- .../pterodactylus/sone/text/FreenetLinkParser.java | 26 ++--------- .../sone/text/FreenetLinkParserContext.java | 53 ++++++++++++++++++++++ .../java/net/pterodactylus/sone/text/Parser.java | 8 +++- .../net/pterodactylus/sone/text/ParserContext.java | 15 ++++++ 6 files changed, 81 insertions(+), 33 deletions(-) create mode 100644 src/main/java/net/pterodactylus/sone/text/FreenetLinkParserContext.java create mode 100644 src/main/java/net/pterodactylus/sone/text/ParserContext.java diff --git a/src/main/java/net/pterodactylus/sone/template/PostAccessor.java b/src/main/java/net/pterodactylus/sone/template/PostAccessor.java index d5dd406..95dc614 100644 --- a/src/main/java/net/pterodactylus/sone/template/PostAccessor.java +++ b/src/main/java/net/pterodactylus/sone/template/PostAccessor.java @@ -24,6 +24,7 @@ import net.pterodactylus.sone.core.Core; import net.pterodactylus.sone.data.Post; import net.pterodactylus.sone.data.Sone; import net.pterodactylus.sone.text.FreenetLinkParser; +import net.pterodactylus.sone.text.FreenetLinkParserContext; import net.pterodactylus.util.template.DataProvider; import net.pterodactylus.util.template.ReflectionAccessor; import net.pterodactylus.util.template.TemplateFactory; @@ -79,10 +80,7 @@ public class PostAccessor extends ReflectionAccessor { return null; } try { - synchronized (linkParser) { - linkParser.setPostingSone(post.getSone()); - return linkParser.parse(new StringReader(text)); - } + return linkParser.parse(new FreenetLinkParserContext(post.getSone()), new StringReader(text)); } catch (IOException ioe1) { /* ignore. */ } diff --git a/src/main/java/net/pterodactylus/sone/template/ReplyAccessor.java b/src/main/java/net/pterodactylus/sone/template/ReplyAccessor.java index 715b87f..db6c528 100644 --- a/src/main/java/net/pterodactylus/sone/template/ReplyAccessor.java +++ b/src/main/java/net/pterodactylus/sone/template/ReplyAccessor.java @@ -24,6 +24,7 @@ import net.pterodactylus.sone.core.Core; import net.pterodactylus.sone.data.Reply; import net.pterodactylus.sone.data.Sone; import net.pterodactylus.sone.text.FreenetLinkParser; +import net.pterodactylus.sone.text.FreenetLinkParserContext; import net.pterodactylus.util.template.Accessor; import net.pterodactylus.util.template.DataProvider; import net.pterodactylus.util.template.ReflectionAccessor; @@ -72,10 +73,7 @@ public class ReplyAccessor extends ReflectionAccessor { } else if (member.equals("text")) { String text = reply.getText(); try { - synchronized (linkParser) { - linkParser.setPostingSone(reply.getSone()); - return linkParser.parse(new StringReader(text)); - } + return linkParser.parse(new FreenetLinkParserContext(reply.getSone()), new StringReader(text)); } catch (IOException ioe1) { /* ignore. */ } diff --git a/src/main/java/net/pterodactylus/sone/text/FreenetLinkParser.java b/src/main/java/net/pterodactylus/sone/text/FreenetLinkParser.java index b50113d..fe602c6 100644 --- a/src/main/java/net/pterodactylus/sone/text/FreenetLinkParser.java +++ b/src/main/java/net/pterodactylus/sone/text/FreenetLinkParser.java @@ -27,7 +27,6 @@ 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 freenet.keys.FreenetURI; @@ -37,7 +36,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); @@ -75,9 +74,6 @@ public class FreenetLinkParser implements Parser { /** The template factory. */ private final TemplateFactory templateFactory; - /** The Sone that posted the currently parsed text. */ - private Sone postingSone; - /** * Creates a new freenet link parser. * @@ -89,22 +85,6 @@ public class FreenetLinkParser implements 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 // @@ -112,7 +92,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; @@ -189,7 +169,7 @@ public class FreenetLinkParser implements Parser { if ((linkType == LinkType.SSK) || (linkType == LinkType.USK)) { try { new FreenetURI(link); - fromPostingSone = link.substring(4, 47).equals(postingSone.getId()); + fromPostingSone = link.substring(4, 47).equals(context.getPostingSone().getId()); parts.add(fromPostingSone ? createTrustedFreenetLinkPart(link, name) : createFreenetLinkPart(link, name)); } catch (MalformedURLException mue1) { /* it’s not a valid link. */ diff --git a/src/main/java/net/pterodactylus/sone/text/FreenetLinkParserContext.java b/src/main/java/net/pterodactylus/sone/text/FreenetLinkParserContext.java new file mode 100644 index 0000000..e524d91 --- /dev/null +++ b/src/main/java/net/pterodactylus/sone/text/FreenetLinkParserContext.java @@ -0,0 +1,53 @@ +/* + * Sone - FreenetLinkParserContext.java - Copyright © 2011 David Roden + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package net.pterodactylus.sone.text; + +import net.pterodactylus.sone.data.Sone; + +/** + * {@link ParserContext} implementation for the {@link FreenetLinkParser}. It + * stores the {@link Sone} that provided the parsed text so that certain links + * can be marked in a different way. + * + * @author David ‘Bombe’ Roden + */ +public class FreenetLinkParserContext implements ParserContext { + + /** The posting Sone. */ + private final Sone postingSone; + + /** + * Creates a new link parser context. + * + * @param postingSone + * The posting Sone + */ + public FreenetLinkParserContext(Sone postingSone) { + this.postingSone = postingSone; + } + + /** + * Returns the Sone that provided the text that is being parsed. + * + * @return The posting Sone + */ + public Sone getPostingSone() { + return postingSone; + } + +} diff --git a/src/main/java/net/pterodactylus/sone/text/Parser.java b/src/main/java/net/pterodactylus/sone/text/Parser.java index ccec36a..e43ed47 100644 --- a/src/main/java/net/pterodactylus/sone/text/Parser.java +++ b/src/main/java/net/pterodactylus/sone/text/Parser.java @@ -24,19 +24,23 @@ import java.io.Reader; * Interface for parsers that can create {@link Part}s from a text source * (usually a {@link Reader}). * + * @param + * The type of the parser context * @author David ‘Bombe’ Roden */ -public interface Parser { +public interface Parser { /** * Create a {@link Part} from the given text source. * + * @param context + * The parser context * @param source * The text source * @return The parsed part * @throws IOException * if an I/O error occurs */ - public Part parse(Reader source) throws IOException; + public Part parse(C context, Reader source) throws IOException; } diff --git a/src/main/java/net/pterodactylus/sone/text/ParserContext.java b/src/main/java/net/pterodactylus/sone/text/ParserContext.java new file mode 100644 index 0000000..7f38ad8 --- /dev/null +++ b/src/main/java/net/pterodactylus/sone/text/ParserContext.java @@ -0,0 +1,15 @@ + +package net.pterodactylus.sone.text; + +/** + * Context for the {@link Parser}. This interface needs to be implemented by + * {@link Parser}s that need to provide more information than just the text to + * parse to {@link Parser#parse(ParserContext, java.io.Reader)}. + * + * @author David ‘Bombe’ Roden + */ +public interface ParserContext { + + /* nothing to see. */ + +} -- 2.7.4