Project

General

Profile

Download (4.82 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.List;
12
import java.util.SortedSet;
13
import java.util.Stack;
14
import java.util.TreeSet;
15

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

    
24
    /**
25
     * Creates a string from tagged text by concatenating all tags with a whitespace.
26
     * @param tags
27
     * @return the concatenated string
28
     * @see #createString(List, HTMLTagRules)
29
     */
30
    public static String createString(List<TaggedText> tags) {
31
        StringBuilder result = new StringBuilder();
32

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

    
50
    /**
51
     * Creates a string from tagged text by concatenating all tags. If no separator tag is defined
52
     * tags are separated by simple whitespace.
53
     * @param tags
54
     * @return
55
     */
56
    public  static String createString(List<TaggedText> tags, HTMLTagRules htmlRules) {
57
        if (htmlRules == null){
58
            return createString(tags);
59
        }
60
        //add whitespace separators
61
        int index = 0;
62
        boolean wasSeparator = true;
63
        while (index < tags.size()){
64

    
65
            if (! tags.get(index).getType().isSeparator()){
66
                if (wasSeparator == false){
67
                    tags.add(index++, TaggedText.NewWhitespaceInstance());
68
                }else{
69
                    wasSeparator = false;
70
                }
71
            }else{
72
                wasSeparator = true;
73
            }
74
            index++;
75
        }
76

    
77
        //create String
78
        StringBuffer result = new StringBuffer();
79

    
80
        Stack<String> htmlStack = new Stack<String>();
81
        for (int i = 0;  i < tags.size(); i++  ){
82
            TaggedText tag = tags.get(i);
83
            TagEnum thisType = tag.getType();
84
            TagEnum lastType = (i == 0? null : tags.get(i - 1).getType());
85
            TagEnum nextType = (i + 1 >= tags.size() ? null : tags.get(i + 1).getType());
86

    
87
            boolean isSeparator = tag.getType().isSeparator();
88
//            boolean lastEqual = tag.getType().equals(lastType);
89
//            boolean nextEqual = tag.getType().equals(nextType);
90
//            boolean bothEqual = lastEqual && nextEqual;
91

    
92
            //compute list of rules (tags)
93
            SortedSet<String> separatorRules;
94
            if (isSeparator){
95
                separatorRules = getCommonRules(htmlRules.getRule(lastType), htmlRules.getRule(nextType));
96
            }else{
97
                separatorRules = htmlRules.getRule(thisType);
98
            }
99

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

    
111
            //open all tags not yet existing
112
            if (! isSeparator){
113
                for (String rule : separatorRules){
114
                    htmlStack.add(rule);
115
                    result.append("<" +  rule + ">");
116
                }
117
            }
118

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

    
129

    
130
    private static void closeHtml(StringBuffer result, Stack<String> htmlStack, int index) {
131
        while (htmlStack.size() > index){
132
            String closeHtml = htmlStack.pop();
133
            result.append("</" +  closeHtml + ">");
134
        }
135
    }
136

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