diff options
author | 2018-05-27 22:46:08 +0300 | |
---|---|---|
committer | 2018-05-27 22:46:08 +0300 | |
commit | 4080ecbdb18bf67275317bdcfa5bcc52d3ac94e4 (patch) | |
tree | f862479b9a0a3424193c01a593f80c673902641f /juick-server-jdbc/src/main/java/com/juick/service/TagServiceImpl.java | |
parent | 15c52d91884988dc3c714739f901b510eb6ffc56 (diff) |
Refactor tag parsing
Diffstat (limited to 'juick-server-jdbc/src/main/java/com/juick/service/TagServiceImpl.java')
-rw-r--r-- | juick-server-jdbc/src/main/java/com/juick/service/TagServiceImpl.java | 32 |
1 files changed, 9 insertions, 23 deletions
diff --git a/juick-server-jdbc/src/main/java/com/juick/service/TagServiceImpl.java b/juick-server-jdbc/src/main/java/com/juick/service/TagServiceImpl.java index 3c35f5b3..d7800390 100644 --- a/juick-server-jdbc/src/main/java/com/juick/service/TagServiceImpl.java +++ b/juick-server-jdbc/src/main/java/com/juick/service/TagServiceImpl.java @@ -33,13 +33,12 @@ import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; -import java.util.ArrayList; +import java.util.Arrays; import java.util.Collection; import java.util.List; import java.util.Objects; -import java.util.regex.Matcher; -import java.util.regex.Pattern; import java.util.stream.Collectors; +import java.util.stream.IntStream; import java.util.stream.Stream; /** @@ -47,9 +46,6 @@ import java.util.stream.Stream; */ @Repository public class TagServiceImpl extends BaseJdbcService implements TagService { - private static final Pattern TAGS_PATTERN1 = Pattern.compile("^(?:(?:\\*[^ \\r\\n\\t]+)|\\s)+$"); - private static final Pattern TAGS_PATTERN2 = Pattern.compile("^\\*([^ \\r\\n\\t]+)[\\s+]?([\\s\\S]+)?"); - private static final Pattern TAG_PATTERN = Pattern.compile("\\*([^ \\r\\n\\t]+)"); @Transactional(readOnly = true) @Override @@ -212,23 +208,13 @@ public class TagServiceImpl extends BaseJdbcService implements TagService { } @Override - public List<Tag> fromString(final String txt, final boolean tagsOnly) { - Pattern tagsPattern = tagsOnly ? TAGS_PATTERN1 : TAGS_PATTERN2; - List<Tag> tags = new ArrayList<>(); - for (String line : txt.split("\n")) { - if (line.startsWith(">")) - continue; - if (tagsPattern.matcher(line).matches()) { - Matcher tagMatcher = TAG_PATTERN.matcher(line); - - // TODO: process readonly, private, friends, public - while (tagMatcher.find()) { - for (int i = 1; i <= tagMatcher.groupCount(); i++) - tags.add(getTag(tagMatcher.group(i), true)); - } - } - } - return tags; + public List<Tag> fromString(final String txt) { + String firstLine = txt.split("\\n", 2)[0]; + List<String> firstWords = Arrays.asList(firstLine.split("\\ ")); + int nonTagIndex = IntStream.range(0, firstWords.size()) + .filter(i -> !firstWords.get(i).startsWith("*")).findFirst().orElse(firstWords.size()); + return firstWords.subList(0, nonTagIndex).stream().map(t -> getTag(t.substring(1), true)) + .collect(Collectors.toList()); } @Transactional(readOnly = true) |