/
HtmlTemplateCompiler.java
662 lines (536 loc) · 23.3 KB
/
HtmlTemplateCompiler.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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
package com.google.sitebricks.compiler;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import com.google.sitebricks.Renderable;
import com.google.sitebricks.Template;
import com.google.sitebricks.conversion.generics.Generics;
import com.google.sitebricks.rendering.Strings;
import com.google.sitebricks.rendering.control.Chains;
import com.google.sitebricks.rendering.control.WidgetChain;
import com.google.sitebricks.rendering.control.WidgetRegistry;
import com.google.sitebricks.routing.PageBook;
import com.google.sitebricks.routing.SystemMetrics;
import org.jetbrains.annotations.NotNull;
import org.jsoup.nodes.*;
import org.softee.util.Preconditions;
import java.lang.reflect.Type;
import java.util.*;
import static com.google.sitebricks.compiler.AnnotationNode.*;
import static com.google.sitebricks.compiler.HtmlParser.LINE_NUMBER_ATTRIBUTE;
import static com.google.sitebricks.compiler.HtmlParser.SKIP_ATTR;
/**
* @author Shawn based on XMLTemplateCompiler by Dhanji R. Prasanna (dhanji@gmail.com)
*/
@Singleton
public class HtmlTemplateCompiler implements TemplateCompiler {
private final WidgetRegistry registry;
private final PageBook pageBook;
private final SystemMetrics metrics;
//special widget types (built-in symbol table)
private static final String REQUIRE_WIDGET = "@require";
private static final String REPEAT_WIDGET = "repeat";
private static final String CHOOSE_WIDGET = "choose";
@Inject
public HtmlTemplateCompiler(WidgetRegistry registry, PageBook pageBook, SystemMetrics metrics) {
this.registry = registry;
this.pageBook = pageBook;
this.metrics = metrics;
}
//
// compiler state
//
class PageCompilingContext {
Class<?> page;
Template template;
List<CompileError> errors = Lists.newArrayList();
List<CompileError> warnings = Lists.newArrayList();
Element form;
Stack<EvaluatorCompiler> lexicalScopes = new Stack<EvaluatorCompiler>();
}
public Renderable compile(Class<?> page, Template template) {
PageCompilingContext pc = new PageCompilingContext();
pc.page = page;
pc.template = template;
pc.lexicalScopes.push(new MvelEvaluatorCompiler(page));
WidgetChain widgetChain;
widgetChain = walk(pc, HtmlParser.parse(template.getText()));
// TODO - get the errors when !(isValid)
if (!pc.errors.isEmpty() || !pc.warnings.isEmpty()) {
// If there were any errors we must track them.
metrics.logErrorsAndWarnings(page, pc.errors, pc.warnings);
// Only explode if there are errors.
if (!pc.errors.isEmpty())
throw new TemplateCompileException(page, template.getText(), pc.errors, pc.warnings);
}
return widgetChain;
}
private WidgetChain walk(PageCompilingContext pc, List<Node> nodes) {
WidgetChain chain = Chains.proceeding();
for (Node n: nodes)
chain.addWidget(widgetize(pc, n, walk(pc, n)));
return chain;
}
/**
* Walks the DOM recursively, and converts elements into corresponding sitebricks widgets.
*/
@NotNull
private <N extends Node> WidgetChain walk(PageCompilingContext pc, N node) {
WidgetChain widgetChain = Chains.proceeding();
for (Node n: node.childNodes()) {
if (n instanceof Element) {
final Element child = (Element) n;
//push form if this is a form tag
if (child.tagName().equals("form"))
pc.form = (Element) n;
//setup a lexical scope if we're going into a repeat widget (by reading the previous node)
final boolean shouldPopScope = lexicalClimb(pc, child);
//continue recursing down, perform a post-order, depth-first traversal of the DOM
WidgetChain childsChildren;
try {
childsChildren = walk(pc, child);
//process the widget itself into a Renderable with child tree
widgetChain.addWidget(widgetize(pc, child, childsChildren));
} finally {
lexicalDescend(pc, child, shouldPopScope);
}
} else if (n instanceof TextNode) {
TextNode child = (TextNode)n;
Renderable textWidget = null;
//setup a lexical scope if we're going into a repeat widget (by reading the previous node)
final boolean shouldPopScope = lexicalClimb(pc, child);
// construct the text widget
try {
textWidget = registry.textWidget(cleanHtml(n), pc.lexicalScopes.peek());
// if there are no annotations, add the text widget to the chain
if (!child.hasAttr(ANNOTATION_KEY)) {
widgetChain.addWidget(textWidget);
}
else {
// construct a new widget chain for this text node
WidgetChain childsChildren = Chains.proceeding().addWidget(textWidget);
// make a new widget for the annotation, making the text chain the child
String widgetName = child.attr(ANNOTATION_KEY).toLowerCase();
Renderable annotationWidget = registry.newWidget(widgetName, child.attr(ANNOTATION_CONTENT), childsChildren, pc.lexicalScopes.peek());
widgetChain.addWidget(annotationWidget);
}
} catch (ExpressionCompileException e) {
pc.errors.add(
CompileError.in(node.outerHtml())
.near(line(node))
.causedBy(e)
);
}
if (shouldPopScope)
pc.lexicalScopes.pop();
} else if ((n instanceof Comment) || (n instanceof DataNode)) {
//process as raw text widget
try {
widgetChain.addWidget(registry.textWidget(cleanHtml(n), pc.lexicalScopes.peek()));
} catch (ExpressionCompileException e) {
pc.errors.add(
CompileError.in(node.outerHtml())
.near(line(node))
.causedBy(e)
);
}
} else if (n instanceof XmlDeclaration) {
try {
widgetChain.addWidget(registry
.xmlDirectiveWidget(((XmlDeclaration)n).getWholeDeclaration(),
pc.lexicalScopes.peek()));
} catch (ExpressionCompileException e) {
pc.errors.add(
CompileError.in(node.outerHtml())
.near(line(node))
.causedBy(e)
);
}
}
}
//return computed chain, or a terminal
return widgetChain;
}
/**
* Complement of HtmlTemplateCompiler#lexicalClimb().
* This method pops off the stack of lexical scopes when
* we're done processing a sitebricks widget.
*/
private void lexicalDescend(PageCompilingContext pc, Element element, boolean shouldPopScope) {
//pop form
if ("form".equals(element.tagName()))
pc.form = null;
//pop compiler if the scope ends
if (shouldPopScope) {
pc.lexicalScopes.pop();
}
}
/**
* Called to push a new lexical scope onto the stack.
*/
private boolean lexicalClimb(PageCompilingContext pc, Node node) {
if (node.attr(ANNOTATION).length()>1) {
// Setup a new lexical scope (symbol table changes on each scope encountered).
if (REPEAT_WIDGET.equalsIgnoreCase(node.attr(ANNOTATION_KEY))
|| CHOOSE_WIDGET.equalsIgnoreCase(node.attr(ANNOTATION_KEY))) {
String[] keyAndContent = {node.attr(ANNOTATION_KEY), node.attr(ANNOTATION_CONTENT)};
pc.lexicalScopes.push(new MvelEvaluatorCompiler(parseRepeatScope(pc, keyAndContent, node)));
return true;
}
// Setup a new lexical scope for compiling against embedded pages (closures).
final PageBook.Page embed = pageBook.forName(node.attr(ANNOTATION_KEY));
if (null != embed) {
final Class<?> embedClass = embed.pageClass();
MvelEvaluatorCompiler compiler = new MvelEvaluatorCompiler(embedClass);
checkEmbedAgainst(pc, compiler, Parsing.toBindMap(node.attr(ANNOTATION_CONTENT)),
embedClass, node);
pc.lexicalScopes.push(compiler);
return true;
}
}
return false;
}
/**
* This method converts an XML element into a specific kind of widget.
* Special cases are the XML widget, Header, @Require widget. Otherwise a standard
* widget is created.
*/
@SuppressWarnings({"JavaDoc"}) @NotNull
private <N extends Node> Renderable widgetize(PageCompilingContext pc, N node, WidgetChain childsChildren) {
if (node instanceof XmlDeclaration) {
try {
XmlDeclaration decl = (XmlDeclaration)node;
return registry.xmlDirectiveWidget(decl.getWholeDeclaration(), pc.lexicalScopes.peek());
} catch (ExpressionCompileException e) {
pc.errors.add(
CompileError.in(node.outerHtml())
.near(line(node))
.causedBy(e)
);
}
}
// Header widget is a special case, where we match by the name of the tag =(
if ("head".equals(node.nodeName())) {
try {
return registry.headWidget(childsChildren, parseAttribs(node.attributes()), pc.lexicalScopes.peek());
} catch (ExpressionCompileException e) {
pc.errors.add(
CompileError.in(node.outerHtml())
.near(line(node))
.causedBy(e)
);
}
}
String annotation = node.attr(ANNOTATION);
//if there is no annotation, treat as a raw xml-widget (i.e. tag)
if ((null == annotation) || 0 == annotation.trim().length())
try {
checkUriConsistency(pc, node);
checkFormFields(pc, node);
return registry.xmlWidget(childsChildren, node.nodeName(), parseAttribs(node.attributes()),
pc.lexicalScopes.peek());
} catch (ExpressionCompileException e) {
pc.errors.add(
CompileError.in(node.outerHtml())
.near(line(node))
.causedBy(e)
);
return Chains.terminal();
}
// Special case: is this annotated with @Require
// if so, tags in head need to be promoted to head of enclosing page.
if (REQUIRE_WIDGET.equalsIgnoreCase(annotation.trim()))
try {
return registry.requireWidget(cleanHtml(node), pc.lexicalScopes.peek());
} catch (ExpressionCompileException e) {
pc.errors.add(
CompileError.in(node.outerHtml())
.near(line(node))
.causedBy(e)
);
return Chains.terminal();
}
// If this is NOT a self-rendering widget, give it a child.
// final String widgetName = node.attr(ANNOTATION_KEY).trim().toLowerCase());
final String widgetName = node.attr(ANNOTATION_KEY).toLowerCase();
if (!registry.isSelfRendering(widgetName))
try {
childsChildren = Chains.singleton(registry.xmlWidget(childsChildren, node.nodeName(),
parseAttribs(node.attributes()), pc.lexicalScopes.peek()));
} catch (ExpressionCompileException e) {
pc.errors.add(
CompileError.in(node.outerHtml())
.near(line(node))
.causedBy(e)
);
}
// Recursively build widget from [Key, expression, child widgets].
try {
return registry.newWidget(widgetName, node.attr(ANNOTATION_CONTENT), childsChildren, pc.lexicalScopes.peek());
} catch (ExpressionCompileException e) {
pc.errors.add(
CompileError.in(node.outerHtml())
.near(line(node))
.causedBy(e)
);
// This should never be used.
return Chains.terminal();
}
}
private Map<String,Type> parseRepeatScope(PageCompilingContext pc, String[] extract, Node node) {
RepeatToken repeat = registry.parseRepeat(extract[1]);
Map<String, Type> context = Maps.newHashMap();
// Verify that @Repeat was parsed correctly.
if (null == repeat.var()) {
pc.errors.add(
CompileError.in(node.outerHtml())
.near(node.siblingIndex()) // TODO - line number
.causedBy(CompileErrors.MISSING_REPEAT_VAR)
);
}
if (null == repeat.items()) {
pc.errors.add(
CompileError.in(node.outerHtml())
.near(node.siblingIndex()) // TODO - line number
.causedBy(CompileErrors.MISSING_REPEAT_ITEMS)
);
}
try {
Type egressType = pc.lexicalScopes.peek().resolveEgressType(repeat.items());
// convert to collection if we need to
Type elementType;
Class<?> egressClass = Generics.erase(egressType);
if (egressClass.isArray()) {
elementType = Generics.getArrayComponentType(egressType);
}
else if (Collection.class.isAssignableFrom(egressClass)) {
elementType = Generics.getTypeParameter(egressType, Collection.class.getTypeParameters()[0]);
}
else {
pc.errors.add(
CompileError.in(node.outerHtml())
.near(node.siblingIndex()) // TODO - line number
.causedBy(CompileErrors.REPEAT_OVER_ATOM)
);
return Collections.emptyMap();
}
context.put(repeat.var(), elementType);
context.put(repeat.pageVar(), pc.page);
context.put("__page", pc.page);
context.put("index", int.class);
context.put("isLast", boolean.class);
} catch (ExpressionCompileException e) {
pc.errors.add(
CompileError.in(node.outerHtml())
.near(node.siblingIndex()) // TODO - line number
.causedBy(e)
);
}
return context;
}
private void checkFormFields(PageCompilingContext pc, Node element) {
if (null == pc.form)
return;
String action = pc.form.attr("action");
// Only look at contextual uris (i.e. hosted by us).
// TODO - relative, not starting with '/'
if (null == action || (!action.startsWith("/")))
return;
final PageBook.Page page = pageBook.get(action);
// Only look at pages we actually have registered.
if (null == page) {
pc.warnings.add(
CompileError.in(element.outerHtml())
.near(line(element))
.causedBy(CompileErrors.UNRESOLVABLE_FORM_ACTION)
);
return;
}
// If we're inside a form do a throw-away compile against the target page.
if ("input".equals(element.nodeName()) || "textarea".equals(element.nodeName())) {
String name = element.attr("name");
// Skip submits and buttons.
if (skippable(element.attr("type")))
return;
//TODO Skip empty?
if (null == name) {
pc.warnings.add(
CompileError.in(element.outerHtml())
.near(line(element))
.causedBy(CompileErrors.FORM_MISSING_NAME)
);
return;
}
// Compile expression path.
final String expression = name;
try {
new MvelEvaluatorCompiler(page.pageClass())
.compile(expression);
} catch (ExpressionCompileException e) {
//TODO Very hacky, needed to strip out xmlns attribution.
pc.warnings.add(
CompileError.in(element.outerHtml())
.near(element.siblingIndex()) // TODO - line number
.causedBy(CompileErrors.UNRESOLVABLE_FORM_BINDING, e)
);
}
}
}
private void checkUriConsistency(PageCompilingContext pc, Node element) {
String uriAttrib = element.attr("action");
if (null == uriAttrib)
uriAttrib = element.attr("src");
if (null == uriAttrib)
uriAttrib = element.attr("href");
if (null != uriAttrib) {
// Verify that such a uri exists in the page book,
// only if it is contextual--ignore abs & relative URIs.
final String uri = uriAttrib;
if (uri.startsWith("/"))
if (null == pageBook.nonCompilingGet(uri))
pc.warnings.add(
CompileError.in(element.outerHtml())
.near(element.siblingIndex()) // TODO - line number
.causedBy(CompileErrors.UNRESOLVABLE_FORM_ACTION, uri)
);
}
}
/**
* @param attributes A list of attribs
* @return Returns a mutable map parsed out of the attribute list
*/
static Map<String, String> parseAttribs(Attributes attributes) {
Map<String, String> attrs = new LinkedHashMap<String, String>(attributes.size() + 4);
for (Attribute a : attributes.asList())
if (SKIP_ATTR.contains(a.getKey()))
continue;
else
attrs.put(a.getKey(), a.getValue());
return attrs;
}
// Ensures that embed bound properties are writable
private void checkEmbedAgainst(PageCompilingContext pc, EvaluatorCompiler compiler, Map<String, String> properties, Class<?> embedClass, Node node) {
// TODO also type check them against expressions
for (String property : properties.keySet()) {
try {
if (!compiler.isWritable(property)) {
pc.errors.add(
CompileError.in(node.outerHtml())
//TODO we need better line number detection if there is whitespace between the annotation and tag.
.near(node.siblingIndex()-1) // TODO - line number of the annotation
.causedBy(CompileErrors.PROPERTY_NOT_WRITEABLE,
String.format("Property %s#%s was not writable. Did you forget to create "
+ "a setter or @Visible annotation?", embedClass.getSimpleName(), property))
);
}
} catch (ExpressionCompileException ece) {
pc.errors.add(
CompileError.in(node.outerHtml())
.near(node.siblingIndex()) // TODO - line number
.causedBy(CompileErrors.ERROR_COMPILING_PROPERTY)
);
}
}
}
static boolean skippable(String kind) {
if (null == kind)
return false;
return ("submit".equals(kind)
|| "button".equals(kind)
|| "reset".equals(kind)
|| "file".equals(kind));
}
// TESTING jsoup.nodes.Node
/**
Get this node's previous sibling.
@return the previous sibling, or null if this is the first sibling
*/
public Node previousSibling(Node node) {
Preconditions.notNull(node);
List<Node> siblings = findSiblings(node);
if (null == siblings) return null;
Integer index = indexInList(node, siblings);
if (null == index) return null;
if (index > 0)
return siblings.get(index-1);
return null;
}
public List<Node> findSiblings(Node node) {
Preconditions.notNull(node);
Node parent = node.parent();
if (null == parent) return null;
return parent.childNodes();
}
/**
* Get the list index of this node in its node sibling list. I.e. if this is the first node
* sibling, returns 0.
* @return position in node sibling list
* @see org.jsoup.nodes.Element#elementSiblingIndex()
*/
public Integer siblingIndex(Node node) {
if (null != node.parent())
Preconditions.notNull(node);
return indexInList(node, findSiblings(node));
}
protected static <N extends Node> Integer indexInList(N search, List<N> nodes) {
Preconditions.notNull(search);
Preconditions.notNull(nodes);
for (int i = 0; i < nodes.size(); i++) {
N node = nodes.get(i);
if (node.equals(search))
return i;
}
return null;
}
private static int line(Node node) {
return Integer.valueOf(node.attr(LINE_NUMBER_ATTRIBUTE));
}
// outerHtml from jsoup.Node, Element with suppressed _attribs
private static String cleanHtml(final Node node) {
if (node instanceof Element) {
Element element = ((Element) node);
StringBuilder accum = new StringBuilder();
accum.append("<").append(element.tagName());
for (Attribute attribute: element.attributes()) {
if (!(attribute.getKey().startsWith("_"))) {
accum.append(" ");
accum.append(attribute.getKey());
accum.append("=\"");
accum.append(attribute.getValue());
accum.append('"');
}
}
if (element.childNodes().isEmpty() && element.tag().isEmpty()) {
accum.append(" />");
} else {
accum.append(">");
for (Node child : element.childNodes())
accum.append(cleanHtml(child));
accum.append("</").append(element.tagName()).append(">");
}
return accum.toString();
} else if (node instanceof TextNode) {
return ((TextNode) node).getWholeText();
} else if (node instanceof XmlDeclaration) {
// HACK
if (node.childNodes().isEmpty()) {
return "";
}
return node.outerHtml();
} else if (node instanceof Comment) {
// HACK: elide comments for now.
return "";
} else if (node instanceof DataNode && node.childNodes().isEmpty()) {
// No child nodes are defined but we have to handle content if such exists, example
// <script language="JavaScript">var a = { name: "${user.name}"}</script>
String content = node.attr("data");
if (Strings.empty(content)) {
return "";
}
return content;
} else {
return node.outerHtml();
}
}
}