import java.util.regex.Matcher;
import java.util.regex.Pattern;
-import net.pterodactylus.sone.core.PostProvider;
-import net.pterodactylus.sone.core.SoneProvider;
import net.pterodactylus.sone.data.Post;
import net.pterodactylus.sone.data.Sone;
+import net.pterodactylus.sone.data.impl.DefaultSone;
+import net.pterodactylus.sone.database.Database;
import net.pterodactylus.util.io.Closer;
import net.pterodactylus.util.logging.Logging;
+
import freenet.keys.FreenetURI;
+import com.google.common.base.Optional;
+
/**
* {@link Parser} implementation that can recognize Freenet URIs.
*
private enum LinkType {
/** Link is a KSK. */
- KSK("KSK@"),
+ KSK("KSK@", true),
/** Link is a CHK. */
- CHK("CHK@"),
+ CHK("CHK@", true),
/** Link is an SSK. */
- SSK("SSK@"),
+ SSK("SSK@", true),
/** Link is a USK. */
- USK("USK@"),
+ USK("USK@", true),
/** Link is HTTP. */
- HTTP("http://"),
+ HTTP("http://", false),
/** Link is HTTPS. */
- HTTPS("https://"),
+ HTTPS("https://", false),
/** Link is a Sone. */
- SONE("sone://"),
+ SONE("sone://", false),
/** Link is a post. */
- POST("post://");
+ POST("post://", false);
/** The scheme identifying this link type. */
private final String scheme;
+ private final boolean freenetLink;
/**
* Creates a new link type identified by the given scheme.
*
* @param scheme
* The scheme of the link type
+ * @param freenetLink
*/
- private LinkType(String scheme) {
+ private LinkType(String scheme, boolean freenetLink) {
this.scheme = scheme;
+ this.freenetLink = freenetLink;
}
/**
return scheme;
}
- }
+ public boolean isFreenetLink() {
+ return freenetLink;
+ }
- /** The Sone provider. */
- private final SoneProvider soneProvider;
+ }
- /** The post provider. */
- private final PostProvider postProvider;
+ private final Database database;
/**
* Creates a new freenet link parser.
*
- * @param soneProvider
- * The Sone provider
- * @param postProvider
- * The post provider
+ * @param database
*/
- public SoneTextParser(SoneProvider soneProvider, PostProvider postProvider) {
- this.soneProvider = soneProvider;
- this.postProvider = postProvider;
+ public SoneTextParser(Database database) {
+ this.database = database;
}
//
// PART METHODS
//
- /**
- * {@inheritDoc}
- */
@Override
public Iterable<Part> parse(SoneTextParserContext context, Reader source) throws IOException {
PartContainer parts = new PartContainer();
}
/* cut off “freenet:” from before keys. */
- if (((linkType == LinkType.KSK) || (linkType == LinkType.CHK) || (linkType == LinkType.SSK) || (linkType == LinkType.USK)) && (next >= 8) && (line.substring(next - 8, next).equals("freenet:"))) {
+ if (linkType.isFreenetLink() && (next >= 8) && (line.substring(next - 8, next).equals("freenet:"))) {
next -= 8;
line = line.substring(0, next) + line.substring(next + 8);
}
if (next > 0) {
parts.add(new PlainTextPart(line.substring(0, next)));
line = line.substring(next);
- next = 0;
}
lineComplete = false;
if (linkType == LinkType.SONE) {
if (line.length() >= (7 + 43)) {
String soneId = line.substring(7, 50);
- Sone sone = soneProvider.getSone(soneId);
- if (sone == null) {
+ Optional<Sone> sone = database.getSone(soneId);
+ if (!sone.isPresent()) {
/*
* don’t use create=true above, we don’t want
* the empty shell.
*/
- sone = new Sone(soneId, false);
+ sone = Optional.<Sone>of(new DefaultSone(database, soneId, false, null));
}
- parts.add(new SonePart(sone));
+ parts.add(new SonePart(sone.get()));
line = line.substring(50);
} else {
parts.add(new PlainTextPart(line));
if (linkType == LinkType.POST) {
if (line.length() >= (7 + 36)) {
String postId = line.substring(7, 43);
- Post post = postProvider.getPost(postId);
- if ((post != null) && (post.getSone() != null)) {
- parts.add(new PostPart(post));
+ Optional<Post> post = database.getPost(postId);
+ if (post.isPresent()) {
+ parts.add(new PostPart(post.get()));
} else {
parts.add(new PlainTextPart(line.substring(0, 43)));
}
continue;
}
- if ((linkType == LinkType.KSK) || (linkType == LinkType.CHK) || (linkType == LinkType.SSK) || (linkType == LinkType.USK)) {
+ if (linkType.isFreenetLink()) {
FreenetURI uri;
if (name.indexOf('?') > -1) {
name = name.substring(0, name.indexOf('?'));
}
for (int partIndex = parts.size() - 1; partIndex >= 0; --partIndex) {
Part part = parts.getPart(partIndex);
- if (!(part instanceof PlainTextPart) || !"\n".equals(((PlainTextPart) part).getText())) {
+ if (!(part instanceof PlainTextPart) || !"\n".equals(part.getText())) {
break;
}
parts.removePart(partIndex);