Search in sources :

Example 76 with XPath

use of javax.xml.xpath.XPath in project oxCore by GluuFederation.

the class XmlService method getNodeValue.

public String getNodeValue(Document xmlDocument, String xPathExpression, String attributeName) throws XPathExpressionException {
    XPath xPath = XPathFactory.newInstance().newXPath();
    XPathExpression formXPathExpression = xPath.compile(xPathExpression);
    if (StringHelper.isEmpty(attributeName)) {
        String nodeValue = (String) formXPathExpression.evaluate(xmlDocument, XPathConstants.STRING);
        return nodeValue;
    }
    Node node = ((Node) formXPathExpression.evaluate(xmlDocument, XPathConstants.NODE));
    if (node == null) {
        return null;
    }
    Node attributeNode = node.getAttributes().getNamedItem(attributeName);
    if (attributeNode == null) {
        return null;
    }
    return attributeNode.getNodeValue();
}
Also used : XPath(javax.xml.xpath.XPath) XPathExpression(javax.xml.xpath.XPathExpression) Node(org.w3c.dom.Node)

Example 77 with XPath

use of javax.xml.xpath.XPath in project oxTrust by GluuFederation.

the class LogDir method readConfig.

private List<LogDir> readConfig(String source) {
    List<LogDir> logDirs = new ArrayList<LogDir>();
    try {
        org.w3c.dom.Document document = xmlService.getXmlDocument(FileUtils.readFileToByteArray(new File(source)));
        XPath xPath = XPathFactory.newInstance().newXPath();
        XPathExpression entriesXPath = xPath.compile("/entries/entry");
        NodeList list = (NodeList) entriesXPath.evaluate(document, XPathConstants.NODESET);
        for (int i = 0; i < list.getLength(); i++) {
            Node node = list.item(i);
            String prefix = null;
            String location = null;
            String extension = null;
            NodeList subList = node.getChildNodes();
            for (int j = 0; j < subList.getLength(); j++) {
                Node subNode = subList.item(j);
                String subNodeName = subNode.getNodeName();
                String subNodeValue = subNode.getTextContent();
                if (StringHelper.equalsIgnoreCase(subNodeName, "prefix")) {
                    prefix = subNodeValue;
                } else if (StringHelper.equalsIgnoreCase(subNodeName, "location")) {
                    location = subNodeValue;
                } else if (StringHelper.equalsIgnoreCase(subNodeName, "extension")) {
                    extension = subNodeValue;
                }
            }
            if (extension == null || extension.trim().equals("")) {
                extension = "log";
            }
            LogDir logDir = new LogDir(prefix, location, extension);
            logDirs.add(logDir);
            log.debug("Prefix: " + prefix + " Location: " + location);
        }
    } catch (Exception ex) {
        log.debug("Exception while reading configuration file: " + ex);
    }
    return logDirs;
}
Also used : XPath(javax.xml.xpath.XPath) XPathExpression(javax.xml.xpath.XPathExpression) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) ArrayList(java.util.ArrayList) File(java.io.File)

Example 78 with XPath

use of javax.xml.xpath.XPath in project oxTrust by GluuFederation.

the class FilterService method parseFilters.

public void parseFilters(GluuSAMLTrustRelationship trustRelationship) throws SAXException, IOException, ParserConfigurationException, FactoryConfigurationError, XPathExpressionException {
    if (trustRelationship.getGluuSAMLMetaDataFilter() != null) {
        XPath xPath = XPathFactory.newInstance().newXPath();
        for (String filterXML : trustRelationship.getGluuSAMLMetaDataFilter()) {
            Document xmlDocument = xmlService.getXmlDocument(filterXML.getBytes());
            if (xmlDocument.getFirstChild().getAttributes().getNamedItem("xsi:type").getNodeValue().equals(VALIDATION_TYPE)) {
                MetadataFilter filter = createMetadataFilter("validation");
                XPathExpression contactCountXPath = xPath.compile("count(/MetadataFilter/ExtensionSchema)");
                int schemasNumber = Integer.parseInt(contactCountXPath.evaluate(xmlDocument));
                for (int i = 1; i <= schemasNumber; i++) {
                    contactCountXPath = xPath.compile("/MetadataFilter/ExtensionSchema[" + i + "]");
                    filter.getExtensionSchemas().add(contactCountXPath.evaluate(xmlDocument));
                }
                trustRelationship.getMetadataFilters().put("validation", filter);
                continue;
            }
            if (xmlDocument.getFirstChild().getAttributes().getNamedItem("xsi:type").getNodeValue().equals(ENTITY_ROLE_WHITE_LIST_TYPE)) {
                MetadataFilter filter = createMetadataFilter("entityRoleWhiteList");
                filter.setRemoveRolelessEntityDescriptors(Boolean.parseBoolean(xmlDocument.getFirstChild().getAttributes().getNamedItem("removeRolelessEntityDescriptors").getNodeValue()));
                filter.setRemoveEmptyEntitiesDescriptors(Boolean.parseBoolean(xmlDocument.getFirstChild().getAttributes().getNamedItem("removeEmptyEntitiesDescriptors").getNodeValue()));
                XPathExpression contactCountXPath = xPath.compile("count(/MetadataFilter/RetainedRole)");
                int schemasNumber = Integer.parseInt(contactCountXPath.evaluate(xmlDocument));
                for (int i = 1; i <= schemasNumber; i++) {
                    contactCountXPath = xPath.compile("/MetadataFilter/RetainedRole[" + i + "]");
                    filter.getRetainedRoles().add(contactCountXPath.evaluate(xmlDocument));
                }
                trustRelationship.getMetadataFilters().put("entityRoleWhiteList", filter);
                continue;
            }
            if (xmlDocument.getFirstChild().getAttributes().getNamedItem("xsi:type").getNodeValue().equals(VALID_UNTIL_REQUIRED_TYPE)) {
                MetadataFilter filter = createMetadataFilter("requiredValidUntil");
                filter.setMaxValidityInterval(Integer.parseInt(xmlDocument.getFirstChild().getAttributes().getNamedItem("maxValidityInterval").getNodeValue()));
                trustRelationship.getMetadataFilters().put("requiredValidUntil", filter);
                continue;
            }
            if (xmlDocument.getFirstChild().getAttributes().getNamedItem("xsi:type").getNodeValue().equals(SIGNATURE_VALIDATION_TYPE)) {
                MetadataFilter filter = createMetadataFilter("signatureValidation");
                filter.setFilterCertFileName(StringHelper.removePunctuation(trustRelationship.getInum()));
                trustRelationship.getMetadataFilters().put("signatureValidation", filter);
                continue;
            }
        }
    }
}
Also used : XPath(javax.xml.xpath.XPath) XPathExpression(javax.xml.xpath.XPathExpression) MetadataFilter(org.gluu.oxtrust.model.MetadataFilter) Document(org.w3c.dom.Document)

Example 79 with XPath

use of javax.xml.xpath.XPath in project uPortal by Jasig.

the class SimpleLayout method getRootId.

@Override
public String getRootId() {
    String rootNode = null;
    try {
        String expression = "/layout/folder";
        XPathFactory fac = XPathFactory.newInstance();
        XPath xpath = fac.newXPath();
        Element rootNodeE = (Element) xpath.evaluate(expression, layout, XPathConstants.NODE);
        rootNode = rootNodeE.getAttribute("ID");
    } catch (Exception e) {
        log.error("Error getting root id.", e);
    }
    return rootNode;
}
Also used : XPath(javax.xml.xpath.XPath) XPathFactory(javax.xml.xpath.XPathFactory) Element(org.w3c.dom.Element) XPathExpressionException(javax.xml.xpath.XPathExpressionException) PortalException(org.apereo.portal.PortalException)

Example 80 with XPath

use of javax.xml.xpath.XPath in project sling by apache.

the class OsgiMetadataUtil method queryNodes.

private static NodeList queryNodes(Document metadata, String xpathQuery) {
    try {
        XPath xpath = XPATH_FACTORY.newXPath();
        xpath.setNamespaceContext(NAMESPACE_CONTEXT);
        return (NodeList) xpath.evaluate(xpathQuery, metadata, XPathConstants.NODESET);
    } catch (XPathExpressionException ex) {
        throw new RuntimeException("Error evaluating XPath: " + xpathQuery, ex);
    }
}
Also used : XPath(javax.xml.xpath.XPath) XPathExpressionException(javax.xml.xpath.XPathExpressionException) NodeList(org.w3c.dom.NodeList)

Aggregations

XPath (javax.xml.xpath.XPath)526 Document (org.w3c.dom.Document)254 NodeList (org.w3c.dom.NodeList)230 XPathFactory (javax.xml.xpath.XPathFactory)215 Node (org.w3c.dom.Node)171 XPathExpressionException (javax.xml.xpath.XPathExpressionException)159 XPathExpression (javax.xml.xpath.XPathExpression)142 DocumentBuilder (javax.xml.parsers.DocumentBuilder)125 Element (org.w3c.dom.Element)118 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)97 IOException (java.io.IOException)89 Test (org.junit.Test)80 InputSource (org.xml.sax.InputSource)59 File (java.io.File)57 SAXException (org.xml.sax.SAXException)57 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)51 ByteArrayInputStream (java.io.ByteArrayInputStream)44 ArrayList (java.util.ArrayList)44 InputStream (java.io.InputStream)39 DSNamespaceContext (org.apache.xml.security.test.dom.DSNamespaceContext)37