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.haulmont.cuba.gui.components.filter.FilterDelegateImpl.java

License:Apache License

@Override
public boolean saveSettings(Element element) {
    Boolean changed = false;//w ww .  j a v  a2  s .  c  o  m
    Element e = element.element("defaultFilter");
    if (e == null)
        e = element.addElement("defaultFilter");

    UUID defaultId = null;
    Boolean applyDefault = false;

    for (FilterEntity filter : filterEntities) {
        if (BooleanUtils.isTrue(filter.getIsDefault())) {
            defaultId = filter.getId();
            applyDefault = filter.getApplyDefault();
            break;
        }
    }

    String newDef = defaultId != null ? defaultId.toString() : null;
    Attribute attr = e.attribute("id");
    String oldDef = attr != null ? attr.getValue() : null;
    if (!Objects.equals(oldDef, newDef)) {
        if (newDef == null && attr != null) {
            e.remove(attr);
        } else {
            if (attr == null)
                e.addAttribute("id", newDef);
            else
                attr.setValue(newDef);
        }
        changed = true;
    }
    Boolean newApplyDef = BooleanUtils.isTrue(applyDefault);
    Attribute applyDefaultAttr = e.attribute("applyDefault");
    Boolean oldApplyDef = applyDefaultAttr != null ? Boolean.valueOf(applyDefaultAttr.getValue()) : false;
    if (!Objects.equals(oldApplyDef, newApplyDef)) {
        if (applyDefaultAttr != null) {
            applyDefaultAttr.setValue(newApplyDef.toString());
        } else {
            e.addAttribute("applyDefault", newApplyDef.toString());
        }
        changed = true;
    }

    Element groupBoxExpandedEl = element.element("groupBoxExpanded");
    if (groupBoxExpandedEl == null)
        groupBoxExpandedEl = element.addElement("groupBoxExpanded");

    Boolean oldGroupBoxExpandedValue = Boolean.valueOf(groupBoxExpandedEl.getText());
    Boolean newGroupBoxExpandedValue = groupBoxLayout.isExpanded();
    if (!Objects.equals(oldGroupBoxExpandedValue, newGroupBoxExpandedValue)) {
        groupBoxExpandedEl.setText(newGroupBoxExpandedValue.toString());
        changed = true;
    }

    return changed;
}

From source file:com.haulmont.cuba.gui.xml.XmlInheritanceProcessor.java

License:Apache License

private void process(Element resultElem, Element extElem) {
    // set text//from  ww w  .  j  a v a 2 s.  c om
    if (!StringUtils.isBlank(extElem.getText()))
        resultElem.setText(extElem.getText());

    // add all attributes from extension
    for (Attribute attribute : Dom4j.attributes(extElem)) {
        if (resultElem == document.getRootElement() && attribute.getName().equals("extends")) {
            // ignore "extends" in root element
            continue;
        }
        resultElem.addAttribute(attribute.getName(), attribute.getValue());
    }

    String idx = extElem.attributeValue(new QName("index", extNs));
    if (resultElem != document.getRootElement() && StringUtils.isNotBlank(idx)) {
        int index = Integer.parseInt(idx);

        Element parent = resultElem.getParent();
        if (index < 0 || index > parent.elements().size()) {
            String message = String.format(
                    "Incorrect extension XML for screen. Could not move existing element %s to position %s",
                    resultElem.getName(), index);

            throw new DevelopmentException(message,
                    ParamsMap.of("element", resultElem.getName(), "index", index));
        }

        parent.remove(resultElem);
        //noinspection unchecked
        parent.elements().add(index, resultElem);
    }

    // add and process elements
    Set<Element> justAdded = new HashSet<>();
    for (Element element : Dom4j.elements(extElem)) {
        // look for suitable locator
        ElementTargetLocator locator = null;
        for (ElementTargetLocator l : targetLocators) {
            if (l.suitableFor(element)) {
                locator = l;
                break;
            }
        }
        if (locator != null) {
            Element target = locator.locate(resultElem, element);
            // process target or a new element if target not found
            if (target != null) {
                process(target, element);
            } else {
                addNewElement(resultElem, element, justAdded);
            }
        } else {
            // if no suitable locator found, look for a single element with the same name
            List<Element> list = Dom4j.elements(resultElem, element.getName());
            if (list.size() == 1 && !justAdded.contains(list.get(0))) {
                process(list.get(0), element);
            } else {
                addNewElement(resultElem, element, justAdded);
            }
        }
    }
}

From source file:com.haulmont.cuba.web.gui.components.WebAbstractTable.java

License:Apache License

@Override
public boolean saveSettings(Element element) {
    if (!isSettingsEnabled()) {
        return false;
    }//from  ww  w  . j a  va2  s.  c  o  m

    if (isUsePresentations()) {
        element.addAttribute("textSelection", String.valueOf(component.isTextSelectionEnabled()));
    }

    Element columnsElem = element.element("columns");
    if (columnsElem != null) {
        element.remove(columnsElem);
    }
    columnsElem = element.addElement("columns");

    Object[] visibleColumns = component.getVisibleColumns();
    for (Object column : visibleColumns) {
        Element colElem = columnsElem.addElement("columns");
        colElem.addAttribute("id", column.toString());

        int width = component.getColumnWidth(column);
        if (width > -1)
            colElem.addAttribute("width", String.valueOf(width));

        Boolean visible = !component.isColumnCollapsed(column);
        colElem.addAttribute("visible", visible.toString());
    }

    MetaPropertyPath sortProperty = (MetaPropertyPath) component.getSortContainerPropertyId();
    if (sortProperty != null) {
        Boolean sortAscending = component.isSortAscending();

        columnsElem.addAttribute("sortProperty", sortProperty.toString());
        columnsElem.addAttribute("sortAscending", sortAscending.toString());
    }

    return true;
}

From source file:com.haulmont.cuba.web.gui.components.WebDataGrid.java

License:Apache License

@Override
public boolean saveSettings(Element element) {
    if (!isSettingsEnabled()) {
        return false;
    }//from  www. j a v a  2s  .co m

    Element columnsElem = element.element("columns");
    if (columnsElem != null) {
        element.remove(columnsElem);
    }
    columnsElem = element.addElement("columns");

    List<Column> visibleColumns = getVisibleColumns();
    for (Column column : visibleColumns) {
        Element colElem = columnsElem.addElement("columns");
        colElem.addAttribute("id", column.toString());

        double width = column.getWidth();
        if (width > -1) {
            colElem.addAttribute("width", String.valueOf(width));
        }

        colElem.addAttribute("collapsed", Boolean.toString(column.isCollapsed()));
    }

    List<com.vaadin.data.sort.SortOrder> sortOrders = component.getSortOrder();
    if (!sortOrders.isEmpty()) {
        com.vaadin.data.sort.SortOrder sortOrder = sortOrders.get(0);
        columnsElem.addAttribute("sortProperty", sortOrder.getPropertyId().toString());
        columnsElem.addAttribute("sortDirection", sortOrder.getDirection().toString());
    }

    return true;
}

From source file:com.haulmont.cuba.web.gui.components.WebGroupTable.java

License:Apache License

@Override
public boolean saveSettings(Element element) {
    if (!isSettingsEnabled()) {
        return false;
    }//from  w w  w .  j a  va 2  s.  c om

    super.saveSettings(element);

    Element groupPropertiesElement = element.element("groupProperties");
    if (groupPropertiesElement != null) {
        element.remove(groupPropertiesElement);
    }

    groupPropertiesElement = element.addElement("groupProperties");

    final Collection<?> groupProperties = component.getGroupProperties();
    for (Object groupProperty : groupProperties) {
        if (getNotCollapsedColumns().contains(getColumn(groupProperty.toString()))) {
            final Element groupPropertyElement = groupPropertiesElement.addElement("property");
            groupPropertyElement.addAttribute("id", groupProperty.toString());
        }
    }

    return true;
}

From source file:com.hihframework.osplugins.dom4j.XmlParseUtil.java

License:Apache License

/**
 *
 * @param element/*  w w w  .j  a  v  a2  s .c om*/
 * @param attributeName
 */
public boolean deleteAllAttribute(Element element, String attributeName) {
    Attribute attribute = (Attribute) element.attribute(attributeName);
    boolean deleteFlag = element.remove(attribute);
    return deleteFlag;
}

From source file:com.hihframework.osplugins.dom4j.XmlParseUtil.java

License:Apache License

/**
 * ?// w ww  . ja va2 s  . c  o m
 *
 * @param element
 *            ?
 */
public boolean deleteAttribute(Element element) {
    boolean deleteFlag = false;
    Iterator<?> iterator = element.attributeIterator();
    while (iterator.hasNext()) {
        Attribute attribute = (Attribute) iterator.next();
        deleteFlag = element.remove(attribute);
    }
    return deleteFlag;
}

From source file:com.jeeframework.util.xml.XMLProperties.java

License:Open Source License

/**
 * Removes the given attribute from the XML document.
 *
 * @param name      the property name to lookup - ie, "foo.bar"
 * @param attribute the name of the attribute, ie "id"
 * @return the value of the attribute of the given property or <tt>null</tt> if
 * it did not exist.//from w  w  w  .j  ava2 s .c o m
 */
public String removeAttribute(String name, String attribute) {
    if (name == null || attribute == null) {
        return null;
    }
    String[] propName = parsePropertyName(name);
    // Search for this property by traversing down the XML hierarchy.
    Element element = document.getRootElement();
    for (String child : propName) {
        element = element.element(child);
        if (element == null) {
            // This node doesn't match this part of the property name which
            // indicates this property doesn't exist so return empty array.
            break;
        }
    }
    String result = null;
    if (element != null) {
        // Get the attribute value and then remove the attribute
        Attribute attr = element.attribute(attribute);
        result = attr.getValue();
        element.remove(attr);
    }
    return result;
}

From source file:com.jeeframework.util.xml.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>/*w  w  w. ja  v a 2  s.  c o  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, boolean isEncrypt) {
    String[] propName = parsePropertyName(name);
    // Search for this property by traversing down the XML hierarchy,
    // 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 hierarchy
        // 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<Element> iter = element.elementIterator(childName);
    while (iter.hasNext()) {
        toRemove.add(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<Node> it = childElement.nodeIterator();
            while (it.hasNext()) {
                Node node = it.next();
                if (node instanceof CDATA) {
                    childElement.remove(node);
                    break;
                }
            }
            childElement.addCDATA(value.substring(9, value.length() - 3));
        } else {
            String propValue = StringEscapeUtils.escapeXml(value);
            // check to see if the property is marked as encrypted
            if (isEncrypt) {
                propValue = EncryptUtil.desEncrypt(encryptKey, value);
                childElement.addAttribute(ENCRYPTED_ATTRIBUTE, "true");
            }
            childElement.setText(propValue);
        }
    }
    saveProperties();

}

From source file:com.jeeframework.util.xml.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.
 * @param isEncrypt ?//from   w ww. ja v a  2s. c  om
 */
public synchronized void setProperty(String name, String value, boolean isEncrypt) {
    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 hierarchy.
    Element element = document.getRootElement();
    for (String aPropName : propName) {
        // If we don't find this part of the property in the XML hierarchy
        // 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 {
        String propValue = StringEscapeUtils.escapeXml(value);
        // check to see if the property is marked as encrypted
        if (isEncrypt) {
            propValue = EncryptUtil.desEncrypt(encryptKey, value);
            element.addAttribute(ENCRYPTED_ATTRIBUTE, "true");
        }
        element.setText(propValue);
    }
    // Write the XML properties to disk
    saveProperties();

}