Exemplo n.º 1
0
        /// <summary>
        /// 创建平行字段
        /// </summary>
        /// <param name="field">要创建平行字段的,包含汉字字符串的源字段</param>
        /// <param name="bTo880">将 field 字段名转为 880</param>
        /// <param name="getPinyin">获得拼音的函数地址</param>
        /// <param name="strError">返回出错信息</param>
        /// <returns>-1: 出错; 0: 成功; 1: field 不是中文的字段($6表示),无法创建平行字段</returns>
        public static int CreateParallelField(MarcField field,
                                              bool bTo880,
                                              Delegate_getPinyin getPinyin,
                                              out string strError)
        {
            strError = "";

            if (field.ChildNodes.count == 0)
            {
                return(1);
            }

            MarcRecord record = (MarcRecord)field.Parent;

            MarcField main_field = null;

            string strFieldName   = "";
            string strNumber      = "";
            string strScriptId    = "";
            string strOrientation = "";

            // 观察平行字段是否已经存在?
            string content_6 = field.select("subfield[@name='6']").FirstContent;

            if (string.IsNullOrEmpty(content_6) == false)
            {
                // 拆解 $6 内容
                _parseSubfield6(content_6,
                                out strFieldName,
                                out strNumber,
                                out strScriptId,
                                out strOrientation);
                if (string.IsNullOrEmpty(strScriptId) == true)
                {
                    strError = "field 的 $6 表明不是中文的字段,无法创建平行字段";
                    return(1);
                }

                // 找到关联的字段
                main_field = _findField(record,
                                        strFieldName,
                                        field.Name,
                                        strNumber);
            }

            bool bNewField = false;

            if (main_field == null)
            {
                string strMainFieldName = field.Name;
                if (field.Name == "880")
                {
                    // 只能靠 $6 中 linking tag
                    if (string.IsNullOrEmpty(strFieldName) == true)
                    {
                        strError = "当前字段为 880 字段,但没有 $6 子字段,无法获得对应的字段名,因此函数调用失败";
                        return(-1);
                    }
                    strMainFieldName = strFieldName;
                }
                main_field = new MarcField(strMainFieldName,
                                           field.Indicator,
                                           "");
                if (field.Name != "880")
                {
                    field.before(main_field);
                }
                else
                {
                    record.ChildNodes.insertSequence(main_field,
                                                     InsertSequenceStyle.PreferTail);
                }
                bNewField = true;
            }
            else
            {
                // 内容全部删除。只是占用原有位置
                main_field.Content   = "";
                main_field.Indicator = field.Indicator;
            }

            if (string.IsNullOrEmpty(strNumber) == true)
            {
                strNumber = _getNewNumber(record);
            }
            {
                // $6
                MarcSubfield subfield_6 = new MarcSubfield("6",
                                                           _buildSubfield6(bTo880 == true ? "880" : field.Name,
                                                                           strNumber, "", strOrientation)
                                                           );
                main_field.ChildNodes.add(subfield_6);
            }

            int nHanziCount = 0;

            List <MarcSubfield> remove_subfields = new List <MarcSubfield>();

            // 其余子字段逐个加入
            foreach (MarcSubfield subfield in field.ChildNodes)
            {
                if (subfield.Name == "6")
                {
                    continue;
                }
                // 2014/10/20
                if (subfield.Name == "9" ||
                    char.IsUpper(subfield.Name[0]) == true)
                {
                    remove_subfields.Add(subfield);
                    continue;
                }

                string strPinyin = getPinyin(subfield.Content, out strError);
                if (strPinyin == null)
                {
                    strError = "创建拼音的过程出错: " + strError;
                    return(-1);
                }
                if (strPinyin != subfield.Content)
                {
                    nHanziCount++;
                }
                main_field.ChildNodes.add(new MarcSubfield(subfield.Name, strPinyin));
            }

            // 2014/10/20
            if (remove_subfields.Count > 0)
            {
                foreach (MarcSubfield subfield in remove_subfields)
                {
                    subfield.detach();
                }
            }

            if (nHanziCount == 0 && bNewField == true)
            {
                main_field.detach();
                return(1);
            }

            // 当前字段加入 $6
            {
                if (string.IsNullOrEmpty(strScriptId) == true)
                {
                    strScriptId = "$1";
                }

                MarcSubfield subfield_6 = null;
                MarcNodeList temp       = field.select("subfield[@name='6']");
                if (temp.count > 0)
                {
                    subfield_6         = temp[0] as MarcSubfield;
                    subfield_6.Content = _buildSubfield6(main_field.Name, strNumber, strScriptId, strOrientation);
                }
                else
                {
                    subfield_6 = new MarcSubfield("6",
                                                  _buildSubfield6(main_field.Name, strNumber, strScriptId, strOrientation)
                                                  );
                    field.ChildNodes.insert(0, subfield_6);
                }
            }

            if (bTo880)
            {
                field.Name = "880";
            }
            return(0);
        }
Exemplo n.º 2
0
        /// <summary>
        /// 创建平行字段
        /// </summary>
        /// <param name="field">要创建平行字段的,包含汉字字符串的源字段</param>
        /// <param name="bTo880">将 field 字段名转为 880</param>
        /// <param name="getPinyin">获得拼音的函数地址</param>
        /// <param name="strError">返回出错信息</param>
        /// <returns>-1: 出错; 0: 成功; 1: field 不是中文的字段($6表示),无法创建平行字段</returns>
        public static int CreateParallelField(MarcField field,
            bool bTo880,
            Delegate_getPinyin getPinyin,
            out string strError)
        {
            strError = "";

            if (field.ChildNodes.count == 0)
                return 1;

            MarcRecord record = (MarcRecord)field.Parent;

            MarcField main_field = null;

            string strFieldName = "";
            string strNumber = "";
            string strScriptId = "";
            string strOrientation = "";

            // 观察平行字段是否已经存在?
            string content_6 = field.select("subfield[@name='6']").FirstContent;
            if (string.IsNullOrEmpty(content_6) == false)
            {
                // 拆解 $6 内容
                _parseSubfield6(content_6,
            out strFieldName,
            out strNumber,
            out strScriptId,
            out strOrientation);
                if (string.IsNullOrEmpty(strScriptId) == true)
                {
                    strError = "field 的 $6 表明不是中文的字段,无法创建平行字段";
                    return 1;
                }

                // 找到关联的字段
                main_field = _findField(record,
                    strFieldName,
                    field.Name,
                    strNumber);
            }

            bool bNewField = false;
            if (main_field == null)
            {
                string strMainFieldName = field.Name;
                if (field.Name == "880")
                {
                    // 只能靠 $6 中 linking tag
                    if (string.IsNullOrEmpty(strFieldName) == true)
                    {
                        strError = "当前字段为 880 字段,但没有 $6 子字段,无法获得对应的字段名,因此函数调用失败";
                        return -1;
                    }
                    strMainFieldName = strFieldName;
                }
                main_field = new MarcField(strMainFieldName, 
                    field.Indicator,
                    "");
                if (field.Name != "880") 
                    field.before(main_field);
                else
                    record.ChildNodes.insertSequence(main_field,
            InsertSequenceStyle.PreferTail); 
                bNewField = true;
            }
            else
            {
                // 内容全部删除。只是占用原有位置
                main_field.Content = "";
                main_field.Indicator = field.Indicator;
            }

            if (string.IsNullOrEmpty(strNumber) == true)
                strNumber = _getNewNumber(record);
            {
                // $6
                MarcSubfield subfield_6 = new MarcSubfield("6",
                    _buildSubfield6(bTo880 == true? "880" : field.Name,
                    strNumber, "", strOrientation)
                );
                main_field.ChildNodes.add(subfield_6);
            }

            int nHanziCount = 0;

            List<MarcSubfield> remove_subfields = new List<MarcSubfield>();

            // 其余子字段逐个加入
            foreach (MarcSubfield subfield in field.ChildNodes)
            {
                if (subfield.Name == "6")
                    continue;
                // 2014/10/20
                if (subfield.Name == "9"
                    || char.IsUpper(subfield.Name[0]) == true)
                {
                    remove_subfields.Add(subfield);
                    continue;
                }

                string strPinyin = getPinyin(subfield.Content, out strError);
                if (strPinyin == null)
                {
                    strError = "创建拼音的过程出错: " + strError;
                    return -1;
                }
                if (strPinyin != subfield.Content)
                    nHanziCount++;
                main_field.ChildNodes.add(new MarcSubfield(subfield.Name, strPinyin));
            }

            // 2014/10/20
            if (remove_subfields.Count > 0)
            {
                foreach (MarcSubfield subfield in remove_subfields)
                {
                    subfield.detach();
                }
            }

            if (nHanziCount == 0 && bNewField == true)
            {
                main_field.detach();
                return 1;
            }

            // 当前字段加入 $6
            {
                if (string.IsNullOrEmpty(strScriptId) == true)
                    strScriptId = "$1";

                MarcSubfield subfield_6 = null;
                MarcNodeList temp = field.select("subfield[@name='6']");
                if (temp.count > 0)
                {
                    subfield_6 = temp[0] as MarcSubfield;
                    subfield_6.Content = _buildSubfield6(main_field.Name, strNumber, strScriptId, strOrientation);
                }
                else
                {
                    subfield_6 = new MarcSubfield("6",
                        _buildSubfield6(main_field.Name, strNumber, strScriptId, strOrientation)
                    );
                    field.ChildNodes.insert(0, subfield_6);
                }
            }

            if (bTo880)
            {
                field.Name = "880";
            }
            return 0;
        }