Search in sources :

Example 1 with Document

use of jodd.lagarto.dom.Document 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 Document

use of jodd.lagarto.dom.Document 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 Document

use of jodd.lagarto.dom.Document 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 Document

use of jodd.lagarto.dom.Document 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 Document

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

the class Jerry method append.

// ---------------------------------------------------------------- DOM
/**
	 * Inserts content, specified by the parameter, to the end of each
	 * element in the set of matched elements.
	 */
public Jerry append(String html) {
    if (html == null) {
        html = StringPool.EMPTY;
    }
    final Document doc = builder.parse(html);
    if (nodes.length == 0) {
        return this;
    }
    for (Node node : nodes) {
        Document workingDoc = doc.clone();
        node.addChild(workingDoc.getChildNodes());
    }
    return this;
}
Also used : Node(jodd.lagarto.dom.Node) Document(jodd.lagarto.dom.Document)

Aggregations

Document (jodd.lagarto.dom.Document)13 Node (jodd.lagarto.dom.Node)7 LagartoDOMBuilder (jodd.lagarto.dom.LagartoDOMBuilder)6 Test (org.junit.Test)6 File (java.io.File)4 Jerry (jodd.jerry.Jerry)2 Element (jodd.lagarto.dom.Element)2