diff options
author | Vitaly Takmazov | 2020-06-03 17:32:55 +0300 |
---|---|---|
committer | Vitaly Takmazov | 2020-06-03 17:32:55 +0300 |
commit | b0bdce22447ff815512ae7612dd4ab4a5d9a409a (patch) | |
tree | 9c33f07584697130a316c6db404844207494a832 /src/main/java/ru/sape/SapePageLinks.java | |
parent | cafe4644cf82267397c21433c0c8965ff90c1073 (diff) |
sape: switch to xml output
Diffstat (limited to 'src/main/java/ru/sape/SapePageLinks.java')
-rw-r--r-- | src/main/java/ru/sape/SapePageLinks.java | 66 |
1 files changed, 17 insertions, 49 deletions
diff --git a/src/main/java/ru/sape/SapePageLinks.java b/src/main/java/ru/sape/SapePageLinks.java index 77715aea..52def3f6 100644 --- a/src/main/java/ru/sape/SapePageLinks.java +++ b/src/main/java/ru/sape/SapePageLinks.java @@ -1,71 +1,39 @@ package ru.sape; +import com.juick.www.ad.models.Site; import org.apache.commons.lang3.StringUtils; import java.net.URI; -import java.util.*; +import java.util.Collections; +import java.util.List; +import java.util.stream.Collectors; public class SapePageLinks { - private boolean showCode; + private final boolean showCode; + private final String linkDelimiter; + private final List<String> pageLinks; - @SuppressWarnings("unchecked") - public SapePageLinks(SapeConnection sapeConnection, String sapeUser, URI request, String sapeCookie, boolean showCode) { + public SapePageLinks(Site site, String sapeUser, URI request, String sapeCookie, boolean showPageCode) { String req = StringUtils.isNotEmpty(request.getQuery()) ? request.getPath() + "?" + request.getQuery() : request.getPath(); - if (sapeUser.equals(sapeCookie)) { - showCode = true; - } - - Map<String, Object> data = sapeConnection.getData(); - if (data.containsKey("__sape_delimiter__")) { - linkDelimiter = (String) data.get("__sape_delimiter__"); - } - - if (data.containsKey(req)) { - pageLinks = new ArrayList<>(((Map<Object, String>) data.get(req)).values()); - } + linkDelimiter = site.getDelimiter(); - if (data.containsKey("__sape_new_url__")) { - if (showCode) { - Object newUrl = data.get("__sape_new_url__"); + this.showCode = showPageCode || sapeUser.equals(sapeCookie); - if (newUrl instanceof Map) { - pageLinks = new ArrayList<>(((Map<Object, String>) newUrl).values()); - } else { - pageLinks = new ArrayList<>(Collections.singletonList((String) newUrl)); - } - } + if (showCode) { + pageLinks = Collections.singletonList(site.getCode()); + } else { + pageLinks = site.getPages().stream().filter(page -> page.getUri().equals(req)) + .flatMap(page -> page.getLinks().stream()).collect(Collectors.toList()); } - - this.showCode = showCode; } - private String linkDelimiter = "."; - private List<String> pageLinks = new ArrayList<>(); - public String render() { - return render(-1); - } - public String render(int count) { + public String render() { StringBuilder s = new StringBuilder(); - - if (count < 0) { - count = pageLinks.size(); - } - - for (Iterator<String> i = pageLinks.iterator(); i.hasNext() && count > 0; count--) { - if (s.length() > 0) { - s.append(linkDelimiter); - } - - String l = i.next(); - - s.append(l); - - i.remove(); - } + s.append(String.join(linkDelimiter, pageLinks)); if (showCode) { s.insert(0, "<sape_noindex>"); |