Search in sources :

Example 1 with LagartoDOMBuilder

use of jodd.lagarto.dom.LagartoDOMBuilder in project jodd by oblac.

the class ParsingProblemsTest method testIssue23.

@Test
public void testIssue23() throws IOException {
    File file = new File(testDataRoot, "index-4.html");
    LagartoDOMBuilder lagartoDOMBuilder = new LagartoDOMBuilder();
    lagartoDOMBuilder.getConfig().setCalculatePosition(true);
    lagartoDOMBuilder.getConfig().setCollectErrors(true);
    Document document = lagartoDOMBuilder.parse(FileUtil.readString(file));
    assertTrue(document.check());
    // (1564 open DTs + 1564 open DDs) 1 open P
    assertEquals(19, document.getErrors().size());
    Jerry doc = Jerry.jerry(FileUtil.readString(file));
    assertEquals(16, doc.$("td.NavBarCell1").size());
    assertEquals(2, doc.$("table td.NavBarCell1Rev").size());
    assertEquals(1, doc.$("dl").size());
    assertEquals(1564, doc.$("dd").size());
    assertEquals(1564, doc.$("dt").size());
    assertEquals(3144, doc.$("dt a").size());
    // http://docs.oracle.com/javase/6/docs/api/index-files/index-4.html
    file = new File(testDataRoot, "index-4-eng.html");
    doc = Jerry.jerry(FileUtil.readString(file));
    assertEquals(16, doc.$("td.NavBarCell1").size());
    assertEquals(2, doc.$("table td.NavBarCell1Rev").size());
    final StringBuilder sb = new StringBuilder();
    doc.$("td.NavBarCell1").each(($this, index) -> {
        sb.append("---\n");
        sb.append($this.text().trim());
        sb.append('\n');
        return true;
    });
    String s = sb.toString();
    s = StringUtil.remove(s, ' ');
    s = StringUtil.remove(s, '\r');
    s = StringUtil.remove(s, ' ');
    s = StringUtil.remove(s, " ");
    assertEquals("---\n" + "Overview\n" + "Package\n" + "Class\n" + "Use\n" + "Tree\n" + "Deprecated\n" + "Index\n" + "Help\n" + "---\n" + "Overview\n" + "---\n" + "Package\n" + "---\n" + "Class\n" + "---\n" + "Use\n" + "---\n" + "Tree\n" + "---\n" + "Deprecated\n" + "---\n" + "Help\n" + "---\n" + "Overview\n" + "Package\n" + "Class\n" + "Use\n" + "Tree\n" + "Deprecated\n" + "Index\n" + "Help\n" + "---\n" + "Overview\n" + "---\n" + "Package\n" + "---\n" + "Class\n" + "---\n" + "Use\n" + "---\n" + "Tree\n" + "---\n" + "Deprecated\n" + "---\n" + "Help\n", s);
}
Also used : LagartoDOMBuilder(jodd.lagarto.dom.LagartoDOMBuilder) Document(jodd.lagarto.dom.Document) Jerry(jodd.jerry.Jerry) File(java.io.File) Test(org.junit.Test)

Example 2 with LagartoDOMBuilder

use of jodd.lagarto.dom.LagartoDOMBuilder in project jodd by oblac.

the class ParsingProblemsTest method testIssue23_0.

@Test
public void testIssue23_0() throws IOException {
    File file = new File(testDataRoot, "index-4-v0.html");
    LagartoDOMBuilder lagartoDOMBuilder = new LagartoDOMBuilder();
    lagartoDOMBuilder.getConfig().setCalculatePosition(true);
    lagartoDOMBuilder.getConfig().setCollectErrors(true);
    Document doc = lagartoDOMBuilder.parse(FileUtil.readString(file));
    assertTrue(doc.check());
    assertEquals(1, doc.getErrors().size());
}
Also used : LagartoDOMBuilder(jodd.lagarto.dom.LagartoDOMBuilder) Document(jodd.lagarto.dom.Document) File(java.io.File) Test(org.junit.Test)

Example 3 with LagartoDOMBuilder

use of jodd.lagarto.dom.LagartoDOMBuilder in project jodd by oblac.

the class ParsingProblemsTest method testNamespaces.

@Test
public void testNamespaces() throws IOException {
    File file = new File(testDataRoot, "namespace.xml");
    LagartoDOMBuilder lagartoDOMBuilder = new LagartoDOMBuilder();
    lagartoDOMBuilder.enableXmlMode();
    lagartoDOMBuilder.getConfig().setCalculatePosition(true);
    Document doc = lagartoDOMBuilder.parse(FileUtil.readString(file));
    assertTrue(doc.check());
    Element cfgTestElement = (Element) doc.getChild(1);
    assertEquals("cfg:test", cfgTestElement.getNodeName());
    Element cfgNode = (Element) cfgTestElement.getChild(0);
    assertEquals("cfg:node", cfgNode.getNodeName());
    Jerry.JerryParser jerryParser = new Jerry.JerryParser();
    ((LagartoDOMBuilder) jerryParser.getDOMBuilder()).enableXmlMode();
    Jerry jerry = jerryParser.parse(FileUtil.readString(file));
    final StringBuilder result = new StringBuilder();
    jerry.$("cfg\\:test").each(($this, index) -> {
        result.append($this.$("cfg\\:node").text());
        return true;
    });
    assertEquals("This is a text", result.toString());
}
Also used : Element(jodd.lagarto.dom.Element) LagartoDOMBuilder(jodd.lagarto.dom.LagartoDOMBuilder) Document(jodd.lagarto.dom.Document) Jerry(jodd.jerry.Jerry) File(java.io.File) Test(org.junit.Test)

Example 4 with LagartoDOMBuilder

use of jodd.lagarto.dom.LagartoDOMBuilder in project jodd by oblac.

the class ParsingProblemsTest method testNonQuotedAttributeValue.

@Test
public void testNonQuotedAttributeValue() {
    String html = "<a href=123>xxx</a>";
    LagartoDOMBuilder lagartoDOMBuilder = new LagartoDOMBuilder();
    lagartoDOMBuilder.getConfig().setCalculatePosition(true);
    Document document = lagartoDOMBuilder.parse(html);
    assertEquals("<a href=\"123\">xxx</a>", document.getHtml());
    assertTrue(document.check());
    html = "<a href=../org/w3c/dom/'http://www.w3.org/TR/2001/REC-xmlschema-1-20010502/#element-list'>xxx</a>";
    lagartoDOMBuilder = new LagartoDOMBuilder();
    lagartoDOMBuilder.getConfig().setCalculatePosition(true);
    document = lagartoDOMBuilder.parse(html);
    assertTrue(document.check());
    assertEquals("<a href=\"../org/w3c/dom/'http://www.w3.org/TR/2001/REC-xmlschema-1-20010502/#element-list'\">xxx</a>", document.getHtml());
}
Also used : LagartoDOMBuilder(jodd.lagarto.dom.LagartoDOMBuilder) Document(jodd.lagarto.dom.Document) Test(org.junit.Test)

Example 5 with LagartoDOMBuilder

use of jodd.lagarto.dom.LagartoDOMBuilder in project jodd by oblac.

the class JerryMiscTest method test250.

@Test
public void test250() {
    String html = "<html>\n" + "  <body>\n" + "    <a href=\"/go?to=foobar&index=null\" title=\"Choice 1\">link</a>\n" + "  </body>\n" + "</html>";
    LagartoDOMBuilder domBuilder = new LagartoDOMBuilder();
    NodeSelector nodeSelector = new NodeSelector(domBuilder.parse(html));
    List<Node> selectedNodes = nodeSelector.select("a[title='Choice 1']");
    System.out.println();
    assertEquals("/go?to=foobar&index=null", selectedNodes.get(0).getAttribute("href"));
}
Also used : Node(jodd.lagarto.dom.Node) LagartoDOMBuilder(jodd.lagarto.dom.LagartoDOMBuilder) NodeSelector(jodd.lagarto.dom.NodeSelector) Test(org.junit.Test)

Aggregations

LagartoDOMBuilder (jodd.lagarto.dom.LagartoDOMBuilder)9 Test (org.junit.Test)8 Document (jodd.lagarto.dom.Document)6 File (java.io.File)5 Jerry (jodd.jerry.Jerry)3 Element (jodd.lagarto.dom.Element)2 Node (jodd.lagarto.dom.Node)2 NodeSelector (jodd.lagarto.dom.NodeSelector)2