aboutsummaryrefslogtreecommitdiff
path: root/juick-www/src/main/java/ru/sape/SapePageLinks.java
blob: e89b4e718ee9f0903f6fb1bb2e895956c9183558 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package ru.sape;

import java.util.*;

public class SapePageLinks {

    private boolean showCode;

    public SapePageLinks(SapeConnection sapeConnection, String sapeUser, String requestUri, String sapeCookie) {
        this(sapeConnection, sapeUser, requestUri, sapeCookie, false);
    }

    @SuppressWarnings("unchecked")
    public SapePageLinks(SapeConnection sapeConnection, String sapeUser, String requestUri, String sapeCookie, boolean showCode) {
        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(requestUri)) {
            pageLinks = new ArrayList<>(((Map<Object, String>) data.get(requestUri)).values());
        }

        if (data.containsKey("__sape_new_url__")) {
            if (showCode) {
                Object newUrl = data.get("__sape_new_url__");

                if (newUrl instanceof Map) {
                    pageLinks = new ArrayList<>(((Map<Object, String>) newUrl).values());
                } else {
                    pageLinks = new ArrayList<>(Collections.singletonList((String) newUrl));
                }
            }
        }

        this.showCode = showCode;
    }
    private String linkDelimiter = ".";
    private List<String> pageLinks = new ArrayList<>();

    public String render() {
        return render(-1);
    }

    public String render(int count) {
        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();
        }

        if (showCode) {
            s.insert(0, "<sape_noindex>");
            s.append("</sape_noindex>");
        }

        return s.toString();
    }
}