Example usage for org.dom4j Element elements

List of usage examples for org.dom4j Element elements

Introduction

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

Prototype

List<Element> elements();

Source Link

Document

Returns the elements contained in this element.

Usage

From source file:com.github.megatronking.svg.applet.graphics.VdTree.java

License:Apache License

private void parseTree(Element currentNode, VdGroup currentGroup) {
    List<?> childrenNodes = currentNode.elements();
    int length = childrenNodes.size();
    for (int i = 0; i < length; i++) {
        Element child = (Element) childrenNodes.get(i);
        if (child.getNodeType() == Element.ELEMENT_NODE) {
            if (SHAPE_GROUP.equals(child.getName())) {
                VdGroup newGroup = parseGroupAttributes(child.attributes());
                currentGroup.add(newGroup);
                parseTree(child, newGroup);
            } else if (SHAPE_PATH.equals(child.getName())) {
                VdPath newPath = parsePathAttributes(child.attributes());
                currentGroup.add(newPath);
            }//  w  w w . j  a  v  a 2 s.  c  o  m
        }
    }
}

From source file:com.glaf.activiti.spring.SpringProcessEngineConfigurationBean.java

License:Apache License

@Override
protected void initSqlSessionFactory() {
    logger.info("-------------------------------------------");
    logger.info("-------------initSqlSessionFactory()-------");
    logger.info("sqlSessionFactory:" + sqlSessionFactory);
    logger.info("transactionFactory:" + transactionFactory);
    if (sqlSessionFactory == null) {
        InputStream inputStream = null;
        try {// w  w  w .  j  av  a2 s .  c  o m
            if (configLocation != null) {
                logger.info("mybatis config:" + this.configLocation.getFile().getAbsolutePath());
                inputStream = this.configLocation.getInputStream();
            } else {
                Resource resource = new ClassPathResource("com/glaf/activiti/activiti.mybatis.conf.xml");
                inputStream = resource.getInputStream();
            }

            if (!ObjectUtils.isEmpty(this.mapperLocations)) {
                SAXReader xmlReader = new SAXReader();
                EntityResolver entityResolver = new XMLMapperEntityResolver();
                xmlReader.setEntityResolver(entityResolver);
                Document doc = xmlReader.read(inputStream);
                Element root = doc.getRootElement();
                Element mappers = root.element("mappers");
                if (mappers != null) {
                    java.util.List<?> list = mappers.elements();
                    Collection<String> files = new HashSet<String>();

                    if (list != null && !list.isEmpty()) {
                        Iterator<?> iterator = list.iterator();
                        while (iterator.hasNext()) {
                            Element elem = (Element) iterator.next();
                            if (elem.attributeValue("resource") != null) {
                                String file = elem.attributeValue("resource");
                                files.add(file);
                            } else if (elem.attributeValue("url") != null) {
                                String file = elem.attributeValue("url");
                                files.add(file);
                            }
                        }
                    }

                    for (Resource mapperLocation : this.mapperLocations) {
                        if (mapperLocation == null) {
                            continue;
                        }
                        String url = mapperLocation.getURL().toString();
                        // logger.debug("find mapper:" + url);
                        if (!files.contains(url)) {
                            Element element = mappers.addElement("mapper");
                            element.addAttribute("url", url);
                        }
                    }
                }

                IOUtils.closeStream(inputStream);

                byte[] bytes = Dom4jUtils.getBytesFromPrettyDocument(doc);
                inputStream = new ByteArrayInputStream(bytes);

            }

            // update the jdbc parameters to the configured ones...
            Environment environment = new Environment("default", transactionFactory, dataSource);
            Reader reader = new InputStreamReader(inputStream);
            Properties properties = new Properties();
            properties.put("prefix", databaseTablePrefix);
            if (databaseType != null) {
                properties.put("limitBefore",
                        DbSqlSessionFactory.databaseSpecificLimitBeforeStatements.get(databaseType));
                properties.put("limitAfter",
                        DbSqlSessionFactory.databaseSpecificLimitAfterStatements.get(databaseType));
                properties.put("limitBetween",
                        DbSqlSessionFactory.databaseSpecificLimitBetweenStatements.get(databaseType));
                properties.put("orderBy",
                        DbSqlSessionFactory.databaseSpecificOrderByStatements.get(databaseType));
            }
            XMLConfigBuilder parser = new XMLConfigBuilder(reader, "", properties);
            Configuration configuration = parser.getConfiguration();
            configuration.setEnvironment(environment);
            configuration.getTypeHandlerRegistry().register(VariableType.class, JdbcType.VARCHAR,
                    new IbatisVariableTypeHandler());
            configuration = parser.parse();

            sqlSessionFactory = new DefaultSqlSessionFactory(configuration);

        } catch (Exception e) {
            throw new ActivitiException("Error while building ibatis SqlSessionFactory: " + e.getMessage(), e);
        } finally {
            IoUtil.closeSilently(inputStream);
        }
    }
}

From source file:com.glaf.core.todo.util.TodoXmlReader.java

License:Apache License

public List<Todo> read(java.io.InputStream inputStream) {
    List<Todo> todos = new java.util.ArrayList<Todo>();
    SAXReader xmlReader = new SAXReader();
    int sortNo = 1;
    try {/*from   ww  w  . ja va  2  s.  c  o  m*/
        Document doc = xmlReader.read(inputStream);
        Element root = doc.getRootElement();
        List<?> rows = root.elements();
        Iterator<?> iterator = rows.iterator();
        while (iterator.hasNext()) {
            Element element = (Element) iterator.next();
            String id = element.attributeValue("id");
            Map<String, Object> rowMap = new java.util.HashMap<String, Object>();
            rowMap.put("id", id);
            List<?> properties = element.elements("property");
            Iterator<?> iter = properties.iterator();
            while (iter.hasNext()) {
                Element elem = (Element) iter.next();
                String propertyName = elem.attributeValue("name");
                String propertyValue = null;
                if (elem.attribute("value") != null) {
                    propertyValue = elem.attributeValue("value");
                } else {
                    propertyValue = elem.getTextTrim();
                }
                if (StringUtils.isNotEmpty(propertyName) && StringUtils.isNotEmpty(propertyValue)) {
                    rowMap.put(propertyName, propertyValue);
                }
            }

            Todo model = new Todo();
            model.setSortNo(sortNo++);
            Tools.populate(model, rowMap);

            todos.add(model);
        }
    } catch (Exception ex) {
        ex.printStackTrace();
        throw new RuntimeException(ex.getMessage());
    }
    return todos;
}

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 .  j av a  2 s  .  c om
        }
        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

protected void processTextNode(Element element, Map<String, DatasetModel> dataSetMap,
        Map<String, Object> dataMap, String systemName, Map<String, Object> params) {
    //LOG.debug("---------------------processTextNode-----------------------");
    if (StringUtils.equals(element.getName(), "foreach")) {
        this.processForEachNode(element, dataSetMap, dataMap, systemName, params);
        return;/*from  ww w.j  a  v a2  s .  c o m*/
    }
    String dsId = element.attributeValue("DataSetId");
    if (StringUtils.isNotEmpty(dsId)) {
        if (dataMap != null && !dataMap.isEmpty()) {
            params.putAll(dataMap);
        }
        this.processNode(element, dataSetMap, systemName, params);
        return;
    }

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

    List<?> attrs = element.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);
        }
    }

    List<?> elements = element.elements();
    Iterator<?> iterator = elements.iterator();
    while (iterator.hasNext()) {
        Element elem = (Element) iterator.next();
        if (StringUtils.equals(elem.getName(), "foreach")) {
            this.processForEachNode(elem, dataSetMap, dataMap, systemName, params);
        } else {
            String dsId2 = elem.attributeValue("DataSetId");
            if (StringUtils.isNotEmpty(dsId2)) {
                if (dataMap != null && !dataMap.isEmpty()) {
                    params.putAll(dataMap);
                }
                this.processNode(elem, dataSetMap, systemName, params);
            } else {
                this.processTextNode(elem, dataSetMap, dataMap, systemName, params);
            }
        }
    }
}

From source file:com.glaf.mail.xml.MailXmlReader.java

License:Apache License

public List<MailTemplate> read(java.io.InputStream inputStream) {
    List<MailTemplate> mailTemplates = new java.util.ArrayList<MailTemplate>();
    SAXReader xmlReader = new SAXReader();
    Document doc = null;/*from ww  w. j a  v  a  2  s.  com*/
    try {
        doc = xmlReader.read(inputStream);
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }
    Element root = doc.getRootElement();
    List<?> rows = root.elements();
    Iterator<?> iterator = rows.iterator();
    while (iterator.hasNext()) {
        Element element = (Element) iterator.next();
        String mailId = element.attributeValue("id");
        String title = element.attributeValue("title");
        String templateId = element.elementText("templateId");
        String templatePath = element.elementText("templatePath");

        MailTemplate mt = new MailTemplate();
        mt.setTitle(title);
        mt.setMailDefId(mailId);
        mt.setTemplateId(templateId);
        mt.setTemplatePath(templatePath);
        mt.setProperties(this.readProperties(element));
        mt.setContent(element.elementText("content"));
        mt.setDescription(element.elementText("description"));

        List<?> datasets = element.elements("dataset");
        Iterator<?> iter = datasets.iterator();
        while (iter.hasNext()) {
            Element elem = (Element) iter.next();
            MailDataSet mailDS = new MailDataSet();
            mailDS.setProperties(this.readProperties(elem));

            List<?> rowsets = elem.elements("rowset");
            Iterator<?> it = rowsets.iterator();
            while (it.hasNext()) {
                Element em = (Element) it.next();
                MailRowSet rs = new MailRowSet();
                rs.setQuery(em.elementTextTrim("query"));
                rs.setMapping(em.attributeValue("mapping"));
                rs.setMailMgr(em.attributeValue("mailMgr"));
                rs.setMailMgrClassName(em.attributeValue("mailMgrClassName"));
                rs.setMailMgrMapping(em.attributeValue("mailMgrMapping"));
                rs.setDataMgr(em.attributeValue("dataMgr"));
                rs.setDataMgrBeanId(em.attributeValue("dataMgrBeanId"));
                rs.setDataMgrClassName(em.attributeValue("dataMgrClassName"));
                rs.setProperties(this.readProperties(em));
                String singleResult = em.attributeValue("singleResult");
                if ("true".equals(singleResult)) {
                    rs.setSingleResult(true);
                }
                mailDS.addRowSet(rs);
            }

            mt.addDataSet(mailDS);
        }
        mailTemplates.add(mt);
    }

    return mailTemplates;
}

From source file:com.glaf.report.xml.ReportDefinitionReader.java

License:Apache License

public List<ReportDefinition> read(java.io.InputStream inputStream) {
    List<ReportDefinition> reports = new java.util.ArrayList<ReportDefinition>();
    SAXReader xmlReader = new SAXReader();
    Document doc = null;/*from  w w w.j  a va 2  s  .c  o  m*/
    try {
        doc = xmlReader.read(inputStream);
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }
    Element root = doc.getRootElement();
    List<?> rows = root.elements();
    Iterator<?> iterator = rows.iterator();
    while (iterator.hasNext()) {
        Element element = (Element) iterator.next();
        String reportId = element.attributeValue("id");
        String templateId = element.elementText("templateId");
        String templateFile = element.elementText("templateFile");

        ReportDefinition rdf = new ReportDefinition();
        rdf.setReportId(reportId);
        rdf.setTemplateId(templateId);
        rdf.setTemplateFile(templateFile);
        rdf.setProperties(this.readProperties(element));

        List<?> datasets = element.elements("dataset");
        Iterator<?> iter = datasets.iterator();
        while (iter.hasNext()) {
            Element elem = (Element) iter.next();
            ReportDataSet rds = new ReportDataSet();
            rds.setDatasourceName(elem.attributeValue("datasourceName"));
            rds.setProperties(this.readProperties(elem));

            List<?> rowsets = elem.elements("rowset");
            Iterator<?> it = rowsets.iterator();
            while (it.hasNext()) {
                Element em = (Element) it.next();
                ReportRowSet rs = new ReportRowSet();
                rs.setQuery(em.elementTextTrim("query"));
                rs.setMapping(em.attributeValue("mapping"));
                rs.setRptMgr(em.attributeValue("rptMgr"));
                rs.setRptMgrClassName(em.attributeValue("rptMgrClassName"));
                rs.setRptMgrMapping(em.attributeValue("rptMgrMapping"));
                rs.setDataMgr(em.attributeValue("dataMgr"));
                rs.setDataMgrBeanId(em.attributeValue("dataMgrBeanId"));
                rs.setDataMgrClassName(em.attributeValue("dataMgrClassName"));
                rs.setProperties(this.readProperties(em));
                String singleResult = em.attributeValue("singleResult");
                if ("true".equals(singleResult)) {
                    rs.setSingleResult(true);
                }
                rds.addRowSet(rs);
            }

            rdf.addDataSet(rds);
        }
        reports.add(rdf);
    }

    return reports;
}

From source file:com.glaf.wechat.util.MessageUtils.java

License:Apache License

/**
 * ???XML// w w  w .ja  va 2 s.  c  om
 * 
 * @param request
 * @return
 * @throws Exception
 */
@SuppressWarnings("unchecked")
public static Map<String, String> parseXml(HttpServletRequest request) {
    // ?HashMap
    Map<String, String> map = new java.util.concurrent.ConcurrentHashMap<String, String>();

    // request??
    InputStream inputStream = null;
    // ??
    SAXReader reader = new SAXReader();
    try {
        inputStream = request.getInputStream();

        Document document = reader.read(inputStream);
        // xml
        Element root = document.getRootElement();
        // ?
        List<Element> elementList = root.elements();

        // ???
        for (Element e : elementList) {
            map.put(e.getName(), e.getText());
        }
    } catch (Exception ex) {
        ex.printStackTrace();
        throw new RuntimeException(ex);
    } finally {
        try {
            // ?
            if (inputStream != null) {
                inputStream.close();
                inputStream = null;
            }
        } catch (IOException ex) {
        }
    }

    return map;
}

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   w ww . j  a v  a 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 static List<Element> getElementByName(Element root, String name) {
    List<Element> elements = new ArrayList<Element>();

    @SuppressWarnings("unchecked")
    List<Element> es = root.elements();
    for (Element e : es) {
        if (name.equals(e.getName()))
            elements.add(e);// w  w w.  j  a  v a 2 s.  c  o m
        else
            elements.addAll(getElementByName(e, name));
    }

    return elements;
}