Project

General

Profile

Download (5 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2015 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.strategy.cache;
10

    
11
import java.util.ArrayList;
12
import java.util.List;
13
import java.util.SortedSet;
14
import java.util.Stack;
15
import java.util.TreeSet;
16

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

    
19
/**
20
 * Formatter class to create Strings from TaggedText Lists.
21
 *
22
 * @author a.mueller
23
 * @since 09.09.2015
24
 */
25
public class TaggedCacheHelper {
26

    
27
    private static final TaggedText WHITESPACE_TAG = TaggedText.NewWhitespaceInstance();
28

    
29
    /**
30
     * Creates a string from tagged text by concatenating all tags with a whitespace.
31
     * @param tags
32
     * @return the concatenated string
33
     * @see #createString(List, HTMLTagRules)
34
     */
35
    public static String createString(List<? extends TaggedText> tags) {
36
        StringBuilder result = new StringBuilder();
37

    
38
        boolean isSeparator;
39
        boolean wasSeparator = true;  //true for start tag
40
        int index = 0;
41
        for (index = 0; index < tags.size(); index++){
42
            TaggedText tag = tags.get(index);
43
            isSeparator = tag.getType().isSeparator();
44
            if (! wasSeparator && ! isSeparator ){
45
                result.append(" ");
46
            }
47
            if (index < tags.size() -1 || tag.getType() != TagEnum.postSeparator ){
48
                result.append(tag.getText());
49
            }
50
            wasSeparator = isSeparator;
51
        }
52
        return result.toString().trim();
53
    }
54

    
55
    /**
56
     * Creates a string from tagged text by concatenating all tags. If no separator tag is defined
57
     * tags are separated by simple whitespace.
58
     */
59
    public  static String createString(List<TaggedText> tagsOrigin, HTMLTagRules htmlRules) {
60
        if (htmlRules == null){
61
            return createString(tagsOrigin);
62
        }
63
        List<TaggedText> tags = new ArrayList<>(tagsOrigin);
64

    
65
        //add whitespace separators
66
        int index = 0;
67
        boolean wasSeparator = true;
68
        while (index < tags.size()){
69

    
70
            if (! tags.get(index).getType().isSeparator()){
71
                if (wasSeparator == false){
72
                    tags.add(index++, WHITESPACE_TAG);
73
                }else{
74
                    wasSeparator = false;
75
                }
76
            }else{
77
                wasSeparator = true;
78
            }
79
            index++;
80
        }
81

    
82
        //create String
83
        StringBuffer result = new StringBuffer();
84

    
85
        Stack<String> htmlStack = new Stack<>();
86
        for (int i = 0;  i < tags.size(); i++  ){
87
            TaggedText tag = tags.get(i);
88
            TaggedText lastTag = (i == 0? null : tags.get(i - 1));
89
            TaggedText nextTag = (i + 1 >= tags.size() ? null : tags.get(i + 1));
90
            TagEnum lastType = (lastTag == null ? null : lastTag.getType());
91
            TagEnum nextType = (nextTag == null ? null : nextTag.getType());
92

    
93
            boolean isSeparator = tag.getType().isSeparator();
94
            boolean isBlankSeparator = isSeparator && StringUtils.isBlank(tag.getText());
95

    
96
            //compute list of rules (tags)
97
            SortedSet<String> separatorRules;
98
            if (isBlankSeparator){
99
                separatorRules = getCommonRules(htmlRules.getRule(lastTag), htmlRules.getRule(nextTag));
100
            }else{
101
                separatorRules = htmlRules.getRule(tag);
102
            }
103

    
104
            //Close all tags not used anymore and remove all common tags from list of rules
105
            for (int j = 0 ;  j < htmlStack.size() ; j++){
106
                String html = htmlStack.get(j);
107
                if (! separatorRules.contains(html)){
108
                    closeHtml(result, htmlStack, j);
109
                    break;
110
                }else{
111
                    separatorRules.remove(html);
112
                }
113
            }
114

    
115
            //open all tags not yet existing
116
            if (! isBlankSeparator){
117
                for (String rule : separatorRules){
118
                    htmlStack.add(rule);
119
                    result.append("<" +  rule + ">");
120
                }
121
            }
122

    
123
            //add whitespace
124
            if (!isSeparator && lastType != null && !lastType.isSeparator() && nextType != null){
125
                result.append(" ");
126
            }
127
            result.append(tag.getText());
128
        }
129
        closeHtml(result, htmlStack, 0);
130
        return result.toString();
131
    }
132

    
133

    
134
    private static void closeHtml(StringBuffer result, Stack<String> htmlStack, int index) {
135
        while (htmlStack.size() > index){
136
            String closeHtml = htmlStack.pop();
137
            result.append("</" +  closeHtml + ">");
138
        }
139
    }
140

    
141
    private static SortedSet<String> getCommonRules(SortedSet<String> rules1,SortedSet<String> rules2) {
142
        SortedSet<String> result = new TreeSet<>();
143
        for (String str : rules1){
144
            if (rules2.contains(str)){
145
                result.add(str);
146
            }
147
        }
148
        return result;
149
    }
150
}
(3-3/5)