Exemplo n.º 1
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);
        }