Class |
Line # |
Actions |
|||||
---|---|---|---|---|---|---|---|
SnippetParser | 44 | 56 | 0% | 22 | 23 |
1 | /* | |
2 | * Licensed to the Apache Software Foundation (ASF) under one | |
3 | * or more contributor license agreements. See the NOTICE file | |
4 | * distributed with this work for additional information | |
5 | * regarding copyright ownership. The ASF licenses this file | |
6 | * to you under the Apache License, Version 2.0 (the | |
7 | * "License"); you may not use this file except in compliance | |
8 | * with the License. You may obtain a copy of the License at | |
9 | * | |
10 | * http://www.apache.org/licenses/LICENSE-2.0 | |
11 | * | |
12 | * Unless required by applicable law or agreed to in writing, | |
13 | * software distributed under the License is distributed on an | |
14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | |
15 | * KIND, either express or implied. See the License for the | |
16 | * specific language governing permissions and limitations | |
17 | * under the License. | |
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 Christophe Friederich | |
42 | * @version 2.4 | |
43 | */ | |
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 | ||
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 | ||
80 | 6 | public SnippetParser insertResourcePath(final String path, final int index) { |
81 | 6 | snippetContext.insertResourcePath(path, index); |
82 | 6 | return this; |
83 | } | |
84 | ||
85 | 3 | public SnippetParser addResourcePath(final String path) { |
86 | 3 | snippetContext.addResourcePath(path); |
87 | 3 | return this; |
88 | } | |
89 | ||
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 | // find all snippets | |
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 | ||
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 | ||
145 | 53 | protected void parse(final ComponentToken token) { |
146 | 53 | state.parse(token); |
147 | } | |
148 | ||
149 | 0 | protected ComponentToken currentToken() { |
150 | 0 | final int size = stack.size(); |
151 | 0 | return size > 0 ? stack.get(size - 1) : null; |
152 | } | |
153 | ||
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 | ||
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 | ||
173 | 58 | protected SnippetContext getSnippetContext() { |
174 | 58 | return snippetContext; |
175 | } | |
176 | } |