From: David ‘Bombe’ Roden Date: Sat, 25 Jun 2016 20:13:36 +0000 (+0200) Subject: Replace Reader with String in Parser interface X-Git-Tag: 0.9.5^2~22 X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=662bcd45433c0ab480ad0e52f3e72e9e886bdfc6;p=Sone.git Replace Reader with String in Parser interface --- diff --git a/src/main/java/net/pterodactylus/sone/template/ParserFilter.java b/src/main/java/net/pterodactylus/sone/template/ParserFilter.java index 109222e..6c74f0b 100644 --- a/src/main/java/net/pterodactylus/sone/template/ParserFilter.java +++ b/src/main/java/net/pterodactylus/sone/template/ParserFilter.java @@ -102,7 +102,7 @@ public class ParserFilter implements Filter { SoneTextParserContext context = new SoneTextParserContext(request, (Sone) sone); StringWriter parsedTextWriter = new StringWriter(); try { - Iterable parts = soneTextParser.parse(context, new StringReader(text)); + Iterable parts = soneTextParser.parse(context, text); if (length > -1) { int allPartsLength = 0; List shortenedParts = new ArrayList(); @@ -253,7 +253,7 @@ public class ParserFilter implements Filter { SoneTextParser parser = new SoneTextParser(core, core); SoneTextParserContext parserContext = new SoneTextParserContext(null, postPart.getPost().getSone()); try { - Iterable parts = parser.parse(parserContext, new StringReader(postPart.getPost().getText())); + Iterable parts = parser.parse(parserContext, postPart.getPost().getText()); StringBuilder excerpt = new StringBuilder(); for (Part part : parts) { excerpt.append(part.getText()); diff --git a/src/main/java/net/pterodactylus/sone/text/Parser.java b/src/main/java/net/pterodactylus/sone/text/Parser.java index f5f8f1b..e654223 100644 --- a/src/main/java/net/pterodactylus/sone/text/Parser.java +++ b/src/main/java/net/pterodactylus/sone/text/Parser.java @@ -41,6 +41,6 @@ public interface Parser { * @throws IOException * if an I/O error occurs */ - public Iterable parse(C context, Reader source) throws IOException; + public Iterable parse(C context, String 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 index 9782008..da60a72 100644 --- a/src/main/java/net/pterodactylus/sone/text/ParserContext.java +++ b/src/main/java/net/pterodactylus/sone/text/ParserContext.java @@ -20,7 +20,7 @@ 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)}. + * parse to {@link Parser#parse(ParserContext, String)}. * * @author David ‘Bombe’ Roden */ diff --git a/src/main/java/net/pterodactylus/sone/text/SoneTextParser.java b/src/main/java/net/pterodactylus/sone/text/SoneTextParser.java index f338286..18ac467 100644 --- a/src/main/java/net/pterodactylus/sone/text/SoneTextParser.java +++ b/src/main/java/net/pterodactylus/sone/text/SoneTextParser.java @@ -21,7 +21,7 @@ import static java.util.logging.Logger.getLogger; import java.io.BufferedReader; import java.io.IOException; -import java.io.Reader; +import java.io.StringReader; import java.net.MalformedURLException; import java.util.logging.Level; import java.util.logging.Logger; @@ -118,9 +118,9 @@ public class SoneTextParser implements Parser { * {@inheritDoc} */ @Override - public Iterable parse(SoneTextParserContext context, Reader source) throws IOException { + public Iterable parse(SoneTextParserContext context, String source) throws IOException { PartContainer parts = new PartContainer(); - BufferedReader bufferedReader = (source instanceof BufferedReader) ? (BufferedReader) source : new BufferedReader(source); + BufferedReader bufferedReader = new BufferedReader(new StringReader(source)); try { String line; boolean lastLineEmpty = true; @@ -272,9 +272,7 @@ public class SoneTextParser implements Parser { lastLineEmpty = false; } } finally { - if (bufferedReader != source) { - Closer.close(bufferedReader); - } + Closer.close(bufferedReader); } for (int partIndex = parts.size() - 1; partIndex >= 0; --partIndex) { Part part = parts.getPart(partIndex); diff --git a/src/main/java/net/pterodactylus/sone/web/WebInterface.java b/src/main/java/net/pterodactylus/sone/web/WebInterface.java index 4185c93..f08ad5d 100644 --- a/src/main/java/net/pterodactylus/sone/web/WebInterface.java +++ b/src/main/java/net/pterodactylus/sone/web/WebInterface.java @@ -796,7 +796,7 @@ public class WebInterface { /* we need no context to find mentioned Sones. */ Set mentionedSones = new HashSet(); try { - for (Part part : soneTextParser.parse(null, new StringReader(text))) { + for (Part part : soneTextParser.parse(null, text)) { if (part instanceof SonePart) { mentionedSones.add(((SonePart) part).getSone()); } diff --git a/src/test/java/net/pterodactylus/sone/text/SoneTextParserTest.java b/src/test/java/net/pterodactylus/sone/text/SoneTextParserTest.java index 051c235..b630ac5 100644 --- a/src/test/java/net/pterodactylus/sone/text/SoneTextParserTest.java +++ b/src/test/java/net/pterodactylus/sone/text/SoneTextParserTest.java @@ -53,17 +53,17 @@ public class SoneTextParserTest extends TestCase { Iterable parts; /* check basic operation. */ - parts = soneTextParser.parse(null, new StringReader("Test.")); + parts = soneTextParser.parse(null, "Test."); assertNotNull("Parts", parts); assertEquals("Part Text", "Test.", convertText(parts, PlainTextPart.class)); /* check empty lines at start and end. */ - parts = soneTextParser.parse(null, new StringReader("\nTest.\n\n")); + parts = soneTextParser.parse(null, "\nTest.\n\n"); assertNotNull("Parts", parts); assertEquals("Part Text", "Test.", convertText(parts, PlainTextPart.class)); /* check duplicate empty lines in the text. */ - parts = soneTextParser.parse(null, new StringReader("\nTest.\n\n\nTest.")); + parts = soneTextParser.parse(null, "\nTest.\n\n\nTest."); assertNotNull("Parts", parts); assertEquals("Part Text", "Test.\n\nTest.", convertText(parts, PlainTextPart.class)); } @@ -80,17 +80,17 @@ public class SoneTextParserTest extends TestCase { Iterable parts; /* check basic links. */ - parts = soneTextParser.parse(null, new StringReader("KSK@gpl.txt")); + parts = soneTextParser.parse(null, "KSK@gpl.txt"); assertNotNull("Parts", parts); assertEquals("Part Text", "[KSK@gpl.txt|gpl.txt|gpl.txt]", convertText(parts, FreenetLinkPart.class)); /* check embedded links. */ - parts = soneTextParser.parse(null, new StringReader("Link is KSK@gpl.txt\u200b.")); + parts = soneTextParser.parse(null, "Link is KSK@gpl.txt\u200b."); assertNotNull("Parts", parts); assertEquals("Part Text", "Link is [KSK@gpl.txt|gpl.txt|gpl.txt]\u200b.", convertText(parts, PlainTextPart.class, FreenetLinkPart.class)); /* check embedded links and line breaks. */ - parts = soneTextParser.parse(null, new StringReader("Link is KSK@gpl.txt\nKSK@test.dat\n")); + parts = soneTextParser.parse(null, "Link is KSK@gpl.txt\nKSK@test.dat\n"); assertNotNull("Parts", parts); assertEquals("Part Text", "Link is [KSK@gpl.txt|gpl.txt|gpl.txt]\n[KSK@test.dat|test.dat|test.dat]", convertText(parts, PlainTextPart.class, FreenetLinkPart.class)); } @@ -107,7 +107,7 @@ public class SoneTextParserTest extends TestCase { Iterable parts; /* check basic links. */ - parts = soneTextParser.parse(null, new StringReader("Some text.\n\nLink to sone://DAxKQzS48mtaQc7sUVHIgx3fnWZPQBz0EueBreUVWrU and stuff.")); + parts = soneTextParser.parse(null, "Some text.\n\nLink to sone://DAxKQzS48mtaQc7sUVHIgx3fnWZPQBz0EueBreUVWrU and stuff."); assertNotNull("Parts", parts); assertEquals("Part Text", "Some text.\n\nLink to [Sone|DAxKQzS48mtaQc7sUVHIgx3fnWZPQBz0EueBreUVWrU] and stuff.", convertText(parts, PlainTextPart.class, SonePart.class)); } @@ -125,7 +125,7 @@ public class SoneTextParserTest extends TestCase { Iterable parts; /* check empty http links. */ - parts = soneTextParser.parse(null, new StringReader("Some text. Empty link: http:// – nice!")); + parts = soneTextParser.parse(null, "Some text. Empty link: http:// – nice!"); assertNotNull("Parts", parts); assertEquals("Part Text", "Some text. Empty link: http:// – nice!", convertText(parts, PlainTextPart.class)); }