| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using HySoft.IVRFlowEditor.IVRControlUtility;
- using HySoft.IVRFlowEditor.Model;
- using System.Xml;
- using System.Xml.Serialization;
- using System.IO;
- using System.Reflection;
- using System.ComponentModel;
- using System.Drawing;
- namespace HySoft.IVRFlowEditor.Utility
- {
- /// <summary>
- /// IVR流程编辑器导入导出操作类
- /// </summary>
- public class IVRFlowSerializer:SerializerBase
- {
-
- /// <summary>
- /// 保存到文件(所有要序列化到文件的属性必须使用 IVRAttribute 特性加以描述)
- /// </summary>
- /// <param name="filePath">文件路径(包含文件名)</param>
- /// <param name="val">要保存的对象集合</param>
- /// <returns></returns>
- public override Model.MessageInfo SaveFileInfo(string filePath, List<IVRFlowSerializerInfo> val)
- {
- MessageInfo msg;
- try
- {
- StringBuilder xmlBuilder = new StringBuilder();
- xmlBuilder.Append("<?xml version=\"1.0\" encoding=\"utf-8\"?> \r\n<ivr> \r\n");
-
- foreach (IVRFlowSerializerInfo o in val)
- {
- if (o.IVRControl != null && o.IVRControl.Count > 0)
- {
- //xmlBuilder.Append("<flow Name=\"" + o.IVR_Name + "\" Type=\"" + o.IVRFlowType.ToString() + "\" IVR_Name=\"" + o.IVR_Name + "\" IVR_Text=\"" + o.IVR_Text + "\" MatchedNum=\"\" MatchedLine=\"\" Concurrency=\"1\"> \r\n");
- //2017-4-28修改
- xmlBuilder.Append("<flow Name=\"" + o.IVR_Name + "\" Type=\"" + o.IVRFlowType.ToString() + "\" IVR_Name=\"" + o.IVR_Name + "\" IVR_Text=\"" + o.IVR_Text + "\" MatchedNum=\".\" MatchedLine=\"|-1|\" Concurrency=\"1\"> \r\n");
- foreach (IVRControlBase ivr in o.IVRControl)
- {
- Dictionary<int,string> disAtt=new Dictionary<int,string>();
- msg = CreateXmlNode("cell", ref xmlBuilder, ivr,true);
- if (!msg.State)
- return msg;
- }
- xmlBuilder.Append("</flow> \r\n");
- }
- }
- xmlBuilder.Append("</ivr> \r\n");
- File.Delete(filePath);
- using (FileStream fs = new FileStream(filePath, FileMode.CreateNew, FileAccess.ReadWrite))
- {
- StreamWriter sw = new StreamWriter(fs);
- sw.Write(xmlBuilder.ToString());
- sw.Close();
- fs.Close();
- }
-
- return new MessageInfo() { Code = 0, State = true, Message = "保存成功!" };
- }
- catch (Exception ex)
- {
- return new MessageInfo() { State = false, Code = -1, Message = "保存到文件发生异常,错误信息:" + ex.Message };
- }
- }
- /// <summary>
- /// 构建XML节点
- /// </summary>
- /// <param name="nodeName">节点名称</param>
- /// <param name="xmlBuilder">返回的XML内容</param>
- /// <param name="b">要解析的对象</param>
- /// <param name="isWriteName">是否写节点的属性名称 针对二级节点(二级子节点没有名称)</param>
- /// <returns></returns>
- private MessageInfo CreateXmlNode(string nodeName,ref StringBuilder xmlBuilder, object b,bool isWriteName)
- {
- try
- {
- Dictionary<int, string> disAtt =new Dictionary<int,string>();
- List<StringBuilder> nodes = new List<StringBuilder>();
- xmlBuilder.Append("<" + nodeName + " "+(isWriteName?" Name=\"" + (b.GetType().GetCustomAttributes(typeof(IVRAttribute), false)[0] as IVRAttribute).Name + "\" ":"")+" ");
-
- foreach(PropertyInfo p in b.GetType().GetProperties())
- {
- if (p.GetCustomAttributes(typeof(IVRAttribute), false) == null || p.GetCustomAttributes(typeof(IVRAttribute), false).Count() == 0)
- continue;
- var ivratt = p.GetCustomAttributes(typeof(IVRAttribute), false).Last() as IVRAttribute;
- switch (ivratt.IVRSerializerType)
- {
- case IVRSerializerType.元素:
- if (p.PropertyType == typeof(IVRControlBase) && p.GetValue(b, null) != null)
- {
- disAtt.Add(ivratt.Number, ivratt.Name.Trim() + "=\"" + p.GetValue(b, null).GetType().GetProperty("ID").GetValue(p.GetValue(b, null), null) + "\"");
- }
- else if (p.PropertyType.IsEnum)
- {
- if (p.PropertyType == typeof(FinishKeyType) && p.PropertyType.GetField(p.GetValue(b, null).ToString()).GetCustomAttributes(false).Count() > 0)
- {
- var des = p.PropertyType.GetField(p.GetValue(b, null).ToString()).GetCustomAttributes(false).Last() as DescriptionAttribute;
- disAtt.Add(ivratt.Number,p.Name + "=\"" + des.Description + "\" ");
- }
- else
- disAtt.Add(ivratt.Number, ivratt.Name + "=\"" + (int)Enum.Parse(p.PropertyType, p.GetValue(b, null).ToString()) + "\" ");
- }
- else
- disAtt.Add(ivratt.Number, ivratt.Name.Trim() + "=\"" + p.GetValue(b, null) + "\"");
- break;
- case IVRSerializerType.节点:
- if (p.PropertyType.IsGenericType)
- {
- if (p.PropertyType == typeof(List<IVRDefinevarDefVar>))
- {
-
- var ivr = (List<IVRDefinevarDefVar>)p.GetValue(b, null);
- foreach (IVRDefinevarDefVar iv in ivr)
- {
- StringBuilder node = new StringBuilder();
- CreateXmlNode(p.Name, ref node, iv,false);
- nodes.Add(node);
- }
- }
- if (p.PropertyType == typeof(List<IVRDefaultVar>))
- {
-
- var ivr = (List<IVRDefaultVar>)p.GetValue(b, null);
- foreach (IVRDefaultVar iv in ivr)
- {
- StringBuilder node = new StringBuilder();
- CreateXmlNode(p.Name, ref node, iv,false);
- nodes.Add(node);
- }
- }
- if (p.PropertyType == typeof(List<IVRInputVar>))
- {
- var ivr = (List<IVRInputVar>)p.GetValue(b, null);
- foreach (IVRInputVar iv in ivr)
- {
- StringBuilder node = new StringBuilder();
- CreateXmlNode(p.Name, ref node, iv,false);
- nodes.Add(node);
- }
- }
- if (p.PropertyType == typeof(List<IVRBranchVar>))
- {
-
- var ivr = (List<IVRBranchVar>)p.GetValue(b, null);
- foreach (IVRBranchVar iv in ivr)
- {
- StringBuilder node = new StringBuilder();
- if (iv.Pos != "0")
- {
- CreateXmlNode(p.Name, ref node, iv,false);
- nodes.Add(node);
- }
- }
- }
-
- }
- break;
- }
- }
- foreach (int key in disAtt.Keys.OrderBy(a=>a))
- {
- if (key == 0)
- continue;
- xmlBuilder.Append(disAtt[key]+" ");
- }
- if (nodes.Count == 0)
- xmlBuilder.Append("/> \r\n");
- else
- {
- xmlBuilder.Append("> \r\n");
- foreach (StringBuilder s in nodes)
- xmlBuilder.Append(s.ToString());
- xmlBuilder.Append("</" + nodeName + ">");
- }
-
- return new MessageInfo() { State = true };
- }
- catch (Exception ex)
- {
- return new MessageInfo() { Code = -1, State = false, Message = "生成配置文件发生异常,错误信息:" + ex.Message };
- }
- }
- /// <summary>
- /// 解析XML文件到IVR流程设计器
- /// </summary>
- /// <param name="filePath">要打开的XML文件路径</param>
- /// <param name="val">返回解析后的对象</param>
- /// <returns></returns>
- public override Model.MessageInfo OpenFileInfo(string filePath, ref List<IVRFlowSerializerInfo> val)
- {
- try
- {
- Dictionary<string, string> lines = new Dictionary<string, string>();
- if (File.Exists(filePath))
- {
- using (StreamReader reader = new StreamReader(filePath))
- {
- XmlDocument doc = new XmlDocument();
- doc.InnerXml = reader.ReadToEnd();
- XmlNodeList flowNodes = doc.GetElementsByTagName("flow");
- foreach (XmlNode node in flowNodes)
- {
- //val.Add(new IVRFlowSerializerInfo() { IVR_Name = node.Attributes["Name"].Value, IVR_Text = node.Attributes["Name"].Value, IVRFlowType = (IVRFlowType)Enum.Parse(typeof(IVRFlowType), node.Attributes["Type"].Value), IVRControl = new List<IVRControlBase>(), LineNodes = new List<LineInfo>(), IVRVar=new List<IVRDefinevarDefVar>() });
- //2017-4-27
- val.Add(new IVRFlowSerializerInfo() { IVR_Name = node.Attributes["IVR_Name"].Value, IVR_Text = node.Attributes["IVR_Text"].Value, IVRFlowType = (IVRFlowType)Enum.Parse(typeof(IVRFlowType), node.Attributes["Type"].Value), IVRControl = new List<IVRControlBase>(), LineNodes = new List<LineInfo>(), IVRVar = new List<IVRDefinevarDefVar>() });
- XmlNodeList CellNode = node.SelectNodes("cell");
- foreach (XmlNode cell in CellNode)
- {
- var type = GlobalController.IVRControls[cell.Attributes["Name"].Value];
- val.Last().IVRControl.Add((IVRControlBase)Activator.CreateInstance(type, new object[] { cell.Attributes["Name"].Value }));
- val.Last().IVRControl.Last().IVR_Width = Int32.Parse(cell.Attributes["IVR_Width"].Value);
- val.Last().IVRControl.Last().IVR_Height = Int32.Parse(cell.Attributes["IVR_Height"].Value);
- val.Last().IVRControl.Last().Pos = cell.Attributes["Pos"].Value;
- val.Last().IVRControl.Last().CenterPoint = new Point(Int32.Parse(cell.Attributes["IVR_X"].Value), Int32.Parse(cell.Attributes["IVR_Y"].Value));
- val.Last().IVRControl.Last().SelectedImage = new Bitmap(@"pic\Selected.png");
- val.Last().IVRControl.Last().Image = new Bitmap(@"pic\NoSelect.png");
- foreach (PropertyInfo p in type.GetProperties())
- {
- if ((p.GetCustomAttributes(typeof(IVRAttribute), false)) == null || p.GetCustomAttributes(typeof(IVRAttribute), false).Count() == 0)
- continue;
- var att = p.GetCustomAttributes(typeof(IVRAttribute), false).Last() as IVRAttribute;
- switch (att.IVRSerializerType)
- {
- case IVRSerializerType.元素:
- if (p.Name == "Name") continue;
- if (p.PropertyType == typeof(IVRControlBase))
- {
- if (cell.Attributes[p.Name].Value != "0" && !string.IsNullOrEmpty(cell.Attributes[p.Name].Value))
- val.Last().LineNodes.Add(new LineInfo() { AttributeName = p.Name , StartNodeID = val.Last().IVRControl.Last().Pos, EndNodeID = cell.Attributes[p.Name].Value });
- }
- else if (p.PropertyType.IsEnum)
- {
- if (p.PropertyType == typeof(FinishKeyType))
- {
- foreach (MemberInfo finkey in p.PropertyType.GetMembers())
- {
- if (finkey.GetCustomAttributes(typeof(DescriptionAttribute), false) != null
- && finkey.GetCustomAttributes(typeof(DescriptionAttribute), false).Count() != 0
- && (finkey.GetCustomAttributes(typeof(DescriptionAttribute), false).Last() as DescriptionAttribute).Description == cell.Attributes[p.Name].Value)
- {
- p.SetValue(val.Last().IVRControl.Last(), (int)Enum.Parse(p.PropertyType, finkey.Name, false), null);
- }
- }
- }
- else
- {
- p.SetValue(val.Last().IVRControl.Last(), Enum.Parse(p.PropertyType, cell.Attributes[p.Name].Value), null);
- }
- }
- else
- {
- if (p.PropertyType == typeof(Int32) || p.PropertyType == typeof(int))
- p.SetValue(val.Last().IVRControl.Last(), Int32.Parse(cell.Attributes[p.Name].Value), null);
- else if (p.PropertyType == typeof(Boolean) || p.PropertyType == typeof(bool))
- //2017-4-28添加
- {
- bool bvalue = false;
- if (cell.Attributes[p.Name].Value.ToString() == "no")
- { bvalue = false; }
- else if (cell.Attributes[p.Name].Value.ToString() == "yes")
- { bvalue = true ; }
- else
- { bvalue = bool.Parse(cell.Attributes[p.Name].Value); }
- p.SetValue(val.Last().IVRControl.Last(), bvalue, null);//p.SetValue(val.Last().IVRControl.Last(), bool.Parse(cell.Attributes[p.Name].Value), null);//2017-4-28
- }
- else
- p.SetValue(val.Last().IVRControl.Last(), cell.Attributes[p.Name].Value.ToString(), null);
- }
- break;
- case IVRSerializerType.节点:
- foreach (XmlNode ivrflowNode in cell.ChildNodes)
- {
- switch (p.Name+" "+ivrflowNode.Name)
- {
- case "Branch Branch":
- ((IVRBranch)val.Last().IVRControl.Last()).Branch.Add(new IVRBranchVar() { Pos = ivrflowNode.Attributes["Pos"].Value, Value = ivrflowNode.Attributes["Value"].Value });
- val.Last().LineNodes.Add(new LineInfo() { AttributeName = ivrflowNode.Attributes["Value"].Value, StartNodeID = ((IVRBranch)val.Last().IVRControl.Last()).ID, EndNodeID = ivrflowNode.Attributes["Pos"].Value });
- break;
- case "DefVar DefVar":
- IVRDefinevarDefVar defval = (new IVRDefinevarDefVar() { VarName = ivrflowNode.Attributes["VarName"].Value, VarVal = ivrflowNode.Attributes["VarVal"].Value, VarType = (NumberType)Enum.Parse(typeof(NumberType), ivrflowNode.Attributes["VarType"].Value) });
- ((IVRDefinevar)val.Last().IVRControl.Last()).DefVar.Add(defval);
- val.Last().IVRVar.Add(defval);
- break;
- case "IVRInputVar IVRInputVar":
- ((IVRSocket)val.Last().IVRControl.Last()).IVRInputVar.Add(new IVRInputVar() { VarVal = ivrflowNode.Attributes["VarVal"].Value, VarType = (NumberType)Enum.Parse(typeof(NumberType), ivrflowNode.Attributes["VarType"].Value) });
- break;
- case "OutputVar OutputVar":
- ((IVRSocket)val.Last().IVRControl.Last()).OutputVar.Add(new IVRDefaultVar() { Var = ivrflowNode.Attributes["Var"].Value });
- break;
- case "Map Map":
- ((IVRRecordSet)val.Last().IVRControl.Last()).Map.Add(new IVRDefaultVar() { Var = ivrflowNode.Attributes["Var"].Value });
- break;
- default:
- break;
- }
- } break;
- }
- }
- }
- }
- return new MessageInfo() { Code = 0, Message = "解析完成!", State = true, };
- }
- }
- else
- {
- return new MessageInfo()
- {
- Code = -1,
- Message = "没有找到IVR图形文件!",
- State = false,
- };
- }
- }
- catch (Exception ex)
- {
- return new MessageInfo() { Code = -1, Message = "读取IVR图形文件发生异常,错误信息:" + ex.Message };
- }
-
- }
-
- }
- }
|