Example usage for org.dom4j Element createCopy

List of usage examples for org.dom4j Element createCopy

Introduction

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

Prototype

Element createCopy();

Source Link

Document

Creates a deep copy of this element The new element is detached from its parent, and getParent() on the clone will return null.

Usage

From source file:org.sipfoundry.openfire.vcard.provider.RestInterface.java

License:Contributor Agreement License

public static String refillMissingContactInfo(String vcardXml, String contactXml) {
    try {//w  w  w . j  a v a  2  s .c om
        SAXReader sreader = new SAXReader();

        Document contactDoc = sreader.read(new StringReader(contactXml));
        Element contactRootElement = contactDoc.getRootElement();

        Document vcardDoc = sreader.read(new StringReader(vcardXml));
        Element vcardRootElement = vcardDoc.getRootElement();

        for (Element el : (List<Element>) contactRootElement.elements()) {
            Element vElement;
            if ((vElement = vcardRootElement.element(el.getName())) == null) {
                logger.debug(" In refillMissingContactInfo Element = [" + el.getName() + "] not found!");
                vcardRootElement.add(el.createCopy());
            } else {
                String newStr = refillMissingContactInfo(vElement.asXML(), el.asXML());
                if ((newStr.compareTo(vElement.asXML()) != 0)) {
                    Document vcardSubDoc = sreader.read(new StringReader(newStr));
                    Element vcardSubRootElement = vcardSubDoc.getRootElement();
                    vcardRootElement.remove(vElement);
                    vcardRootElement.add(vcardSubRootElement.createCopy());
                }
            }
        }
        logger.debug("vcard XML string after refill is " + vcardRootElement.asXML());
        return vcardRootElement.asXML();
    } catch (Exception ex) {
        logger.error(ex.getMessage());
        return null;
    }

}

From source file:org.sipfoundry.openfire.vcard.provider.SipXVCardProvider.java

License:Contributor Agreement License

protected static Element getAvatarCopy(Element vcard) {
    Element avatarElement = null;
    if (vcard != null) {
        Element photoElement = vcard.element(AVATAR_ELEMENT);
        if (photoElement != null) {
            avatarElement = photoElement.createCopy();
        }//from ww  w  .  j  av a 2 s.c o  m
    }

    return avatarElement;
}

From source file:org.talend.mdm.webapp.browserecords.server.service.UploadService.java

License:Open Source License

protected void fillFieldValue(Element currentElement, String fieldPath, String fieldValue, Row row,
        String[] record) throws Exception {
    String parentPath = null;//from   w w w .ja v  a 2 s .com
    boolean isAttribute = false;
    List<String> valueList = null;
    List<Element> valueNodeList = new ArrayList<Element>();
    if (fieldPath.endsWith(Constants.XSI_TYPE_QUALIFIED_NAME)) {
        isAttribute = true;
        String field[] = fieldPath.split(Constants.FILE_EXPORT_IMPORT_SEPARATOR);
        fieldPath = field[0];
    }
    fieldValue = transferFieldValue(fieldPath, fieldValue, multipleValueSeparator);
    String[] xpathPartArray = fieldPath.split("/"); //$NON-NLS-1$
    String xpath = xpathPartArray[0];
    if (!isAttribute) {
        if (multipleValueSeparator != null && !multipleValueSeparator.isEmpty()) {
            valueList = splitString(fieldValue, "\\" + String.valueOf(multipleValueSeparator.charAt(0))); //$NON-NLS-1$
        }
    }
    for (int i = 1; i < xpathPartArray.length; i++) {
        if (currentElement != null) {
            parentPath = xpath;
            xpath = xpath + "/" + xpathPartArray[i]; //$NON-NLS-1$
            if (entityModel.getTypeModel(xpath).isMultiOccurrence() && multiNodeMap.get(xpath) == null) {
                List<Element> multiNodeList = new ArrayList<Element>();
                if (valueList != null) {
                    if (isPartialUpdate) {
                        if (currentElement.element(xpathPartArray[i]) == null) {
                            currentElement.addElement(xpathPartArray[i]);
                        }
                        List<Element> elementList = currentElement.elements(xpathPartArray[i]);
                        for (Element e : elementList) {
                            multiNodeList.add(e);
                        }
                        if (elementList.size() < valueList.size()) {
                            for (int j = 1; j <= valueList.size() - elementList.size(); j++) {
                                currentElement.addElement(xpathPartArray[i]);
                                multiNodeList.add((Element) currentElement.content()
                                        .get(currentElement.content().size() - 1));
                            }
                        }
                    } else {
                        for (int j = 0; j < valueList.size(); j++) {
                            Element element = currentElement.element(xpathPartArray[i]);
                            int index = currentElement.content().indexOf(element);
                            if (index + j >= currentElement.content().size() || currentElement.content()
                                    .get(currentElement.content().indexOf(element) + j) != element) {
                                Element createCopy = element.createCopy();
                                currentElement.content().add(createCopy);
                                multiNodeList.add(createCopy);
                            } else {
                                multiNodeList.add(element);
                            }
                        }
                    }
                }
                multiNodeMap.put(xpath, multiNodeList);
                if (multiNodeList.size() > 0) {
                    currentElement = multiNodeList.get(multiNodeList.size() - 1);
                }
            } else if (multiNodeMap.get(parentPath) != null) {
                List<Element> parentlist = multiNodeMap.get(parentPath);
                for (int j = 0; j < parentlist.size(); j++) {
                    Element parentElement = parentlist.get(j);
                    Element element = parentElement.element(xpathPartArray[i]);
                    if (element == null) {
                        element = parentElement.addElement(xpathPartArray[i]);
                    }
                    valueNodeList.add(element);
                }
                if (valueNodeList.size() > 0) {
                    currentElement = valueNodeList.get(valueNodeList.size() - 1);
                }
            } else {
                if (currentElement.element(xpathPartArray[i]) != null) {
                    currentElement = currentElement.element(xpathPartArray[i]);
                } else {
                    currentElement = currentElement.addElement(xpathPartArray[i]);
                }
            }
            if (i == xpathPartArray.length - 1) {
                if (isAttribute) {
                    setAttributeValue(currentElement, fieldValue);
                } else {
                    if (valueNodeList.size() > 0) {
                        for (int j = 0; j < valueList.size(); j++) {
                            setFieldValue(valueNodeList.get(j), valueList.get(j));
                        }
                    } else if (multiNodeMap.get(xpath) != null) {
                        List<Element> multiNodeList = multiNodeMap.get(xpath);
                        for (int j = 0; j < valueList.size(); j++) {
                            setFieldValue(multiNodeList.get(j), valueList.get(j));
                        }
                    } else {
                        setFieldValue(currentElement, fieldValue);
                    }
                }
            } else {
                String currentElemntPath = currentElement.getPath().substring(1);
                if (inheritanceNodePathList != null && inheritanceNodePathList.contains(currentElemntPath)) {
                    Integer xsiTypeIndex = xsiTypeMap
                            .get(currentElemntPath + "/@" + Constants.XSI_TYPE_QUALIFIED_NAME); //$NON-NLS-1$
                    if (xsiTypeIndex != null) {
                        String xsiTypeValue = ""; //$NON-NLS-1$
                        if (FILE_TYPE_EXCEL_SUFFIX.equals(fileType.toLowerCase())
                                || FILE_TYPE_EXCEL2010_SUFFIX.equals(fileType.toLowerCase())) {
                            xsiTypeValue = row.getCell(xsiTypeIndex).getRichStringCellValue().getString();
                        } else if (FILE_TYPE_CSV_SUFFIX.equals(fileType.toLowerCase())) {
                            xsiTypeValue = record[i];
                        }
                        setAttributeValue(currentElement, xsiTypeValue);
                    } else {
                        throw new UploadException(
                                MESSAGES.getMessage(new Locale(currentElemntPath), "missing_attribute", //$NON-NLS-1$
                                        currentElemntPath + "/@" + Constants.XSI_TYPE_QUALIFIED_NAME)); //$NON-NLS-1$
                    }
                }
            }
        }
    }
}

From source file:org.unitime.timetable.gwt.server.MenuServlet.java

License:Open Source License

private void merge(Element menu, Element custom) {
    if ("remove".equals(custom.getName())) {
        menu.getParent().remove(menu);/* ww w .ja  v  a 2 s.  c  o  m*/
        return;
    }
    for (Iterator<Attribute> i = custom.attributeIterator(); i.hasNext();) {
        Attribute a = i.next();
        menu.addAttribute(a.getName(), a.getValue());
    }
    for (Iterator<Element> i = custom.elementIterator(); i.hasNext();) {
        Element e = i.next();
        if ("parameter".equals(e.getName())) {
            for (Iterator<Element> j = menu.elementIterator("parameter"); j.hasNext();) {
                menu.remove(j.next());
            }
            menu.add(e.createCopy());
            continue;
        }
        if ("condition".equals(e.getName())) {
            menu.add(e.createCopy());
            continue;
        }
        if ("new-condition".equals(e.getName())) {
            for (Iterator<Element> j = menu.elementIterator("condition"); j.hasNext();) {
                menu.remove(j.next());
            }
            Element f = e.createCopy();
            f.setName("condition");
            menu.add(f);
            continue;
        }
        String name = e.attributeValue("name");
        Element x = null;
        if (name != null) {
            for (Iterator<Element> j = menu.elementIterator(); j.hasNext();) {
                Element f = j.next();
                if (name.equals(f.attributeValue("name"))) {
                    x = f;
                    break;
                }
            }
        }
        if (x != null) {
            merge(x, e);
        } else {
            int pos = Integer.parseInt(e.attributeValue("position", "-1"));
            if (pos >= 0) {
                List<Element> after = new ArrayList<Element>();
                for (Iterator<Element> j = menu.elementIterator(); j.hasNext();) {
                    Element f = j.next();
                    if ("condition".equals(f.getName()))
                        continue;
                    if (pos > 0) {
                        pos--;
                    } else {
                        after.add(f);
                        menu.remove(f);
                    }
                }
                menu.add(e.createCopy());
                for (Element f : after)
                    menu.add(f);
            } else
                menu.add(e.createCopy());
        }
    }
}

From source file:ubic.pubmedgate.interactions.DuplicateXMLParses.java

License:Apache License

private void fixXML(Document document, String path, Map<String, String> attributes) {
    List list = document.selectNodes(path);
    Iterator iter = list.iterator();
    while (iter.hasNext()) {
        String attribute = attributes.keySet().iterator().next();
        Element el = (Element) iter.next();

        if (el.attribute("parser") != null
                && el.attribute("parser").getValue().equals("Charniak-Johnson-McClosky")) {
            el.addAttribute("parser", "Charniak-Lease");
        }/*from  w  w w.  j  a  v a2  s  . c  om*/

        String tokenizer = el.attribute(attribute).getValue();
        String replacement = attributes.get(attribute);
        if (!tokenizer.equals(replacement)) {
            log.info(tokenizer + " -> " + replacement);
            Element newElement = el.createCopy();
            for (String attributeFix : attributes.keySet()) {
                newElement.addAttribute(new QName(attributeFix), attributes.get(attributeFix));
            }
            el.getParent().add(newElement);
        }

    }
}