Search in sources :

Example 26 with Node

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

the class Jerry method form.

// ---------------------------------------------------------------- form
/**
	 * Processes all forms, collects all form parameters and calls back the
	 * {@link JerryFormHandler}.
	 */
public Jerry form(String formCssSelector, JerryFormHandler jerryFormHandler) {
    Jerry form = find(formCssSelector);
    // process each form
    for (Node node : form.nodes) {
        Jerry singleForm = new Jerry(this, node);
        final Map<String, String[]> parameters = new HashMap<>();
        // process all input elements
        singleForm.$("input").each(($inputTag, index) -> {
            String type = $inputTag.attr("type");
            if (type == null) {
                type = "text";
            }
            boolean isCheckbox = type.equals("checkbox");
            boolean isRadio = type.equals("radio");
            if (isRadio || isCheckbox) {
                if (!($inputTag.nodes[0].hasAttribute("checked"))) {
                    return true;
                }
            }
            String name = $inputTag.attr("name");
            if (name == null) {
                return true;
            }
            String tagValue = $inputTag.attr("value");
            if (tagValue == null) {
                if (isCheckbox) {
                    tagValue = "on";
                }
            }
            // add tag value
            String[] value = parameters.get(name);
            if (value == null) {
                value = new String[] { tagValue };
            } else {
                value = ArraysUtil.append(value, tagValue);
            }
            parameters.put(name, value);
            return true;
        });
        // process all select elements
        singleForm.$("select").each(($selectTag, index) -> {
            final String name = $selectTag.attr("name");
            $selectTag.$("option").each(($optionTag, index1) -> {
                if ($optionTag.nodes[0].hasAttribute("selected")) {
                    String tagValue = $optionTag.attr("value");
                    // add tag value
                    String[] value = parameters.get(name);
                    if (value == null) {
                        value = new String[] { tagValue };
                    } else {
                        value = ArraysUtil.append(value, tagValue);
                    }
                    parameters.put(name, value);
                }
                return true;
            });
            return true;
        });
        // process all text areas
        singleForm.$("textarea").each(($textarea, index) -> {
            String name = $textarea.attr("name");
            String value = $textarea.text();
            parameters.put(name, new String[] { value });
            return true;
        });
        // done
        jerryFormHandler.onForm(singleForm, parameters);
    }
    return this;
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Node(jodd.lagarto.dom.Node)

Example 27 with Node

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

the class Jerry method prevAll.

/**
	 * Get all preceding siblings of each element in the set of matched 
	 * elements, optionally filtered by a selector.
	 */
public Jerry prevAll() {
    List<Node> result = new NodeList(nodes.length);
    if (nodes.length > 0) {
        for (Node node : nodes) {
            Node currentSiblingElement = node.getPreviousSiblingElement();
            while (currentSiblingElement != null) {
                result.add(currentSiblingElement);
                currentSiblingElement = currentSiblingElement.getPreviousSiblingElement();
            }
        }
    }
    return new Jerry(this, result);
}
Also used : Node(jodd.lagarto.dom.Node)

Example 28 with Node

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

the class Jerry method prepend.

/**
	 * Insert content, specified by the parameter, to the beginning of each 
	 * element in the set of matched elements.
	 */
public Jerry prepend(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.insertChild(workingDoc.getChildNodes(), 0);
    }
    return this;
}
Also used : Node(jodd.lagarto.dom.Node) Document(jodd.lagarto.dom.Document)

Example 29 with Node

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

the class Jerry method css.

/**
	 * Sets one or more CSS properties for the set of matched elements.
	 */
public Jerry css(String... css) {
    if (nodes.length == 0) {
        return this;
    }
    for (Node node : nodes) {
        String styleAttrValue = node.getAttribute("style");
        Map<String, String> styles = createPropertiesMap(styleAttrValue, ';', ':');
        for (int i = 0; i < css.length; i += 2) {
            String propertyName = css[i];
            propertyName = StringUtil.fromCamelCase(propertyName, '-');
            String value = css[i + 1];
            if (value.length() == 0) {
                styles.remove(propertyName);
            } else {
                styles.put(propertyName, value);
            }
        }
        styleAttrValue = generateAttributeValue(styles, ';', ':');
        node.setAttribute("style", styleAttrValue);
    }
    return this;
}
Also used : Node(jodd.lagarto.dom.Node)

Example 30 with Node

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

Node (jodd.lagarto.dom.Node)30 Document (jodd.lagarto.dom.Document)7 NodeSelector (jodd.lagarto.dom.NodeSelector)6 LagartoDOMBuilder (jodd.lagarto.dom.LagartoDOMBuilder)2 Test (org.junit.Test)2 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 Text (jodd.lagarto.dom.Text)1