Example usage for org.dom4j Element elementText

List of usage examples for org.dom4j Element elementText

Introduction

In this page you can find the example usage for org.dom4j Element elementText.

Prototype

String elementText(QName qname);

Source Link

Usage

From source file:net.sf.jvifm.model.ShortcutsManager.java

License:Open Source License

@SuppressWarnings("unchecked")
private void init() {

    storePath = HomeLocator.getConfigHome() + File.separator + "commands.xml";
    SAXReader saxReader = new SAXReader();
    File file = new File(storePath);
    Document document = null;/*  w  w  w .ja v  a 2  s.  co  m*/

    try {

        if (file.exists()) {
            FileInputStream is = new FileInputStream(file);
            InputStreamReader reader = new InputStreamReader(is, "UTF-8");
            BufferedReader br = new BufferedReader(reader);
            document = saxReader.read(br);

            List elementList = document.selectNodes("//command");

            for (Iterator it = elementList.iterator(); it.hasNext();) {

                Element shortcutElement = (Element) it.next();
                String name = shortcutElement.elementText("name");
                String text = shortcutElement.elementText("text");

                Shortcut shortcut = new Shortcut();
                shortcut.setText(text);
                shortcut.setName(name);

                shortcutsList.add(shortcut);
            }

            reader.close();
            is.close();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

}

From source file:net.sourceforge.sqlexplorer.dbproduct.Alias.java

License:Open Source License

/**
 * Constructs an Alias from XML, previously obtained from describeAsXml()
 * //from w  ww . j  a  v a  2s .  c  om
 * @param root
 */
public Alias(Element root) {
    autoLogon = Boolean.parseBoolean(root.attributeValue(AUTO_LOGON));
    connectAtStartup = Boolean.parseBoolean(root.attributeValue(CONNECT_AT_STARTUP));
    driverId = root.attributeValue(DRIVER_ID);
    String str = root.attributeValue(HAS_NO_USER_NAME);
    if (str != null) {
        hasNoUserName = Boolean.parseBoolean(str);
    }
    name = root.elementText(NAME);
    url = root.elementText(URL);
    folderFilterExpression = root.elementText(FOLDER_FILTER_EXPRESSION);
    nameFilterExpression = root.elementText(NAME_FILTER_EXPRESSION);
    schemaFilterExpression = root.elementText(SCHEMA_FILTER_EXPRESSION);

    if (hasNoUserName) {
        User user = new User("", "");
        addUser(user);
        setDefaultUser(user);
    } else {
        Element usersElem = root.element(USERS);
        if (usersElem != null) {
            List<Element> list = usersElem.elements(User.USER);
            if (list != null) {
                for (Element userElem : list) {
                    User user = new User(userElem);
                    // if (user.getUserName() != null && user.getUserName().trim().length() > 0) {
                    addUser(user);
                    // }
                }
            }
            String defaultUserName = root.elementText(DEFAULT_USER);
            if (defaultUserName != null) {
                User user = users.get(defaultUserName);
                if (user != null) {
                    defaultUser = user;
                }
            }

        }
    }
}

From source file:net.sourceforge.sqlexplorer.dbproduct.AliasManager.java

License:Open Source License

/**
 * Upgrades a v3 definition (java beans style) to v3.5.0beta2 and onwards
 * /*  w ww  . j a v  a  2 s .  c  o m*/
 * @param beans
 * @return
 */
protected Element convertToV350(Element beans) {
    Element result = new DefaultElement(Alias.ALIASES);

    for (Element bean : beans.elements("Bean")) {
        Element alias = result.addElement(Alias.ALIAS);
        alias.addAttribute(Alias.AUTO_LOGON,
                Boolean.toString(getBoolean(bean.elementText("autoLogon"), false)));
        alias.addAttribute(Alias.CONNECT_AT_STARTUP,
                Boolean.toString(getBoolean(bean.elementText("connectAtStartup"), false)));
        alias.addAttribute(Alias.DRIVER_ID, bean.element("driverIdentifier").elementText("string"));
        alias.addElement(Alias.NAME).setText(bean.elementText("name"));
        Element userElem = alias.addElement(Alias.USERS).addElement(User.USER);
        userElem.addElement(User.USER_NAME).setText(bean.elementText("userName"));
        userElem.addElement(User.PASSWORD).setText(bean.elementText("password"));
        alias.addElement(Alias.URL).setText(bean.elementText("url"));
        alias.addElement(Alias.FOLDER_FILTER_EXPRESSION).setText(bean.elementText("folderFilterExpression"));
        alias.addElement(Alias.NAME_FILTER_EXPRESSION).setText(bean.elementText("nameFilterExpression"));
        alias.addElement(Alias.SCHEMA_FILTER_EXPRESSION).setText(bean.elementText("schemaFilterExpression"));
    }

    return result;
}

From source file:net.sourceforge.sqlexplorer.dbproduct.DriverManager.java

License:Open Source License

/**
 * Converts from the old v3 format (which is a JavaBean encoding)
 * @param root/*from ww w.j a v  a  2 s.  co m*/
 * @return
 */
protected Element convertFromV3(Element root) {
    Element result = new DefaultElement(DRIVERS);
    for (Element elem : root.elements("Bean")) {
        String str;
        Element driver = result.addElement(DRIVER);

        try {
            str = elem.element("identifier").elementText("string");
            driver.addAttribute(ID, str);

            str = elem.elementText("driverClass");
            if (str != null)
                driver.addElement(DRIVER_CLASS).setText(str);

            str = elem.elementText("name");
            driver.addElement(NAME).setText(str);

            str = elem.elementText("url");
            driver.addElement(URL).setText(str);

            Element jars = driver.addElement(JARS);
            Element jarFileNames = elem.element("jarFileNames");
            for (Element jarBeanElem : jarFileNames.elements("Bean")) {
                str = jarBeanElem.elementText("string");
                if (str != null && str.trim().length() > 0)
                    jars.addElement(JAR).setText(str);
            }
        } catch (IllegalArgumentException e) {
            SQLExplorerPlugin.error("Error loading v3 driver " + driver.attributeValue(ID), e);
            throw e;
        }
    }

    return result;
}

From source file:net.unicon.toro.installer.tools.MergeConfiguration.java

License:Open Source License

private void mergePropertiesChanges(Element el, File path) throws Exception {
    System.out.println("Merging properties changes to " + path.getAbsolutePath());
    Utils.instance().backupFile(path, true);

    // create temp properties file to merge from
    Properties props = new Properties();
    for (Iterator i = el.elementIterator("add-or-replace"); i.hasNext();) {
        Element replace = (Element) i.next();
        String name = replace.elementText("name");
        String value = replace.elementText("value");
        props.setProperty(name, value);//  w  w  w.j a  v  a  2 s . c o m
    }

    File mergeFrom = new File(path.getParentFile(), path.getName() + "_mergeFrom.properties");
    FileOutputStream fos = null;

    try {
        fos = new FileOutputStream(mergeFrom);
        props.store(new FileOutputStream(mergeFrom), "");
    } finally {
        if (fos != null) {
            fos.close();
            fos = null;
        }
    }

    // add properties to remove
    PrintWriter out = null;

    try {
        for (Iterator i = el.elementIterator("remove"); i.hasNext();) {
            if (out == null) {
                out = new PrintWriter(new FileWriter(mergeFrom, true));
            }
            Element remove = (Element) i.next();
            String name = remove.elementText("name");
            out.println('-' + name + '=');
        }
    } finally {
        if (out != null) {
            out.close();
            out = null;
        }
    }

    try {
        OverwriteProperties overwriter = new OverwriteProperties();
        overwriter.setBaseProperties(path);
        overwriter.setProperties(mergeFrom);
        overwriter.setIncludeRoot(new File("."));
        overwriter.setVerbose(false);
        overwriter.execute();
    } catch (Exception e) {
        StringWriter sw = new StringWriter();
        e.printStackTrace(new PrintWriter(sw));
        throw new RuntimeException(sw.toString());
    } finally {
        mergeFrom.delete();
    }
}

From source file:net.unicon.toro.installer.tools.MergeConfiguration.java

License:Open Source License

private void processNodeAddOrReplace(Element el, Element source, File path) {
    List list = el.selectNodes("add-or-replace[@where='end']");
    if (list == null)
        return;//from   ww  w. java2 s .c  o  m

    removeOldNodes(source);

    //saveXml(source, new File("/tmp/removals."+path.getName()));

    Iterator itr = list.iterator();
    while (itr.hasNext()) {
        Element replace = (Element) itr.next();
        String xpath = replace.elementText("xpath");

        addNode(source, replace, xpath);
    }
}

From source file:net.unicon.toro.installer.tools.MergeConfiguration.java

License:Open Source License

private void processNodeAddOrReplace2(Element el, Element source) {
    List list = el.selectNodes("add-or-replace-node[@type='node']");
    if (list == null)
        return;// ww w .  ja  v a2  s. c  o m

    Iterator itr = list.iterator();
    while (itr.hasNext()) {
        Element replace = (Element) itr.next();
        String xpath = replace.elementText("xpath");
        Element replacementElement = (Element) replace.element("value").elements().get(0);

        Element sourceEl = (Element) source.selectSingleNode(xpath);
        if (sourceEl != null) {
            System.out.println("Replacing node at path: " + xpath);
            replaceElement(source, replacementElement);
        } else {
            addNode(source, replace, xpath);
        }
    }
}

From source file:net.unicon.toro.installer.tools.MergeConfiguration.java

License:Open Source License

private void processNodeReplacements(Element el, Element source) {
    List list = el.selectNodes("replace[@type='node']");
    if (list == null)
        return;//from  ww w .ja v  a  2  s.  c o  m

    Iterator itr = list.iterator();
    while (itr.hasNext()) {
        Element replace = (Element) itr.next();
        String xpath = replace.elementText("xpath");
        Element replacementElement = (Element) replace.element("value").elements().get(0);

        Element sourceEl = (Element) source.selectSingleNode(xpath);
        if (sourceEl == null) {
            throw new RuntimeException("xpath expression doesn't resolve to a node: " + xpath);
        }
        System.out.println("Replacing node at path: " + xpath);
        replaceElement(sourceEl, replacementElement);
    }
}

From source file:net.unicon.toro.installer.tools.MergeConfiguration.java

License:Open Source License

private void processNodeRemovals(Element el, Element source) {
    List list = el.selectNodes("remove[@type='node']");
    if (list == null)
        return;/*from   w ww.ja  v a  2 s  .  c  o  m*/

    Iterator itr = list.iterator();
    while (itr.hasNext()) {
        Element replace = (Element) itr.next();
        String xpath = replace.elementText("xpath");
        Element sourceEl = (Element) source.selectSingleNode(xpath);
        if (sourceEl == null) {
            throw new RuntimeException("xpath expression doesn't resolve to a node: " + xpath);
        }
        sourceEl.detach();
    }
}

From source file:net.unicon.toro.installer.tools.MergeConfiguration.java

License:Open Source License

private void processElementValueReplacements(Element el, Element source) {
    List list = el.selectNodes("replace[@type='element_value']");
    if (list == null)
        return;/*from   w ww .j av  a  2s .  co m*/

    Iterator itr = list.iterator();
    while (itr.hasNext()) {
        Element replace = (Element) itr.next();
        String xpath = replace.elementText("xpath");
        String value = replace.elementText("value");

        Element sourceEl = (Element) source.selectSingleNode(xpath);
        if (sourceEl == null) {
            throw new RuntimeException("xpath expression doesn't resolve to a node: " + xpath);
        }

        System.out.println("Replacing text at path: " + xpath + " with: " + value);

        sourceEl.setText(value);
    }
}