public String textOwn() { return text(false); }
protected String text(boolean recursive) { if (children == null || children.isEmpty()) { return null; } if (children.size() == 1) { if (recursive) return children.get(0).text(recursive); else return children.get(0).textOwn(); } StringBuilder sb = new StringBuilder(); text(recursive, sb); return sb.toString(); }
Assert.assertEquals("ololo", new XmlParser().parse(s).text()); Assert.assertEquals("ololo", new XmlParser().parse(s).textOwn()); s = "<xml><child/><child/><child>ololo</child></xml>"; Assert.assertEquals(3, new XmlParser().parse(s).size()); Assert.assertEquals("ololo", new XmlParser().parse(s).text()); Assert.assertEquals("", new XmlParser().parse(s).textOwn()); s = "<xml><child/><child/><child>ololo</child>lo</xml>"; Assert.assertEquals(4, new XmlParser().parse(s).size()); Assert.assertEquals("ololo lo", new XmlParser().parse(s).text()); Assert.assertEquals("lo", new XmlParser().parse(s).textOwn()); Assert.assertEquals("ololo", new XmlParser().parse(s).text());
public String text() { return text(true); }
public boolean check(Node n) { boolean b = false; if (command.equals("text()")) { b = result.equals(n.text()); } else if (command.startsWith("@")) { b = result.equals(n.attr(command.substring(1))); } return b && (next == null || next.check(n)); } }
protected void text(boolean recursive, StringBuilder sb) { if (children == null || children.isEmpty()) return; for (Node node : children) { if (sb.length() > 0) sb.append(' '); if (recursive) node.text(recursive, sb); else { String text = node.ownText(); if (text != null) sb.append(text); } } }
protected StringBuilder toXML(String offset, StringBuilder sb, boolean prettyPrint) { if (prettyPrint) sb.append(offset); if (this instanceof TextNode) { sb.append(text()); if (prettyPrint) sb.append("\n"); } else { sb.append("<").append(name); if (attributes != null) { for (Map.Entry<String, String> attr : attributes.entrySet()) { sb.append(" ").append(attr.getKey()).append("=\"").append(escape(attr.getValue())).append("\""); } } if (children != null) { sb.append(">"); if (prettyPrint) sb.append("\n"); for (Node child : children) child.toXML(prettyPrint ? offset + "\t" : "", sb, prettyPrint); if (prettyPrint) sb.append(offset); sb.append("</").append(name).append(">"); } else sb.append("/>"); if (prettyPrint) sb.append("\n"); } return sb; }
@Test public void gsp_4() throws IOException { String s = "<div><script>\n" + " var a\n" + " for(var i=0; i<10;i++) {\n" + " a+=i;" + " }\n" + "</script></div>"; Node root = new GspParser().parse(s); Node div = root.children().get(0); Assert.assertEquals(1, div.children().size()); Node script = div.children.get(0); Assert.assertEquals("var a\n" + " for(var i=0; i<10;i++) {\n" + " a+=i;" + " }\n", script.text()); }
@Test public void html_3() throws IOException { String s = "<div><script>\n" + " var a\n" + " for(var i=0; i<10;i++) {\n" + " a+=i;" + " }\n" + "</script></div>"; Node root = new HtmlParser().parse(s); Node div = root.children().get(0); Assert.assertEquals(1, div.children().size()); Node script = div.children.get(0); Assert.assertEquals("var a\n" + " for(var i=0; i<10;i++) {\n" + " a+=i;" + " }\n", script.text()); }
@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()); }
@Test public void gsp_3() throws IOException { String s = "<div id=\"${id}\"><span>foo:</span>${foo}</div>"; Node root = new GspParser().parse(s); Node div = root.children().get(0); Assert.assertEquals("div", div.name()); Assert.assertEquals(2, div.children().size()); Assert.assertEquals(1, div.attributes().size()); Assert.assertEquals("${id}", div.attr("id")); Node span = div.children().get(0); Assert.assertEquals("span", span.name()); Assert.assertEquals(1, span.children().size()); Assert.assertEquals("foo:", span.text()); Node foo = div.children().get(1); Assert.assertEquals("${foo}", foo.text()); }