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.devoteam.srit.xmlloader.core.utils.XMLTree.java

License:Open Source License

/**
 * Restore this XMLTree to his original state
 *///from   w  w  w  .j av a  2s.c  om
public void restore() {
    for (int i = elementsOrder.size() - 1; i >= 0; i--) {
        Element e = elementsOrder.get(i);
        List<Element> list = elementsMap.get(e);
        if (null != list && !list.isEmpty()) {
            // all Elements in this list should have the same parent
            Element parent = list.get(0).getParent();
            if (null != parent) {
                DefaultElementInterface.insertNode((DefaultElement) parent, list.get(0), e);
                for (Node oldChild : list) {
                    parent.remove(oldChild);
                }

                if (1 == list.size() && e == root) {
                    root = list.get(0);
                }

            } else {
                root = list.get(0);
            }
        }
    }
}

From source file:com.devoteam.srit.xmlloader.master.master.utils.DataMaster.java

License:Open Source License

private void parse() throws Exception {
    this.testDatas = new LinkedList<DataTest>();

    List<Element> parametersMaster = (List<Element>) root.elements("parameter");

    /* Duplicate the parameter elements from the <master> level and copy
     * them at the begining of the <test> element, before the <test>
     * element's own parameter elements.
     *//*  w  ww  .j  av a2  s. co  m*/
    List<Element> tests = (List<Element>) root.elements("test");
    for (Element test : tests) {
        List<Element> parametersTest = (List<Element>) test.elements("parameter");
        for (Element parameter : parametersTest) {
            test.remove(parameter);
        }

        for (Element parameter : parametersMaster) {
            test.add(parameter.createCopy());
        }

        for (Element parameter : parametersTest) {
            test.add(parameter);
        }

        testDatas.add(new DataTest(test, this.xmlDocument.getXMLFile()));
    }

    //
    // Test for TestData names uniqueness
    HashSet hashSet = new HashSet();
    for (DataTest testData : testDatas) {
        if (hashSet.contains(testData.getName())) {
            throw new Exception("Test name should be unique (" + testData.getName() + ").");
        } else {
            hashSet.add(testData.getName());
        }
    }
}

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

License:Open Source License

private void updateConfig(RecordCollection collection) {

    SolrConfig config = collection.getSolrConfiguration();
    SolrConfigServices solrConfigServices = ConstellioSpringUtils.getSolrConfigServices();
    SolrConfig defaultConfig = solrConfigServices.getDefaultConfig();

    if (config == null) {
        config = defaultConfig;/*from  ww  w . ja  va 2s . c o m*/
    }
    ensureCore(collection);
    String collectionName = collection.getName();

    Document solrConfigDocument = readSolrConfig(collection);
    Element rootElement = solrConfigDocument.getRootElement();
    Element queryElement = rootElement.element("query");
    if (queryElement == null) {
        queryElement = rootElement.addElement("query");
    }
    updateNotNullConfigCache(queryElement, "fieldValueCache",
            (Cache) getPropertyValue(config, defaultConfig, "fieldValueCacheConfig"));
    updateNotNullConfigCache(queryElement, "filterCache",
            (Cache) getPropertyValue(config, defaultConfig, "filterCacheConfig"));
    updateNotNullConfigCache(queryElement, "queryResultCache",
            (Cache) getPropertyValue(config, defaultConfig, "queryResultCacheConfig"));
    updateNotNullConfigCache(queryElement, "documentCache",
            (Cache) getPropertyValue(config, defaultConfig, "documentCacheConfig"));

    addNotNullElement(queryElement, "useFilterForSortedQuery",
            getPropertyValue(config, defaultConfig, "useFilterForSortedQuery"));
    addNotNullElement(queryElement, "queryResultWindowSize",
            getPropertyValue(config, defaultConfig, "queryResultWindowSize"));
    Element hashDocSet = queryElement.element("HashDocSet");
    if (hashDocSet != null) {
        queryElement.remove(hashDocSet);
    }
    Object hashDocSetMaxSize = getPropertyValue(config, defaultConfig, "hashDocSetMaxSize");
    Object hashDocSetLoadFactor = getPropertyValue(config, defaultConfig, "hashDocSetLoadFactor");
    if (hashDocSetMaxSize != null || hashDocSetLoadFactor != null) {
        hashDocSet = queryElement.addElement("HashDocSet");
        addNotNullAttribute(hashDocSet, "maxSize", hashDocSetMaxSize);
        addNotNullAttribute(hashDocSet, "loadFactor", hashDocSetLoadFactor);
    }
    writeSolrConfig(collection, solrConfigDocument);
    initCore(collection);

}

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 a2 s . c o  m*/
}

From source file:com.glaf.core.xml.XmlBuilder.java

License:Apache License

public void processElement(String systemName, Element root, Map<String, Object> params) {
    /**//from w w w  . ja v  a 2s.  c o  m
     * ??
     */
    Map<String, DatasetModel> dataSetMap = new HashMap<String, DatasetModel>();
    List<?> elements = root.elements("DataSet");
    Iterator<?> iterator = elements.iterator();
    while (iterator.hasNext()) {
        Element element = (Element) iterator.next();
        String id = element.attributeValue("id");
        String sql = element.attributeValue("sql");
        String queryId = element.attributeValue("queryId");
        String title = element.attributeValue("title");
        String single = element.attributeValue("single");
        String splits = element.attributeValue("splits");
        String foreachPerRow = element.attributeValue("foreachPerRow");
        if (sql == null) {
            sql = element.elementText("sql");
        }

        DatasetModel dsm = new DatasetModel();
        dsm.setId(id);
        dsm.setSql(sql);
        dsm.setQueryId(queryId);
        dsm.setTitle(title);
        if (StringUtils.equalsIgnoreCase(single, "true")) {
            dsm.setSingle(true);
        }

        if (StringUtils.equalsIgnoreCase(foreachPerRow, "true")) {
            dsm.setForeachPerRow(true);
        }

        if (StringUtils.isNotEmpty(splits)) {
            dsm.setSplitList(StringTools.split(splits));
        }

        List<?> attrs = element.attributes();
        if (attrs != null && !attrs.isEmpty()) {
            Iterator<?> iter = attrs.iterator();
            while (iter.hasNext()) {
                Attribute attr = (Attribute) iter.next();
                dsm.addAttribute(attr.getName(), attr.getStringValue());
            }
        }

        List<?> providers = element.elements("FieldConverter");
        if (providers != null && !providers.isEmpty()) {
            Iterator<?> iter = providers.iterator();
            while (iter.hasNext()) {
                Element elem = (Element) iter.next();
                String fromName = elem.attributeValue("fromName");
                String toName = elem.attributeValue("toName");
                String provider = elem.attributeValue("provider");
                FieldController c = new FieldController();
                c.setFromName(fromName);
                c.setToName(toName);
                c.setProvider(provider);
                dsm.addController(c);
            }
        }
        dataSetMap.put(dsm.getId(), dsm);
    }

    elements = root.elements();
    iterator = elements.iterator();
    while (iterator.hasNext()) {
        Element element = (Element) iterator.next();
        if (StringUtils.equals(element.getName(), "DataSet")) {
            continue;
        }
        this.processNode(element, dataSetMap, systemName, params);
    }

    elements = root.elements("DataSet");
    iterator = elements.iterator();
    while (iterator.hasNext()) {
        Element element = (Element) iterator.next();
        root.remove(element);
    }
}

From source file:com.glaf.core.xml.XmlBuilder.java

License:Apache License

@SuppressWarnings({ "unchecked", "rawtypes" })
protected void processForEachNode(Element element, Map<String, DatasetModel> dataSetMap,
        Map<String, Object> myDataMap, String systemName, Map<String, Object> params) {
    //LOG.debug("---------------------processForEachNode-----------------------");
    Element parent = element.getParent();
    String dsId = element.attributeValue("DataSetId");
    if (StringUtils.isNotEmpty(dsId)) {
        if (myDataMap != null && !myDataMap.isEmpty()) {
            params.putAll(myDataMap);/*from  w  w  w  . ja  v  a  2  s. co m*/
        }
        DatasetModel dsm = dataSetMap.get(dsId);
        String sql = dsm.getSql();
        String queryId = dsm.getQueryId();
        List<Map<String, Object>> rows = null;
        if (StringUtils.isNotEmpty(queryId)) {
            Environment.setCurrentSystemName(systemName);
            List<Object> list = entityService.getList(queryId, params);
            if (list != null && !list.isEmpty()) {
                rows = new ArrayList<Map<String, Object>>();
                for (Object object : list) {
                    if (object instanceof Map) {
                        Map dataMap = (Map) object;
                        rows.add(dataMap);
                    } else {
                        try {
                            Map dataMap = BeanUtils.describe(object);
                            rows.add(dataMap);
                        } catch (Exception e) {
                        }
                    }
                }
            }
        } else if (StringUtils.isNotEmpty(sql)) {
            LOG.debug("sql:" + sql);
            LOG.debug("params:" + params);
            rows = queryHelper.getResultList(systemName, sql, params);
            // ITablePageService tablePageService =
            // ContextFactory.getBean("tablePageService");
            // rows = queryHelper.getResultList(systemName, sql, params);
            // Environment.setCurrentSystemName(systemName);
            // rows = tablePageService.getListData(sql, params);
            LOG.debug("#1 rows:" + rows.size());
        }
        if (rows != null && !rows.isEmpty()) {
            int sortNo = 0;
            List<?> elements = element.elements();
            if (elements != null && elements.size() == 1) {
                Element elem = (Element) elements.get(0);
                LOG.debug("name:" + elem.getName());
                for (Map<String, Object> dataMap : rows) {
                    sortNo = sortNo + 1;
                    dataMap.put("sortNo", sortNo);
                    Element e = elem.createCopy();
                    if (dsm.getControllers() != null && !dsm.getControllers().isEmpty()) {
                        List<FieldController> controllers = dsm.getControllers();
                        for (FieldController c : controllers) {
                            Class<?> x = ClassUtils.classForName(c.getProvider());
                            FieldConverter fp = (FieldConverter) ReflectUtils.newInstance(x);
                            fp.convert(c.getFromName(), c.getToName(), dataMap);
                        }
                    }

                    if (e.isTextOnly()) {
                        String value = e.getStringValue();
                        if (StringUtils.contains(value, "#{") && StringUtils.contains(value, "}")) {
                            String text = QueryUtils.replaceBlankParas(value, dataMap);
                            e.setText(text);
                        }
                    }

                    List<?> attrs = e.attributes();
                    Iterator<?> iter = attrs.iterator();
                    while (iter.hasNext()) {
                        Attribute attr = (Attribute) iter.next();
                        String value = attr.getValue();
                        if (StringUtils.contains(value, "#{") && StringUtils.contains(value, "}")) {
                            String text = QueryUtils.replaceBlankParas(value, dataMap);
                            attr.setValue(text);
                        }
                    }

                    e.setParent(null);
                    parent.add(e);
                }
            }
        }
    }

    parent.remove(element);

}

From source file:com.glaf.core.xml.XmlBuilder.java

License:Apache License

@SuppressWarnings({ "rawtypes", "unchecked" })
protected void processNode(Element element, Map<String, DatasetModel> dataSetMap, String systemName,
        Map<String, Object> params) {
    //LOG.debug("---------------------processNode-----------------------");
    String dsId = element.attributeValue("DataSetId");

    if (StringUtils.isNotEmpty(dsId) && dataSetMap.get(dsId) != null) {
        //LOG.debug(element.getName() + " ds->" + dsId);
        element.remove(element.attribute("DataSetId"));
        Element parent = element.getParent();
        DatasetModel dsm = dataSetMap.get(dsId);
        String sql = dsm.getSql();
        String queryId = dsm.getQueryId();
        boolean single = dsm.isSingle();
        boolean foreachPerRow = dsm.isForeachPerRow();
        List<Map<String, Object>> rows = null;
        List<String> splits = dsm.getSplitList();
        if (foreachPerRow) {
            LOG.debug("sql:" + sql);
            LOG.debug("params:" + params);
            rows = queryHelper.getResultList(systemName, sql, params);
            // ITablePageService tablePageService =
            // ContextFactory.getBean("tablePageService");
            // rows = queryHelper.getResultList(systemName, sql, params);
            // Environment.setCurrentSystemName(systemName);
            // rows = tablePageService.getListData(sql, params);
            LOG.debug("#2 rows:" + rows.size());
        } else {//  w  w w.  j av  a 2  s .co m
            rows = ResultThreadLocal.getResultList(dsId);
            if (rows == null) {
                if (StringUtils.isNotEmpty(queryId)) {
                    Environment.setCurrentSystemName(systemName);
                    List<Object> list = entityService.getList(queryId, params);
                    if (list != null && !list.isEmpty()) {
                        rows = new ArrayList<Map<String, Object>>();
                        for (Object object : list) {
                            if (object instanceof Map) {
                                Map dataMap = (Map) object;
                                rows.add(dataMap);
                            } else {
                                try {
                                    Map dataMap = BeanUtils.describe(object);
                                    rows.add(dataMap);
                                } catch (Exception e) {
                                }
                            }
                        }
                    }
                } else if (StringUtils.isNotEmpty(sql)) {
                    LOG.debug("sql:" + sql);
                    LOG.debug("params:" + params);
                    rows = queryHelper.getResultList(systemName, sql, params);
                    // ITablePageService tablePageService =
                    // ContextFactory.getBean("tablePageService");
                    // rows = queryHelper.getResultList(systemName, sql,
                    // params);
                    // Environment.setCurrentSystemName(systemName);
                    // rows = tablePageService.getListData(sql, params);
                    LOG.debug("#3 rows:" + rows.size());
                }
                ResultThreadLocal.setResultList(dsId, rows);
            }
        }
        if (rows != null && !rows.isEmpty()) {
            if (single) {
                Map<String, Object> dataMap = rows.get(0);
                if (dsm.getControllers() != null && !dsm.getControllers().isEmpty()) {
                    List<FieldController> controllers = dsm.getControllers();
                    for (FieldController c : controllers) {
                        Class<?> x = ClassUtils.classForName(c.getProvider());
                        FieldConverter fp = (FieldConverter) ReflectUtils.newInstance(x);
                        fp.convert(c.getFromName(), c.getToName(), dataMap);
                    }
                }
                this.processTextNode(element, dataSetMap, dataMap, systemName, params);
            } else {
                int sortNo = 0;
                for (Map<String, Object> dataMap : rows) {
                    if (splits != null && !splits.isEmpty()) {
                        int splitCnt = 0;
                        Iterator<String> it = splits.iterator();
                        while (it.hasNext()) {
                            String str = it.next();
                            if (str != null && ObjectUtils.equals(dataMap.get(str), params.get(str))) {
                                splitCnt = splitCnt + 1;
                            } else if (str != null && ObjectUtils.equals(dataMap.get(str.toLowerCase()),
                                    params.get(str.toLowerCase()))) {
                                splitCnt = splitCnt + 1;
                            }
                        }
                        /**
                         * ???
                         */
                        if (splitCnt != splits.size()) {
                            // LOG.debug("dsId="+dsId);
                            // LOG.debug("params:"+params );
                            // LOG.debug(dataMap + " ???");
                            continue;
                        }
                    }
                    sortNo = sortNo + 1;
                    dataMap.put("sortNo", sortNo);
                    if (dsm.getControllers() != null && !dsm.getControllers().isEmpty()) {
                        List<FieldController> controllers = dsm.getControllers();
                        for (FieldController c : controllers) {
                            Class<?> x = ClassUtils.classForName(c.getProvider());
                            FieldConverter fp = (FieldConverter) ReflectUtils.newInstance(x);
                            fp.convert(c.getFromName(), c.getToName(), dataMap);
                        }
                    }

                    Element e = element.createCopy();
                    this.processTextNode(e, dataSetMap, dataMap, systemName, params);
                    parent.add(e);

                }
                parent.remove(element);
            }
        } else {
            parent.remove(element);
        }
    } else {
        this.processTextNode(element, dataSetMap, null, systemName, params);
    }
}

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

License:Apache License

private void repairWt() throws Exception {
    List<File> fs = getExcelRepairFiles();

    for (File f : fs) {
        String content = FileUtil.readFile(f, "utf-8");

        XmlParser parser = new XmlParser();
        org.dom4j.Document document = parser.parseXml(content);
        Element element = document.getRootElement();

        @SuppressWarnings("unchecked")
        List<Element> wts = element.selectNodes("//t");

        for (Element wt : wts) {
            if (wt == null)
                continue;

            @SuppressWarnings("unchecked")
            List<Element> es = wt.elements();
            if (!wt.isTextOnly()) {
                String text = wt.getStringValue();
                for (Element e : es) {
                    wt.remove(e);
                }//from  www . j  a  va 2 s.c o m

                wt.setText(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.ExcelRepairer.java

License:Apache License

private void repairExcelSharedStrings() throws Exception {
    File f = new File(path + "/xl/sharedStrings.xml");
    if (!f.exists())
        return;//  www . j a va2  s.c om

    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;/*www. ja va 2  s  . co 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);
    }
}