Search in sources :

Example 36 with XPathFactory

use of javax.xml.xpath.XPathFactory in project lucene-solr by apache.

the class SimplePostTool method getNodesFromXP.

//
// Utility methods for XPath handing
//
/**
   * Gets all nodes matching an XPath
   */
public static NodeList getNodesFromXP(Node n, String xpath) throws XPathExpressionException {
    XPathFactory factory = XPathFactory.newInstance();
    XPath xp = factory.newXPath();
    XPathExpression expr = xp.compile(xpath);
    return (NodeList) expr.evaluate(n, XPathConstants.NODESET);
}
Also used : XPath(javax.xml.xpath.XPath) XPathExpression(javax.xml.xpath.XPathExpression) XPathFactory(javax.xml.xpath.XPathFactory) NodeList(org.w3c.dom.NodeList)

Example 37 with XPathFactory

use of javax.xml.xpath.XPathFactory in project lucene-solr by apache.

the class EnumField method init.

/**
   * {@inheritDoc}
   */
@Override
protected void init(IndexSchema schema, Map<String, String> args) {
    super.init(schema, args);
    enumsConfigFile = args.get(PARAM_ENUMS_CONFIG);
    if (enumsConfigFile == null) {
        throw new SolrException(SolrException.ErrorCode.NOT_FOUND, "No enums config file was configured.");
    }
    enumName = args.get(PARAM_ENUM_NAME);
    if (enumName == null) {
        throw new SolrException(SolrException.ErrorCode.NOT_FOUND, "No enum name was configured.");
    }
    InputStream is = null;
    try {
        is = schema.getResourceLoader().openResource(enumsConfigFile);
        final DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        try {
            final Document doc = dbf.newDocumentBuilder().parse(is);
            final XPathFactory xpathFactory = XPathFactory.newInstance();
            final XPath xpath = xpathFactory.newXPath();
            final String xpathStr = String.format(Locale.ROOT, "/enumsConfig/enum[@name='%s']", enumName);
            final NodeList nodes = (NodeList) xpath.evaluate(xpathStr, doc, XPathConstants.NODESET);
            final int nodesLength = nodes.getLength();
            if (nodesLength == 0) {
                String exceptionMessage = String.format(Locale.ENGLISH, "No enum configuration found for enum '%s' in %s.", enumName, enumsConfigFile);
                throw new SolrException(SolrException.ErrorCode.NOT_FOUND, exceptionMessage);
            }
            if (nodesLength > 1) {
                if (log.isWarnEnabled())
                    log.warn("More than one enum configuration found for enum '{}' in {}. The last one was taken.", enumName, enumsConfigFile);
            }
            final Node enumNode = nodes.item(nodesLength - 1);
            final NodeList valueNodes = (NodeList) xpath.evaluate("value", enumNode, XPathConstants.NODESET);
            for (int i = 0; i < valueNodes.getLength(); i++) {
                final Node valueNode = valueNodes.item(i);
                final String valueStr = valueNode.getTextContent();
                if ((valueStr == null) || (valueStr.length() == 0)) {
                    final String exceptionMessage = String.format(Locale.ENGLISH, "A value was defined with an no value in enum '%s' in %s.", enumName, enumsConfigFile);
                    throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, exceptionMessage);
                }
                if (enumStringToIntMap.containsKey(valueStr)) {
                    final String exceptionMessage = String.format(Locale.ENGLISH, "A duplicated definition was found for value '%s' in enum '%s' in %s.", valueStr, enumName, enumsConfigFile);
                    throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, exceptionMessage);
                }
                enumIntToStringMap.put(i, valueStr);
                enumStringToIntMap.put(valueStr, i);
            }
        } catch (ParserConfigurationException | XPathExpressionException | SAXException e) {
            throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Error parsing enums config.", e);
        }
    } catch (IOException e) {
        throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Error while opening enums config.", e);
    } finally {
        try {
            if (is != null) {
                is.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    if ((enumStringToIntMap.size() == 0) || (enumIntToStringMap.size() == 0)) {
        String exceptionMessage = String.format(Locale.ENGLISH, "Invalid configuration was defined for enum '%s' in %s.", enumName, enumsConfigFile);
        throw new SolrException(SolrException.ErrorCode.NOT_FOUND, exceptionMessage);
    }
    args.remove(PARAM_ENUMS_CONFIG);
    args.remove(PARAM_ENUM_NAME);
}
Also used : XPath(javax.xml.xpath.XPath) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) InputStream(java.io.InputStream) XPathExpressionException(javax.xml.xpath.XPathExpressionException) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) IOException(java.io.IOException) Document(org.w3c.dom.Document) SAXException(org.xml.sax.SAXException) XPathFactory(javax.xml.xpath.XPathFactory) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SolrException(org.apache.solr.common.SolrException)

Example 38 with XPathFactory

use of javax.xml.xpath.XPathFactory in project gerrit by GerritCodeReview.

the class HtmlDomUtil method compact.

private static void compact(Document doc) {
    try {
        String expr = "//text()[normalize-space(.) = '']";
        XPathFactory xp = XPathFactory.newInstance();
        XPathExpression e = xp.newXPath().compile(expr);
        NodeList empty = (NodeList) e.evaluate(doc, XPathConstants.NODESET);
        for (int i = 0; i < empty.getLength(); i++) {
            Node node = empty.item(i);
            node.getParentNode().removeChild(node);
        }
    } catch (XPathExpressionException e) {
    // Don't do the whitespace removal.
    }
}
Also used : XPathExpression(javax.xml.xpath.XPathExpression) XPathFactory(javax.xml.xpath.XPathFactory) XPathExpressionException(javax.xml.xpath.XPathExpressionException) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node)

Example 39 with XPathFactory

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

the class XmlUtilitiesImplTest method testGetUniqueXPath.

@Test
public void testGetUniqueXPath() throws Exception {
    final Document testDoc = loadTestDocument();
    final XPathFactory xPathFactory = XPathFactory.newInstance();
    final XPath xPath = xPathFactory.newXPath();
    final XPathExpression xPathExpression = xPath.compile("//*[@ID='11']");
    final Node node = (Node) xPathExpression.evaluate(testDoc, XPathConstants.NODE);
    final XmlUtilitiesImpl xmlUtilities = new XmlUtilitiesImpl();
    final String nodePath = xmlUtilities.getUniqueXPath(node);
    assertEquals("/layout/folder[2]/folder[3]", nodePath);
}
Also used : XPath(javax.xml.xpath.XPath) XPathExpression(javax.xml.xpath.XPathExpression) XPathFactory(javax.xml.xpath.XPathFactory) Node(org.w3c.dom.Node) Document(org.w3c.dom.Document) Test(org.junit.Test)

Example 40 with XPathFactory

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

the class Shibboleth3ConfService method isFederationMetadata.

public boolean isFederationMetadata(String spMetaDataFN) {
    if (spMetaDataFN == null) {
        return false;
    }
    File spMetaDataFile = new File(getSpMetadataFilePath(spMetaDataFN));
    InputStream is = null;
    InputStreamReader isr = null;
    Document xmlDocument = null;
    try {
        is = FileUtils.openInputStream(spMetaDataFile);
        isr = new InputStreamReader(is, "UTF-8");
        try {
            xmlDocument = xmlService.getXmlDocument(new InputSource(isr));
        } catch (Exception ex) {
            log.error("Failed to parse metadata file '{}'", ex, spMetaDataFile.getAbsolutePath());
            ex.printStackTrace();
        }
    } catch (IOException ex) {
        log.error("Failed to read metadata file '{}'", ex, spMetaDataFile.getAbsolutePath());
        ex.printStackTrace();
    } finally {
        IOUtils.closeQuietly(isr);
        IOUtils.closeQuietly(is);
    }
    if (xmlDocument == null) {
        return false;
    }
    XPathFactory factory = XPathFactory.newInstance();
    XPath xPath = factory.newXPath();
    String federationTag = null;
    try {
        federationTag = xPath.compile("count(/EntitiesDescriptor)").evaluate(xmlDocument);
    } catch (XPathExpressionException ex) {
        log.error("Failed to find IDP metadata file in relaying party file '{}'", ex, spMetaDataFile.getAbsolutePath());
        ex.printStackTrace();
    }
    return Integer.parseInt(federationTag) > 0;
}
Also used : XPath(javax.xml.xpath.XPath) XPathFactory(javax.xml.xpath.XPathFactory) InputSource(org.xml.sax.InputSource) InputStreamReader(java.io.InputStreamReader) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) XPathExpressionException(javax.xml.xpath.XPathExpressionException) IOException(java.io.IOException) Document(org.w3c.dom.Document) SubversionFile(org.gluu.oxtrust.model.SubversionFile) File(java.io.File) InvalidConfigurationException(org.xdi.util.exception.InvalidConfigurationException) XPathExpressionException(javax.xml.xpath.XPathExpressionException) SAXException(org.xml.sax.SAXException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) EncryptionException(org.xdi.util.security.StringEncrypter.EncryptionException) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Aggregations

XPathFactory (javax.xml.xpath.XPathFactory)75 XPath (javax.xml.xpath.XPath)59 XPathExpression (javax.xml.xpath.XPathExpression)40 Document (org.w3c.dom.Document)34 NodeList (org.w3c.dom.NodeList)34 XPathExpressionException (javax.xml.xpath.XPathExpressionException)26 DocumentBuilder (javax.xml.parsers.DocumentBuilder)24 Node (org.w3c.dom.Node)24 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)22 InputSource (org.xml.sax.InputSource)16 Test (org.junit.Test)15 IOException (java.io.IOException)13 PBXNativeTarget (com.facebook.buck.apple.xcode.xcodeproj.PBXNativeTarget)11 PBXTarget (com.facebook.buck.apple.xcode.xcodeproj.PBXTarget)11 ImmutableMap (com.google.common.collect.ImmutableMap)11 Path (java.nio.file.Path)11 SAXException (org.xml.sax.SAXException)11 PBXFileReference (com.facebook.buck.apple.xcode.xcodeproj.PBXFileReference)10 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)9 InternalErrorException (cz.metacentrum.perun.core.api.exceptions.InternalErrorException)7