Example usage for org.dom4j Element setAttributes

List of usage examples for org.dom4j Element setAttributes

Introduction

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

Prototype

void setAttributes(List<Attribute> attributes);

Source Link

Document

Sets the attributes that this element contains

Usage

From source file:com.globalsight.everest.tm.util.ttx.TtxToTmx.java

License:Apache License

/**
 * Creates new elements for TU and TUV with lowercase names by
 * constructing new elements and inserting them in place of the
 * old ones.//w w  w . ja v  a  2s.c o m
 */
private Element lowercaseElements(Element p_tu) {
    Element tu = m_factory.createElement("tu");
    tu.setAttributes(p_tu.attributes());

    List tuvs = p_tu.selectNodes("//Tuv");

    for (int i = 0, max = tuvs.size(); i < max; i++) {
        Element tuv = (Element) tuvs.get(i);
        // Detach the TUV or else TUVs will accumulate on
        // subsequent TUs (sic!).
        tuv.detach();

        Element newTuv = m_factory.createElement("tuv");
        newTuv.setAttributes(tuv.attributes());
        newTuv.setContent(tuv.content());

        tu.add(newTuv);
    }

    return tu;
}

From source file:com.mg.framework.support.ui.UIProducer.java

License:Open Source License

private static Element copyElement(Element element, RuntimeMacrosLoader runtimeMacrosLoader) {
    //if (INCLUDE_TAG_NAME.equals(element.getQualifiedName()))
    //return includeMacros();

    Element result = DocumentHelper.createElement(element.getQualifiedName());
    result.setAttributes(element.attributes());
    List<Element> childElements = MiscUtils.convertUncheckedList(Element.class, element.elements());
    for (Element childElement : childElements) {
        Element copy;//from   w  w  w .  j  ava  2 s .c o m
        if (childElement.getQualifiedName().equals(INCLUDE_TAG_NAME)) {
            copy = includeMacros(result, childElement, runtimeMacrosLoader);
            /*Document macros = null;
            String runtimeMacros = childElement.attributeValue(RUNTIME_MACROS_NAME_ATTR);
            if (runtimeMacros == null)
            macros = loadMacros(childElement.attributeValue(MACROS_NAME_ATTR));
            else
            macros = loadRuntimeMacros(runtimeMacros, runtimeMacrosLoader);
            String macrosType = macros.getRootElement().getQualifiedName();
            if (macrosType.equals(EMPTY_MACROS))
            copy = null; //handle special case for empty macros
            else if (macrosType.equals(WRAP_MACROS)) {
            //copy macros contents exclude root element
            List<Element> macrosChildElements = MiscUtils.convertUncheckedList(Element.class, macros.getRootElement().elements());
            for (Element macrosChild : macrosChildElements) {
              Element macrosElement = copyElement(macrosChild, runtimeMacrosLoader);
              if (macrosElement != null)
             result.add(macrosElement);
            }
            copy = null;
            }
            else
            copy = copyElement(macros.getRootElement(), runtimeMacrosLoader); //copy root element
            */
        } else {
            copy = copyElement(childElement, runtimeMacrosLoader);
        }
        if (copy != null)
            result.add(copy);
    }
    return result;
}

From source file:org.craftercms.cstudio.alfresco.dm.service.impl.DmContentTypeServiceImpl.java

License:Open Source License

/**
 * copy the given element to the target node
 *
 * @param site/*from   w ww .j av a2  s .  com*/
 * @param parentPath
 * @param element
 * @param targetNode
 */
protected void copyContent(String site, String parentPath, Element element, Node targetNode) {
    if (targetNode != null) {
        String name = element.getName();
        String currentPath = parentPath + name;
        boolean noCopyOnConvert = DmUtils.getBooleanValue(targetNode.valueOf("@no-copy"), false);
        if (!this.getExcludedPaths(site).contains(currentPath) && !noCopyOnConvert) {
            Element targetElement = (Element) targetNode;
            boolean multiValued = DmUtils.getBooleanValue(targetNode.valueOf("@copy-children"), false);
            List<Element> childElements = element.elements();
            if (childElements != null && childElements.size() > 0) {
                for (Element childElement : childElements) {
                    Node targetChildNode = null;
                    if (this.getMultiValuedPaths(site).contains(currentPath) || multiValued) {
                        if (LOGGER.isDebugEnabled()) {
                            LOGGER.debug("[CHANGE-TEMPLATE] " + currentPath + " is multi-valued.");
                        }
                        targetChildNode = (Node) targetElement.addElement(childElement.getName());
                    } else {
                        String uniquePath = childElement.getUniquePath(element);
                        targetChildNode = targetElement.selectSingleNode(uniquePath);
                    }
                    if (LOGGER.isDebugEnabled()) {
                        LOGGER.debug("[CHANGE-TEMPLATE] copying child node of " + currentPath + " : "
                                + childElement.getUniquePath(element));
                    }
                    copyContent(site, currentPath + "/", childElement, targetChildNode);
                }
            }
            targetElement.setText(element.getText());
            targetElement.setAttributes(element.attributes());
        } else {
            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug("[CHANGE-TEMPLATE] " + currentPath
                        + " is not being copied since it is in exlcudedPaths or no copy on convert node.");
            }
        }
    } else {
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug(
                    "[CHANGE-TEMPLATE] " + element.getUniquePath() + " does not exist in target location.");
        }
    }

}

From source file:org.hightides.annotations.util.SpringXMLUtil.java

License:Apache License

/**
 * Method to add a bean to a spring configuration.
 * @param xmlFilename/*from  ww  w .j av a 2 s.co  m*/
 * @param bean
 * @param backup
 * @return
 */
@SuppressWarnings("unchecked")
public static Element addBean(String xmlFilename, Element bean, boolean backup, String syncMode) {
    String filename = FileUtil.getFilename(xmlFilename);
    // some checking first
    if (StringUtil.isEmpty(bean.attributeValue("id"))) {
        _log.warn("Attempt to insert empty bean id on [" + filename + "].");
        return null;
    }
    Document doc = readXMLDocument(xmlFilename);
    if (doc == null)
        return null;
    Element root = doc.getRootElement();
    // beans
    List<Element> elements = root.elements();
    List<Element> backupBeans = new ArrayList<Element>();
    boolean found = false;
    for (Element element : elements) {
        // check if bean already exist
        if (bean.attributeValue("id").equals(element.attributeValue("id"))) {
            if (syncMode.equals("UPDATE")) {
                // copy all the attributes
                bean.setAttributes(element.attributes());
                // inside bean (property)
                List<Element> properties = element.elements();
                List<Element> beanProperties = bean.elements();
                for (Element property : properties) {
                    found = false;
                    for (Element beanProperty : beanProperties) {
                        // check if property already exists in bean
                        if (beanProperty.attributeValue("name").equals(property.attributeValue("name"))) {
                            // remove existing property in bean
                            bean.remove(beanProperty);
                            cloneChildElement(bean, property);
                            found = true;
                            break;
                        }
                    }
                    if (!found) {
                        // add property to bean
                        cloneChildElement(bean, property);
                    }
                }
            }
            root.remove(element);
            found = true;
        } else if (found) {
            backupBeans.add(element);
            root.remove(element);
        }
    }
    cloneChildElement(root, bean);
    if (backupBeans != null) {
        for (Element backUpBean : backupBeans) {
            cloneChildElement(root, backUpBean);
        }
    }
    if (saveXMLDocument(xmlFilename, doc, backup)) {
        if (found)
            _log.info("Updated bean [" + bean.attributeValue("id") + "] on [" + filename + "].");
        else
            _log.info("Added bean [" + bean.attributeValue("id") + "] on [" + filename + "].");
    }
    return bean;
}

From source file:org.hightides.annotations.util.SpringXMLUtil.java

License:Apache License

/**
 * Copies the node to target. This is necessary because
 * DOM4J appends xmlns when bean is inserted as addElement().
 * @param target/*from   w  w w. ja v a 2  s . com*/
 * @param node
 */
@SuppressWarnings("unchecked")
private static void cloneChildElement(Element target, Element node) {
    // create new element
    Element newBean = target.addElement(node.getName());
    // copy all attributes
    newBean.setAttributes(node.attributes());
    // copy text elements
    if (!StringUtil.isEmpty(node.getText()))
        newBean.addText(node.getText());
    List<Element> elements = node.elements();
    // copy all elements
    for (Element elem : elements) {
        cloneChildElement(newBean, elem);
    }
}

From source file:org.jbpm.graph.node.ProcessState.java

License:Open Source License

public void execute(ExecutionContext executionContext) {
    Token superProcessToken = executionContext.getToken();

    ProcessDefinition usedSubProcessDefinition = subProcessDefinition;
    // if this process has late binding
    if ((subProcessDefinition == null) && (subProcessName != null)) {
        SubProcessResolver subProcessResolver = getSubProcessResolver();
        List attributes = new ArrayList();
        String subProcessNameResolved = (String) JbpmExpressionEvaluator.evaluate(subProcessName,
                executionContext);//w  w w .j a v a  2  s .c o m
        if (log.isDebugEnabled()) {
            log.debug("SubProcessName after eval: " + subProcessNameResolved);
        }
        attributes.add(new FlyweightAttribute("name", subProcessNameResolved));
        Element subProcessElement = new DefaultElement("sub-process");
        subProcessElement.setAttributes(attributes);

        usedSubProcessDefinition = subProcessResolver.findSubProcess(subProcessElement);
    }

    // create the subprocess
    ProcessInstance subProcessInstance = superProcessToken.createSubProcessInstance(usedSubProcessDefinition);

    // fire the subprocess created event
    fireEvent(Event.EVENTTYPE_SUBPROCESS_CREATED, executionContext);

    // feed the readable variableInstances
    if ((variableAccesses != null) && (!variableAccesses.isEmpty())) {

        ContextInstance superContextInstance = executionContext.getContextInstance();
        ContextInstance subContextInstance = subProcessInstance.getContextInstance();
        subContextInstance.setTransientVariables(superContextInstance.getTransientVariables());

        // loop over all the variable accesses
        Iterator iter = variableAccesses.iterator();
        while (iter.hasNext()) {
            VariableAccess variableAccess = (VariableAccess) iter.next();
            // if this variable access is readable
            if (variableAccess.isReadable()) {
                // the variable is copied from the super process variable name
                // to the sub process mapped name
                String variableName = variableAccess.getVariableName();
                Object value = superContextInstance.getVariable(variableName, superProcessToken);
                String mappedName = variableAccess.getMappedName();
                log.debug("copying super process var '" + variableName + "' to sub process var '" + mappedName
                        + "': " + value);
                if (value != null) {
                    subContextInstance.setVariable(mappedName, value);
                }
            }
        }
    }

    // send the signal to start the subprocess
    subProcessInstance.signal();
}

From source file:org.makumba.commons.tags.MakumbaTLDGenerator.java

License:Open Source License

/**
 * Replaces the content of a tag attribute using "specifiedIn" by the actual content
 * // www. ja  va  2 s . c om
 * @param processedTags
 *            the hashmap of already processed tags
 * @param errorMsg
 *            the error message appearing in case the referenced attribute can't be found
 * @param tagName
 *            the name of the parent tag
 * @param attributeTagContent
 *            the content of the attribute
 */
public static void replaceReferencedAttribute(HashMap<String, Element> processedTags, final String errorMsg,
        String tagName, Element attributeTagContent) {
    Element newTag = getReferencedAttributes(processedTags, errorMsg, tagName, attributeTagContent, true);
    attributeTagContent.setAttributes(newTag.attributes());
    final List<Element> elements = getElementList(newTag);
    for (Element element : elements) {
        attributeTagContent.add((Element) element.clone());
    }
}

From source file:org.projectforge.business.gantt.GanttChartDao.java

License:Open Source License

/**
 * Removes all unnecessary GanttObject elements from the DOM (those without any information rather than the id).
 *///from w  ww . j a va  2 s. com
private boolean removeUnnecessaryElements(final Element element) {
    if (CollectionUtils.isNotEmpty(element.elements()) == true) {
        for (final Object childObj : element.elements()) {
            final Element child = (Element) childObj;
            if (removeUnnecessaryElements(child) == true) {
                element.remove(child);
            }
        }
    }
    if (CollectionUtils.isNotEmpty(element.elements()) == true) {
        // Element has descendants.
        return false;
    }
    if (StringUtils.isBlank(element.getText()) == false) {
        // Element has non blank content.
        return false;
    }
    // Element has no descendants:
    if (CollectionUtils.isEmpty(element.attributes()) == true) {
        // Element has no attributes.
        return true;
    }
    if ("predecessor".equals(element.getName()) == true) {
        if (element.attribute(XmlObjectWriter.ATTR_ID) != null) {
            // Describes a complete Gantt task which is referenced, so full output is needed.
            return false;
        } else {
            final Attribute idAttr = element.attribute("id");
            final Attribute refIdAttr = element.attribute(XmlObjectWriter.ATTR_REF_ID);
            element.setAttributes(null);
            if (refIdAttr != null) {
                element.addAttribute(XmlObjectWriter.ATTR_REF_ID, refIdAttr.getValue());
            } else if (idAttr != null) {
                // External reference (no ref-id, but task id):
                element.addAttribute("id", idAttr.getValue());
            } else {
                // Should not occur.
                return true;
            }
            return false;
        }
    } else if (element.attributes().size() == 1 && element.attribute("id") != null) {
        // Element has only id attribute and is not a predecessor definition for tasks outside the current Gantt object tree.
        return true;
    }
    return false;
}