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;
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. */
}
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;
} 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. */
}
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;
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. */
--- /dev/null
+/*
+ * 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 <http://www.gnu.org/licenses/>.
+ */
+
+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 <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+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;
+ }
+
+}
* Interface for parsers that can create {@link Part}s from a text source
* (usually a {@link Reader}).
*
+ * @param <C>
+ * The type of the parser context
* @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
*/
-public interface Parser {
+public interface Parser<C extends ParserContext> {
/**
* 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;
}
--- /dev/null
+
+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 <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public interface ParserContext {
+
+ /* nothing to see. */
+
+}