Example usage for org.dom4j.xpath DefaultXPath setNamespaceURIs

List of usage examples for org.dom4j.xpath DefaultXPath setNamespaceURIs

Introduction

In this page you can find the example usage for org.dom4j.xpath DefaultXPath setNamespaceURIs.

Prototype

public void setNamespaceURIs(Map<String, String> map) 

Source Link

Usage

From source file:de.bbe_consulting.mavento.AbstractArchetypeMojo.java

License:Apache License

protected String getXmlNodeValueFromPom(String xpathNode, Document pom) throws MojoExecutionException {
    DefaultXPath path = new DefaultXPath(xpathNode);
    Map<String, String> namespaces = new TreeMap<String, String>();
    namespaces.put("x", "http://maven.apache.org/POM/4.0.0");

    path.setNamespaceURIs(namespaces);
    Node n = path.selectSingleNode(pom.getRootElement());

    return n.getStringValue();
}

From source file:org.gbif.harvest.digir.DigirMetadataHandler.java

License:Open Source License

/**
 * Iterates over the metadata mapping file, populating the various
 * elements-of-interest maps. Regular expressions divide the mapping file's
 * properties into the appropriate element-of-interest map.
 * Note: The mapping file's properties are in the following format:
 * [element-of-interest name] + [property name] = [XPath expresson]
 * The regular expression matches according to the [element-of-interest
 * name]/*  w  ww  .  j  a  v  a 2 s.  co  m*/
 * The corresponding element-of-interest map is then populated with: key =
 * [property name] & value = [XPath expression]
 *
 * @param mappingFile name
 * @param protocol    name
 *
 * @throws HarvesterException thrown if method fails
 */
private void populateElementOfInterestsMapsFromMappingFile(String mappingFile, String protocol)
        throws HarvesterException {

    // Create regex patterns
    // we're interested in all non-contact related properties
    Pattern metadataKeyPattern = Pattern.compile("metadata([\\S]*)");
    Pattern providerContactKeyPattern = Pattern.compile("providerContact([\\S]*)");
    Pattern resourceContactKeyPattern = Pattern.compile("resourceContact([\\S]*)");

    // properties we harvest are read from file
    Properties mapping = new Properties();
    String mappingFilePath = fileUtils.constructMappingFilePath(BASE_LOCATION, protocol, MAPPING_DIRECTORY_NAME,
            mappingFile);
    InputStream is = null;
    try {
        is = DigirMetadataHandler.class.getResourceAsStream(mappingFilePath);
        mapping.load(is);

        // Divide the mapping properties into various element-of-interest maps
        for (Object key : mapping.keySet()) {
            Boolean matched = false;
            // Matchers matching keys belonging to repeating element groups
            Matcher metadataKeyMatcher = metadataKeyPattern.matcher((String) key);

            if (metadataKeyMatcher.matches()) {
                String property = metadataKeyMatcher.group(1);
                metadataElementsOfInterest.put(property, mapping.getProperty((String) key));
                matched = true;
            }
            if (!matched) {
                Matcher providerContactKeyMatcher = providerContactKeyPattern.matcher((String) key);
                if (providerContactKeyMatcher.matches()) {
                    String property = providerContactKeyMatcher.group(1);
                    metadataProviderContactElementsOfInterest.put(property, mapping.getProperty((String) key));
                    matched = true;

                }
                if (!matched) {
                    Matcher resourceContactKeyMatcher = resourceContactKeyPattern.matcher((String) key);
                    if (resourceContactKeyMatcher.matches()) {
                        String property = resourceContactKeyMatcher.group(1);
                        metadataResourceContactElementsOfInterest.put(property,
                                mapping.getProperty((String) key));
                        matched = true;

                    }
                    if (!matched) {
                        // Determines the XPath expressions used to isolate repeating elements in a
                        // metadata xml response.
                        if (metadataRepeatingElementsXpath.keySet().contains(key)) {
                            // construct an XPath expression for repeating Element
                            DefaultXPath xpath = new DefaultXPath(mapping.getProperty((String) key));
                            xpath.setNamespaceURIs(namespaceMap);
                            metadataRepeatingElementsXpath.put((String) key, xpath);
                        }
                    }
                }
            }
        }
    } catch (NullPointerException e) {
        log.info("error.mappingFileExists", new String[] { mappingFilePath, e.getMessage() }, e);
        throw new HarvesterException(e.getMessage(), e);
    } catch (Exception e) {
        log.error("error.populateElementOfInterestsMapsFromMappingFile",
                new String[] { mappingFile, e.getMessage() }, e);
        throw new HarvesterException(e.getMessage(), e);
    } finally {
        if (is != null) {
            try {
                is.close();
            } catch (IOException e) {
                log.error(
                        "An error occurred closing input stream on " + mappingFilePath + ": " + e.getMessage(),
                        e);
            }
        }
    }
}

From source file:org.gbif.harvest.tapir.TapirMetadataHandler.java

License:Open Source License

/**
 * Iterates over the metadata mapping file (properties file), populating the
 * various elements-of-interest maps.//from www.j av a  2s  . c  om
 * In most cases, regular expressions divide the mapping file's properties
 * into the appropriate element-of-interest map.
 * Where some properties actually represent repeating elements in a metadata
 * xml response, the standardised set of repeating element names are
 * located in a static list. Each repeating element name matches a key name
 * in the indexMapping properties file and is used to get at its XPath
 * expression.
 * Note: The mapping file's properties are in the following format:
 * [element-of-interest categoriser] + [property name] = [XPath expresson]
 * The regular expression matches according to the [element-of-interest
 * categoriser]
 * The corresponding element-of-interest map is then populated with: key =
 * [property name] & value = [XPath expression]
 *
 * @param mappingFile name
 * @param protocol    name
 *
 * @throws HarvesterException thrown if method fails
 */
private void populateElementOfInterestsMapsFromMappingFile(String mappingFile, String protocol)
        throws HarvesterException {

    // Create regex patterns
    // contact-related patterns
    Pattern relatedEntityKeyPattern = Pattern.compile("relatedEntity([\\S]*)");
    Pattern hasContactKeyPattern = Pattern.compile("hasContact([\\S]*)");

    // non-contact, metadata related pattern
    Pattern metadataKeyPattern = Pattern.compile("metadata([\\S]*)");

    // non-contact, non-metadata, settings related pattern
    Pattern settingKeyPattern = Pattern.compile("setting([\\S]*)");

    // properties we harvest are read from file
    Properties mapping = new Properties();
    String mappingFilePath = fileUtils.constructMappingFilePath(BASE_LOCATION, protocol, MAPPING_DIRECTORY_NAME,
            mappingFile);
    InputStream is = null;
    try {
        is = TapirMetadataHandler.class.getResourceAsStream(mappingFilePath);
        mapping.load(is);

        // Divide the mapping properties into various element-of-interest maps
        for (Object key : mapping.keySet()) {
            Boolean matched = false;
            // Matchers matching keys belonging to repeating element groups
            Matcher relatedEntityKeyMatcher = relatedEntityKeyPattern.matcher((String) key);

            if (relatedEntityKeyMatcher.matches()) {
                String property = relatedEntityKeyMatcher.group(1);
                harvestedRelatedEntityElementsOfInterest.put(property, mapping.getProperty((String) key));
                matched = true;
            }
            if (!matched) {
                Matcher hasContactKeyMatcher = hasContactKeyPattern.matcher((String) key);
                if (hasContactKeyMatcher.matches()) {
                    String property = hasContactKeyMatcher.group(1);
                    harvestedHasContactElementsOfInterest.put(property, mapping.getProperty((String) key));
                    matched = true;
                }
                if (!matched) {
                    Matcher contactKeyMatcher = metadataKeyPattern.matcher((String) key);
                    if (contactKeyMatcher.matches()) {
                        String property = contactKeyMatcher.group(1);
                        metadataElementsOfInterest.put(property, mapping.getProperty((String) key));
                        matched = true;
                    }
                    if (!matched) {
                        Matcher settingKeyMatcher = settingKeyPattern.matcher((String) key);
                        if (settingKeyMatcher.matches()) {
                            String property = settingKeyMatcher.group(1);
                            settingsElementsOfInterest.put(property, mapping.getProperty((String) key));
                            matched = true;
                        }
                        if (!matched) {
                            // Determines the XPath expressions used to isolate repeating elements in a
                            // metadata xml response.
                            if (metadataRepeatingElementsXpath.keySet().contains(key)) {
                                // construct an XPath expression for repeating Element
                                DefaultXPath xpath = new DefaultXPath(mapping.getProperty((String) key));
                                xpath.setNamespaceURIs(namespaceMap);
                                metadataRepeatingElementsXpath.put((String) key, xpath);
                            }
                        }
                    }
                }
            }
        }
    } catch (NullPointerException e) {
        log.info("error.mappingFileExists", new String[] { mappingFilePath, e.getMessage() }, e);
        throw new HarvesterException(e.getMessage(), e);
    } catch (Exception e) {
        log.error("error.populateElementOfInterestsMapsFromMappingFile",
                new String[] { mappingFile, e.getMessage() }, e);
        throw new HarvesterException(e.getMessage(), e);
    } finally {
        if (is != null) {
            try {
                is.close();
            } catch (IOException e) {
                log.error(
                        "An error occurred closing input stream on " + mappingFilePath + ": " + e.getMessage(),
                        e);
            }
        }
    }
}