Last active
March 10, 2019 12:20
-
-
Save ezhov-da/407cbb39f8d69084e8485f3ba4ab6c77 to your computer and use it in GitHub Desktop.
java парсер ссылок
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[code:]java[:code]package ru.ezhov.test.noui; | |
import java.text.MessageFormat; | |
import java.util.ArrayList; | |
import java.util.List; | |
import java.util.logging.Logger; | |
import java.util.stream.Stream; | |
/** | |
* | |
* @author ezhov_da | |
*/ | |
public class TreatmentLinks | |
{ | |
private static final Logger LOG = Logger.getLogger(TreatmentLinks.class.getName()); | |
private static String TEST_TEXT = "http://docs.groovy-lang.org/latest/html/documentation/index.html#_named_arguments---это просто описание;" | |
+ "http://groovy-lang.org/databases.html---а это просто WTF;" | |
+ "http://groovy-lang.org/databases.html---ха, вот так муть );" | |
+ "https://habrahabr.ru/company/acronis/blog/209226/---шняга;" | |
+ "https://habrahabr.ru/post/152815/---ыыыыыыыыыыыыыыыыыы;" | |
+ "https://habrahabr.ru/post/152815/"; | |
public static void main(String[] args) | |
{ | |
String[] array = TEST_TEXT.trim().split(";"); | |
List<Link> listLinks = new ArrayList(); | |
Stream.of(array).forEach(str -> | |
{ | |
String[] link = str.trim().split("---"); | |
if (link.length > 1) | |
{ | |
listLinks.add(new Link(link[0], link[1])); | |
} else if (link.length == 1) | |
{ | |
listLinks.add(new Link(link[0], link[0])); | |
} else if (link.length == 0) | |
{ | |
listLinks.add(new Link("", "")); | |
} | |
}); | |
System.out.println(createLinks(listLinks)); | |
} | |
private static String createLinks(List<Link> listLinks) | |
{ | |
String template = "<a href=\"{0}\">{1}</a><br>\n"; | |
StringBuilder sb = new StringBuilder(); | |
listLinks.forEach(link -> | |
{ | |
sb.append(MessageFormat.format(template, link.getLink(), link.getDescription())); | |
}); | |
return sb.toString(); | |
} | |
private static class Link | |
{ | |
private String link; | |
private String description; | |
public Link(String link, String description) | |
{ | |
this.link = link; | |
this.description = description; | |
} | |
public String getLink() | |
{ | |
return link; | |
} | |
public String getDescription() | |
{ | |
return description; | |
} | |
} | |
} | |
[/code] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment