class XPathHelper {

    public static List<Node> getNodes(Node node, String xPath, Map<String, String> namespaces) {
        Validate.notNull(node, "node must not be null.");
        Validate.notEmpty(xPath, "xPath must not be empty.");

        List<Node> ret = new ArrayList<>();

        XPathFactory factory = XPathFactory.newInstance();
        XPath xPathObject = factory.newXPath();

        MyNamespaceContext namespaceContext = new MyNamespaceContext();
        namespaceContext.addNamespace("xhtml", XHTML_NAMESPACE);
        if (namespaces != null) {
            for (Entry<String, String> entry : namespaces.entrySet()) {
                String prefix = entry.getKey();
                String uri = entry.getValue();
                namespaceContext.addNamespace(prefix, uri);
            }
        }
        xPathObject.setNamespaceContext(namespaceContext);
        try {
            XPathExpression xPathExpression = xPathObject.compile(xPath);
            NodeList nodes = (NodeList) xPathExpression.evaluate(node, XPathConstants.NODESET);
            for (int i = 0; i < nodes.getLength(); i++) {
                ret.add(nodes.item(i));
            }
        } catch (XPathExpressionException e) {
            // TODO this exception should be thrown
            LOGGER.error("{} for XPath \"{}\" : {}", new Object[]{e, xPath, e.getMessage(), e});
        }

        return ret;
    }

    private  XPathHelper();

}