- emptyLines = 0;
- /*
- * lineComplete tracks whether the block you are parsing is the
- * first block of the line. this is important because sometimes
- * you have to add an additional line break.
- */
- boolean lineComplete = true;
- while (line.length() > 0) {
- Optional<NextLink> nextLink = findNextLink(line);
- if (!nextLink.isPresent()) {
- if (lineComplete && !lastLineEmpty) {
- parts.add(new PlainTextPart("\n" + line));
- } else {
- parts.add(new PlainTextPart(line));
- }
- break;
- }
-
- int next = nextLink.get().getNextIndex();
- LinkType linkType = nextLink.get().getLinkType();
-
- /* cut off “freenet:” from before keys. */
- if (linkType.isFreenetLink() && (next >= 8) && (line.substring(next - 8, next).equals("freenet:"))) {
- next -= 8;
- line = line.substring(0, next) + line.substring(next + 8);
- }
-
- /* if there is text before the next item, write it out. */
- if (lineComplete && !lastLineEmpty) {
- parts.add(new PlainTextPart("\n"));
- }
- if (next > 0) {
- parts.add(new PlainTextPart(line.substring(0, next)));
- line = line.substring(next);
- }
- lineComplete = false;
-
- int nextSpace = findNextWhitespace(line);
- String link = line.substring(0, nextSpace);
- String name = link;
- logger.log(Level.FINER, String.format("Found link: %s", link));
-
- /* if there is no text after the scheme, it’s not a link! */
- if (link.equals(linkType.getScheme())) {
- parts.add(new PlainTextPart(linkType.getScheme()));
- line = line.substring(linkType.getScheme().length());
- continue;
- }
-
- if (linkType == LinkType.SONE) {
- if (lineIsLongEnoughToContainASoneLink(line)) {
- String soneId = line.substring(7, 50);
- Optional<Sone> sone = database.getSone(soneId);
- if (!sone.isPresent()) {
- /*
- * don’t use create=true above, we don’t want
- * the empty shell.
- */
- sone = Optional.<Sone>of(new DefaultSone(database, soneId, false, null));
- }
- parts.add(new SonePart(sone.get()));
- line = line.substring(50);
- } else {
- parts.add(new PlainTextPart(line));
- line = "";
- }
- continue;
- }
- if (linkType == LinkType.POST) {
- if (lineIsLongEnoughToContainAPostLink(line)) {
- String postId = line.substring(7, 43);
- Optional<Post> post = database.getPost(postId);
- if (post.isPresent()) {
- parts.add(new PostPart(post.get()));
- } else {
- parts.add(new PlainTextPart(line.substring(0, 43)));
- }
- line = line.substring(43);
- } else {
- parts.add(new PlainTextPart(line));
- line = "";
- }
- continue;
- }
-
- if (linkType.isFreenetLink()) {
- FreenetURI uri;
- if (name.indexOf('?') > -1) {
- name = name.substring(0, name.indexOf('?'));
- }
- if (name.endsWith("/")) {
- name = name.substring(0, name.length() - 1);
- }
- try {
- uri = new FreenetURI(name);
- name = uri.lastMetaString();
- if (name == null) {
- name = uri.getDocName();
- }
- if (name == null) {
- name = link.substring(0, Math.min(9, link.length()));
- }
- boolean fromPostingSone = ((linkType == LinkType.SSK) || (linkType == LinkType.USK)) && linkMatchesPostingSone(context, link);
- parts.add(new FreenetLinkPart(link, name, fromPostingSone));
- } catch (MalformedURLException mue1) {
- /* not a valid link, insert as plain text. */
- parts.add(new PlainTextPart(link));
- } catch (NullPointerException npe1) {
- /* FreenetURI sometimes throws these, too. */
- parts.add(new PlainTextPart(link));
- } catch (ArrayIndexOutOfBoundsException aioobe1) {
- /* oh, and these, too. */
- parts.add(new PlainTextPart(link));
- }
- } else if (linkType.isInternetLink()) {
- name = link.substring(linkType.getScheme().length());
- int firstSlash = name.indexOf('/');
- int lastSlash = name.lastIndexOf('/');
- if ((lastSlash - firstSlash) > 3) {
- name = name.substring(0, firstSlash + 1) + "…" + name.substring(lastSlash);
- }
- if (name.endsWith("/")) {
- name = name.substring(0, name.length() - 1);
- }
- if (((name.indexOf('/') > -1) && (name.indexOf('.') < name.lastIndexOf('.', name.indexOf('/'))) || ((name.indexOf('/') == -1) && (name.indexOf('.') < name.lastIndexOf('.')))) && name.startsWith("www.")) {
- name = name.substring(4);
- }
- if (name.indexOf('?') > -1) {
- name = name.substring(0, name.indexOf('?'));
- }
- parts.add(new LinkPart(link, name));
- }
- line = line.substring(nextSpace);
- }
- lastLineEmpty = false;