1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
package org.devacfr.maven.skins.reflow.snippet; |
20 |
|
|
21 |
|
import java.util.List; |
22 |
|
import java.util.Map; |
23 |
|
import java.util.regex.MatchResult; |
24 |
|
import java.util.regex.Matcher; |
25 |
|
import java.util.regex.Pattern; |
26 |
|
|
27 |
|
import com.google.common.base.Strings; |
28 |
|
import com.google.common.collect.Lists; |
29 |
|
import com.google.common.collect.Maps; |
30 |
|
import org.apache.commons.lang3.StringEscapeUtils; |
31 |
|
import org.devacfr.maven.skins.reflow.snippet.ComponentToken.Tag; |
32 |
|
import org.devacfr.maven.skins.reflow.snippet.ComponentToken.Type; |
33 |
|
import org.jsoup.nodes.Document; |
34 |
|
import org.jsoup.nodes.Element; |
35 |
|
import org.jsoup.nodes.Node; |
36 |
|
import org.jsoup.select.Collector; |
37 |
|
import org.jsoup.select.Elements; |
38 |
|
import org.jsoup.select.Evaluator; |
39 |
|
import org.jsoup.select.NodeTraversor; |
40 |
|
import org.jsoup.select.NodeVisitor; |
41 |
|
import org.jsoup.select.QueryParser; |
42 |
|
import org.slf4j.Logger; |
43 |
|
import org.slf4j.LoggerFactory; |
44 |
|
|
45 |
|
|
46 |
|
|
47 |
|
|
48 |
|
@author |
49 |
|
@version |
50 |
|
|
|
|
| 85,6% |
Uncovered Elements: 13 (90) |
Complexity: 25 |
Complexity Density: 0,47 |
|
51 |
|
public class ComponentResolver { |
52 |
|
|
53 |
|
private static final Logger LOGGER = LoggerFactory.getLogger(ComponentResolver.class); |
54 |
|
|
55 |
|
|
56 |
|
private static final Pattern RESOLVER_PATTERN = Pattern.compile( |
57 |
|
"\\{\\{(<|%) (\\/?)([\\w\\-_]*)(\\s?(?:[\\w\\-_]*)(?:=[\\u201c|\"](?:[\\s\\w\\p{Punct}]*)[\\u201d|\"])?)* (\\/?)(>|%)\\}\\}", |
58 |
|
Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CHARACTER_CLASS); |
59 |
|
|
60 |
|
private static final Pattern ATTRIBUTE_PATTERN = Pattern.compile("\\s?(\\w*)=(\\\")?(\\w*)\2\\s?", |
61 |
|
Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CHARACTER_CLASS); |
62 |
|
|
63 |
|
|
64 |
|
|
65 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
66 |
22 |
public ComponentResolver() {... |
67 |
22 |
super(); |
68 |
|
} |
69 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
70 |
523 |
public static boolean isSnippet(final Node node) {... |
71 |
523 |
return node.hasAttr("shortcode") || node.hasAttr("webcomponent"); |
72 |
|
} |
73 |
|
|
74 |
|
|
75 |
|
|
76 |
|
|
77 |
|
@param |
78 |
|
|
79 |
|
@return@link |
80 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
81 |
16 |
public Elements collect(final Element document) {... |
82 |
16 |
return collect(document, RESOLVER_PATTERN); |
83 |
|
} |
84 |
|
|
85 |
|
|
86 |
|
@param |
87 |
|
|
88 |
|
@return |
89 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
90 |
34 |
public static boolean hasIncludedSnippetComponent(final Element document) {... |
91 |
34 |
return Collector.findFirst(QueryParser.parse("[shortcode],[webcomponent]"), document) != null; |
92 |
|
} |
93 |
|
|
94 |
|
|
95 |
|
@link |
96 |
|
|
97 |
|
@param |
98 |
|
|
99 |
|
@return@link |
100 |
|
|
|
|
| 87,1% |
Uncovered Elements: 4 (31) |
Complexity: 7 |
Complexity Density: 0,33 |
|
101 |
9 |
public Document normalize(final Document document) {... |
102 |
|
|
103 |
9 |
final Elements elements = collect(document); |
104 |
9 |
if (LOGGER.isDebugEnabled()) { |
105 |
0 |
LOGGER.debug("Snippet Collected"); |
106 |
0 |
LOGGER.debug(elements.toString()); |
107 |
|
} |
108 |
|
|
109 |
9 |
if (!elements.isEmpty()) { |
110 |
9 |
final Elements sections = Collector.collect(new Evaluator.Tag("section"), document); |
111 |
9 |
sections.forEach(Element::unwrap); |
112 |
|
} |
113 |
|
|
114 |
9 |
elements.forEach(element -> { |
115 |
55 |
String text = StringEscapeUtils.unescapeHtml4(element.html()); |
116 |
55 |
final Matcher matcher = RESOLVER_PATTERN.matcher(text); |
117 |
|
|
118 |
55 |
final List<MatchResult> results = Lists.newArrayList(); |
119 |
|
|
120 |
110 |
while (matcher.find()) { |
121 |
55 |
final MatchResult matchResult = matcher.toMatchResult(); |
122 |
55 |
if (matchResult.start() > 0 || matchResult.end() < text.length()) { |
123 |
7 |
results.add(0, matcher.toMatchResult()); |
124 |
|
} |
125 |
|
} |
126 |
55 |
if (!results.isEmpty()) { |
127 |
3 |
for (final MatchResult matchResult : results) { |
128 |
7 |
final String snippet = text.substring(matchResult.start(), matchResult.end()); |
129 |
7 |
text = text.substring(0, matchResult.start()) + "<span>" + StringEscapeUtils.escapeHtml4(snippet) |
130 |
|
+ "</span>" + text.substring(matchResult.end()); |
131 |
|
} |
132 |
3 |
element.html(text); |
133 |
|
} |
134 |
|
}); |
135 |
9 |
return document; |
136 |
|
} |
137 |
|
|
138 |
|
|
139 |
|
@link |
140 |
|
|
141 |
|
@param |
142 |
|
|
143 |
|
@return@link |
144 |
|
|
|
|
| 94,7% |
Uncovered Elements: 1 (19) |
Complexity: 5 |
Complexity Density: 0,45 |
|
145 |
66 |
public ComponentToken create(final Element element) {... |
146 |
66 |
if (isSnippet(element)) { |
147 |
2 |
Type type = null; |
148 |
2 |
if (element.hasAttr("shortcode")) { |
149 |
1 |
type = Type.shortcode; |
150 |
1 |
} else if (element.hasAttr("webcomponent")) { |
151 |
1 |
type = Type.webComponent; |
152 |
|
} |
153 |
2 |
return new ComponentToken(element, element.tagName(), Tag.html, type); |
154 |
|
} else { |
155 |
64 |
final Matcher matcher = RESOLVER_PATTERN.matcher(element.ownText()); |
156 |
|
|
157 |
64 |
if (matcher.matches()) { |
158 |
63 |
return createToken(element, matcher); |
159 |
|
} |
160 |
|
} |
161 |
1 |
return null; |
162 |
|
} |
163 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (17) |
Complexity: 6 |
Complexity Density: 0,67 |
|
164 |
63 |
private ComponentToken createToken(final Element element, final Matcher matcher) {... |
165 |
63 |
if (!Strings.isNullOrEmpty(matcher.group(2)) && !Strings.isNullOrEmpty(matcher.group(5))) { |
166 |
|
|
167 |
1 |
throw new RuntimeException("malformed component"); |
168 |
|
} |
169 |
62 |
final Type type = "<".equals(matcher.group(1)) ? Type.shortcode : Type.webComponent; |
170 |
62 |
Tag tag = Tag.start; |
171 |
62 |
if ("/".equals(matcher.group(2))) { |
172 |
26 |
tag = Tag.end; |
173 |
36 |
} else if ("/".equals(matcher.group(5))) { |
174 |
5 |
tag = Tag.empty; |
175 |
|
} |
176 |
|
|
177 |
62 |
return new ComponentToken(element, matcher.group(3), tag, type); |
178 |
|
} |
179 |
|
|
|
|
| 0% |
Uncovered Elements: 7 (7) |
Complexity: 2 |
Complexity Density: 0,4 |
|
180 |
0 |
protected static Map<String, String> extractAttributes(final String text) {... |
181 |
0 |
final Map<String, String> attrs = Maps.newHashMap(); |
182 |
0 |
final Matcher matcher = ATTRIBUTE_PATTERN.matcher(text); |
183 |
0 |
while (matcher.find()) { |
184 |
0 |
attrs.put(matcher.group(1).toLowerCase(), matcher.group(3)); |
185 |
|
} |
186 |
0 |
return attrs; |
187 |
|
} |
188 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0,33 |
|
189 |
16 |
public static Elements collect(final Element root, final Pattern searchPattern) {... |
190 |
16 |
final Elements elements = new Elements(); |
191 |
16 |
NodeTraversor.traverse(new Accumulator(root, elements, searchPattern), root); |
192 |
16 |
return elements; |
193 |
|
} |
194 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (24) |
Complexity: 12 |
Complexity Density: 1 |
|
195 |
|
private static class Accumulator implements NodeVisitor { |
196 |
|
|
197 |
|
|
198 |
|
private final Pattern searchPattern; |
199 |
|
|
200 |
|
private final Element root; |
201 |
|
|
202 |
|
private final Elements elements; |
203 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0,33 |
|
204 |
16 |
Accumulator(final Element root, final Elements elements, final Pattern searchPattern) {... |
205 |
16 |
this.root = root; |
206 |
16 |
this.elements = elements; |
207 |
16 |
this.searchPattern = searchPattern; |
208 |
|
} |
209 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (8) |
Complexity: 3 |
Complexity Density: 0,75 |
|
210 |
817 |
@Override... |
211 |
|
public void head(final Node node, final int depth) { |
212 |
817 |
if (node instanceof Element) { |
213 |
293 |
final Element el = (Element) node; |
214 |
293 |
if (matches(root, el)) { |
215 |
102 |
elements.add(el); |
216 |
|
} |
217 |
|
} |
218 |
|
} |
219 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 5 |
Complexity Density: 1,67 |
|
220 |
293 |
public boolean matches(final Element root, final Element element) {... |
221 |
|
|
222 |
293 |
if ("pre".equals(element.tagName()) || "code".equals(element.tagName()) |
223 |
|
|| element.hasParent() && "pre".equals(element.parent().tagName())) { |
224 |
12 |
return false; |
225 |
|
} |
226 |
281 |
return searchPattern.matcher(element.ownText()).find(); |
227 |
|
} |
228 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 3 |
Complexity Density: 1,5 |
|
229 |
817 |
@Override... |
230 |
|
public void tail(final Node node, final int depth) { |
231 |
817 |
if (node instanceof Element && isSnippet(node)) { |
232 |
6 |
elements.add((Element) node); |
233 |
|
} |
234 |
|
} |
235 |
|
} |
236 |
|
} |