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 net.pterodactylus.sone.data.impl.IdOnlySone;
import net.pterodactylus.sone.database.PostProvider;
import net.pterodactylus.sone.database.SoneProvider;
-import net.pterodactylus.util.io.Closer;
import com.google.common.base.Optional;
@Override
public Iterable<Part> parse(@Nonnull String source, @Nullable SoneTextParserContext context) {
PartContainer parts = new PartContainer();
- BufferedReader bufferedReader = new BufferedReader(new StringReader(source));
- try {
+ try (Reader sourceReader = new StringReader(source);
+ BufferedReader bufferedReader = new BufferedReader(sourceReader)) {
String line;
boolean lastLineEmpty = true;
int emptyLines = 0;
} catch (IOException ioe1) {
// a buffered reader around a string reader should never throw.
throw new RuntimeException(ioe1);
- } finally {
- Closer.close(bufferedReader);
}
for (int partIndex = parts.size() - 1; partIndex >= 0; --partIndex) {
Part part = parts.getPart(partIndex);
private int findEndOfLink(String line) {
Matcher matcher = whitespacePattern.matcher(line);
- if (!matcher.find(0)) {
- return line.length();
- }
- int nextWhitespace = matcher.start();
- int lastPunctuation = nextWhitespace;
- while (isPunctuation(line.charAt(lastPunctuation - 1))) {
- lastPunctuation -= 1;
- }
- if (lastPunctuation < nextWhitespace) {
- return lastPunctuation;
+ int endOfLink = matcher.find() ? matcher.start() : line.length();
+ while ((endOfLink > 0) && isPunctuation(line.charAt(endOfLink - 1))) {
+ endOfLink--;
}
int openParens = 0;
- for (int i = 0; i < nextWhitespace; i++) {
+ for (int i = 0; i < endOfLink; i++) {
switch (line.charAt(i)) {
case '(':
openParens++;
default:
}
}
- return nextWhitespace;
+ return endOfLink;
}
- private boolean isPunctuation(char character) {
- return (character == '.') || (character == ',');
+ private static boolean isPunctuation(char character) {
+ return (character == '.') || (character == ',') || (character == '!') || (character == '?');
}
private static class NextLink {