From 8ee3de9a7ed7c65b5f695dd60da6e55d94c1125e Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Sat, 25 Jun 2016 22:24:07 +0200 Subject: [PATCH] Remove IOException from parser interface --- .../pterodactylus/sone/template/ParserFilter.java | 84 ++++++++++------------ .../java/net/pterodactylus/sone/text/Parser.java | 4 +- .../pterodactylus/sone/text/SoneTextParser.java | 5 +- .../net/pterodactylus/sone/web/WebInterface.java | 12 +--- 4 files changed, 46 insertions(+), 59 deletions(-) diff --git a/src/main/java/net/pterodactylus/sone/template/ParserFilter.java b/src/main/java/net/pterodactylus/sone/template/ParserFilter.java index 53862e3..ac092d8 100644 --- a/src/main/java/net/pterodactylus/sone/template/ParserFilter.java +++ b/src/main/java/net/pterodactylus/sone/template/ParserFilter.java @@ -101,41 +101,37 @@ public class ParserFilter implements Filter { FreenetRequest request = (FreenetRequest) templateContext.get("request"); SoneTextParserContext context = new SoneTextParserContext(request, (Sone) sone); StringWriter parsedTextWriter = new StringWriter(); - try { - Iterable parts = soneTextParser.parse(context, text); - if (length > -1) { - int allPartsLength = 0; - List shortenedParts = new ArrayList(); - for (Part part : parts) { - if (part instanceof PlainTextPart) { - String longText = part.getText(); - if (allPartsLength < cutOffLength) { - if ((allPartsLength + longText.length()) > cutOffLength) { - shortenedParts.add(new PlainTextPart(longText.substring(0, cutOffLength - allPartsLength) + "…")); - } else { - shortenedParts.add(part); - } - } - allPartsLength += longText.length(); - } else if (part instanceof LinkPart) { - if (allPartsLength < cutOffLength) { - shortenedParts.add(part); - } - allPartsLength += part.getText().length(); - } else { - if (allPartsLength < cutOffLength) { + Iterable parts = soneTextParser.parse(context, text); + if (length > -1) { + int allPartsLength = 0; + List shortenedParts = new ArrayList(); + for (Part part : parts) { + if (part instanceof PlainTextPart) { + String longText = part.getText(); + if (allPartsLength < cutOffLength) { + if ((allPartsLength + longText.length()) > cutOffLength) { + shortenedParts.add(new PlainTextPart(longText.substring(0, cutOffLength - allPartsLength) + "…")); + } else { shortenedParts.add(part); } } + allPartsLength += longText.length(); + } else if (part instanceof LinkPart) { + if (allPartsLength < cutOffLength) { + shortenedParts.add(part); + } + allPartsLength += part.getText().length(); + } else { + if (allPartsLength < cutOffLength) { + shortenedParts.add(part); + } } - if (allPartsLength >= length) { - parts = shortenedParts; - } } - render(parsedTextWriter, parts); - } catch (IOException ioe1) { - /* no exceptions in a StringReader or StringWriter, ignore. */ + if (allPartsLength >= length) { + parts = shortenedParts; + } } + render(parsedTextWriter, parts); return parsedTextWriter.toString(); } @@ -252,26 +248,22 @@ public class ParserFilter implements Filter { private void render(Writer writer, PostPart postPart) { SoneTextParser parser = new SoneTextParser(core, core); SoneTextParserContext parserContext = new SoneTextParserContext(null, postPart.getPost().getSone()); - try { - Iterable parts = parser.parse(parserContext, postPart.getPost().getText()); - StringBuilder excerpt = new StringBuilder(); - for (Part part : parts) { - excerpt.append(part.getText()); - if (excerpt.length() > 20) { - int lastSpace = excerpt.lastIndexOf(" ", 20); - if (lastSpace > -1) { - excerpt.setLength(lastSpace); - } else { - excerpt.setLength(20); - } - excerpt.append("…"); - break; + Iterable parts = parser.parse(parserContext, postPart.getPost().getText()); + StringBuilder excerpt = new StringBuilder(); + for (Part part : parts) { + excerpt.append(part.getText()); + if (excerpt.length() > 20) { + int lastSpace = excerpt.lastIndexOf(" ", 20); + if (lastSpace > -1) { + excerpt.setLength(lastSpace); + } else { + excerpt.setLength(20); } + excerpt.append("…"); + break; } - renderLink(writer, "viewPost.html?post=" + postPart.getPost().getId(), excerpt.toString(), SoneAccessor.getNiceName(postPart.getPost().getSone()), "in-sone"); - } catch (IOException ioe1) { - /* StringReader shouldn’t throw. */ } + renderLink(writer, "viewPost.html?post=" + postPart.getPost().getId(), excerpt.toString(), SoneAccessor.getNiceName(postPart.getPost().getSone()), "in-sone"); } /** diff --git a/src/main/java/net/pterodactylus/sone/text/Parser.java b/src/main/java/net/pterodactylus/sone/text/Parser.java index e654223..531efa7 100644 --- a/src/main/java/net/pterodactylus/sone/text/Parser.java +++ b/src/main/java/net/pterodactylus/sone/text/Parser.java @@ -38,9 +38,7 @@ public interface Parser { * @param source * The text source * @return The parsed parts - * @throws IOException - * if an I/O error occurs */ - public Iterable parse(C context, String source) throws IOException; + public Iterable parse(C context, String source); } diff --git a/src/main/java/net/pterodactylus/sone/text/SoneTextParser.java b/src/main/java/net/pterodactylus/sone/text/SoneTextParser.java index 18ac467..18dd53f 100644 --- a/src/main/java/net/pterodactylus/sone/text/SoneTextParser.java +++ b/src/main/java/net/pterodactylus/sone/text/SoneTextParser.java @@ -118,7 +118,7 @@ public class SoneTextParser implements Parser { * {@inheritDoc} */ @Override - public Iterable parse(SoneTextParserContext context, String source) throws IOException { + public Iterable parse(SoneTextParserContext context, String source) { PartContainer parts = new PartContainer(); BufferedReader bufferedReader = new BufferedReader(new StringReader(source)); try { @@ -271,6 +271,9 @@ public class SoneTextParser implements Parser { } lastLineEmpty = false; } + } catch (IOException ioe1) { + // a buffered reader around a string reader should never throw. + throw new RuntimeException(ioe1); } finally { Closer.close(bufferedReader); } diff --git a/src/main/java/net/pterodactylus/sone/web/WebInterface.java b/src/main/java/net/pterodactylus/sone/web/WebInterface.java index f08ad5d..cabc44e 100644 --- a/src/main/java/net/pterodactylus/sone/web/WebInterface.java +++ b/src/main/java/net/pterodactylus/sone/web/WebInterface.java @@ -21,7 +21,6 @@ import static com.google.common.collect.FluentIterable.from; import static java.util.logging.Logger.getLogger; import static net.pterodactylus.util.template.TemplateParser.parse; -import java.io.IOException; import java.io.StringReader; import java.util.ArrayList; import java.util.Collection; @@ -36,7 +35,6 @@ import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledFuture; import java.util.concurrent.TimeUnit; -import java.util.logging.Level; import java.util.logging.Logger; import javax.annotation.Nonnull; import javax.annotation.Nullable; @@ -795,14 +793,10 @@ public class WebInterface { private Collection getMentionedSones(String text) { /* we need no context to find mentioned Sones. */ Set mentionedSones = new HashSet(); - try { - for (Part part : soneTextParser.parse(null, text)) { - if (part instanceof SonePart) { - mentionedSones.add(((SonePart) part).getSone()); - } + for (Part part : soneTextParser.parse(null, text)) { + if (part instanceof SonePart) { + mentionedSones.add(((SonePart) part).getSone()); } - } catch (IOException ioe1) { - logger.log(Level.WARNING, String.format("Could not parse post text: %s", text), ioe1); } return Collections2.filter(mentionedSones, Sone.LOCAL_SONE_FILTER); } -- 2.7.4