-
-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathHtmlVisitor.java
294 lines (258 loc) · 9.16 KB
/
HtmlVisitor.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/*
* MIT License
*
* Copyright (c) 2014-18, mcarvalho (gamboa.pt) and lcduarte (github.com/lcduarte)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package htmlflow.visitor;
import htmlflow.exceptions.HtmlFlowAppendException;
import org.xmlet.htmlapifaster.Area;
import org.xmlet.htmlapifaster.Base;
import org.xmlet.htmlapifaster.Br;
import org.xmlet.htmlapifaster.Col;
import org.xmlet.htmlapifaster.Element;
import org.xmlet.htmlapifaster.ElementVisitor;
import org.xmlet.htmlapifaster.Embed;
import org.xmlet.htmlapifaster.Hr;
import org.xmlet.htmlapifaster.Img;
import org.xmlet.htmlapifaster.Input;
import org.xmlet.htmlapifaster.Link;
import org.xmlet.htmlapifaster.Meta;
import org.xmlet.htmlapifaster.Param;
import org.xmlet.htmlapifaster.Root;
import org.xmlet.htmlapifaster.Source;
import org.xmlet.htmlapifaster.Text;
import java.io.IOException;
import static htmlflow.visitor.Tags.*;
/**
* This is the base implementation of the ElementVisitor (from HtmlApiFaster library).
*
* @author Miguel Gamboa
* created on 04-08-2022
*/
public abstract class HtmlVisitor extends ElementVisitor {
protected Appendable out;
/**
* keep track of current indentation.
*/
protected int depth;
/**
* If the beginning tag is closed, or not, i.e. if it is {@code "<elem>"} or it is {@code "<elem"}.
* On element visit the beginning tag is left open to include additional attributes.
*/
protected boolean isClosed = true;
/**
* It the HTML output should be indented or not.
*/
public final boolean isIndented;
/**
* keep track of current indentation.
*/
public final int getDepth() {
return depth;
}
/**
* Set current indentation.
*/
public final void setDepth(int v) {
depth = v;
}
public final void setIsClosed(boolean isClosed) {
this.isClosed = isClosed;
}
HtmlVisitor(Appendable out, boolean isIndented) {
this.out = out;
this.isIndented = isIndented;
}
public final Appendable out() {
return out;
}
public final HtmlVisitor setAppendable(Appendable appendable) {
this.out = appendable;
return this;
}
public final void write(String text) {
try {
this.out.append(text);
} catch (IOException e) {
throw new HtmlFlowAppendException(e);
}
}
protected final void write(char c) {
try {
this.out.append(c);
} catch (IOException e) {
throw new HtmlFlowAppendException(e);
}
}
/**
* Adds a new line and indentation.
* Checks whether the parent element is still opened or not (!isClosed).
* If it is open then it closes the parent begin tag with ">" (!isClosed).
*/
public final void newlineAndIndent(){
if (isClosed){
if(isIndented) {
write(Indentation.tabs(depth)); // \n\t\t\t\...
}
} else {
depth++;
if(isIndented)
write(Indentation.closedTabs(depth)); // >\n\t\t\t\...
else
write(FINISH_TAG);
isClosed = true;
}
}
/**
* This method appends the String {@code "<elementName"} and it leaves the element
* open to include additional attributes.
* Before that it may close the parent begin tag with {@code ">"} if it is
* still opened (!isClosed).
* The newlineAndIndent() is responsible for this job to check whether the parent element
* is still opened or not.
*
* @param element
*/
@Override
public final void visitElement(Element element) {
newlineAndIndent();
beginTag(out, element.getName()); // "<elementName"
isClosed = false;
}
/**
* Writes the end tag for elementName: {@code "</elementName>."}.
* This visit occurs when the º() is invoked.
*/
@Override
public final void visitParent(Element element) {
depth--;
newlineAndIndent();
endTag(out, element.getName()); // </elementName>
}
/**
* Void elements: area, base, br, col, embed, hr, img, input, link, meta, param, source, track, wbr.
* This method is invoked by visitParent specialization methods (at the end of this class)
* for each void element such as area, base, etc.
*/
protected final void visitParentOnVoidElements(){
if (!isClosed){
write(FINISH_TAG);
}
isClosed = true;
}
@Override
public final void visitAttribute(String attributeName, String attributeValue) {
addAttribute(out, attributeName, attributeValue);
}
/**
* To distinguish boolean attributes from others.
*
* @param name Attribute name
* @param value Attribute value
*/
@Override
public final void visitAttributeBoolean(String name, String value) {
if(!value.equals("false"))
addAttribute(out, name, value);
}
@Override
public final <R> void visitText(Text<? extends Element, R> text) {
newlineAndIndent();
write(text.getValue());
}
@Override
public final <R> void visitComment(Text<? extends Element, R> text) {
newlineAndIndent();
addComment(out, text.getValue());
}
/*=========================================================================*/
/*------------ Abstract HOOK Methods -------------------*/
/*=========================================================================*/
/**
* Processing output.
*/
public abstract void resolve(Object model);
/**
* Since HtmlVisitor is immutable this is the preferred way to create a copy of the
* existing HtmlVisitor instance with a different isIndented state.
*
* @param isIndented If the new visitor should indent HTML output or not.
*/
public abstract HtmlVisitor clone(boolean isIndented);
/*=========================================================================*/
/*------------ Root Element Methods --------------------*/
/*=========================================================================*/
@Override
public final <Z extends Element> void visitElementRoot(Root<Z> var1) { }
@Override
public final <Z extends Element> void visitParentRoot(Root<Z> var1) { }
/*=========================================================================*/
/*------------ Parent Methods for Void Elements --------------------*/
/*=========================================================================*/
@Override
public final <Z extends Element> void visitParentHr(Hr<Z> element) {
visitParentOnVoidElements ();
}
@Override
public final <Z extends Element> void visitParentEmbed(Embed<Z> element) {
visitParentOnVoidElements ();
}
@Override
public final <Z extends Element> void visitParentInput(Input<Z> element) {
visitParentOnVoidElements ();
}
@Override
public final <Z extends Element> void visitParentMeta(Meta<Z> element) {
visitParentOnVoidElements ();
}
@Override
public final <Z extends Element> void visitParentBr(Br<Z> element) {
visitParentOnVoidElements ();
}
@Override
public final <Z extends Element> void visitParentCol(Col<Z> element) {
visitParentOnVoidElements ();
}
@Override
public final <Z extends Element> void visitParentSource(Source<Z> element) {
visitParentOnVoidElements ();
}
@Override
public final <Z extends Element> void visitParentImg(Img<Z> element) {
visitParentOnVoidElements ();
}
@Override
public final <Z extends Element> void visitParentArea(Area<Z> element) {
visitParentOnVoidElements ();
}
@Override
public final <Z extends Element> void visitParentLink(Link<Z> element) {
visitParentOnVoidElements ();
}
@Override
public final <Z extends Element> void visitParentParam(Param<Z> element) {
visitParentOnVoidElements ();
}
@Override
public final <Z extends Element> void visitParentBase(Base<Z> element) {
visitParentOnVoidElements ();
}
}