protected void readImports(Node html, Set<String> imports) { Node page = html.get("%@"); if (page != null && page.hasAttr("page")) { html.children().remove(page); if (page.hasAttr("import")) { imports.addAll(Arrays.asList(page.attr("import").split("; *"))); } } }
@Override public void decorate(Node from, Node to) { replace(to.find("g:layoutHead"), from.get("html/head")); } }
@Override public void decorate(Node from, Node to) { replace(to.find("g:layoutBody"), from.get("html/body")); } }
public Node get(String path) { if (children == null || path == null) return null; int l = path.indexOf('/'); if (l == -1) { l = path.indexOf("["); XPathExpression exp = null; if (l != -1) { exp = new XPathExpression(path.substring(l, path.length())); path = path.substring(0, l); } for (Node node : children) { if (node.name != null && node.name.equals(path) && (exp == null || exp.check(node))) return node; } } else { String tag = path.substring(0, l); path = path.substring(l + 1); for (Node node : children) { if (node.name != null && node.name.equals(tag)) { Node r = node.get(path); if (r != null) return r; } } } return null; }
@Override public void decorate(Node from, Node to) { Node title = from.get("html/head/title"); if (title != null) title.parent().children().remove(title); Node layoutTitle = to.find("g:layoutTitle"); String defaultTitle = layoutTitle.attr("default"); if (defaultTitle == null) defaultTitle = ""; replace(layoutTitle, title != null ? title : new Node("title").addText(defaultTitle)); }
@Test public void html_doctype2() throws IOException { String s = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\" \"http://www.w3.org/TR/html4/strict.dtd\">\n"; Node doc = new HtmlParser().parse(s); Assert.assertEquals(1, doc.children().size()); Assert.assertEquals("!DOCTYPE", doc.get(0).name()); Assert.assertEquals(4, doc.get(0).attributes().size()); Assert.assertEquals(true, doc.get(0).attributes().containsKey("HTML")); Assert.assertEquals(null, doc.get(0).attributes().get("HTML")); Assert.assertEquals(true, doc.get(0).attributes().containsKey("PUBLIC")); Assert.assertEquals(null, doc.get(0).attributes().get("PUBLIC")); Assert.assertEquals(true, doc.get(0).attributes().containsKey("\"-//W3C//DTD HTML 4.01//EN\"")); Assert.assertEquals(null, doc.get(0).attributes().get("\"-//W3C//DTD HTML 4.01//EN\"")); Assert.assertEquals(true, doc.get(0).attributes().containsKey("\"http://www.w3.org/TR/html4/strict.dtd\"")); Assert.assertEquals(null, doc.get(0).attributes().get("\"http://www.w3.org/TR/html4/strict.dtd\"")); }
@Test public void html_doctype() throws IOException { String s = "<!DOCTYPE html>"; Node doc = new HtmlParser().parse(s); Assert.assertEquals(1, doc.children().size()); Assert.assertEquals("!DOCTYPE", doc.get(0).name()); Assert.assertEquals(1, doc.get(0).attributes().size()); Assert.assertEquals(true, doc.get(0).attributes().containsKey("html")); Assert.assertEquals(null, doc.get(0).attributes().get("html")); }
protected RenderableList prepareView(String view, String offset) { String template = resourceTools.getResourceAsString(view); if (template == null) throw new IllegalArgumentException("view '" + view + "' not found"); String dir = view.substring(0, view.lastIndexOf("/") + 1); RenderableList l = new RenderableList(); Node html = new GspParser().parse(template); Set<String> imports = new HashSet<>(); readImports(html, imports); Node layoutTag = html.get("html/head/meta[@name=layout]"); if (layoutTag != null) { layoutTag.parent().children().remove(layoutTag); String layoutContent = resourceTools.getResourceAsString("views/layouts/" + layoutTag.attr("content") + ".gsp"); if (layoutContent == null) throw new IllegalArgumentException("Layout '" + layoutTag.attr("content") + "' not found"); Node layout = new GspParser().parse(layoutContent); readImports(layout, imports); for (Decorator decorator : DependencyFactory.get(DecoratorLib.class).list()) { decorator.decorate(html, layout); } html = layout; } prepare(html.children(), l, dir, offset, imports); return l; }
@Test public void gsp_page() throws IOException { String s = "<%@ page contentType=\"text/html;charset=UTF-8\" %>\n" + "<!DOCTYPE html>\n" + "<html>\n" + "<head>\n" + " <title>title</title>\n" + " %{-- comment! --}%\n" + "</head>" + "</html>"; Node doc = new GspParser().parse(s); Assert.assertEquals(3, doc.children().size()); Assert.assertEquals("%@", doc.get(0).name()); Assert.assertEquals("!DOCTYPE", doc.get(1).name()); Assert.assertEquals("html", doc.get(2).name()); }
@Test public void gsp_page2() throws IOException { String s = "<%@ page contentType=\"text/html;charset=UTF-8\" %>\n" + "<!DOCTYPE html>\n" + "<html>\n" + "<head>\n" + " <title>title</title>\n" + "</head>" + "</html>"; Node doc = new GspParser().parse(s); Assert.assertEquals(3, doc.children().size()); Assert.assertEquals("%@", doc.get(0).name()); Assert.assertEquals("!DOCTYPE", doc.get(1).name()); Assert.assertEquals("html", doc.get(2).name()); } }
@Test public void gsp_comment_4() throws IOException { String s = "<div>\n" + " before\n" + " %{--<p>text</p>--}%\n" + " after\n" + "</div>"; Node root = new GspParser().parse(s); Node div = root.children().get(0); Assert.assertEquals("div", div.name()); Assert.assertEquals(3, div.children().size()); Assert.assertEquals("before", div.get(0).text()); Assert.assertEquals("%{--<p>text</p>--}%", div.get(1).text()); Assert.assertEquals("after", div.get(2).text()); }
@Test public void gsp_html_comment() throws IOException { String s = "<div>\n" + " before\n" + " <!--<p>text</p>-->\n" + " after\n" + "</div>"; Node root = new GspParser().parse(s); Node div = root.children().get(0); Assert.assertEquals("div", div.name()); Assert.assertEquals(3, div.children().size()); Assert.assertEquals("before", div.get(0).text()); Assert.assertEquals("<!--<p>text</p>-->", div.get(1).text()); Assert.assertEquals("after", div.get(2).text()); }