Example usage for org.dom4j Document getRootElement

List of usage examples for org.dom4j Document getRootElement

Introduction

In this page you can find the example usage for org.dom4j Document getRootElement.

Prototype

Element getRootElement();

Source Link

Document

Returns the root Element for this document.

Usage

From source file:com.glaf.jbpm.deploy.MxJbpmProcessDeployer.java

License:Apache License

public ProcessDefinition deploy(JbpmContext jbpmContext, byte[] zipBytes) {
    SAXReader xmlReader = new SAXReader();
    JpdlXmlReader jpdlReader = new JpdlXmlReader();
    Document doc = null;
    InputStream inputStream = null;
    ZipInputStream zipInputStream = null;
    ProcessDefinition processDefinition = null;
    Map<String, byte[]> zipMap = null;
    try {//from  www. j ava 2  s  . co m
        zipInputStream = new ZipInputStream(new ByteArrayInputStream(zipBytes));
        zipMap = ZipUtils.getZipBytesMap(zipInputStream);
        zipInputStream.close();
        zipInputStream = null;
        if (zipMap != null) {
            if (zipMap.get("processdefinition.xml") != null) {
                byte[] bytes = zipMap.get("processdefinition.xml");
                inputStream = new ByteArrayInputStream(bytes);
                doc = xmlReader.read(inputStream);
                jpdlReader.checkTaskNames(doc);
                this.reconfigProcessDefinition(jbpmContext, doc);
                Element root = doc.getRootElement();
                String encoding = doc.getXMLEncoding();
                String processName = root.attributeValue("name");

                try {
                    SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss", Locale.getDefault());
                    String ret = formatter.format(new Date());
                    String filename = SystemProperties.getConfigRootPath() + "/deploy/" + processName + ret
                            + ".zip";
                    // ?
                    FileUtils.save(filename, zipBytes);
                } catch (Exception ex) {
                    if (LogUtils.isDebug()) {
                        logger.debug(ex);
                        ex.printStackTrace();
                    }
                }

                byte[] pdBytes = Dom4jUtils.getBytesFromPrettyDocument(doc, encoding);
                zipMap.put("processdefinition.xml", pdBytes);

                // ??
                byte[] newZipBytes = ZipUtils.toZipBytes(zipMap);
                zipInputStream = new ZipInputStream(new ByteArrayInputStream(newZipBytes));
                processDefinition = ProcessDefinition.parseParZipInputStream(zipInputStream);
                jbpmContext.deployProcessDefinition(processDefinition);
                zipInputStream.close();
                zipInputStream = null;

                String processDefinitionId = String.valueOf(processDefinition.getId());

                logger.debug("processDefinitionId:" + processDefinitionId);
                logger.debug("processName:" + processName);
                Map<String, Task> taskMap = processDefinition.getTaskMgmtDefinition().getTasks();
                if (taskMap != null && taskMap.size() > 0) {
                    Iterator<String> iter = taskMap.keySet().iterator();
                    while (iter.hasNext()) {
                        String taskName = iter.next();
                        logger.debug("taskName:" + taskName);
                    }
                }

                try {
                    SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss", Locale.getDefault());
                    String ret = formatter.format(new Date());
                    String filename = SystemProperties.getConfigRootPath() + "/deploy/" + processName + ret
                            + "_repack.zip";
                    // ??
                    FileUtils.save(filename, newZipBytes);
                } catch (Exception ex) {
                    if (LogUtils.isDebug()) {
                        logger.debug(ex);
                    }
                }
            }

            if (zipMap.get("process.cfg.xml") != null) {
                byte[] x_bytes = zipMap.get("process.cfg.xml");
                if (x_bytes != null) {
                    JbpmExtensionReader reader = new JbpmExtensionReader();
                    List<Extension> extensions = reader.readTasks(new ByteArrayInputStream(x_bytes));
                    if (extensions != null && extensions.size() > 0) {

                        JbpmExtensionManager jbpmExtensionManager = ProcessContainer.getContainer()
                                .getJbpmExtensionManager();
                        jbpmExtensionManager.reconfig(jbpmContext, extensions);
                    }
                }
            }
        }
        return processDefinition;
    } catch (Throwable ex) {
        if (LogUtils.isDebug()) {
            logger.debug(ex);
            ex.printStackTrace();
        }
        throw new JbpmException(ex);
    } finally {
        try {
            if (zipInputStream != null) {
                zipInputStream.close();
                zipInputStream = null;
            }
        } catch (Exception ex) {
            if (LogUtils.isDebug()) {
                logger.debug(ex);
            }
        }
        try {
            if (inputStream != null) {
                inputStream.close();
                inputStream = null;
            }
        } catch (Exception ex) {
            if (LogUtils.isDebug()) {
                logger.debug(ex);
            }
        }
    }
}

From source file:com.glaf.jbpm.deploy.MxJbpmProcessDeployer.java

License:Apache License

public void reconfigProcessDefinition(JbpmContext jbpmContext, Document doc) {
    Element root = doc.getRootElement();
    String processName = root.attributeValue("name");
    String className = null;//from  ww w. j a va 2 s.c  om
    Map<String, String> actionMap = new java.util.HashMap<String, String>();
    List<?> actionList = root.elements("action");
    if (actionList != null && actionList.size() > 0) {
        Iterator<?> iterator = actionList.iterator();
        while (iterator.hasNext()) {
            Element element = (Element) iterator.next();
            String name = element.attributeValue("name");
            String clazz = element.attributeValue("class");
            actionMap.put(name, clazz);
        }
    }

    if (StringUtils.isEmpty(className)) {
        className = CustomProperties.getString(processName + ".jbpm.action.class");
    }

    if (StringUtils.isEmpty(className)) {
        className = CustomProperties.getString("jbpm.action.class");
    }

    if (StringUtils.isEmpty(className)) {
        className = SystemProperties.getString("jbpm.action.class");
    }

    List<?> taskNodeList = root.elements("task-node");
    if (taskNodeList == null || taskNodeList.size() == 0) {
        return;
    }

    Iterator<?> iterator = taskNodeList.iterator();
    while (iterator.hasNext()) {
        Element element = (Element) iterator.next();
        Map<String, String> taskMap = new java.util.HashMap<String, String>();
        List<?> taskList = element.elements("task");
        if (taskList != null && taskList.size() > 0) {
            Iterator<?> iter = taskList.iterator();
            while (iter.hasNext()) {
                Element elem = (Element) iter.next();
                taskMap.put(elem.attributeValue("name"), elem.attributeValue("description"));
            }
        }

        List<?> eventList = element.elements("event");
        if (eventList != null && eventList.size() > 0) {
            Iterator<?> iter = eventList.iterator();
            while (iter.hasNext()) {
                Element elem = (Element) iter.next();
                String eventType = elem.attributeValue("type");
                if (StringUtils.equals(eventType, "node-enter")) {
                    List<?> actionRefList = elem.elements("action");
                    if (actionRefList != null && actionRefList.size() > 0) {
                        Iterator<?> it = actionRefList.iterator();
                        while (it.hasNext()) {
                            Element e = (Element) it.next();
                            String ref = e.attributeValue("ref-name");
                            if (StringUtils.isNotEmpty(ref)) {
                                if (!actionMap.containsKey(ref)) {
                                    Element newAction = root.addElement("action");
                                    newAction.addAttribute("name", ref);
                                    newAction.addAttribute("class", className);
                                    newAction.addElement("extensionName").setText(ref);
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

From source file:com.glaf.jbpm.export.MxJbpmProcessExporter.java

License:Apache License

public void addElement(Document doc, Map<String, Object> context) {
    Element root = doc.getRootElement();
    GraphSession graphSession = null;//from  w  w w .ja  v a2  s.  c om
    JbpmContext jbpmContext = null;
    try {
        jbpmContext = ProcessContainer.getContainer().createJbpmContext();
        graphSession = jbpmContext.getGraphSession();
        List<ProcessDefinition> processDefinitions = graphSession.findLatestProcessDefinitions();
        if (processDefinitions != null && processDefinitions.size() > 0) {
            Element element = root.addElement("processes");
            Iterator<ProcessDefinition> iterator = processDefinitions.iterator();
            while (iterator.hasNext()) {
                ProcessDefinition pd = iterator.next();
                Element elem = element.addElement("process");
                elem.addAttribute("name", pd.getName());
                elem.addAttribute("description", pd.getDescription());
            }
        }
    } catch (Exception ex) {
        if (LogUtils.isDebug()) {
            ex.printStackTrace();
        }
    } finally {
        Context.close(jbpmContext);
    }

    Element jbpmCfgTemplate = root.addElement("jbpm-cfg-template");

    String configPath = SystemProperties.getConfigRootPath();
    String path = configPath + "/conf/jbpm/template";
    java.io.File directory = new java.io.File(path);
    String[] filelist = directory.list();
    InputStream inputStream = null;
    SAXReader xmlReader = new SAXReader();
    for (int i = 0; i < filelist.length; i++) {
        String filename = directory.getAbsolutePath() + "/" + filelist[i];
        File file = new File(filename);
        if (file.isFile() && file.getName().endsWith(".xml")) {
            try {
                inputStream = new FileInputStream(file);
                Document doc2x = xmlReader.read(inputStream);
                Element root2x = doc2x.getRootElement();
                List<?> elements = root2x.elements("action-definition");
                Iterator<?> iterator = elements.iterator();
                while (iterator.hasNext()) {
                    Element elem = (Element) iterator.next();
                    elem.setParent(jbpmCfgTemplate);
                    elem.setDocument(doc);
                    jbpmCfgTemplate.add(elem);
                }
                inputStream.close();
                inputStream = null;
            } catch (Exception ex) {
                throw new RuntimeException(ex);
            }
        }
    }
}

From source file:com.glaf.jbpm.xml.JpdlXmlReader.java

License:Apache License

public List<String> getTaskNames(InputStream inputStream) {
    List<String> taskNames = new ArrayList<String>();
    SAXReader xmlReader = new SAXReader();
    Document doc = null;
    try {/* w ww  .  j  a  v a2s .com*/
        doc = xmlReader.read(inputStream);
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }
    Element root = doc.getRootElement();
    List<?> rows = root.elements("task-node");
    Iterator<?> iterator = rows.iterator();
    while (iterator.hasNext()) {
        Element element = (Element) iterator.next();
        List<?> tasks = element.elements("task");
        Iterator<?> iter = tasks.iterator();
        while (iter.hasNext()) {
            Element elem = (Element) iter.next();
            String taskName = elem.attributeValue("name");
            if (taskNames.contains(taskName)) {
                throw new RuntimeException("task name '" + taskName + "' is exists.");
            }
            taskNames.add(taskName);
        }
    }

    return taskNames;
}

From source file:com.glaf.jbpm.xml.JpdlXmlReader.java

License:Apache License

public void checkTaskNames(Document doc) {
    List<String> taskNames = new ArrayList<String>();
    Element root = doc.getRootElement();
    List<?> rows = root.elements("task-node");
    Iterator<?> iterator = rows.iterator();
    while (iterator.hasNext()) {
        Element element = (Element) iterator.next();
        List<?> tasks = element.elements("task");
        Iterator<?> iter = tasks.iterator();
        while (iter.hasNext()) {
            Element elem = (Element) iter.next();
            String taskName = elem.attributeValue("name");
            if (taskNames.contains(taskName)) {
                throw new RuntimeException("task name '" + taskName + "' is exists.");
            }/* www.ja  v a 2s  . co m*/
            taskNames.add(taskName);
        }
    }
}

From source file:com.glaf.jbpm.xml.JpdlXmlReader.java

License:Apache License

public List<Todo> read(InputStream inputStream) {
    List<Todo> todoList = new java.util.ArrayList<Todo>();
    SAXReader xmlReader = new SAXReader();
    int sortNo = 1;
    try {//w  w  w .j  av a 2  s. c  o  m
        Document doc = xmlReader.read(inputStream);
        Element root = doc.getRootElement();
        String processName = root.attributeValue("name");
        String moduleName = root.elementText("description");
        List<?> rows = root.elements("task-node");
        Iterator<?> iterator = rows.iterator();
        while (iterator.hasNext()) {
            Element element = (Element) iterator.next();
            List<?> tasks = element.elements("task");
            Iterator<?> iter = tasks.iterator();
            while (iter.hasNext()) {
                Element elem = (Element) iter.next();
                String taskName = elem.attributeValue("name");
                if (taskName.startsWith("task55")) {
                    continue;
                }
                Todo model = new Todo();
                model.setSortNo(sortNo++);
                model.setProcessName(processName);
                model.setModuleName(moduleName);
                model.setTitle(moduleName + " " + elem.attributeValue("description") + "??");
                model.setContent(moduleName + " " + elem.attributeValue("description") + "??");
                model.setCode(processName + "_" + taskName);
                model.setTaskName(taskName);
                model.setProvider("jbpm");
                model.setLinkType(processName + "_" + taskName);
                String detail_url = conf.get("jbpm_task_detail_url");
                String list_url = conf.get("jbpm_task_url");
                model.setLink(detail_url + "&rowId=#{rowId}&x_method=view&app_name=" + processName);
                model.setListLink(list_url + "&taskType=running&app_name=" + processName);
                model.setLimitDay(2);
                model.setXa(6);
                model.setXb(6);
                todoList.add(model);
            }
        }
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }
    return todoList;
}

From source file:com.glaf.mail.MxMailHelper.java

License:Apache License

public Map<String, String> getAttributes(String attribute) {
    Map<String, String> dataMap = new java.util.HashMap<String, String>();
    StringReader stringReader = new StringReader(attribute);
    SAXReader xmlReader = new SAXReader();
    try {/*from   ww w.  ja  v  a2s.  c o m*/
        Document doc = xmlReader.read(stringReader);
        Element root = doc.getRootElement();
        List<?> elements = root.elements("element");
        if (elements != null && elements.size() > 0) {
            Iterator<?> iterator = elements.iterator();
            while (iterator.hasNext()) {
                Element element = (Element) iterator.next();
                String name = element.attributeValue("name");
                String value = element.attributeValue("value");
                if (StringUtils.isEmpty(value)) {
                    value = element.getStringValue();
                }
                dataMap.put(name, value);
            }
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return dataMap;
}

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;
    try {//from   w ww .j av  a2  s  .c  o m
        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;
    try {//from  w  ww .  j  av a  2s .  com
        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.sdk.WeixinExecutor.java

License:Apache License

private void parseInputStream() throws Exception {
    SAXReader xmlReader = new SAXReader();
    Document doc = null;
    try {/*from  w  ww  . j  a  v  a  2  s  .  c om*/
        doc = xmlReader.read(request.getInputStream());
    } catch (DocumentException ex) {
        ex.printStackTrace();
        doc = xmlReader.read(new ByteArrayInputStream(request.getParameter("xml").getBytes("UTF-8")));
    }

    String uri = request.getRequestURI();
    String id = uri.substring(uri.lastIndexOf("/") + 1);
    Long accountId = Long.parseLong(id);
    WxUser user = WxIdentityFactory.getUserByAccountId(accountId);

    Element root = doc.getRootElement();
    logger.debug(root.asXML());

    String type = root.elementText(IMessage.TAG_MSGTYPE);

    if (StringUtils.equalsIgnoreCase(type, MESSAGE_TEXT)) {
        message = new TextMessage();
        messageHandler = new TextMessageHandler();
    } else if (StringUtils.equalsIgnoreCase(type, MESSAGE_EVENT)) {
        // do subscribe event
        message = new EventMessage();
        messageHandler = new EventMessageHandler();
    } else if (StringUtils.equalsIgnoreCase(type, MESSAGE_IMAGE)) {
        message = new ImageMessage();
        messageHandler = new ImageMessageHandler();
    } else if (StringUtils.equalsIgnoreCase(type, MESSAGE_LINK)) {
        message = new LinkMessage();
        messageHandler = new LinkMessageHandler();
    } else if (StringUtils.equalsIgnoreCase(type, MESSAGE_LOCATION)) {
        message = new LocationMessage();
        messageHandler = new LocationMessageHandler();
    } else if (StringUtils.equalsIgnoreCase(type, MESSAGE_VOICE)) {
        message = new VoiceMessage();
        messageHandler = new VoiceMessageHandler();
    } else if (StringUtils.equalsIgnoreCase(type, MESSAGE_VIDEO)) {
        message = new VideoMessage();
        messageHandler = new VideoMessageHandler();
    }

    message.setRoot(root);
    message.setAccountId(accountId);
    message.setCustomer(user.getActorId());
    message.setContextPath(request.getContextPath());
    message.setRequestParameters(RequestUtils.getParameterMap(request));
    String serviceUrl = WechatUtils.getServiceUrl(request);
    message.setServiceUrl(serviceUrl);
    message.setRemoteIPAddr(RequestUtils.getIPAddress(request));

    // do the default/common parse!
    messageHandler.parseMessage(message, root);
}