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 javax.annotation.Nonnull; |
22 |
|
import javax.annotation.Nullable; |
23 |
|
|
24 |
|
import java.io.IOException; |
25 |
|
import java.util.ArrayList; |
26 |
|
import java.util.Iterator; |
27 |
|
|
28 |
|
import org.devacfr.maven.skins.reflow.ISkinConfig; |
29 |
|
import org.devacfr.maven.skins.reflow.snippet.Processor.ShortcodeProcessor; |
30 |
|
import org.devacfr.maven.skins.reflow.snippet.Processor.WebComponentProcessor; |
31 |
|
import org.jsoup.Jsoup; |
32 |
|
import org.jsoup.nodes.Document; |
33 |
|
import org.jsoup.nodes.Element; |
34 |
|
import org.jsoup.select.Elements; |
35 |
|
import org.slf4j.Logger; |
36 |
|
import org.slf4j.LoggerFactory; |
37 |
|
|
38 |
|
import static java.util.Objects.requireNonNull; |
39 |
|
|
40 |
|
|
41 |
|
@author |
42 |
|
@version |
43 |
|
|
|
|
| 72,6% |
Uncovered Elements: 23 (84) |
Complexity: 22 |
Complexity Density: 0,39 |
|
44 |
|
public class SnippetParser { |
45 |
|
|
46 |
|
private static final Logger LOGGER = LoggerFactory.getLogger(SnippetParser.class); |
47 |
|
|
48 |
|
|
49 |
|
private final ComponentResolver resolver; |
50 |
|
|
51 |
|
|
52 |
|
private final ArrayList<ComponentToken> stack; |
53 |
|
|
54 |
|
|
55 |
|
private Iterator<Element> it; |
56 |
|
|
57 |
|
|
58 |
|
private Processor state = null; |
59 |
|
|
60 |
|
|
61 |
|
private final WebComponentProcessor webComponentProcessor; |
62 |
|
|
63 |
|
|
64 |
|
private final ShortcodeProcessor shortcodeProcessor; |
65 |
|
|
66 |
|
|
67 |
|
private final SnippetContext snippetContext; |
68 |
|
|
69 |
|
|
70 |
|
private ComponentToken currentToken; |
71 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0,2 |
|
72 |
7 |
public SnippetParser() {... |
73 |
7 |
resolver = new ComponentResolver(); |
74 |
7 |
stack = new ArrayList<>(32); |
75 |
7 |
snippetContext = new SnippetContext(this); |
76 |
7 |
webComponentProcessor = new WebComponentProcessor(this); |
77 |
7 |
shortcodeProcessor = new ShortcodeProcessor(this); |
78 |
|
} |
79 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0,5 |
|
80 |
6 |
public SnippetParser insertResourcePath(final String path, final int index) {... |
81 |
6 |
snippetContext.insertResourcePath(path, index); |
82 |
6 |
return this; |
83 |
|
} |
84 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0,5 |
|
85 |
3 |
public SnippetParser addResourcePath(final String path) {... |
86 |
3 |
snippetContext.addResourcePath(path); |
87 |
3 |
return this; |
88 |
|
} |
89 |
|
|
|
|
| 72,7% |
Uncovered Elements: 6 (22) |
Complexity: 5 |
Complexity Density: 0,31 |
|
90 |
7 |
public SnippetContext parse(@Nonnull final ISkinConfig config, @Nullable String htmlSource)... |
91 |
|
throws IOException { |
92 |
7 |
requireNonNull(config); |
93 |
7 |
if (htmlSource == null) { |
94 |
0 |
htmlSource = ""; |
95 |
|
} |
96 |
|
|
97 |
7 |
snippetContext.reset(); |
98 |
7 |
snippetContext.setConfig(config); |
99 |
|
|
100 |
7 |
if (LOGGER.isDebugEnabled()) { |
101 |
0 |
LOGGER.debug("Parse Snippet"); |
102 |
0 |
LOGGER.debug(htmlSource); |
103 |
|
} |
104 |
|
|
105 |
|
|
106 |
7 |
final Document doc = resolver.normalize(Jsoup.parse(htmlSource)); |
107 |
|
|
108 |
7 |
final Elements elements = resolver.collect(doc); |
109 |
|
|
110 |
36 |
for (it = elements.iterator(); it.hasNext();) { |
111 |
29 |
try { |
112 |
29 |
parse(); |
113 |
|
} catch (final Exception ex) { |
114 |
0 |
throw new SnippetParseException( |
115 |
|
"error on parse token " + currentToken + " when generate file " + config.getFileId(), ex); |
116 |
|
} |
117 |
|
} |
118 |
7 |
snippetContext.setHtmlSource(doc.html()); |
119 |
7 |
return snippetContext; |
120 |
|
} |
121 |
|
|
|
|
| 71,4% |
Uncovered Elements: 6 (21) |
Complexity: 5 |
Complexity Density: 0,29 |
|
122 |
53 |
protected void parse() {... |
123 |
53 |
if (!it.hasNext()) { |
124 |
0 |
throw new SnippetParseException("EOF"); |
125 |
|
} |
126 |
53 |
final Element element = it.next(); |
127 |
53 |
currentToken = resolver.create(element); |
128 |
53 |
if (currentToken == null) { |
129 |
0 |
throw new SnippetParseException("unknown component: " + element); |
130 |
|
} |
131 |
53 |
switch (currentToken.type()) { |
132 |
27 |
case webComponent: |
133 |
27 |
state = webComponentProcessor; |
134 |
27 |
break; |
135 |
26 |
case shortcode: |
136 |
26 |
state = shortcodeProcessor; |
137 |
26 |
break; |
138 |
0 |
default: |
139 |
0 |
throw new SnippetParseException("unknown token type " + currentToken.type()); |
140 |
|
} |
141 |
53 |
parse(currentToken); |
142 |
53 |
currentToken = null; |
143 |
|
} |
144 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
145 |
53 |
protected void parse(final ComponentToken token) {... |
146 |
53 |
state.parse(token); |
147 |
|
} |
148 |
|
|
|
|
| 0% |
Uncovered Elements: 4 (4) |
Complexity: 2 |
Complexity Density: 1 |
|
149 |
0 |
protected ComponentToken currentToken() {... |
150 |
0 |
final int size = stack.size(); |
151 |
0 |
return size > 0 ? stack.get(size - 1) : null; |
152 |
|
} |
153 |
|
|
|
|
| 63,6% |
Uncovered Elements: 4 (11) |
Complexity: 3 |
Complexity Density: 0,43 |
|
154 |
24 |
protected ComponentToken pop() {... |
155 |
24 |
final int size = stack.size(); |
156 |
24 |
if (LOGGER.isDebugEnabled()) { |
157 |
0 |
LOGGER.debug("Stack size befor pop: {}", size); |
158 |
|
} |
159 |
24 |
final ComponentToken element = stack.remove(size - 1); |
160 |
24 |
if (LOGGER.isDebugEnabled()) { |
161 |
0 |
LOGGER.debug("Remove component from stack: {}", element); |
162 |
|
} |
163 |
24 |
return element; |
164 |
|
} |
165 |
|
|
|
|
| 60% |
Uncovered Elements: 2 (5) |
Complexity: 2 |
Complexity Density: 0,67 |
|
166 |
24 |
protected void push(final ComponentToken element) {... |
167 |
24 |
stack.add(element); |
168 |
24 |
if (LOGGER.isDebugEnabled()) { |
169 |
0 |
LOGGER.debug("Add component to stack: {}", element); |
170 |
|
} |
171 |
|
} |
172 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
173 |
58 |
protected SnippetContext getSnippetContext() {... |
174 |
58 |
return snippetContext; |
175 |
|
} |
176 |
|
} |