Search in sources :

Example 6 with LagartoDOMBuilder

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

the class JerryParserTest method testAttributeCaseSensitive.

@Test
public void testAttributeCaseSensitive() {
    String str = "<dIV id='one' myAttr='aaa'>xxx</dIV>";
    Jerry.JerryParser jerryParser = Jerry.jerry();
    ((LagartoDOMBuilder) jerryParser.getDOMBuilder()).enableHtmlMode();
    // default, case not sensitive
    Jerry doc = jerryParser.parse(str);
    Document document = (Document) doc.get(0);
    Element divNode = (Element) document.getChild(0);
    assertEquals("div", divNode.getNodeName());
    assertNotNull(divNode.getAttribute("myattr"));
    assertNotNull(divNode.getAttribute("myAttr"));
    Element divNode2 = (Element) doc.$("div[myattr=aaa]").nodes[0];
    assertSame(divNode, divNode2);
    assertEquals("<div id=\"one\" myattr=\"aaa\">xxx</div>", doc.html());
    // case sensitive
    ((LagartoDOMBuilder) jerryParser.getDOMBuilder()).getConfig().setCaseSensitive(true);
    doc = jerryParser.parse(str);
    document = (Document) doc.get(0);
    divNode = (Element) document.getChild(0);
    assertEquals("dIV", divNode.getNodeName());
    assertNull(divNode.getAttribute("myattr"));
    assertEquals("<dIV id=\"one\" myAttr=\"aaa\">xxx</dIV>", doc.html());
    assertEquals(0, doc.$("div[myattr=aaa]").nodes.length);
    divNode2 = (Element) doc.$("dIV[myAttr=aaa]").nodes[0];
    assertSame(divNode, divNode2);
}
Also used : Element(jodd.lagarto.dom.Element) LagartoDOMBuilder(jodd.lagarto.dom.LagartoDOMBuilder) Document(jodd.lagarto.dom.Document) Test(org.junit.Test)

Example 7 with LagartoDOMBuilder

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

the class ParsingProblemsTest method testPreserveCC.

@Test
public void testPreserveCC() throws IOException {
    File file = new File(testDataRoot, "preserve-cc.html");
    String expectedResult = FileUtil.readString(file);
    Jerry.JerryParser jerryParser = new Jerry.JerryParser();
    ((LagartoDOMBuilder) jerryParser.getDOMBuilder()).enableHtmlMode();
    ((LagartoDOMBuilder) jerryParser.getDOMBuilder()).getConfig().setEnableConditionalComments(false);
    Jerry jerry = jerryParser.parse(expectedResult);
    String result = jerry.html();
    assertEquals(expectedResult, result);
}
Also used : LagartoDOMBuilder(jodd.lagarto.dom.LagartoDOMBuilder) Jerry(jodd.jerry.Jerry) File(java.io.File) Test(org.junit.Test)

Example 8 with LagartoDOMBuilder

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

the class ParsingProblemsTest method testIssue23_1.

@Test
public void testIssue23_1() throws IOException {
    File file = new File(testDataRoot, "index-4-v1.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 9 with LagartoDOMBuilder

use of jodd.lagarto.dom.LagartoDOMBuilder in project jmeter by apache.

the class JoddExtractor method extract.

/**
     * @see org.apache.jmeter.extractor.Extractor#extract(String, String, int, String, List, int, String)
     */
@Override
public int extract(String expression, String attribute, int matchNumber, String inputString, List<String> result, int found, String cacheKey) {
    NodeSelector nodeSelector;
    if (cacheKey != null) {
        nodeSelector = (NodeSelector) JMeterContextService.getContext().getSamplerContext().get(CACHE_KEY_PREFIX + cacheKey);
        if (nodeSelector == null) {
            LagartoDOMBuilder domBuilder = new LagartoDOMBuilder();
            jodd.lagarto.dom.Document doc = domBuilder.parse(inputString);
            nodeSelector = new NodeSelector(doc);
            JMeterContextService.getContext().getSamplerContext().put(CACHE_KEY_PREFIX + cacheKey, nodeSelector);
        }
    } else {
        LagartoDOMBuilder domBuilder = new LagartoDOMBuilder();
        jodd.lagarto.dom.Document doc = domBuilder.parse(inputString);
        nodeSelector = new NodeSelector(doc);
    }
    List<Node> elements = nodeSelector.select(expression);
    for (Node element : elements) {
        if (matchNumber <= 0 || found != matchNumber) {
            result.add(extractValue(attribute, element));
            found++;
        } else {
            break;
        }
    }
    return found;
}
Also used : Node(jodd.lagarto.dom.Node) NodeSelector(jodd.lagarto.dom.NodeSelector) LagartoDOMBuilder(jodd.lagarto.dom.LagartoDOMBuilder)

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