All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.dreamhead.moco.extractor.XPathRequestExtractor Maven / Gradle / Ivy

Go to download

Moco is an easy setup stub framework, mainly focusing on testing and integration.

There is a newer version: 1.5.0
Show newest version
package com.github.dreamhead.moco.extractor;

import com.github.dreamhead.moco.HttpRequest;
import com.github.dreamhead.moco.HttpRequestExtractor;
import com.google.common.base.Optional;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import java.util.List;

import static com.google.common.base.Optional.absent;
import static com.google.common.base.Optional.of;
import static com.google.common.collect.Lists.newArrayList;

public class XPathRequestExtractor extends HttpRequestExtractor {
    private final XmlExtractorHelper helper = new XmlExtractorHelper();
    private final ContentRequestExtractor extractor = new ContentRequestExtractor();
    private final XPathExpression xPathExpression;

    public XPathRequestExtractor(final String xpath) {
        XPathFactory xPathfactory = XPathFactory.newInstance();
        XPath target = xPathfactory.newXPath();
        try {
            xPathExpression = target.compile(xpath);
        } catch (XPathExpressionException e) {
            throw new IllegalArgumentException(e);
        }
    }

    @Override
    protected final Optional doExtract(final HttpRequest request) {
        try {
            Optional source = helper.extractAsInputSource(request, extractor);
            if (!source.isPresent()) {
                return absent();
            }
            NodeList list = (NodeList) xPathExpression.evaluate(source.get(), XPathConstants.NODESET);
            if (list.getLength() == 0) {
                return absent();
            }

            return doExtract(list);
        } catch (XPathExpressionException e) {
            return absent();
        }
    }

    private Optional doExtract(final NodeList list) {
        List values = newArrayList();
        for (int i = 0; i < list.getLength(); i++) {
            Node node = list.item(i);
            values.add(node.getNodeValue());
        }

        return of(values.toArray(new String[values.size()]));
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy