Example usage for org.dom4j Node selectSingleNode

List of usage examples for org.dom4j Node selectSingleNode

Introduction

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

Prototype

Node selectSingleNode(String xpathExpression);

Source Link

Document

selectSingleNode evaluates an XPath expression and returns the result as a single Node instance.

Usage

From source file:org.talend.mdm.webapp.browserecords.server.ruleengine.DisplayRuleEngine.java

License:Open Source License

private void dependentDataTypeOrder(List<TypeModel> hasBeenProcessed, Set<TypeModel> stack, TypeModel bean,
        org.dom4j.Document doc4j) {
    stack.add(bean);/*  www  . j a  v  a  2  s. c o  m*/
    ExpressionUtil expUtil = new ExpressionUtil(bean.getDefaultValueExpression());
    List<String> typePathes = expUtil.getDepTypes();
    if (typePathes != null && typePathes.size() > 0) {
        for (String typePath : typePathes) {
            if (typePath.startsWith("./") || typePath.startsWith("../")) { //$NON-NLS-1$//$NON-NLS-2$
                String xpath = CommonUtil.typePathToXpath(bean.getTypePath());
                Node node = doc4j.selectSingleNode(xpath);
                if (node != null) {
                    node = node.selectSingleNode(typePath);
                    if (node != null) {
                        typePath = getRealTypePath((org.dom4j.Element) node);
                    }
                }
            }
            TypeModel depExpression = metaDatas.get(typePath);
            if (depExpression != null && !stack.contains(depExpression)) {
                if (depExpression.getDefaultValueExpression() != null) {
                    dependentDataTypeOrder(hasBeenProcessed, stack, depExpression, doc4j);
                }
            }
        }
    }
    if (!hasBeenProcessed.contains(bean)) {
        hasBeenProcessed.add(bean);
    }
}

From source file:org.waarp.ftp.simpleimpl.config.FileBasedConfiguration.java

License:Open Source License

/**
 * Initiate the configuration from the xml file
 * //  w w w  . j  a  v  a2 s  .c o  m
 * @param filename
 * @return True if OK
 */
@SuppressWarnings("unchecked")
public boolean setConfigurationFromXml(String filename) {
    Document document = null;
    // Open config file
    try {
        document = new SAXReader().read(filename);
    } catch (DocumentException e) {
        logger.error("Unable to read the XML Config file: " + filename, e);
        return false;
    }
    if (document == null) {
        logger.error("Unable to read the XML Config file: " + filename);
        return false;
    }
    Node nodebase, node = null;
    node = document.selectSingleNode(XML_SERVER_PASSWD);
    if (node == null) {
        logger.error("Unable to find Password in Config file: " + filename);
        return false;
    }
    String passwd = node.getText();
    setPassword(passwd);
    node = document.selectSingleNode(XML_SERVER_PORT);
    int port = 21;
    if (node != null) {
        port = Integer.parseInt(node.getText());
    }
    setServerPort(port);
    node = document.selectSingleNode(XML_SERVER_ADDRESS);
    String address = null;
    if (node != null) {
        address = node.getText();
    }
    setServerAddress(address);
    node = document.selectSingleNode(XML_SERVER_HOME);
    if (node == null) {
        logger.error("Unable to find Home in Config file: " + filename);
        return false;
    }
    String path = node.getText();
    File file = new File(path);
    try {
        setBaseDirectory(FilesystemBasedDirImpl.normalizePath(file.getCanonicalPath()));
    } catch (IOException e1) {
        logger.error("Unable to set Home in Config file: " + filename);
        return false;
    }
    if (!file.isDirectory()) {
        logger.error("Home is not a directory in Config file: " + filename);
        return false;
    }
    node = document.selectSingleNode(XML_SERVER_THREAD);
    if (node != null) {
        SERVER_THREAD = Integer.parseInt(node.getText());
    }
    node = document.selectSingleNode(XML_CLIENT_THREAD);
    if (node != null) {
        CLIENT_THREAD = Integer.parseInt(node.getText());
    }
    if (SERVER_THREAD == 0 || CLIENT_THREAD == 0) {
        computeNbThreads();
    }
    node = document.selectSingleNode(XML_LIMITGLOBAL);
    if (node != null) {
        serverGlobalReadLimit = Long.parseLong(node.getText());
        if (serverGlobalReadLimit <= 0) {
            serverGlobalReadLimit = 0;
        }
        serverGlobalWriteLimit = serverGlobalReadLimit;
        logger.warn("Global Limit: {}", serverGlobalReadLimit);
    }
    node = document.selectSingleNode(XML_LIMITSESSION);
    if (node != null) {
        serverChannelReadLimit = Long.parseLong(node.getText());
        if (serverChannelReadLimit <= 0) {
            serverChannelReadLimit = 0;
        }
        serverChannelWriteLimit = serverChannelReadLimit;
        logger.warn("SessionInterface Limit: {}", serverChannelReadLimit);
    }
    delayLimit = AbstractTrafficShapingHandler.DEFAULT_CHECK_INTERVAL;
    node = document.selectSingleNode(XML_TIMEOUTCON);
    if (node != null) {
        TIMEOUTCON = Integer.parseInt(node.getText());
    }
    node = document.selectSingleNode(XML_DELETEONABORT);
    if (node != null) {
        deleteOnAbort = Integer.parseInt(node.getText()) == 1 ? true : false;
    }
    node = document.selectSingleNode(XML_USENIO);
    if (node != null) {
        FilesystemBasedFileParameterImpl.useNio = Integer.parseInt(node.getText()) == 1 ? true : false;
    }
    node = document.selectSingleNode(XML_USEFASTMD5);
    if (node != null) {
        FilesystemBasedDigest.useFastMd5 = Integer.parseInt(node.getText()) == 1 ? true : false;
    } else {
        FilesystemBasedDigest.useFastMd5 = false;
    }
    node = document.selectSingleNode(XML_BLOCKSIZE);
    if (node != null) {
        BLOCKSIZE = Integer.parseInt(node.getText());
    }
    node = document.selectSingleNode(XML_RANGE_PORT_MIN);
    int min = 100;
    if (node != null) {
        min = Integer.parseInt(node.getText());
    }
    node = document.selectSingleNode(XML_RANGE_PORT_MAX);
    int max = 65535;
    if (node != null) {
        max = Integer.parseInt(node.getText());
    }
    CircularIntValue rangePort = new CircularIntValue(min, max);
    setRangePort(rangePort);
    // We use Apache Commons IO
    FilesystemBasedDirJdkAbstract.ueApacheCommonsIo = true;
    node = document.selectSingleNode(XML_AUTHENTIFICATION_FILE);
    if (node == null) {
        logger.error("Unable to find Authentication file in Config file: " + filename);
        return false;
    }
    String fileauthent = node.getText();
    document = null;
    try {
        document = new SAXReader().read(fileauthent);
    } catch (DocumentException e) {
        logger.error("Unable to read the XML Authentication file: " + fileauthent, e);
        return false;
    }
    if (document == null) {
        logger.error("Unable to read the XML Authentication file: " + fileauthent);
        return false;
    }
    List<Node> list = document.selectNodes(XML_AUTHENTIFICATION_BASED);
    Iterator<Node> iterator = list.iterator();
    while (iterator.hasNext()) {
        nodebase = iterator.next();
        node = nodebase.selectSingleNode(XML_AUTHENTIFICATION_USER);
        if (node == null) {
            continue;
        }
        String user = node.getText();
        node = nodebase.selectSingleNode(XML_AUTHENTIFICATION_PASSWD);
        if (node == null) {
            continue;
        }
        String userpasswd = node.getText();
        node = nodebase.selectSingleNode(XML_AUTHENTIFICATION_ADMIN);
        boolean isAdmin = false;
        if (node != null) {
            isAdmin = node.getText().equals("1") ? true : false;
        }
        List<Node> listaccount = nodebase.selectNodes(XML_AUTHENTIFICATION_ACCOUNT);
        String[] account = null;
        if (!listaccount.isEmpty()) {
            account = new String[listaccount.size()];
            int i = 0;
            Iterator<Node> iteratoraccount = listaccount.iterator();
            while (iteratoraccount.hasNext()) {
                node = iteratoraccount.next();
                account[i] = node.getText();
                // logger.debug("User: {} Acct: {}", user, account[i]);
                i++;
            }
        }
        SimpleAuth auth = new SimpleAuth(user, userpasswd, account);
        auth.setAdmin(isAdmin);
        authentications.put(user, auth);
    }
    document = null;
    return true;
}

From source file:pt.webdetails.cdb.exporters.ExporterEngine.java

License:Open Source License

public String listExporters() {
    JSONArray arr = new JSONArray();

    Document doc = getConfigFile();
    List<Node> exporters = doc.selectNodes("//exporter");
    for (Node exporter : exporters) {
        String id = exporter.selectSingleNode("@id").getText();
        try {//from  w  w  w . ja v  a  2s.c o m
            JSONObject jsonExporter = new JSONObject();
            JSONObject jsonModes = new JSONObject();
            jsonExporter.put("id", id);
            jsonExporter.put("label", exporter.selectSingleNode("@label").getText());
            List<Node> modes = exporter.selectNodes(".//mode");
            for (Node mode : modes) {
                jsonModes.put(mode.selectSingleNode("@type").getText(), true);
            }
            jsonExporter.put("modes", jsonModes);
            arr.put(jsonExporter);
        } catch (JSONException e) {
            logger.error("Failed to list exporter " + id + ". Reason: " + e);
        }
    }
    try {
        return arr.toString(2);
    } catch (JSONException e) {
        return null;
    }
}

From source file:pt.webdetails.cdf.dd.util.Utils.java

License:Open Source License

public static String getNodeText(final String xpath, final Node node, final String defaultValue) {
    if (node == null) {
        return defaultValue;
    }/*from   w w w .  ja va 2s  .c om*/
    Node n = node.selectSingleNode(xpath);
    if (n == null) {
        return defaultValue;
    }
    return n.getText();
}

From source file:pt.webdetails.cdv.notifications.EmailOutlet.java

License:Open Source License

private static Map<String, String> getSettingsFromNode(Node settings) {
    Map<String, String> out = new HashMap<String, String>();
    for (Node node : (List<Node>) settings.selectNodes(".//property")) {
        String key = node.selectSingleNode("./@name").getText();
        String val = node.selectSingleNode("./@value").getText();
        out.put(key, val);
    }/*  w w w .  j  a v a 2s .c om*/
    return out;
}

From source file:pt.webdetails.cdv.notifications.NotificationEngine.java

License:Open Source License

private void listOutlets(Document doc) {
    outlets = new HashMap<String, Class>();
    List<Node> outletNodes = (List<Node>) doc.selectNodes("//outlets/outlet");
    for (Node outletNode : outletNodes) {
        String outletClassName = "", outletName;
        try {//from w  w w .j ava2 s . c o m
            outletClassName = outletNode.selectSingleNode("./@class").getStringValue();
            outletName = outletNode.selectSingleNode("./@name").getStringValue();
            Class outletClass = Class.forName(outletClassName);
            outlets.put(outletName, outletClass);
            Method setDefaults = outletClass.getDeclaredMethod("setDefaults", Node.class);
            setDefaults.invoke(null, outletNode.selectSingleNode(".//conf"));
        } catch (ClassNotFoundException ex) {
            logger.error("Class for outlet " + outletClassName + " not found.", ex);
        } catch (NoSuchMethodException ex) {
            logger.error("Class " + outletClassName + " doesn't provide the necessary interface");
            logger.error(ex);
        } catch (InvocationTargetException ex) {
            logger.error("Failed to set defaults on " + outletClassName);
            logger.error(ex);
        } catch (IllegalAccessException ex) {
            logger.error("Failed to set defaults on " + outletClassName);
            logger.error(ex);
        }
    }
}

From source file:pt.webdetails.cdv.notifications.NotificationEngine.java

License:Open Source License

private void listAlerts(Document doc) {
    alerts = new HashMap<NotificationKey, List<NotificationOutlet>>();
    List<Node> alertNodes = doc.selectNodes("//alerts/alert");
    for (Node alertNode : alertNodes) {
        String outletName = "";
        try {/*from   ww  w  .  ja  v a 2s  . c o  m*/
            outletName = alertNode.selectSingleNode("./@outlet").getStringValue();
            Class outletClass = outlets.get(outletName);
            if (outletClass == null) {
                throw new ClassNotFoundException();
            }
            List<Node> groups = alertNode.selectNodes("./groups/group");
            for (Node group : groups) {
                Constructor cons = outletClass.getConstructor(Node.class);
                NotificationOutlet outlet = (NotificationOutlet) cons.newInstance(alertNode);
                String minLevel = group.selectSingleNode("./@threshold").getStringValue();
                if (minLevel.equals("*")) {
                    minLevel = "ALL";
                }
                String groupName = group.selectSingleNode("./@name").getStringValue();
                Level level;
                try {
                    level = Level.valueOf(minLevel.toUpperCase());
                } catch (Exception ex) {
                    level = Level.WARN;
                }
                NotificationKey key = new NotificationKey(level, groupName);
                if (!alerts.containsKey(key)) {
                    alerts.put(key, new ArrayList<NotificationOutlet>());
                }
                alerts.get(key).add(outlet);
            }
        } catch (InstantiationException e) {
            logger.error("Failed to instantiate " + outletName, e);
        } catch (ClassNotFoundException ex) {
            logger.error("Failed to get class for outlet " + outletName, ex);
        } catch (NoSuchMethodException ex) {
            logger.error("Class " + outletName + " doesn't provide the necessary interface");
            logger.error(ex);
        } catch (InvocationTargetException ex) {
            logger.error("Failed to set defaults on " + outletName);
            logger.error(ex);
        } catch (IllegalAccessException ex) {
            logger.error("Failed to set defaults on " + outletName);
            logger.error(ex);
        }
    }
}

From source file:pt.webdetails.cpf.plugins.Plugin.java

License:Open Source License

/**
 * what's a registered entity?//  ww  w  . j ava2s  .  c  om
 */
@JsonIgnore
public Node getRegisteredEntities(String entityName) {
    if (hasSettingsXML()) {
        try {
            Node documentNode = XmlDom4JUtils.getDocumentFromFile(pluginDirAccess, SETTINGS_XML_FILENAME);
            return documentNode.selectSingleNode("/settings" + entityName);
        } catch (IOException e) {
            logger.error(e);
        }
    }
    return null;
}

From source file:pt.webdetails.cpf.VersionChecker.java

License:Open Source License

private static String getStringValue(Node node, String xpath, String defaultValue) {
    Node valNode = node.selectSingleNode(xpath);
    if (valNode != null) {
        String value = valNode.getText();
        if (!StringUtils.isEmpty(value)) {
            return value;
        }//from w w  w  .  ja  v  a  2  s.  c  o m
    }
    return defaultValue;
}

From source file:pt.webdetails.di.baserver.utils.inspector.WadlParser.java

License:Open Source License

private Endpoint parseMethod(Node methodNode, final String path) {
    Endpoint endpoint = new Endpoint();
    endpoint.setId(methodNode.valueOf("@id"));
    endpoint.setHttpMethod(HttpMethod.valueOf(methodNode.valueOf("@name")));
    endpoint.setPath(shortPath(path));//www.j  a  va 2 s  .  c  om

    Node requestNode = methodNode.selectSingleNode("*[name() = 'request']");
    if (requestNode != null) {
        for (Object queryParamNode : requestNode.selectNodes("*[name() = 'param']")) {
            endpoint.getQueryParams().add(parseQueryParam((Node) queryParamNode));
        }
    }

    return endpoint;
}