Example usage for org.dom4j Node selectNodes

List of usage examples for org.dom4j Node selectNodes

Introduction

In this page you can find the example usage for org.dom4j Node selectNodes.

Prototype

List<Node> selectNodes(String xpathExpression);

Source Link

Document

selectNodes evaluates an XPath expression and returns the result as a List of Node instances or String instances depending on the XPath expression.

Usage

From source file:org.jdbcluster.domain.DomainConfigImpl.java

License:Apache License

/**
 * starts parsing process of entries in 
 * configuration xml file/*from  ww  w . jav  a 2  s  . c o  m*/
 * @param configuration
 */
@SuppressWarnings("unchecked")
public void setConfiguration(String configuration) {
    this.configuration = configuration;
    dependancies = new HashMap<String, EntrySet>();
    try {
        document = read(configuration);
    } catch (DocumentException e) {
        e.printStackTrace();
        throw new ConfigurationException("Error in DomainConfig File", e);
    }

    String xPathDomains = "//jdbcluster/domaindependancy/domain";
    List<Node> domainNodes = document.selectNodes(xPathDomains);
    if (domainNodes == null)
        return;

    // iterate over all configured "domain" elements
    for (Node domainNode : domainNodes) {
        String domainId = domainNode.valueOf("@domainid");
        List<Node> domEntryNodes = domainNode.selectNodes("entry");
        EntrySet es = new EntrySet(domainId, domEntryNodes);
        dependancies.put(domainId, es);
    }
}

From source file:org.jdbcluster.domain.EntrySet.java

License:Apache License

/**
 * filles structure/*from  w ww.  j  a v a2  s  .  c  om*/
 * @param domEntryNodes
 */
@SuppressWarnings("unchecked")
void fill(List<Node> domEntryNodes, HashMap<String, HashMap<String, ValidEntryList>> topList) {
    for (Node n : domEntryNodes) {
        String domEntryValue = n.valueOf("@value");
        String domEntrySlaveDomain = n.valueOf("@slavedomainid");

        HashMap<String, ValidEntryList> slaveDomainMap = topList.get(domEntryValue);
        if (slaveDomainMap == null) {
            slaveDomainMap = new HashMap<String, ValidEntryList>();
            topList.put(domEntryValue, slaveDomainMap);
        }
        ValidEntryList validValueList = slaveDomainMap.get(domEntrySlaveDomain);
        if (validValueList == null) {
            List<Node> validEntries = n.selectNodes("valid");
            List<Node> invalidEntries = n.selectNodes("invalid");
            List<Node> addMasterEntries = n.selectNodes("additionalmaster");
            validValueList = new ValidEntryList(validEntries, invalidEntries, addMasterEntries);
            slaveDomainMap.put(domEntrySlaveDomain, validValueList);
        }
    }
}

From source file:org.jdbcluster.domain.ValidEntryList.java

License:Apache License

/**
 * parses additional Master Dependancies
 * @param addMasterEntries 1st level of tags
 *//*from  www .  j av a 2 s  .c  o  m*/
@SuppressWarnings("unchecked")
private void fillAddMasterEntries(List<Node> addMasterEntries) {
    /**
     * maps MasterDomainID -> AddMasterDomEntryValue -> ValidEntryList
     */
    addMasterMap = new HashMap<String, HashMap<String, ValidEntryList>>();

    for (Node n : addMasterEntries) {
        String domEntryValue = n.valueOf("@value");
        String domEntryMasterDomain = n.valueOf("@masterdomainid");

        HashMap<String, ValidEntryList> masterDomainMap = addMasterMap.get(domEntryMasterDomain);
        if (masterDomainMap == null) {
            masterDomainMap = new HashMap<String, ValidEntryList>();
            addMasterMap.put(domEntryMasterDomain, masterDomainMap);
        }

        ValidEntryList validValueList = masterDomainMap.get(domEntryValue);
        if (validValueList == null) {
            List<Node> validEntries = n.selectNodes("valid");
            List<Node> invalidEntries = n.selectNodes("invalid");
            List<Node> nextAddMasterEntries = n.selectNodes("additionalmaster");
            validValueList = new ValidEntryList(validEntries, invalidEntries, nextAddMasterEntries);
            masterDomainMap.put(domEntryValue, validValueList);
        }
    }
}

From source file:org.jivesoftware.openfire.container.PluginCacheConfigurator.java

License:Open Source License

private Map<String, String> readInitParams(Node configData) {
    Map<String, String> paramMap = new HashMap<String, String>();
    List<Node> params = configData.selectNodes("init-params/init-param");
    for (Node param : params) {
        String paramName = param.selectSingleNode("param-name").getStringValue();
        String paramValue = param.selectSingleNode("param-value").getStringValue();
        paramMap.put(paramName, paramValue);
    }//from  ww  w . ja va  2s.co m

    return paramMap;
}

From source file:org.jivesoftware.spark.PluginManager.java

License:Open Source License

/**
 * Loads public plugins.//from   w w  w . j av  a 2 s  .  c  o m
 *
 * @param pluginDir the directory of the expanded public plugin.
 * @return the new Plugin model for the Public Plugin.
 */
private Plugin loadPublicPlugin(File pluginDir) {

    File pluginFile = new File(pluginDir, "plugin.xml");
    SAXReader saxReader = new SAXReader();
    Document pluginXML = null;
    try {
        pluginXML = saxReader.read(pluginFile);
    } catch (DocumentException e) {
        Log.error(e);
    }

    Plugin pluginClass = null;

    List<? extends Node> plugins = pluginXML.selectNodes("/plugin");
    for (Node plugin1 : plugins) {
        PublicPlugin publicPlugin = new PublicPlugin();

        String clazz = null;
        String name;
        String minVersion;

        try {

            name = plugin1.selectSingleNode("name").getText();
            clazz = plugin1.selectSingleNode("class").getText();

            try {
                String lower = name.replaceAll("[^0-9a-zA-Z]", "").toLowerCase();
                // Dont load the plugin if its on the Blacklist
                if (_blacklistPlugins.contains(lower) || _blacklistPlugins.contains(clazz)
                        || SettingsManager.getLocalPreferences().getDeactivatedPlugins().contains(name)) {
                    return null;
                }
            } catch (Exception e) {
                // Whatever^^
                return null;
            }

            // Check for minimum Spark version
            try {
                minVersion = plugin1.selectSingleNode("minSparkVersion").getText();

                String buildNumber = JiveInfo.getVersion();
                boolean ok = buildNumber.compareTo(minVersion) >= 0;

                if (!ok) {
                    return null;
                }
            } catch (Exception e) {
                Log.error("Unable to load plugin " + name
                        + " due to missing <minSparkVersion>-Tag in plugin.xml.");
                return null;
            }

            // Check for minimum Java version
            try {
                String javaversion = plugin1.selectSingleNode("java").getText().replaceAll("[^0-9]", "");
                javaversion = javaversion == null ? "0" : javaversion;
                int jv = Integer.parseInt(attachMissingZero(javaversion));

                String myversion = System.getProperty("java.version").replaceAll("[^0-9]", "");
                int mv = Integer.parseInt(attachMissingZero(myversion));

                boolean ok = (mv >= jv);

                if (!ok) {
                    Log.error("Unable to load plugin " + name + " due to old JavaVersion.\nIt Requires "
                            + plugin1.selectSingleNode("java").getText() + " you have "
                            + System.getProperty("java.version"));
                    return null;
                }

            } catch (NullPointerException e) {
                Log.warning("Plugin " + name + " has no <java>-Tag, consider getting a newer Version");
            }

            // set dependencies
            try {
                List<? extends Node> dependencies = plugin1.selectNodes("depends/plugin");
                dependencies.stream().map((depend1) -> (Element) depend1).map((depend) -> {
                    PluginDependency dependency = new PluginDependency();
                    dependency.setVersion(depend.selectSingleNode("version").getText());
                    dependency.setName(depend.selectSingleNode("name").getText());
                    return dependency;
                }).forEach((dependency) -> {
                    publicPlugin.addDependency(dependency);
                });
            } catch (Exception e) {
                e.printStackTrace();
            }

            // Do operating system check.
            boolean operatingSystemOK = isOperatingSystemOK(plugin1);
            if (!operatingSystemOK) {
                return null;
            }

            publicPlugin.setPluginClass(clazz);
            publicPlugin.setName(name);

            try {
                String version = plugin1.selectSingleNode("version").getText();
                publicPlugin.setVersion(version);

                String author = plugin1.selectSingleNode("author").getText();
                publicPlugin.setAuthor(author);

                String email = plugin1.selectSingleNode("email").getText();
                publicPlugin.setEmail(email);

                String description = plugin1.selectSingleNode("description").getText();
                publicPlugin.setDescription(description);

                String homePage = plugin1.selectSingleNode("homePage").getText();
                publicPlugin.setHomePage(homePage);
            } catch (Exception e) {
                if (Log.debugging)
                    Log.debug("We can ignore these.");
            }

            try {
                pluginClass = (Plugin) getParentClassLoader().loadClass(clazz).newInstance();
                if (Log.debugging)
                    Log.debug(name + " has been loaded.");
                publicPlugin.setPluginDir(pluginDir);
                publicPlugins.add(publicPlugin);

                registerPlugin(pluginClass);
            } catch (ClassNotFoundException | IllegalAccessException | InstantiationException e) {
                Log.error("Unable to load plugin " + clazz + ".", e);
            }
        } catch (NumberFormatException ex) {
            Log.error("Unable to load plugin " + clazz + ".", ex);
        }

    }

    return pluginClass;
}

From source file:org.jivesoftware.sparkimpl.plugin.emoticons.EmoticonManager.java

License:Open Source License

/**
 * Loads an emoticon set.// w w w . j  a v a  2  s .c  o m
 *
 * @param packName the name of the pack.
 */
public void addEmoticonPack(String packName) {
    File emoticonSet = new File(EMOTICON_DIRECTORY, packName + ".adiumemoticonset");
    if (!emoticonSet.exists()) {
        emoticonSet = new File(EMOTICON_DIRECTORY, packName + ".AdiumEmoticonset");
    }

    if (!emoticonSet.exists()) {
        emoticonSet = new File(EMOTICON_DIRECTORY, "Default.adiumemoticonset");
        packName = "Default";
        setActivePack("Default");
    }

    List<Emoticon> emoticons = new ArrayList<>();

    final File plist = new File(emoticonSet, "Emoticons.plist");

    // Create SaxReader and set to non-validating parser.
    // This will allow for non-http problems to not break spark :)
    final SAXReader saxParser = new SAXReader();
    saxParser.setValidation(false);
    try {
        saxParser.setFeature("http://xml.org/sax/features/validation", false);
        saxParser.setFeature("http://xml.org/sax/features/namespaces", false);
        saxParser.setFeature("http://apache.org/xml/features/validation/schema", false);
        saxParser.setFeature("http://apache.org/xml/features/validation/schema-full-checking", false);
        saxParser.setFeature("http://apache.org/xml/features/validation/dynamic", false);
        saxParser.setFeature("http://apache.org/xml/features/allow-java-encodings", true);
        saxParser.setFeature("http://apache.org/xml/features/continue-after-fatal-error", true);
        saxParser.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false);
        saxParser.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
    } catch (SAXException e) {
        e.printStackTrace();
    }

    Document emoticonFile;
    try {
        emoticonFile = saxParser.read(plist);
    } catch (DocumentException e) {
        Log.error(e);
        return;
    }

    Node root = emoticonFile.selectSingleNode("/plist/dict/dict");

    List<?> keyList = root.selectNodes("key");
    List<?> dictonaryList = root.selectNodes("dict");

    Iterator<?> keys = keyList.iterator();
    Iterator<?> dicts = dictonaryList.iterator();

    while (keys.hasNext()) {
        Element keyEntry = (Element) keys.next();
        String key = keyEntry.getText();

        Element dict = (Element) dicts.next();
        String name = dict.selectSingleNode("string").getText();

        // Load equivilants
        final List<String> equivs = new ArrayList<>();
        final List<?> equivilants = dict.selectNodes("array/string");
        equivilants.stream().map((equivilant1) -> (Element) equivilant1)
                .map((equivilant) -> equivilant.getText()).forEach((equivilantString) -> {
                    equivs.add(equivilantString);
                });

        final Emoticon emoticon = new Emoticon(key, name, equivs, emoticonSet);
        emoticons.add(emoticon);
    }

    emoticonMap.put(packName, emoticons);
}

From source file:org.kitodo.production.plugin.importer.massimport.googlecode.fascinator.redbox.sru.NLAIdentity.java

License:Open Source License

private List<Map<String, String>> getSourceIdentities() {
    List<Map<String, String>> returnList = new ArrayList<>();

    // Top level institution
    Map<String, String> idMap = new HashMap<>();
    Node institutionNode = eac.selectSingleNode("eac:eac-cpf/eac:control/eac:maintenanceAgency/eac:agencyName");
    String institutionString = institutionNode.getText();
    // Top level name
    Node nlaNamesNode = eac.selectSingleNode("eac:eac-cpf/eac:cpfDescription/eac:identity");
    // Get all the names this ID lists
    List<Map<String, String>> nameList = getNames(nlaNamesNode);
    for (Map<String, String> name : nameList) {
        // Only use the longest top-level name for display purposes
        String oldDisplayName = idMap.get(DISPLAY_NAME);
        String thisDisplayName = name.get(DISPLAY_NAME);
        if (oldDisplayName == null
                || (thisDisplayName != null && thisDisplayName.length() > oldDisplayName.length())) {
            // Clear any old data
            idMap.clear();//w ww.ja  va 2  s . c o m
            // Store this ID
            idMap.putAll(name);
            idMap.put(INSTITUTION, institutionString);
        }
    }
    // And add to the list
    returnList.add(idMap);

    // All name entities from contributing institutions
    List<Node> sourceIdentities = eac.selectNodes("eac:eac-cpf/eac:cpfDescription//eac:eac-cpf");
    for (Node identity : sourceIdentities) {
        // Institution for this ID
        institutionNode = identity.selectSingleNode("*//eac:maintenanceAgency/eac:agencyName");

        if (Objects.nonNull(institutionNode)) {
            institutionString = institutionNode.getText();

            // Any names for this ID
            List<Node> idNodes = identity.selectNodes("*//eac:identity");
            for (Node idNode : idNodes) {
                // A Map for each name
                idMap = new HashMap<>();
                // Get all the names this ID lists
                nameList = getNames(idNode);
                for (Map<String, String> name : nameList) {
                    idMap.putAll(name);
                }
                // Indicate the institution for each one
                idMap.put(INSTITUTION, institutionString);
                // And add to the list
                returnList.add(idMap);
            }
        }
    }

    return returnList;
}

From source file:org.kitodo.production.plugin.importer.massimport.googlecode.fascinator.redbox.sru.NLAIdentity.java

License:Open Source License

private List<Map<String, String>> getNames(Node node) {
    List<Map<String, String>> nameList = new ArrayList<>();

    // Any names for this ID
    List<Node> names = node.selectNodes("eac:nameEntry");
    for (Node name : names) {
        Map<String, String> nameMap = getNameMap(name);
        nameList.add(nameMap);//from  ww  w  .  jav  a  2  s .  c o  m
    }

    return nameList;
}

From source file:org.kitodo.production.plugin.importer.massimport.googlecode.fascinator.redbox.sru.NLAIdentity.java

License:Open Source License

private Map<String, String> getNameMap(Node name) {
    Map<String, String> nameMap = new HashMap<>();

    String thisDisplay = null;/*  www  .j  av  a  2  s.  c  o m*/
    String thisFirstName = null;
    String thisSurname = null;
    String title = null;

    // First name
    Node firstNameNode = name
            .selectSingleNode("eac:part[(@localType=\"forename\") " + "or (@localType=\"givenname\")]");
    if (Objects.nonNull(firstNameNode)) {
        thisFirstName = firstNameNode.getText();
    }

    // Surname
    Node surnameNode = name
            .selectSingleNode("eac:part[(@localType=\"surname\") " + "or (@localType=\"familyname\")]");
    if (Objects.nonNull(surnameNode)) {
        thisSurname = surnameNode.getText();
    }

    // Title
    Node titleNode = name.selectSingleNode("eac:part[@localType=\"title\"]");
    if (Objects.nonNull(titleNode)) {
        title = titleNode.getText();
    }

    // Display Name
    if (Objects.nonNull(thisSurname)) {
        thisDisplay = thisSurname;
        nameMap.put("surname", thisSurname);
        if (Objects.nonNull(thisFirstName)) {
            thisDisplay += ", " + thisFirstName;
            nameMap.put("firstName", thisFirstName);
        }
        if (Objects.nonNull(title)) {
            thisDisplay += " (" + title + ")";
        }
        nameMap.put(DISPLAY_NAME, thisDisplay);
    }

    // Last ditch effort... we couldn't find simple name information
    // from recommended values. So just concatenate what we can see.
    if (Objects.isNull(thisDisplay)) {
        // Find every part
        List<Node> parts = name.selectNodes("eac:part");
        for (Node part : parts) {
            String value = getValueFromPart((Element) part);

            // And add to the display name
            if (Objects.isNull(thisDisplay)) {
                thisDisplay = value;
            } else {
                thisDisplay += ", " + value;
            }
        }
        nameMap.put(DISPLAY_NAME, thisDisplay);
    }

    return nameMap;
}

From source file:org.kitodo.production.plugin.importer.massimport.googlecode.fascinator.redbox.sru.SRUClient.java

License:Open Source License

/**
 * <p>//from   w  w  w  . j  ava  2 s  .  c  o  m
 * Search for a record from the National Library of Australia with the
 * provided identifier. If multiple records match this identifier only the
 * first will be returned.
 * </p>
 *
 * @param id
 *            The identifier to search for
 * @return the record matching this identifier, empty String if not found
 */
public String nlaGetNationalId(String id) {
    Node node = nlaGetRecordNodeById(id);

    if (node == null) {
        return "";
    }

    List<Node> otherIds = node.selectNodes("eac:control/eac:otherRecordId");
    for (Node idNode : otherIds) {
        String otherId = idNode.getText();
        if (otherId.startsWith("http://nla.gov.au")) {
            return otherId;
        }
    }

    return "";
}