Project

General

Profile

Download (4.21 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2018 EDIT
3
* European Distributed Institute of Taxonomy
4
* http://www.e-taxonomy.eu
5
*
6
* The contents of this file are subject to the Mozilla Public License Version 1.1
7
* See LICENSE.TXT at the top of this package for the full license terms.
8
*/
9
package eu.etaxonomy.cdm.format;
10

    
11
import java.util.LinkedList;
12
import java.util.StringTokenizer;
13
import java.util.regex.Matcher;
14
import java.util.regex.Pattern;
15

    
16
import org.apache.commons.lang3.StringUtils;
17

    
18
import eu.etaxonomy.cdm.model.common.CdmBase;
19

    
20
/**
21
 * @author a.kohlbecker
22
 * @since Dec 14, 2018
23
 */
24
public abstract class AbstractEllypsisFormatter<T extends CdmBase> implements EllypsisFormatter<T> {
25

    
26
    protected static final String DELIM = " ";
27
    protected String MORE = " \u2026";
28

    
29
    @Override
30
    public String ellypsis(T entity, String preserveString) {
31

    
32
        EllipsisData ed = entityEllypsis(entity, preserveString);
33
        String label = ed.truncated;
34

    
35
        return label;
36
    }
37

    
38
    protected abstract EllipsisData entityEllypsis(T entity, String filterString);
39

    
40
    protected String stringEllypsis(String text, int maxCharsVisible, int minNumOfWords) {
41

    
42
        if(text.length() < maxCharsVisible){
43
            return text;
44
        }
45

    
46
        String ellipsedText = "";
47
        StringTokenizer tokenizer = new StringTokenizer(text, DELIM);
48
        int wordCount = 0;
49
        while(tokenizer.hasMoreElements()){
50
            String token = tokenizer.nextToken();
51
            if(ellipsedText.length() + token.length() + DELIM.length() <= maxCharsVisible || wordCount < minNumOfWords){
52
                ellipsedText = ellipsedText + (ellipsedText.isEmpty() ? "" : DELIM) + token;
53
            } else {
54
                break;
55
            }
56
            wordCount++;
57
        }
58
        return ellipsedText + MORE;
59
    }
60

    
61
    protected String preserveString(String preserveString, String text, Pattern pattern, String textEllipsed) {
62
        String matchingSubstring = null;
63
        if(!preserveString.isEmpty()){
64
            Matcher m = pattern.matcher(text);
65
            if(m.find()){
66
                matchingSubstring = m.group(1);
67
            }
68
        }
69
        if(matchingSubstring != null && !textEllipsed.toLowerCase().contains(preserveString)){
70
            textEllipsed += matchingSubstring + MORE;
71
        }
72
        return textEllipsed;
73
    }
74

    
75
    protected boolean isEllypsis(String label) {
76
        return label.contains(MORE);
77
    }
78

    
79

    
80
    protected String titleCacheOnlyEllypsis(String titleCache, int maxCharsVisible, int minNumOfWords) {
81
        // tokens = titleCache.split("\\s");
82
        String head = titleCache.substring(0, Math.round(titleCache.length() / 2));
83
        String tail = titleCache.substring(Math.round(titleCache.length() / 2), titleCache.length());
84

    
85
        head = stringEllypsis(head, maxCharsVisible, minNumOfWords);
86
        tail = stringEllypsis(StringUtils.reverse(tail), maxCharsVisible, minNumOfWords).replace(MORE, "");
87
        return head + StringUtils.reverse(tail);
88
    }
89

    
90

    
91
    public void applyAndSplit(LinkedList<EllipsisData> edList, String textpart, String textpartEllypsis) {
92
        // apply on last element in list
93
        EllipsisData last = edList.getLast();
94
        int pos1 = last.original.indexOf(textpart);
95
        if(pos1 > -1){
96
            if(pos1 > 0){
97
                String textPartBefore = last.original.substring(0, pos1);
98
                if(textPartBefore.matches(".*\\w+.*")){ // eliminate non word clutter
99
                    edList.add(edList.size() - 1, new EllipsisData(textPartBefore, null)); // to be processed later
100
                }
101
            }
102
            edList.add(edList.size() - 1, new EllipsisData(textpart, textpartEllypsis));
103
            // replace the original with the part which comes after the matching textpart
104
            String newOriginal = last.original.substring(pos1 + textpart.length());
105
            last.original = newOriginal;
106
            if(StringUtils.isEmpty(last.original)){
107
                edList.removeLast();
108
            }
109
        }
110
    }
111

    
112
    public static class EllipsisData {
113
        public String original;
114
        public String truncated;
115

    
116
        public EllipsisData(String original, String truncated) {
117
            super();
118
            this.original = original;
119
            this.truncated = truncated;
120
        }
121
    }
122
}
(2-2/7)