Example usage for org.dom4j Element content

List of usage examples for org.dom4j Element content

Introduction

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

Prototype

List<Node> content();

Source Link

Document

Returns the content nodes of this branch as a backed List so that the content of this branch may be modified directly using the List interface.

Usage

From source file:com.amalto.workbench.utils.LocalTreeObjectRepository.java

License:Open Source License

private void transferElementsWithCategoryPath(String categoryXpath, Element srcElemRoot,
        Element targtElemRoot) {//w  w  w .  j  a va2  s  .  com
    categoryXpath += "[text()='" + TreeObject.CATEGORY_FOLDER + "']";//$NON-NLS-1$//$NON-NLS-2$
    // clear up the context of targtElemRoot firstly
    Element elemCategoryInTagt = pingElement(categoryXpath, targtElemRoot);
    List elems = elemCategoryInTagt.content();
    List xobjects = new ArrayList();
    for (Object obj : elems) {
        if (obj instanceof Element) {
            Element elem = (Element) obj;
            if (!elem.getTextTrim().equals(TreeObject.CATEGORY_FOLDER + "")) {//$NON-NLS-1$
                Element division = drillUpForDevisionElement(elem);
                division.addElement(elem.getName()).setText(elem.getTextTrim());
                xobjects.add(elem);
            }
        }
    }

    elemCategoryInTagt.content().removeAll(xobjects);

    Element elemCategoryInSrc = pingElement(categoryXpath, srcElemRoot);
    elems = elemCategoryInSrc.content();
    for (Object obj : elems) {
        if (obj instanceof Element) {
            Element elem = (Element) obj;
            if (!elem.getTextTrim().equals(TreeObject.CATEGORY_FOLDER + "")) {//$NON-NLS-1$
                String xpath = ".//descendant::" + elem.getName() + "[text()='" + elem.getTextTrim() + "']";//$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$
                List<Element> es = targtElemRoot.selectNodes(xpath);
                Element newElem = null;
                for (Element elemExist : es) {
                    if (elemExist.getParent() != null
                            && elemExist.getParent().getTextTrim().equals(TreeObject.EVENT_MANAGEMENT + "")) {
                        continue;
                    }
                    if (elemExist != null) {
                        Element parentExist = elemExist.getParent();
                        parentExist.remove(elemExist);
                    }
                    elemExist = pingElement(categoryXpath, targtElemRoot);
                    newElem = elemExist.addElement(elem.getName());
                    newElem.setText(elem.getTextTrim());
                }
                if (es.size() == 0 || (es.size() > 0 && newElem == null)) {
                    Element elemExist = pingElement(categoryXpath, targtElemRoot);
                    newElem = elemExist.addElement(elem.getName());
                    newElem.setText(elem.getTextTrim());
                }
            }
        }

    }
}

From source file:com.amalto.workbench.utils.LocalTreeObjectRepository.java

License:Open Source License

public void parseElementForOutput(TreeObject[] xobjs) {
    for (TreeObject xobj : xobjs) {
        TreeObject subParent = xobj;//from  w  w  w .  jav a2s  .c o  m
        while (subParent.getParent().getType() != 0) {
            subParent = subParent.getParent();
        }
        Element modelElem = getParentElement(subParent);

        if (!outPutSchemas.containsKey(modelElem.getName())) {
            Element copyElem = (Element) modelElem.clone();
            copyElem.clearContent();
            copyElem.setText(modelElem.getTextTrim());
            outPutSchemas.put(modelElem.getName(), copyElem);
        }

        subParent = xobj;
        TreeObject categorySubRoot = null;
        while (subParent.getParent().getType() == TreeObject.CATEGORY_FOLDER) {
            categorySubRoot = subParent.getParent();
            subParent = subParent.getParent();
        }

        Element divisionElem = null;
        Element copyModelElem = outPutSchemas.get(modelElem.getName());
        Document doc = credentials.get(UnifyUrl(xobj.getServerRoot().getWsKey().toString())).doc;
        String division = xobj.getType() == TreeObject.TRANSFORMER ? "Process" : "Trigger";//$NON-NLS-1$//$NON-NLS-2$
        String xpathForDivision = ".//child::" + division + "[text()='" + xobj.getType() + "']";//$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$

        if (categorySubRoot != null) {
            Element categoryElem = getParentElement(categorySubRoot);
            if (categoryElem.getParent() != modelElem) {
                divisionElem = pingElement(xpathForDivision, copyModelElem);
                if (divisionElem == null) {
                    divisionElem = copyModelElem.addElement(categoryElem.getParent().getName());
                    divisionElem.setText(categoryElem.getParent().getTextTrim());
                }
            } else {
                divisionElem = copyModelElem;
            }

            Element categoryElementClone = (Element) categoryElem.clone();
            String xpath = "./child::" + categoryElem.getName() + "[text()='" + TreeObject.CATEGORY_FOLDER //$NON-NLS-1$//$NON-NLS-2$
                    + "']";//$NON-NLS-1$
            if (divisionElem.selectNodes(xpath).size() == 0) {
                divisionElem.add(categoryElementClone);
            }
        } else {
            // individual xobject
            Element xobjElem = pingElement(getXPathForTreeObject(xobj), doc.getRootElement());
            Element parentElem = xobjElem.getParent();
            if (parentElem == modelElem) {
                parentElem = copyModelElem;
            } else {
                divisionElem = pingElement(xpathForDivision, copyModelElem);
                if (divisionElem == null) {
                    divisionElem = copyModelElem.addElement(parentElem.getName());
                    divisionElem.setText(parentElem.getTextTrim());
                }
            }

            String xpath = ".//child::" + xobjElem.getName() + "[text()='" + xobjElem.getTextTrim() + "']";//$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$
            if (divisionElem != null && pingElement(xpath, divisionElem) == null) {
                divisionElem.add((Element) xobjElem.clone());
            }
        }
    }

    // filter those excluded from xobjects out of categorys
    String xpath = ".//descendant::*[text() ='" + TreeObject.CATEGORY_FOLDER + "']";//$NON-NLS-1$//$NON-NLS-2$
    Iterator<Element> iter = outPutSchemas.values().iterator();
    while (iter.hasNext()) {
        Element divisionElement = iter.next();
        List<Element> categorys = divisionElement.selectNodes(xpath);

        if (categorys != null) {
            for (Element categoryElems : categorys) {
                List objs = categoryElems.content();
                List<Element> elemToDel = new ArrayList<Element>();
                for (Object obj : objs) {
                    if (obj instanceof Element) {
                        Element categoryElement = (Element) obj;
                        if (categoryElement.getTextTrim().equals(TreeObject.CATEGORY_FOLDER + "")) {
                            continue;
                        }
                        boolean match = false;
                        for (TreeObject xobj : xobjs) {
                            if (filterOutBlank(xobj.getDisplayName()).equals(categoryElement.getName())
                                    && categoryElement.getTextTrim().equals(xobj.getType() + "")) {//$NON-NLS-1$
                                match = true;
                                break;
                            }
                        }
                        if (!match) {
                            elemToDel.add(categoryElement);
                        }
                    }
                }

                for (Element del : elemToDel) {
                    categoryElems.remove(del);
                }
            }
        }
    }

    ArrayList<String> schemas = new ArrayList<String>();
    Iterator<Element> iterd = outPutSchemas.values().iterator();
    while (iterd.hasNext()) {
        schemas.add(iterd.next().asXML());
    }
}

From source file:com.cladonia.xngreditor.actions.ToolsSortNodeAction.java

License:Open Source License

private void insert(int index, Node e, Element parent) throws Exception {

    List list = parent.content();
    try {/*from ww w. j av a2 s. c  o  m*/
        int nodeCount = 0;
        int elementCount = 0;
        //          List list = elements();
        while (index > nodeCount) {
            Object node = list.get(nodeCount);
            /*
             * if ( node instanceof XElement) { elementCount++;
             */
            nodeCount++;
        }
        if (nodeCount < list.size()) {
            list.add(nodeCount, e);
        } else {
            list.add(e);
        }
    } catch (Exception e1) {
        //if exception is thrown, just add at the end of the document
        //need to find out if the it should add at the very end or are
        // there elements before that
        list.add(e);
    }
}

From source file:com.doculibre.constellio.services.ElevateServicesImpl.java

License:Open Source License

@SuppressWarnings("unchecked")
private synchronized void replaceQueryElement(String queryText, Element newQueryElement,
        String collectionName) {/*from w w w  .  j  a v a 2  s . co m*/
    Document document = SolrServicesImpl.readXMLConfigInCloud(collectionName, "elevate.xml");
    Element rootElement = document.getRootElement();
    int beanNodeIndex = -1;
    int i = 0;
    for (Node rootContent : (List<Node>) rootElement.content()) {
        if (rootContent instanceof Element) {
            Element possibleBeanElement = (Element) rootContent;
            if (queryText.equals(possibleBeanElement.attributeValue("text"))) {
                beanNodeIndex = i;
                break;
            }
        }
        i++;
    }

    if (beanNodeIndex != -1) {
        if (newQueryElement != null) {
            rootElement.content().set(beanNodeIndex, newQueryElement);
        } else {
            rootElement.content().remove(beanNodeIndex);
        }
    } else if (newQueryElement != null) {
        rootElement.add(newQueryElement);
    }

    // lets write to a file
    SolrServicesImpl.writeXMLConfigInCloud(collectionName, "elevate.xml", document);
}

From source file:com.flaptor.util.parser.HtmlParser.java

License:Apache License

private void removeNamespace(Element elem) {
    if (null != elem) {
        elem.remove(elem.getNamespace());
        elem.setQName(QName.get(elem.getName(), Namespace.NO_NAMESPACE));
        removeNamespace(elem.content());
    }// w w w  . j av a  2s . com
}

From source file:com.globalsight.cxe.adapter.msoffice.ExcelRepairer.java

License:Apache License

private void repairExcelSharedStrings() throws Exception {
    File f = new File(path + "/xl/sharedStrings.xml");
    if (!f.exists())
        return;/* w w w  .  j  a v  a 2 s. c  o m*/

    String content = FileUtil.readFile(f, "utf-8");

    XmlParser parser = new XmlParser();
    org.dom4j.Document document = parser.parseXml(content);
    Element element = document.getRootElement();
    List<Element> rs = getElementByName(element, "r");
    for (Element r : rs) {
        @SuppressWarnings("rawtypes")
        List els = r.content();

        StringBuffer sb = new StringBuffer();
        Element wt = null;
        List<DefaultText> texts = new ArrayList<DefaultText>();

        for (Object el : els) {
            if (el instanceof DefaultText) {
                DefaultText text = (DefaultText) el;
                String s = text.getStringValue();
                if ("\n".equals(s))
                    continue;

                texts.add(text);
                sb.append(text.getStringValue());
            } else if (el instanceof Element) {
                Element elm = (Element) el;
                if ("t".equals(elm.getName())) {
                    wt = elm;
                    sb.append(elm.getStringValue());
                }
            }
        }

        if (wt == null) {
            wt = r.addElement("t");
            wt.addAttribute("xml:space", "preserve");
        }

        if (sb.length() == 0)
            sb.append(" ");

        wt.clearContent();
        wt.addText(sb.toString());

        for (DefaultText text : texts) {
            r.remove(text);
        }
    }

    Writer fileWriter = new OutputStreamWriter(new FileOutputStream(f), "UTF-8");
    XMLWriter xmlWriter = new XMLWriter(fileWriter);
    xmlWriter.write(document);
    xmlWriter.close();
}

From source file:com.globalsight.cxe.adapter.msoffice.WordRepairer.java

License:Apache License

@SuppressWarnings("unchecked")
private static void forHyperlinkInWr(Element element) {
    List<Node> ts = element.selectNodes("//w:r/w:hyperlink/w:r");

    for (Node t : ts) {
        Element hyperlink = t.getParent();

        if (hyperlink == null)
            continue;

        Element wr = hyperlink.getParent();
        if (wr == null)
            continue;

        Element wrParent = wr.getParent();
        if (wrParent == null)
            continue;

        boolean beforeWt = false;

        List<?> els = wr.content();

        for (Object el : els) {
            if (el instanceof Element) {
                Element elm = (Element) el;
                if ("t".equals(elm.getName())) {
                    beforeWt = false;/*from  w  ww .  java  2 s.c o m*/
                    break;
                } else if (hyperlink.equals(elm)) {
                    beforeWt = true;
                    break;
                }
            }
        }

        wr.remove(hyperlink);

        @SuppressWarnings("rawtypes")
        List es = wrParent.elements();
        int index = es.indexOf(wr);
        index = beforeWt ? index : index + 1;

        hyperlink.setParent(wrParent);
        es.add(index, hyperlink);
    }
}

From source file:com.globalsight.cxe.adapter.msoffice.WordRepairer.java

License:Apache License

/**
 * For GBS-3085.//www.j  a v a2 s .c  o  m
 */
@SuppressWarnings("unchecked")
private static void forHyperlinkInWt(Element element) {
    List<Node> ts = element.selectNodes("//w:r/w:t/w:hyperlink/w:r");

    boolean find = false;

    for (Node t : ts) {
        find = true;
        Element hyperlink = t.getParent();

        if (hyperlink == null)
            continue;

        Element wt = hyperlink.getParent();
        if (wt == null)
            continue;

        Element wr = wt.getParent();
        if (wr == null)
            continue;

        Element wrParent = wr.getParent();
        if (wrParent == null)
            continue;

        List wtNodes = wt.content();
        List removedNodes = new ArrayList();

        int index = wtNodes.indexOf(hyperlink);
        for (int i = wtNodes.size() - 1; i >= index; i--) {
            removedNodes.add(0, wtNodes.remove(i));
        }

        Element cloneWr = (Element) wr.clone();
        List es = wrParent.content();
        index = es.indexOf(wr);

        wrParent.remove(cloneWr);
        es.add(index, cloneWr);
        es.add(index + 1, hyperlink);

        wt.clearContent();
        List wts = wt.content();
        for (int i = 1; i < removedNodes.size(); i++) {
            wts.add(removedNodes.get(i));
        }
    }

    if (find) {
        forHyperlinkInWt(element);
    }
}

From source file:com.globalsight.cxe.adapter.msoffice.WordRepairer.java

License:Apache License

private static void forWrInWr(Element element) {
    @SuppressWarnings("unchecked")
    List<Node> ts = element.selectNodes("//w:r/w:r");

    for (Node t : ts) {
        Element wr = t.getParent();

        if (wr == null)
            continue;

        List<?> els = wr.content();

        StringBuffer sb = new StringBuffer();
        Element wt = null;/*from   w w  w. j a va 2s.co m*/
        List<Element> wrs = new ArrayList<Element>();

        for (Object el : els) {
            if (el instanceof Element) {
                Element elm = (Element) el;
                if ("t".equals(elm.getName())) {
                    wt = elm;
                    sb.append(elm.getStringValue());
                } else if ("r".equals(elm.getName())) {
                    sb.append(elm.getStringValue());
                    wrs.add(elm);
                }
            }
        }

        if (wt == null) {
            wt = wr.addElement("w:t");
            wt.addAttribute("xml:space", "preserve");
        }

        wt.setText(sb.toString());

        for (Element w : wrs) {
            wr.remove(w);
        }
    }
}

From source file:com.globalsight.cxe.adapter.msoffice.WordRepairer.java

License:Apache License

private static void forTextInWr(Element element) {
    @SuppressWarnings("unchecked")
    List<Node> ts = element.selectNodes("//w:r/text()");

    for (Node t : ts) {
        if (t.getText().matches("[\n\r]*")) {
            continue;
        }//www  .  ja va2 s  . c  o m

        Element wr = t.getParent();

        if (wr == null) {
            continue;
        }

        List<?> els = wr.content();

        StringBuffer sb = new StringBuffer();
        Element wt = null;
        List<DefaultText> texts = new ArrayList<DefaultText>();

        for (Object el : els) {
            if (el instanceof DefaultText) {
                DefaultText text = (DefaultText) el;
                texts.add(text);
                sb.append(text.getStringValue());
            } else if (el instanceof Element) {
                Element elm = (Element) el;
                if ("t".equals(elm.getName())) {
                    wt = elm;
                    sb.append(elm.getStringValue());
                }
            }
        }

        if (wt == null) {
            wt = wr.addElement("w:t");
            wt.addAttribute("xml:space", "preserve");
        }

        wt.setText(sb.toString());

        for (DefaultText text : texts) {
            wr.remove(text);
        }
    }
}