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:com.devoteam.srit.xmlloader.gui.frames.JFrameEditableParameters.java

License:Open Source License

public void fill(final List<Element> elts, boolean resetDefaults) {
    if (this.elements == null) {
        for (Element e : elts) {
            try {
                String value = e.attributeValue("value");
                if (null != value) {
                    e.attribute("value").setValue(parsing(value));
                }/*from w  ww.jav  a 2s .co  m*/
            } catch (Exception ee) {
                GlobalLogger.instance().getApplicationLogger().error(TextEvent.Topic.CORE, ee,
                        "Error in editable parameters window hile parsing parameters value : ", e);
            }
        }

        this.elements = new LinkedList<Element>();
        elementsOrigin = new LinkedList<Element>();
        if (resetDefaults) {
            elementsDefault = new LinkedList<Element>();
            this.elements.clear();
            for (Element e : elts) {
                this.elements.add(e.createCopy());
                elementsDefault.add(e.createCopy());
            }
        } else {
            this.elements.clear();
            for (Element e : elts) {
                this.elements.add(e.createCopy());
            }
        }

        EventQueue.invokeLater(new Runnable() {
            public void run() {
                doFill(elements);
            }
        });
    }
}

From source file:com.devoteam.srit.xmlloader.gui.frames.JFrameEditableParameters.java

License:Open Source License

/**
 * Returns the elements concerned by the key (node selected in the tree)
 *
 * @param key - TreePath in string format
 * @return//  ww  w  . ja v a 2s  . co  m
 */
private List<Element> getEltsForTable(String key) {
    if (key == null)
        return this.elements;
    if (key.contains("[root,"))
        key = key.substring(7, key.length() - 1); // 7 => '[root, '
    else
        key = key.substring(1, key.length() - 1);
    key = key.replaceAll(", ", ".");
    List<Element> dispElts = new LinkedList<Element>();
    for (Element e : this.elements) {
        if (e.attributeValue("name").contains(".")) {
            if (e.attributeValue("name").startsWith("[" + key + ".")) {
                Element elt = e.createCopy();
                elt.attribute("name").setValue(e.attributeValue("name").substring(key.length() + 2,
                        e.attributeValue("name").length() - 1));
                dispElts.add(elt);
            }
        } else
            dispElts.add(e.createCopy());
    }
    return dispElts;
}

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 a2s.  com*/
    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.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);//  ww  w  .j a  v  a 2s  .c  o 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 {/*from  w  w w .  j av  a2 s  . c  o 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.haulmont.cuba.core.sys.AbstractViewRepository.java

License:Apache License

protected void addFile(Element commonRootElem, String fileName) {
    if (readFileNames.contains(fileName))
        return;/*from w w  w  .  ja v  a2 s. c  om*/

    log.debug("Deploying views config: " + fileName);
    readFileNames.add(fileName);

    InputStream stream = null;
    try {
        stream = resources.getResourceAsStream(fileName);
        if (stream == null) {
            throw new IllegalStateException("Resource is not found: " + fileName);
        }

        SAXReader reader = new SAXReader();
        Document doc;
        try {
            doc = reader.read(new InputStreamReader(stream, StandardCharsets.UTF_8));
        } catch (DocumentException e) {
            throw new RuntimeException("Unable to parse view file " + fileName, e);
        }
        Element rootElem = doc.getRootElement();

        for (Element includeElem : Dom4j.elements(rootElem, "include")) {
            String incFile = includeElem.attributeValue("file");
            if (!StringUtils.isBlank(incFile))
                addFile(commonRootElem, incFile);
        }

        for (Element viewElem : Dom4j.elements(rootElem, "view")) {
            commonRootElem.add(viewElem.createCopy());
        }
    } finally {
        IOUtils.closeQuietly(stream);
    }
}

From source file:com.haulmont.cuba.restapi.XMLConverter2.java

License:Apache License

protected String paramElementContentAsString(Element paramEl) {
    Element nestedEl = paramEl.element("instance");
    if (nestedEl == null) {
        nestedEl = paramEl.element("instances");
    }//from  w  ww.j  av  a  2  s . c  o  m

    if (nestedEl == null) {
        return paramEl.getText();
    } else {
        Document doc = DocumentHelper.createDocument(nestedEl.createCopy());
        return doc.asXML();
    }
}

From source file:com.liferay.alloy.tools.builder.base.BaseBuilder.java

License:Open Source License

protected List<Component> getAllComponents() throws Exception {
    DocumentFactory factory = SAXReaderUtil.getDocumentFactory();

    Document doc = factory.createDocument();

    String taglibsXML = "<components></components>";

    Document taglibsDoc = SAXReaderUtil
            .read(new InputSource(new ByteArrayInputStream(taglibsXML.getBytes("utf-8"))));

    Element root = taglibsDoc.getRootElement();

    for (Document currentDoc : getComponentDefinitionDocs()) {
        currentDoc = _getExtendedDocument(currentDoc);

        Element currentRoot = currentDoc.getRootElement();

        String defaultPackage = currentRoot.attributeValue("short-name");
        List<Element> extComponentNodes = currentRoot.elements("component");

        for (Element extComponent : extComponentNodes) {
            String extComponentPackage = Convert.toString(extComponent.attributeValue("package"),
                    defaultPackage);//ww  w .  j a  va  2  s. c  o m

            extComponent.addAttribute("package", extComponentPackage);
        }

        Element authors = currentRoot.element("author");

        List<Element> components = currentRoot.elements("component");

        for (Element component : components) {
            Element copy = component.createCopy();
            Element componentAuthors = copy.element("authors");

            if ((authors != null) && (componentAuthors == null)) {
                copy.add(authors.createCopy());
            }

            root.add(copy);
        }

        List<org.dom4j.Attribute> attributes = currentRoot.attributes();

        for (org.dom4j.Attribute attribute : attributes) {
            root.addAttribute(attribute.getName(), attribute.getValue());
        }
    }

    doc.add(root.createCopy());

    return getComponents(doc);
}

From source file:com.liferay.alloy.tools.builder.base.BaseBuilder.java

License:Open Source License

protected Document mergeXMLAttributes(Document doc1, Document doc2) {
    Element doc2Root = doc2.getRootElement();
    Element doc1Root = doc1.getRootElement();
    Element docRoot = doc2Root.createCopy();

    docRoot.clearContent();/*w ww.jav  a 2  s.com*/

    if (doc1Root != null) {
        Iterator<Object> attributesIterator = doc1Root.attributeIterator();

        while (attributesIterator.hasNext()) {
            org.dom4j.Attribute attribute = (org.dom4j.Attribute) attributesIterator.next();

            if (attribute.getName().equals("extends")) {
                continue;
            }

            docRoot.addAttribute(attribute.getName(), attribute.getValue());
        }

        Element descriptionElement = doc1Root.element("description");

        if (descriptionElement != null) {
            docRoot.add(descriptionElement.createCopy());
        }
    }

    DocumentFactory factory = SAXReaderUtil.getDocumentFactory();

    Document doc = factory.createDocument();
    doc.setRootElement(docRoot);

    List<Element> doc2Components = doc2Root.elements(_COMPONENT);

    for (Element doc2Component : doc2Components) {
        Element component = doc2Component.createCopy();

        String name = doc2Component.attributeValue("name");

        Element doc1Component = getComponentNode(doc1, name);

        if (doc1Component != null) {
            Element doc1ComponentDescriptionElement = doc1Component.element("description");

            if (doc1ComponentDescriptionElement != null) {
                Element descriptionElement = component.element("description");

                if (descriptionElement != null) {
                    component.remove(descriptionElement);
                }

                component.add(doc1ComponentDescriptionElement.createCopy());
            }

            Iterator<Object> attributesIterator = doc1Component.attributeIterator();

            while (attributesIterator.hasNext()) {
                org.dom4j.Attribute attribute = (org.dom4j.Attribute) attributesIterator.next();

                component.addAttribute(attribute.getName(), attribute.getValue());
            }

            Element doc1AttributesNode = doc1Component.element(_ATTRIBUTES);

            Element attributesNode = component.element(_ATTRIBUTES);

            if ((doc1AttributesNode != null) && (attributesNode != null)) {
                List<Element> doc1Attributes = doc1AttributesNode.elements(_ATTRIBUTE);

                List<Element> attributes = attributesNode.elements(_ATTRIBUTE);

                for (Element doc1Attribute : doc1Attributes) {
                    Element attribute = getElementByName(attributes, doc1Attribute.elementText("name"));

                    if (attribute != null) {
                        attributesNode.remove(attribute);
                    }

                    attributesNode.add(doc1Attribute.createCopy());
                }
            }

            Element doc1EventsNode = doc1Component.element(_EVENTS);

            Element eventsNode = component.element(_EVENTS);

            if ((doc1EventsNode != null) && (eventsNode != null)) {
                List<Element> doc1Events = doc1EventsNode.elements(_EVENT);

                List<Element> events = eventsNode.elements(_EVENT);

                for (Element doc1Event : doc1Events) {
                    Element event = getElementByName(events, doc1Event.elementText("name"));

                    if (event != null) {
                        eventsNode.add(event);
                    }

                    eventsNode.add(doc1Event.createCopy());
                }
            }
        }

        doc.getRootElement().add(component);
    }

    if (doc1Root != null) {
        List<Element> doc1Components = doc1Root.elements(_COMPONENT);

        for (Element doc1Component : doc1Components) {
            Element component = doc1Component.createCopy();

            String name = doc1Component.attributeValue("name");

            Element doc2Component = getComponentNode(doc2, name);

            if (doc2Component == null) {
                doc.getRootElement().add(component);
            }
        }
    }

    return doc;
}

From source file:com.liferay.alloy.tools.builder.base.BaseBuilder.java

License:Open Source License

private Document _getExtendedDocument(Document document) {
    String parentXMLPath = document.getRootElement().attributeValue("extends");

    File parentXML = null;/*from  w w  w. j  av  a 2 s. com*/

    if (StringUtil.isNotBlank(parentXMLPath)) {
        parentXML = new File(parentXMLPath);

        if (!parentXML.exists()) {
            parentXMLPath = PropsUtil.getString(parentXMLPath);
            parentXML = new File(parentXMLPath);
        }

        if (parentXML.exists()) {
            try {
                Document parentDoc = SAXReaderUtil.read(parentXML);

                Element extensionElement = document.getRootElement().element("extension");

                document = mergeXMLAttributes(document, parentDoc);

                if (extensionElement != null) {
                    document.getRootElement().add(extensionElement.createCopy());
                }
            } catch (DocumentException e) {
                e.printStackTrace();
            }
        } else {
            System.out.println("Could not extend from: " + parentXMLPath + ". File does not exist.");
        }
    }

    return document;
}