int NewHostObject( out MarcDetailHost hostObj, out string strError) { strError = ""; hostObj = null; Type entryClassType = ScriptManager.GetDerivedClassType( this.m_autogenDataAssembly, "dp2Catalog.MarcDetailHost"); if (entryClassType == null) { strError = "dp2Catalog.MarcDetailHost的派生类都没有找到"; return -1; } // new一个MarcDetailHost派生对象 hostObj = (MarcDetailHost)entryClassType.InvokeMember(null, BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.CreateInstance, null, null, null); if (hostObj == null) { strError = "new MarcDetailHost的派生类对象时失败"; return -1; } // 为DetailHost派生类设置参数 hostObj.DetailForm = this; hostObj.Assembly = this.m_autogenDataAssembly; return 0; }
/// <summary> /// 为 MARC 编辑器内的记录加拼音 /// </summary> /// <param name="strCfgXml">拼音配置 XML</param> /// <param name="bUseCache">是否使用记录中以前缓存的结果?</param> /// <param name="style">风格</param> /// <param name="strPrefix">前缀字符串。缺省为空 [暂时没有使用本参数]</param> /// <param name="strDuoyinStyle">是否自动选择多音字。auto/first 之一或者组合</param> /// <returns>-1: 出错。包括中断的情况; 0: 正常</returns> public virtual int AddPinyin(string strCfgXml, bool bUseCache, // = true, PinyinStyle style, // = PinyinStyle.None, string strPrefix, string strDuoyinStyle) // bool bAutoSel = false) { string strError = ""; XmlDocument cfg_dom = new XmlDocument(); try { cfg_dom.LoadXml(strCfgXml); } catch (Exception ex) { strError = "strCfgXml装载到XMLDOM时出错: " + ex.Message; goto ERROR1; } this.DetailForm.MarcEditor.Enabled = false; Hashtable old_selected = (bUseCache == true) ? this.DetailForm.GetSelectedPinyin() : new Hashtable(); Hashtable new_selected = new Hashtable(); try { // PinyinStyle style = PinyinStyle.None; // 在这里修改拼音大小写风格 for (int i = 0; i < DetailForm.MarcEditor.Record.Fields.Count; i++) { Field field = DetailForm.MarcEditor.Record.Fields[i]; List <PinyinCfgItem> cfg_items = null; int nRet = GetPinyinCfgLine( cfg_dom, field.Name, field.Indicator, out cfg_items); if (nRet <= 0) { continue; } string strHanzi = ""; string strNextSubfieldName = ""; string strField = field.Text; foreach (PinyinCfgItem item in cfg_items) { for (int k = 0; k < item.From.Length; k++) { if (item.From.Length != item.To.Length) { strError = "配置事项 fieldname='" + item.FieldName + "' from='" + item.From + "' to='" + item.To + "' 其中from和to参数值的字符数不等"; goto ERROR1; } string from = new string(item.From[k], 1); string to = new string(item.To[k], 1); for (int j = 0; ; j++) { // return: // -1 error // 0 not found // 1 found nRet = MarcUtil.GetSubfield(strField, ItemType.Field, from, j, out strHanzi, out strNextSubfieldName); if (nRet != 1) { break; } if (strHanzi.Length <= 1) { break; } strHanzi = strHanzi.Substring(1); // 2013/6/13 if (MarcDetailHost.ContainHanzi(strHanzi) == false) { continue; } string strPinyin = ""; strPinyin = (string)old_selected[strHanzi]; if (string.IsNullOrEmpty(strPinyin) == true) { // 把字符串中的汉字和拼音分离 // return: // -1 出错 // 0 用户希望中断 // 1 正常 if (string.IsNullOrEmpty(this.DetailForm.MainForm.PinyinServerUrl) == true || this.DetailForm.MainForm.ForceUseLocalPinyinFunc == true) { nRet = this.DetailForm.MainForm.HanziTextToPinyin( this.DetailForm, true, // 本地,快速 strHanzi, style, strDuoyinStyle, out strPinyin, out strError); } else { // 汉字字符串转换为拼音 // 如果函数中已经MessageBox报错,则strError第一字符会为空格 // return: // -1 出错 // 0 用户希望中断 // 1 正常 // 2 结果字符串中有没有找到拼音的汉字 nRet = this.DetailForm.MainForm.SmartHanziTextToPinyin( this.DetailForm, strHanzi, style, strDuoyinStyle, out strPinyin, out strError); } if (nRet == -1) { new_selected = null; goto ERROR1; } if (nRet == 0) { new_selected = null; strError = "用户中断。拼音子字段内容可能不完整。"; goto ERROR1; } } if (new_selected != null && nRet != 2) { new_selected[strHanzi] = strPinyin; } nRet = MarcUtil.DeleteSubfield( ref strField, to, j); nRet = MarcUtil.InsertSubfield( ref strField, from, j, new string(MarcUtil.SUBFLD, 1) + to + strPinyin, 1); field.Text = strField; } } } } if (new_selected != null) { this.DetailForm.SetSelectedPinyin(new_selected); } } finally { this.DetailForm.MarcEditor.Enabled = true; this.DetailForm.MarcEditor.Focus(); } return(0); ERROR1: if (string.IsNullOrEmpty(strError) == false) { if (strError[0] != ' ') { MessageBox.Show(this.DetailForm, strError); } } return(-1); }
// 初始化 dp2catalog_marc_autogen.cs 的 Assembly,并new MarcDetailHost对象 // return: // -2 清除了Assembly // -1 error // 0 没有重新初始化Assembly,而是直接用以前Cache的Assembly (可能本来就是空) // 1 重新(或者首次)初始化了Assembly public int InitialAutogenAssembly(out string strError) { strError = ""; int nRet = 0; if (m_nDisableInitialAssembly > 0) return 0; bool bAssemblyReloaded = false; string strAutogenDataCfgFilename = ""; string strAutogenDataCfgRefFilename = ""; string strProtocol = ""; dp2SearchForm dp2_searchform = null; if (String.IsNullOrEmpty(this.SavePath) == true) { if (this.CurrentRecord == null) { this.m_autogenDataAssembly = null; this.m_detailHostObj = null; strError = "SavePath和CurrentRecord都为空,Assembly被清除"; return -2; } string strCfgFileName = "dp2catalog_marc_autogen.cs"; string strMarcSyntaxOID = ""; strMarcSyntaxOID = this.GetCurrentMarcSyntaxOID(out strError); if (String.IsNullOrEmpty(strMarcSyntaxOID) == true) { strError = "因为: " + strError + ",无法获得配置文件 '" + strCfgFileName + "'"; goto ERROR1; } strAutogenDataCfgFilename = this.MainForm.DataDir + "\\" + strMarcSyntaxOID.Replace(".", "_") + "\\" + strCfgFileName; strAutogenDataCfgRefFilename = this.MainForm.DataDir + "\\" + strMarcSyntaxOID.Replace(".", "_") + "\\" + strCfgFileName + ".ref"; strProtocol = "localfile"; goto BEGIN; } string strPath = ""; nRet = Global.ParsePath(this.SavePath, out strProtocol, out strPath, out strError); if (nRet == -1) goto ERROR1; // dtlp协议的自动创建数据 if (strProtocol.ToLower() == "dtlp") { // TODO: 建立数据库名映射到MarcSyntac的机制 /* strError = "暂不支持来自DTLP协议的数据自动创建功能"; goto ERROR1; * */ if (this.CurrentRecord == null) { this.m_autogenDataAssembly = null; this.m_detailHostObj = null; strError = "CurrentRecord为空,Assembly被清除"; return -2; } string strCfgFileName = "dp2catalog_marc_autogen.cs"; string strMarcSyntaxOID = ""; strMarcSyntaxOID = this.GetCurrentMarcSyntaxOID(out strError); if (String.IsNullOrEmpty(strMarcSyntaxOID) == true) { strError = "因为: " + strError + ",无法获得配置文件 '" + strCfgFileName + "'"; goto ERROR1; } strAutogenDataCfgFilename = this.MainForm.DataDir + "\\" + strMarcSyntaxOID.Replace(".", "_") + "\\" + strCfgFileName; strAutogenDataCfgRefFilename = this.MainForm.DataDir + "\\" + strMarcSyntaxOID.Replace(".", "_") + "\\" + strCfgFileName + ".ref"; strProtocol = "localfile"; goto BEGIN; } // dp2library协议的自动创建数据 else if (strProtocol.ToLower() == "dp2library") { dp2_searchform = this.GetDp2SearchForm(); if (dp2_searchform == null) { strError = "没有连接的或者打开的dp2检索窗,无法进行数据创建"; goto ERROR1; } string strServerName = ""; string strLocalPath = ""; // 解析记录路径。 // 记录路径为如下形态 "中文图书/1 @服务器" dp2SearchForm.ParseRecPath(strPath, out strServerName, out strLocalPath); string strCfgFileName = "dp2catalog_marc_autogen.cs"; if (string.Compare(strServerName, "mem", true) == 0 || string.Compare(strServerName, "file", true) == 0) { string strMarcSyntaxOID = ""; strMarcSyntaxOID = this.GetCurrentMarcSyntaxOID(out strError); if (String.IsNullOrEmpty(strMarcSyntaxOID) == true) { strError = "因为: " + strError + ",无法获得配置文件 '" + strCfgFileName + "'"; goto ERROR1; } strAutogenDataCfgFilename = this.MainForm.DataDir + "\\" + strMarcSyntaxOID.Replace(".", "_") + "\\" + strCfgFileName; strAutogenDataCfgRefFilename = this.MainForm.DataDir + "\\" + strMarcSyntaxOID.Replace(".", "_") + "\\" + strCfgFileName + ".ref"; strProtocol = "localfile"; goto BEGIN; } string strBiblioDbName = dp2SearchForm.GetDbName(strLocalPath); strAutogenDataCfgFilename = strBiblioDbName + "/cfgs/" + strCfgFileName + "@" + strServerName; strAutogenDataCfgRefFilename = strBiblioDbName + "/cfgs/" + strCfgFileName + ".ref@" + strServerName; } // amazon 协议的自动创建数据 else if (strProtocol.ToLower() == "amazon") { if (this.CurrentRecord == null) { this.m_autogenDataAssembly = null; this.m_detailHostObj = null; strError = "CurrentRecord为空,Assembly被清除"; return -2; } string strCfgFileName = "dp2catalog_marc_autogen.cs"; string strMarcSyntaxOID = ""; strMarcSyntaxOID = this.GetCurrentMarcSyntaxOID(out strError); if (String.IsNullOrEmpty(strMarcSyntaxOID) == true) { strError = "因为: " + strError + ",无法获得配置文件 '" + strCfgFileName + "'"; goto ERROR1; } strAutogenDataCfgFilename = this.MainForm.DataDir + "\\" + strMarcSyntaxOID.Replace(".", "_") + "\\" + strCfgFileName; strAutogenDataCfgRefFilename = this.MainForm.DataDir + "\\" + strMarcSyntaxOID.Replace(".", "_") + "\\" + strCfgFileName + ".ref"; strProtocol = "localfile"; goto BEGIN; } else { strError = "暂不支持来自 '"+strProtocol+"' 协议的数据自动创建功能"; goto ERROR1; } BEGIN: // 如果必要,重新准备Assembly if (m_autogenDataAssembly == null || m_strAutogenDataCfgFilename != strAutogenDataCfgFilename) { this.m_autogenDataAssembly = this.MainForm.AssemblyCache.FindObject(strAutogenDataCfgFilename); this.m_detailHostObj = null; // 如果Cache中没有现成的Assembly if (this.m_autogenDataAssembly == null) { string strCode = ""; string strRef = ""; byte[] baCfgOutputTimestamp = null; if (strProtocol.ToLower() == "dp2library") { // return: // -1 error // 0 not found // 1 found nRet = dp2_searchform.GetCfgFile(strAutogenDataCfgFilename, out strCode, out baCfgOutputTimestamp, out strError); if (nRet == -1 || nRet == 0) goto ERROR1; nRet = dp2_searchform.GetCfgFile(strAutogenDataCfgRefFilename, out strRef, out baCfgOutputTimestamp, out strError); if (nRet == -1) goto ERROR1; } else if (strProtocol.ToLower() == "localfile") { if (File.Exists(strAutogenDataCfgFilename) == false) { /* if (bOnlyFillMenu == true) return; * */ strError = "配置文件 '" + strAutogenDataCfgFilename + "' 不存在..."; goto ERROR1; } if (File.Exists(strAutogenDataCfgRefFilename) == false) { strError = "配置文件 '" + strAutogenDataCfgRefFilename + "' 不存在(但是其配套的.cs文件已经存在)..."; goto ERROR1; } try { Encoding encoding = FileUtil.DetectTextFileEncoding(strAutogenDataCfgFilename); using (StreamReader sr = new StreamReader(strAutogenDataCfgFilename, encoding)) { strCode = sr.ReadToEnd(); } encoding = FileUtil.DetectTextFileEncoding(strAutogenDataCfgRefFilename); using (StreamReader sr = new StreamReader(strAutogenDataCfgRefFilename, encoding)) { strRef = sr.ReadToEnd(); } } catch (Exception ex) { strError = ex.Message; goto ERROR1; } } try { // 准备Assembly Assembly assembly = null; nRet = GetCsScriptAssembly( strCode, strRef, out assembly, out strError); if (nRet == -1) { strError = "编译脚本文件 '" + strAutogenDataCfgFilename + "' 时出错:" + strError; goto ERROR1; } // 记忆到缓存 this.MainForm.AssemblyCache.SetObject(strAutogenDataCfgFilename, assembly); this.m_autogenDataAssembly = assembly; bAssemblyReloaded = true; } catch (Exception ex) { strError = "准备脚本代码过程中发生异常: \r\n" + ExceptionUtil.GetDebugText(ex); goto ERROR1; } } bAssemblyReloaded = true; m_strAutogenDataCfgFilename = strAutogenDataCfgFilename; // 至此,Assembly已经纯备好了 Debug.Assert(this.m_autogenDataAssembly != null, ""); } Debug.Assert(this.m_autogenDataAssembly != null, ""); // 准备 host 对象 if (this.m_detailHostObj == null || bAssemblyReloaded == true) { try { MarcDetailHost host = null; nRet = NewHostObject( out host, out strError); if (nRet == -1) { strError = "执行脚本文件 '" + m_strAutogenDataCfgFilename + "' 时出错:" + strError; goto ERROR1; } this.m_detailHostObj = host; } catch (Exception ex) { strError = "准备脚本代码过程中发生异常: \r\n" + ExceptionUtil.GetDebugText(ex); goto ERROR1; } } Debug.Assert(this.m_detailHostObj != null, ""); if (bAssemblyReloaded == true) return 1; return 0; ERROR1: return -1; }