1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
|
21 |
|
package jalview.util; |
22 |
|
|
23 |
|
import java.util.Locale; |
24 |
|
|
25 |
|
import java.util.ArrayList; |
26 |
|
import java.util.List; |
27 |
|
import java.util.StringTokenizer; |
28 |
|
import java.util.regex.Pattern; |
29 |
|
|
30 |
|
|
31 |
|
|
32 |
|
|
33 |
|
@author |
34 |
|
|
35 |
|
|
|
|
| 0% |
Uncovered Elements: 69 (69) |
Complexity: 22 |
Complexity Density: 0.52 |
|
36 |
|
public class ParseHtmlBodyAndLinks |
37 |
|
{ |
38 |
|
private static final Pattern LEFT_ANGLE_BRACKET_PATTERN = Pattern |
39 |
|
.compile("<"); |
40 |
|
|
41 |
|
String orig = null; |
42 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
43 |
0 |
public String getOrig()... |
44 |
|
{ |
45 |
0 |
return orig; |
46 |
|
} |
47 |
|
|
48 |
|
boolean htmlContent = true; |
49 |
|
|
50 |
|
|
51 |
|
@return |
52 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
53 |
0 |
public boolean isHtmlContent()... |
54 |
|
{ |
55 |
0 |
return htmlContent; |
56 |
|
} |
57 |
|
|
58 |
|
List<String> links = new ArrayList<String>(); |
59 |
|
|
60 |
|
String content; |
61 |
|
|
62 |
|
|
63 |
|
|
64 |
|
|
65 |
|
@return |
66 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
67 |
0 |
public String getContent()... |
68 |
|
{ |
69 |
|
|
70 |
0 |
return content; |
71 |
|
} |
72 |
|
|
73 |
|
|
74 |
|
|
75 |
|
|
76 |
|
@return |
77 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
78 |
0 |
public List<String> getLinks()... |
79 |
|
{ |
80 |
0 |
return links; |
81 |
|
} |
82 |
|
|
83 |
|
|
84 |
|
|
85 |
|
|
86 |
|
|
87 |
|
|
88 |
|
|
89 |
|
|
90 |
|
|
91 |
|
|
92 |
|
@param |
93 |
|
|
94 |
|
@param |
95 |
|
|
96 |
|
|
97 |
|
@param |
98 |
|
|
|
|
| 0% |
Uncovered Elements: 51 (51) |
Complexity: 15 |
Complexity Density: 0.45 |
|
99 |
0 |
public ParseHtmlBodyAndLinks(String description, boolean removeHTML,... |
100 |
|
String newline) |
101 |
|
{ |
102 |
0 |
if (description == null || description.length() == 0) |
103 |
|
{ |
104 |
0 |
htmlContent = false; |
105 |
0 |
return; |
106 |
|
} |
107 |
0 |
StringBuilder sb = new StringBuilder(description.length()); |
108 |
0 |
if (description.toUpperCase(Locale.ROOT).indexOf("<HTML>") == -1) |
109 |
|
{ |
110 |
0 |
htmlContent = false; |
111 |
|
} |
112 |
0 |
orig = description; |
113 |
0 |
StringTokenizer st = new StringTokenizer(description, "<"); |
114 |
0 |
String token, link; |
115 |
0 |
int startTag; |
116 |
0 |
String tag = null; |
117 |
0 |
while (st.hasMoreElements()) |
118 |
|
{ |
119 |
0 |
token = st.nextToken(">"); |
120 |
0 |
if (token.equalsIgnoreCase("html") || token.startsWith("/")) |
121 |
|
{ |
122 |
0 |
continue; |
123 |
|
} |
124 |
|
|
125 |
0 |
tag = null; |
126 |
0 |
startTag = token.indexOf("<"); |
127 |
|
|
128 |
0 |
if (startTag > -1) |
129 |
|
{ |
130 |
0 |
tag = token.substring(startTag + 1); |
131 |
0 |
token = token.substring(0, startTag); |
132 |
|
} |
133 |
|
|
134 |
0 |
if (tag != null && tag.toUpperCase(Locale.ROOT).startsWith("A HREF=")) |
135 |
|
{ |
136 |
0 |
if (token.length() > 0) |
137 |
|
{ |
138 |
0 |
sb.append(token); |
139 |
|
} |
140 |
0 |
link = tag.substring(tag.indexOf("\"") + 1, tag.length() - 1); |
141 |
0 |
String label = st.nextToken("<>"); |
142 |
0 |
links.add(label + "|" + link); |
143 |
0 |
sb.append(label + "%LINK%"); |
144 |
|
} |
145 |
0 |
else if (tag != null && tag.equalsIgnoreCase("br")) |
146 |
|
{ |
147 |
0 |
sb.append(newline); |
148 |
|
} |
149 |
|
else |
150 |
|
{ |
151 |
0 |
sb.append(token); |
152 |
|
} |
153 |
|
} |
154 |
0 |
if (removeHTML && !htmlContent) |
155 |
|
{ |
156 |
|
|
157 |
|
|
158 |
0 |
sb = new StringBuilder(LEFT_ANGLE_BRACKET_PATTERN.matcher(description) |
159 |
|
.replaceAll("<")); |
160 |
|
} |
161 |
0 |
content = translateEntities(sb.toString()); |
162 |
|
} |
163 |
|
|
|
|
| 0% |
Uncovered Elements: 4 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
164 |
0 |
private String translateEntities(String s)... |
165 |
|
{ |
166 |
0 |
s = s.replaceAll("&", "&"); |
167 |
0 |
s = s.replaceAll("<", "<"); |
168 |
0 |
s = s.replaceAll(">", ">"); |
169 |
0 |
return s; |
170 |
|
} |
171 |
|
|
172 |
|
|
173 |
|
|
174 |
|
|
175 |
|
|
176 |
|
@return |
177 |
|
|
|
|
| 0% |
Uncovered Elements: 3 (3) |
Complexity: 2 |
Complexity Density: 2 |
|
178 |
0 |
public String getNonHtmlContent()... |
179 |
|
{ |
180 |
0 |
return isHtmlContent() ? content : orig; |
181 |
|
} |
182 |
|
|
183 |
|
} |