1
|
/**
|
2
|
*
|
3
|
*/
|
4
|
package eu.etaxonomy.cdm.strategy.cache;
|
5
|
|
6
|
import java.util.ArrayList;
|
7
|
import java.util.List;
|
8
|
import java.util.SortedSet;
|
9
|
import java.util.TreeSet;
|
10
|
|
11
|
/**
|
12
|
* Rules to define tagging for HTML tagged string output.
|
13
|
*
|
14
|
* @author a.mueller
|
15
|
* @since 14/02/2012
|
16
|
*/
|
17
|
public class HTMLTagRules {
|
18
|
|
19
|
private List<TagRule> rules = new ArrayList<>();
|
20
|
private boolean includeSingleInstanceHtml = false;
|
21
|
|
22
|
private class TagRule{
|
23
|
private TagRule(TagEnum type, String htmlTag){
|
24
|
this.type = type;
|
25
|
this.htmlTag = htmlTag;
|
26
|
}
|
27
|
private TagEnum type;
|
28
|
private String htmlTag;
|
29
|
// public TagEnum getType(){return this.type;}
|
30
|
// public String getString(){return this.htmlTag;}
|
31
|
@Override public String toString(){return type.name() + "-><" + htmlTag + ">";}
|
32
|
}
|
33
|
|
34
|
|
35
|
public HTMLTagRules addRule(TagEnum type, String htmlTag){
|
36
|
if (type == null || htmlTag == null){
|
37
|
throw new NullPointerException("Null tpye or htmlTag not allowed for HTMLTagRule");
|
38
|
}
|
39
|
rules.add(new TagRule(type, htmlTag));
|
40
|
return this;
|
41
|
}
|
42
|
|
43
|
public SortedSet<String> getRule(TaggedText taggedText){
|
44
|
SortedSet<String> result = new TreeSet<>();
|
45
|
if (taggedText != null){
|
46
|
result = getRule(taggedText.getType());
|
47
|
if (this.includeSingleInstanceHtml && !taggedText.htmlTags().isEmpty()){
|
48
|
result.addAll(taggedText.htmlTags());
|
49
|
}
|
50
|
}
|
51
|
return result;
|
52
|
}
|
53
|
|
54
|
public SortedSet<String> getRule(TagEnum type){
|
55
|
SortedSet<String> result = new TreeSet<>();
|
56
|
for (TagRule rule : rules){
|
57
|
if (rule.type.equals(type)){
|
58
|
result.add(rule.htmlTag);
|
59
|
}
|
60
|
}
|
61
|
return result;
|
62
|
}
|
63
|
|
64
|
public boolean hasRule(TagEnum type, String htmlTag){
|
65
|
for (TagRule rule : rules){
|
66
|
if (rule.type.equals(type) && htmlTag.equals(htmlTag)){
|
67
|
return true;
|
68
|
}
|
69
|
}
|
70
|
return false;
|
71
|
}
|
72
|
|
73
|
public boolean isIncludeSingleInstanceHtml() {
|
74
|
return includeSingleInstanceHtml;
|
75
|
}
|
76
|
|
77
|
public void setIncludeSingleInstanceHtml(boolean includeSingleInstanceHtml) {
|
78
|
this.includeSingleInstanceHtml = includeSingleInstanceHtml;
|
79
|
}
|
80
|
|
81
|
@Override
|
82
|
public String toString(){
|
83
|
String result = "HTMLTagRules[";
|
84
|
for (TagRule rule : rules){
|
85
|
result += rule.toString() + ";";
|
86
|
}
|
87
|
result = result.substring(0, rules.isEmpty()? result.length(): result.length()-1) + "]";
|
88
|
return result;
|
89
|
}
|
90
|
|
91
|
|
92
|
|
93
|
}
|