Project

General

Profile

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

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

    
17
/**
18
 * @author a.mueller
19
 * @date 09.09.2015
20
 *
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
        StringBuffer result = new StringBuffer();
32

    
33
        boolean isSeparator;
34
        boolean wasSeparator = true;  //true for start tag
35
        for (TaggedText tag: tags){
36
            isSeparator = tag.getType().isSeparator();
37
            if (! wasSeparator && ! isSeparator ){
38
                result.append(" ");
39
            }
40
            result.append(tag.getText());
41
            wasSeparator = isSeparator;
42
        }
43
        return result.toString().trim();
44
    }
45

    
46

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

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

    
74
        //create String
75
        StringBuffer result = new StringBuffer();
76

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

    
84
            boolean isSeparator = tag.getType().isSeparator();
85
//            boolean lastEqual = tag.getType().equals(lastType);
86
//            boolean nextEqual = tag.getType().equals(nextType);
87
//            boolean bothEqual = lastEqual && nextEqual;
88

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

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

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

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

    
126

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

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