Example usage for org.dom4j Element clearContent

List of usage examples for org.dom4j Element clearContent

Introduction

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

Prototype

void clearContent();

Source Link

Document

Clears the content for this branch, removing any Node instances this branch may contain.

Usage

From source file:com.ah.be.parameter.BeParaModuleDefImpl.java

public void insertDefaultCustomReportField() {
    try {//  w w w  . j  ava  2 s .co  m
        long rowCount = QueryUtil.findRowCount(AhCustomReportField.class, null);

        if (rowCount > 0) {
            return;
        }

        List<AhCustomReportField> reportFields = new ArrayList<>();

        SAXReader reader = new SAXReader();
        String docName = AhDirTools.getHmRoot() + "resources" + File.separator + "customReport" + File.separator
                + "custom_report_table.xml";
        Document doc = reader.read(new File(docName));
        Element root = doc.getRootElement();
        List<?> rootLst = root.elements();

        for (Object obj : rootLst) {
            List<?> rowlst = ((Element) obj).elements();
            AhCustomReportField reportField = new AhCustomReportField();
            for (int j = 0; j < rowlst.size(); j++) {
                Element elm = (Element) rowlst.get(j);
                //               String name = elm.attributeValue("name");
                String value = elm.attributeValue("value");
                if (j == 0) {
                    reportField.setId(Long.parseLong(value));
                } else if (j == 1) {
                    reportField.setType(Integer.parseInt(value));
                } else if (j == 2) {
                    reportField.setDetailType(Integer.parseInt(value));
                } else if (j == 3) {
                    reportField.setTableName(value);
                } else if (j == 4) {
                    reportField.setTableField(value);
                } else if (j == 5) {
                    reportField.setFieldString(value);
                } else if (j == 6) {
                    reportField.setStrUnit(value);
                } else if (j == 7) {
                    reportField.setDescription(value);
                }
            }
            reportFields.add(reportField);
        }

        root.clearContent();
        doc.clearContent();

        QueryUtil.bulkCreateBos(reportFields);
    } catch (Exception e) {
        setDebugMessage("insert default custom report field: ", e);
    }
}

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  .ja va  2 s . 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.chingo247.structureapi.plan.document.DocumentPluginElement.java

public void setList(String listName, List<IStructurePlanElement> elements) {
    // Get the list Element
    Element e = (Element) pluginElement.selectSingleNode(listName);

    // If we found the element and the new value was null or an empty list.
    // Delete the element
    if (e != null && (elements == null || elements.isEmpty())) {
        e.detach();/*from   www  . jav  a  2  s  .  c om*/
        return;
    }

    if (e == null) {
        pluginElement.add(new BaseElement(listName));
        e = (Element) pluginElement.selectSingleNode(listName);
    }

    e.clearContent();

    //
    List<Element> toElements = new LinkedList<>();
    for (IStructurePlanElement se : elements) {
        toElements.add(se.asElement());
    }
    e.setContent(toElements);
}

From source file:com.chingo247.structureapi.plan.document.PlanDocument.java

License:Open Source License

@Override
protected void save(PlanDocumentPluginElement element) {
    Element e = (Element) document.getRootElement().selectSingleNode(element.pluginName);
    e.clearContent();
    e.setContent(element.pluginElement.elements());
    documentManager.save(element);//from w  w  w . j  av  a  2  s.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;/*from w w  w. j  av  a 2 s .co 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

/**
 * For GBS-3085./* w w w  . j av a  2  s .co 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.everest.edit.offline.page.TmxUtil.java

License:Apache License

/**
 * Remove the embed tags to consist with the TM. EX: The input might
 * contains: <bpt type="bold" x="1">&lt;b&gt;</bpt>, Just remove
 * "&lt;b&gt;"./*from  w w w  .  j a  v  a  2  s.  com*/
 * 
 * @param root
 *            the root element.
 */
private static void removeEmbedTags(Element root) {
    List elems = root.elements();
    for (int i = 0; i < elems.size(); i++) {
        Object obj = elems.get(i);
        if (obj instanceof org.dom4j.Element) {
            org.dom4j.Element element = (org.dom4j.Element) obj;
            element.clearContent();
        }
    }
}

From source file:com.globalsight.everest.edit.offline.page.TmxUtil.java

License:Apache License

/**
 * Removes the given <sub> element from the segment. <sub> is special since
 * it does not only surround embedded tags but also text, which must be
 * pulled out of the <sub> and added to the parent tag.
 *//*from  www . j a  va2  s.co  m*/
public static void replaceNbsp(Element p_element) {
    Element parent = p_element.getParent();
    int index = parent.indexOf(p_element);

    // We copy the current content, clear out the parent, and then
    // re-add the old content, inserting the <sub>'s textual
    // content instead of the <sub> (this clears any embedded TMX
    // tags in the subflow).

    ArrayList newContent = new ArrayList();
    List content = parent.content();

    for (int i = content.size() - 1; i >= 0; --i) {
        Node node = (Node) content.get(i);

        newContent.add(node.detach());
    }

    Collections.reverse(newContent);
    parent.clearContent();

    for (int i = 0, max = newContent.size(); i < max; ++i) {
        Node node = (Node) newContent.get(i);

        if (i == index) {
            parent.addText("\u00A0");
        } else {
            parent.add(node);
        }
    }
}

From source file:com.globalsight.everest.edit.offline.page.TmxUtil.java

License:Apache License

/**
 * Removes the given <sub> element from the segment. <sub> is special since
 * it does not only surround embedded tags but also text, which must be
 * pulled out of the <sub> and added to the parent tag.
 *///from  w w  w  . j ava2s .c om
public static void removeSubElement(Element p_element) {
    Element parent = p_element.getParent();
    int index = parent.indexOf(p_element);

    // We copy the current content, clear out the parent, and then
    // re-add the old content, inserting the <sub>'s textual
    // content instead of the <sub> (this clears any embedded TMX
    // tags in the subflow).

    ArrayList newContent = new ArrayList();
    List content = parent.content();

    for (int i = content.size() - 1; i >= 0; --i) {
        Node node = (Node) content.get(i);

        newContent.add(node.detach());
    }

    Collections.reverse(newContent);
    parent.clearContent();

    for (int i = 0, max = newContent.size(); i < max; ++i) {
        Node node = (Node) newContent.get(i);

        if (i == index) {
            parent.addText(p_element.getText());
        } else {
            parent.add(node);
        }
    }
}

From source file:com.globalsight.everest.edit.offline.page.TmxUtil.java

License:Apache License

/**
 * Removes the given TMX 1.4 <hi> element from the segment. <hi> is special
 * since it does not surround embedded tags but text, which must be pulled
 * out of the <hi> and added to the parent segment.
 *///from w w  w .  j ava  2s .  com
private static void removeHiElement(Element p_element) {
    Element parent = p_element.getParent();
    int index = parent.indexOf(p_element);

    // We copy the current content, clear out the parent, and then
    // re-add the old content, inserting the <hi>'s content
    // instead of the <hi>.

    ArrayList newContent = new ArrayList();
    List content = parent.content();

    for (int i = content.size() - 1; i >= 0; --i) {
        Node node = (Node) content.get(i);

        newContent.add(node.detach());
    }

    Collections.reverse(newContent);
    parent.clearContent();

    for (int i = 0, max = newContent.size(); i < max; ++i) {
        Node node = (Node) newContent.get(i);

        if (i == index) {
            parent.appendContent(p_element);
        } else {
            parent.add(node);
        }
    }
}