Example usage for org.dom4j Element remove

List of usage examples for org.dom4j Element remove

Introduction

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

Prototype

boolean remove(Text text);

Source Link

Document

Removes the given Text if the node is an immediate child of this element.

Usage

From source file:com.suneee.core.util.XMLProperties.java

License:Open Source License

/**
 * Deletes the specified property./*from ww  w. jav a  2  s .co  m*/
 *
 * @param name the property to delete.
 */
public synchronized void deleteProperty(String name) {
    // Remove property from cache.
    propertyCache.remove(name);

    String[] propName = parsePropertyName(name);
    // Search for this property by traversing down the XML heirarchy.
    Element element = document.getRootElement();
    for (int i = 0; i < propName.length - 1; i++) {
        element = element.element(propName[i]);
        // Can't find the property so return.
        if (element == null) {
            return;
        }
    }
    // Found the correct element to remove, so remove it...
    element.remove(element.element(propName[propName.length - 1]));
    // .. then write to disk.
    saveProperties();

    // Generate event.
    /*      Map<String, Object> params = Collections.emptyMap();
          PropertyEventDispatcher.dispatchEvent(name, PropertyEventDispatcher.EventType.xml_property_deleted, params);
      */}

From source file:com.suneee.core.util.XMLProperties.java

License:Open Source License

public void setPropertiesList(String name, List<Map<String, String>> list) {
    String[] propName = parsePropertyName(name);
    // Search for this property by traversing down the XML heirarchy,
    // stopping one short.
    Element element = document.getRootElement();
    for (int i = 0; i < propName.length - 1; i++) {
        // If we don't find this part of the property in the XML heirarchy
        // we add it as a new node
        if (element.element(propName[i]) == null) {
            element.addElement(propName[i]);
        }/*from  w  w  w . j  a va  2 s.  co m*/
        element = element.element(propName[i]);
    }
    String childName = propName[propName.length - 1];
    // We found matching property, clear all children.
    List<Element> toRemove = new ArrayList<Element>();
    Iterator iter = element.elementIterator(childName);
    while (iter.hasNext()) {
        toRemove.add((Element) iter.next());
    }
    for (iter = toRemove.iterator(); iter.hasNext();) {
        element.remove((Element) iter.next());
    }
    // Add the new children.
    for (int j = 0; j < list.size(); j++) {
        Element childElement = element.addElement(childName);
        Map<String, String> map = (Map<String, String>) list.get(j);
        for (Map.Entry<String, String> record : map.entrySet()) {
            Element e = childElement.addElement(record.getKey());
            e.setText(record.getValue());
            //            setText(StringEscapeUtils.escapeXml(value));
        }
    }
    saveProperties();

    // Generate event.
    /*Map<String, Object> params = new HashMap<String, Object>();
    params.put("value", values);
    PropertyEventDispatcher.dispatchEvent(name,
        PropertyEventDispatcher.EventType.xml_property_set, params);*/
}

From source file:com.tao.realweb.util.XMLProperties.java

License:Open Source License

/**
 * Sets a property to an array of values. Multiple values matching the same property
 * is mapped to an XML file as multiple elements containing each value.
 * For example, using the name "foo.bar.prop", and the value string array containing
 * {"some value", "other value", "last value"} would produce the following XML:
 * <pre>//from   w w w  .  j  a v  a  2s.co m
 * &lt;foo&gt;
 *     &lt;bar&gt;
 *         &lt;prop&gt;some value&lt;/prop&gt;
 *         &lt;prop&gt;other value&lt;/prop&gt;
 *         &lt;prop&gt;last value&lt;/prop&gt;
 *     &lt;/bar&gt;
 * &lt;/foo&gt;
 * </pre>
 *
 * @param name the name of the property.
 * @param values the values for the property (can be empty but not null).
 */
public void setProperties(String name, List<String> values) {
    String[] propName = parsePropertyName(name);
    // Search for this property by traversing down the XML heirarchy,
    // stopping one short.
    Element element = document.getRootElement();
    for (int i = 0; i < propName.length - 1; i++) {
        // If we don't find this part of the property in the XML heirarchy
        // we add it as a new node
        if (element.element(propName[i]) == null) {
            element.addElement(propName[i]);
        }
        element = element.element(propName[i]);
    }
    String childName = propName[propName.length - 1];
    // We found matching property, clear all children.
    List<Element> toRemove = new ArrayList<Element>();
    Iterator iter = element.elementIterator(childName);
    while (iter.hasNext()) {
        toRemove.add((Element) iter.next());
    }
    for (iter = toRemove.iterator(); iter.hasNext();) {
        element.remove((Element) iter.next());
    }
    // Add the new children.
    for (String value : values) {
        Element childElement = element.addElement(childName);
        if (value.startsWith("<![CDATA[")) {
            Iterator it = childElement.nodeIterator();
            while (it.hasNext()) {
                Node node = (Node) it.next();
                if (node instanceof CDATA) {
                    childElement.remove(node);
                    break;
                }
            }
            childElement.addCDATA(value.substring(9, value.length() - 3));
        } else {
            childElement.setText(StringEscapeUtils.escapeXml(value));
        }
    }
    saveProperties();

    // Generate event.
    Map<String, Object> params = new HashMap<String, Object>();
    params.put("value", values);
    PropertyEventDispatcher.dispatchEvent(name, PropertyEventDispatcher.EventType.xml_property_set, params);
}

From source file:com.tao.realweb.util.XMLProperties.java

License:Open Source License

/**
 * Sets the value of the specified property. If the property doesn't
 * currently exist, it will be automatically created.
 *
 * @param name  the name of the property to set.
 * @param value the new value for the property.
 *///  w  w w . j av a2s  .  c om
public synchronized void setProperty(String name, String value) {
    if (!StringEscapeUtils.escapeXml(name).equals(name)) {
        throw new IllegalArgumentException("Property name cannot contain XML entities.");
    }
    if (name == null) {
        return;
    }
    if (value == null) {
        value = "";
    }

    // Set cache correctly with prop name and value.
    propertyCache.put(name, value);

    String[] propName = parsePropertyName(name);
    // Search for this property by traversing down the XML heirarchy.
    Element element = document.getRootElement();
    for (String aPropName : propName) {
        // If we don't find this part of the property in the XML heirarchy
        // we add it as a new node
        if (element.element(aPropName) == null) {
            element.addElement(aPropName);
        }
        element = element.element(aPropName);
    }
    // Set the value of the property in this node.
    if (value.startsWith("<![CDATA[")) {
        Iterator it = element.nodeIterator();
        while (it.hasNext()) {
            Node node = (Node) it.next();
            if (node instanceof CDATA) {
                element.remove(node);
                break;
            }
        }
        element.addCDATA(value.substring(9, value.length() - 3));
    } else {
        element.setText(value);
    }
    // Write the XML properties to disk
    saveProperties();

    // Generate event.
    Map<String, Object> params = new HashMap<String, Object>();
    params.put("value", value);
    PropertyEventDispatcher.dispatchEvent(name, PropertyEventDispatcher.EventType.xml_property_set, params);
}

From source file:com.tao.realweb.util.XMLProperties.java

License:Open Source License

/**
 * Deletes the specified property.// www .  j a v  a2  s  .co m
 *
 * @param name the property to delete.
 */
public synchronized void deleteProperty(String name) {
    // Remove property from cache.
    propertyCache.remove(name);

    String[] propName = parsePropertyName(name);
    // Search for this property by traversing down the XML heirarchy.
    Element element = document.getRootElement();
    for (int i = 0; i < propName.length - 1; i++) {
        element = element.element(propName[i]);
        // Can't find the property so return.
        if (element == null) {
            return;
        }
    }
    // Found the correct element to remove, so remove it...
    element.remove(element.element(propName[propName.length - 1]));
    // .. then write to disk.
    saveProperties();

    // Generate event.
    Map<String, Object> params = Collections.emptyMap();
    PropertyEventDispatcher.dispatchEvent(name, PropertyEventDispatcher.EventType.xml_property_deleted, params);
}

From source file:com.taobao.android.builder.tools.manifest.ManifestFileUtils.java

License:Apache License

/**
 * libManifest/*w w  w .  ja  va  2  s.  c  om*/
 *
 * @param libManifestFile
 * @param mainManifestFileObject param updateSdkVersion
 */
public static void updatePreProcessManifestFile(File libManifestFile, ManifestFileObject mainManifestFileObject,
        boolean updateSdkVersion) throws IOException, DocumentException {

    if (!libManifestFile.exists()) {
        return;
    }

    File orgManifestFile = new File(libManifestFile.getParentFile(), "AndroidManifest-org.xml");
    if (orgManifestFile.exists()) {
        return;
    }

    libManifestFile.renameTo(orgManifestFile);

    SAXReader reader = new SAXReader();
    OutputFormat format = OutputFormat.createPrettyPrint();
    format.setEncoding("UTF-8");// XML??

    Document document = reader.read(orgManifestFile);// ?XML
    Element root = document.getRootElement();// 
    if (updateSdkVersion) {
        Element useSdkElement = root.element("uses-sdk");

        if (null == useSdkElement) {
            useSdkElement = root.addElement("uses-sdk");
        }

        if (null != useSdkElement) {
            updateElement(useSdkElement, mainManifestFileObject.getUseSdkProperties());
        }
    }

    // ?tools:removetools:replace?ManifestMerge??
    Element applicationElement = root.element("application");
    Map<String, String> replaceAttrs = mainManifestFileObject.getReplaceApplicationAttribute();
    List<String> removeAttrs = mainManifestFileObject.getRemoveApplicationAttribute();

    if (null != applicationElement) {
        // libtools
        List<Attribute> toRomoved = new ArrayList<Attribute>();
        for (Attribute attribute : applicationElement.attributes()) {
            if (attribute.getName().equals("replace") || attribute.getName().equals("remove")) {
                // applicationElement.remove(attribute);
                // applicationElement.attributes().remove(attribute);
                toRomoved.add(attribute);
            }
        }
        if (toRomoved.size() > 0) {
            for (Attribute attribute : toRomoved) {
                applicationElement.remove(attribute);
            }
        }

        updateApplicationElement(applicationElement, replaceAttrs, removeAttrs);
    }

    //?packageName TODO
    String packageName = root.attributeValue("package");
    if (StringUtils.isEmpty(packageName)) {
        packageName = ManifestFileUtils.getPackage(orgManifestFile);
    }

    List<? extends Node> applicatNodes = root.selectNodes("//application");
    for (Node node : applicatNodes) {
        Element element = (Element) node;
        Attribute attribute = element.attribute("name");
        if (attribute != null) {
            if (!attribute.getValue().startsWith(packageName)) {
                attribute.setValue(packageName + attribute.getValue());
            }
        }
    }

    fillFullClazzName(root, packageName, "activity");
    fillFullClazzName(root, packageName, "provider");
    fillFullClazzName(root, packageName, "receiver");
    fillFullClazzName(root, packageName, "service");

    saveFile(document, format, libManifestFile);
}

From source file:com.taobao.android.builder.tools.manifest.ManifestFileUtils.java

License:Apache License

/**
 * Application?//  w w  w  . j a va2s. c  om
 *
 * @param element
 * @param replaceAttrs
 * @param removeAttrs
 */
private static void updateApplicationElement(Element element, Map<String, String> replaceAttrs,
        List<String> removeAttrs) {
    for (Map.Entry<String, String> entry : replaceAttrs.entrySet()) {
        String key = entry.getKey();
        key = StringUtils.substringAfter(key, ":");
        Attribute attribute = element.attribute(key);
        if (null != attribute) {
            element.remove(attribute);
        }
    }
    for (String key : removeAttrs) {
        key = StringUtils.substringAfter(key, ":");
        Attribute attribute = element.attribute(key);
        if (null != attribute) {
            element.remove(attribute);
        }
    }
}

From source file:com.tedi.engine.XMLOutput.java

License:Open Source License

/**
 * Clean the element.// w  w w  .  j a v  a2  s .  co m
 * 
 * @param ele
 *            The element.
 */
private void cleanElement(Element ele) {

    if (logger.isDebugEnabled()) {
        logger.debug("Cleaning the element " + ele);
    }
    // Attribute check shouldn't be necessary since only trying to write
    // populated attributes
    // but leave for now just in case
    for (Iterator i = ele.attributes().iterator(); i.hasNext();) {
        Attribute a = (Attribute) i.next();
        if (!mapFile.isSuppressAttribIfEmpty() || a.getText().length() > 0)
            continue;
        if (!mapFile.isSuppressAttribIfHasOnlyWhitespace() || a.getText().trim().length() > 0)
            continue;
        ele.remove(a);
    }
    for (Iterator i = ele.elements().iterator(); i.hasNext();) {
        cleanElement((Element) i.next());
    }
    if (!mapFile.isSuppressElementIfHasNoChildren() || ele.elements().size() > 0)
        return;
    if (!mapFile.isSuppressElementIfHasOnlyEmptyAttribs() || ele.attributes().size() > 0)
        return;
    if (!mapFile.isSuppressElementIfEmpty() || ele.getText().length() > 0)
        return;
    if (!mapFile.isSuppressElementIfHasOnlyWhitespace() || ele.getTextTrim().length() > 0)
        return;
    ele.getParent().remove(ele);
}

From source file:com.thinkberg.moxo.dav.data.DavResource.java

License:Apache License

@SuppressWarnings({ "WeakerAccess" })
protected boolean addLockDiscoveryProperty(Element root) {
    Element lockdiscoveryEl = root.addElement(PROP_LOCK_DISCOVERY);
    try {//from   ww w.ja  va 2  s  . c om
        List<Lock> locks = LockManager.getInstance().discoverLock(object);
        if (locks != null && !locks.isEmpty()) {
            for (Lock lock : locks) {
                if (lock != null && !ignoreValues) {
                    lock.serializeToXml(lockdiscoveryEl);
                }
            }
        }
        return true;
    } catch (FileSystemException e) {
        e.printStackTrace();
        root.remove(lockdiscoveryEl);
        return false;
    }
}

From source file:com.thinkberg.webdav.data.DavResource.java

License:Apache License

protected boolean addLockDiscoveryProperty(Element root, boolean ignoreValue) {
    Element lockdiscoveryEl = root.addElement(PROP_LOCK_DISCOVERY);
    try {//w  w  w  .  j a v a  2 s . c  o  m
        List<Lock> locks = LockManager.getInstance().discoverLock(object);
        if (locks != null && !locks.isEmpty()) {
            for (Lock lock : locks) {
                if (lock != null && !ignoreValue) {
                    lock.serializeToXml(lockdiscoveryEl);
                }
            }
        }
        return true;
    } catch (FileSystemException e) {
        root.remove(lockdiscoveryEl);
        e.printStackTrace();
        return false;
    }
}