コード例 #1
0
        public override byte[] ConvertToXML(BpmnModel model, string encoding)
        {
            try
            {
                System.IO.MemoryStream outputStream = new System.IO.MemoryStream();

                //XMLOutputFactory xof = XMLOutputFactory.newInstance();
                //System.IO.StreamWriter @out = new System.IO.StreamWriter(outputStream, encoding);
                XDocument       doc    = XDocument.Load(outputStream);
                XElement        writer = doc.Root; //xof.createXElement(@out);
                XMLStreamWriter xtw    = new IndentingXMLStreamWriter(writer);

                DefinitionsRootExport.WriteRootElement(model, xtw, encoding);
                CollaborationExport.WritePools(model, xtw);
                DataStoreExport.WriteDataStores(model, xtw);
                SignalAndMessageDefinitionExport.WriteSignalsAndMessages(model, xtw);

                foreach (Process process in model.Processes)
                {
                    if (process.FlowElements.Count == 0 && process.Lanes.Count == 0)
                    {
                        // empty process, ignore it
                        continue;
                    }

                    ProcessExport.WriteProcess(process, xtw);

                    foreach (FlowElement flowElement in process.FlowElements)
                    {
                        CreateXML(flowElement, model, xtw);
                    }

                    foreach (Artifact artifact in process.Artifacts)
                    {
                        CreateXML(artifact, model, xtw);
                    }

                    // end process element
                    xtw.WriteEndElement();
                }

                // refactor each subprocess into a separate Diagram
                IList <BpmnModel> subModels = ParseSubModels(model);
                foreach (BpmnModel tempModel in subModels)
                {
                    if (tempModel.FlowLocationMap.Count > 0 || tempModel.LocationMap.Count > 0)
                    {
                        BPMNDIExport.WriteBPMNDI(tempModel, xtw);
                    }
                }

                // end definitions root element
                xtw.WriteEndElement();
                xtw.WriteEndDocument();

                xtw.Flush();
                outputStream.Seek(0, System.IO.SeekOrigin.Begin);
                byte[] bytes = outputStream.ToArray();

                // cleanup
                outputStream.Close();
                //XElementHelper.Close(xtw);
                xtw.Close();

                return(bytes);
            }
            catch (Exception e)
            {
                logger.LogError(e, "Error writing BPMN XML");
                throw new XMLException("Error writing BPMN XML", e);
            }
        }
コード例 #2
0
        public virtual byte[] ConvertToXML(BpmnModel model, string encoding)
        {
            try
            {
                using (MemoryStream ms = new MemoryStream())
                {
                    XmlWriterSettings settings = new XmlWriterSettings
                    {
                        //要求缩进
                        Indent = true,
                        //注意如果不设置encoding默认将输出utf-16
                        //注意这儿不能直接用Encoding.UTF8如果用Encoding.UTF8将在输出文本的最前面添加4个字节的非xml内容
                        Encoding = new UTF8Encoding(false),

                        //设置换行符
                        NewLineChars       = Environment.NewLine,
                        NamespaceHandling  = NamespaceHandling.OmitDuplicates,
                        OmitXmlDeclaration = true
                    };

                    using (XmlWriter xmlWriter = XmlWriter.Create(ms, settings))
                    {
                        XMLStreamWriter writer = new XMLStreamWriter(xmlWriter);

                        DefinitionsRootExport.WriteRootElement(model, writer, encoding);
                        CollaborationExport.WritePools(model, writer);
                        DataStoreExport.WriteDataStores(model, writer);
                        SignalAndMessageDefinitionExport.WriteSignalsAndMessages(model, writer);

                        foreach (Process process in model.Processes)
                        {
                            if (process.FlowElements?.Count == 0 && process.Lanes?.Count == 0)
                            {
                                // empty process, ignore it
                                continue;
                            }

                            ProcessExport.WriteProcess(process, writer);

                            foreach (FlowElement flowElement in process.FlowElements)
                            {
                                CreateXML(flowElement, model, writer);
                            }

                            foreach (Artifact artifact in process.Artifacts)
                            {
                                CreateXML(artifact, model, writer);
                            }

                            // end process element
                            writer.WriteEndElement();
                        }

                        try
                        {
                            BPMNDIExport.WriteBPMNDI(model, writer);
                        }
                        catch (Exception ex)
                        {
                            ///TODO: 不支持仅模型没有坐标位置的流程图
                        }
                        writer.WriteEndElement();
                    }

                    return(ms.ToArray());
                }
            }
            catch (Exception e)
            {
                log.LogError("Error writing BPMN XML", e);
                throw new XMLException("Error writing BPMN XML", e);
            }
        }