Пример #1
0
        /// <summary>
        /// 创建一个新的 MarcSubfield 节点对象,从当前对象复制出全部内容
        /// </summary>
        /// <returns>新的节点对象</returns>
        public override MarcNode clone()
        {
            MarcNode new_node = new MarcSubfield();

            new_node.Text   = this.Text;
            new_node.Parent = null; // 尚未和任何对象连接
            return(new_node);
        }
Пример #2
0
        static void ResetPunctuation(MarcSubfield subfield,
                                     string strPunctuation,
                                     string strTailPunctuation)
        {
            bool bTrimed = false;

            // 添加本身的符号
            if (string.IsNullOrEmpty(strPunctuation) == false)
            {
                string content = TrimStartEndChar(subfield.Content);

                bTrimed = true;

                if (strPunctuation.Length == 1)
                {
                    subfield.Content = strPunctuation + content;
                }
                else if (strPunctuation.Length == 2)
                {
                    subfield.Content = strPunctuation[0] + content + strPunctuation[1];
                }
            }

            // 添加尾部符号
            {
                string content = null;
                if (bTrimed == false)
                {
                    content = TrimEndChar(subfield.Content);
                }
                else
                {
                    content = subfield.Content;
                }

                if (string.IsNullOrEmpty(strTailPunctuation))
                {
                    subfield.Content = content;
                    return;
                }

                subfield.Content = content + AddBlank(strTailPunctuation);
                return;
            }
        }
Пример #3
0
 /// <summary>
 /// 创建一个新的 MarcSubfield 节点对象,从当前对象复制出全部内容
 /// </summary>
 /// <returns>新的节点对象</returns>
 public override MarcNode clone()
 {
     MarcNode new_node = new MarcSubfield();
     new_node.Text = this.Text;
     new_node.Parent = null; // 尚未和任何对象连接
     return new_node;
 }
Пример #4
0
        // fangbian diaoyong
        /// <summary>
        /// 在下级节点末尾追加一个子字段节点
        /// </summary>
        /// <param name="subfield">字段节点</param>
        public void add(MarcSubfield subfield)
        {
            this.ChildNodes.add(subfield);

        }
Пример #5
0
        public string GetMarc()
        {
            MarcRecord record = new MarcRecord(this._header);

            MarcField current_field_node = null;
            foreach (EasyLine line in this.Items)
            {
                if (line is FieldLine)
                {
                    FieldLine field = line as FieldLine;

                    MarcField field_node = null;

                    if (field.IsControlField == true)
                        field_node = new MarcField(field.Name, "", field.Content);
                    else
                        field_node = new MarcField(field.Name, field.Indicator, "");

                    record.ChildNodes.add(field_node);
                    current_field_node = field_node;
                }
                else if (line is SubfieldLine)
                {
                    SubfieldLine subfield = line as SubfieldLine;

                    MarcSubfield subfield_node = new MarcSubfield(subfield.Name, subfield.Content);
                    current_field_node.ChildNodes.add(subfield_node);
                }
            }

            return record.Text;
        }
Пример #6
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);
        }
Пример #7
0
        // 根据机内格式的片断字符串,构造若干MarcSubfield对象
        // parameters:
        //      parent      要赋给新创建的MarcSubfield对象的Parent值
        //      strLeadingString   [out] 第一个 31 字符以前的文本部分
        /// <summary>
        /// 根据机内格式 MARC 字符串,创建若干 MarcSubfield 对象
        /// </summary>
        /// <param name="strText">MARC 机内格式字符串</param>
        /// <param name="strLeadingString">返回前导部分字符串。也就是 strText 中第一个子字段符号以前的部分</param>
        /// <returns>新创建的 MarcSubfield 对象集合</returns>
        public static MarcNodeList createSubfields(
            string strText,
            out string strLeadingString)
        {
            strLeadingString = "";
            MarcNodeList results = new MarcNodeList();

            List <string> segments = new List <string>();
            StringBuilder prefix   = new StringBuilder(); // 第一个 31 出现以前的一段文字
            StringBuilder segment  = new StringBuilder(); // 正在追加的内容段落

            for (int i = 0; i < strText.Length; i++)
            {
                char ch = strText[i];
                if (ch == 31)
                {
                    // 如果先前有累积的,推走
                    if (segment.Length > 0)
                    {
                        segments.Add(segment.ToString());
                        segment.Clear();
                    }

                    segment.Append(ch);
                }
                else
                {
                    if (segment.Length > 0 || segments.Count > 0)
                    {
                        segment.Append(ch);
                    }
                    else
                    {
                        prefix.Append(ch);// 第一个子字段符号以前的内容放在这里
                    }
                }
            }

            if (segment.Length > 0)
            {
                segments.Add(segment.ToString());
                segment.Clear();
            }

            if (prefix.Length > 0)
            {
                strLeadingString = prefix.ToString();
            }

            foreach (string s in segments)
            {
                MarcSubfield subfield = new MarcSubfield();
                if (s.Length < 2)
                {
                    subfield.Text = MarcQuery.SUBFLD + "?";  // TODO: 或者可以忽略?
                }
                else
                {
                    subfield.Text = s;
                }
                results.add(subfield);
                // Debug.Assert(subfield.Parent == parent, "");
            }

            return(results);
        }
Пример #8
0
        // 给 MARC21 记录中 245 字段内的子字段添加正确的标点符号
        /// <summary>
        /// 给 MARC21 记录中 245 字段内的子字段添加正确的标点符号
        /// </summary>
        /// <param name="field">MARC 字段</param>
        /// <param name="strStyle">风格</param>
        /// <returns>记录是否发生了修改</returns>
        public static bool PunctuationMarc21Field245(MarcField field, string strStyle)
        {
            List <SubfieldInfo> infos = new List <SubfieldInfo>();

            foreach (MarcSubfield subfield in field.ChildNodes)
            {
                SubfieldInfo info = new SubfieldInfo();
                info.Subfield = subfield;
                infos.Add(info);
            }

            // 添加标点符号
            // $a题名 (不可重复)
            // $b题名其余部分 空: (少数情况;)
            // $c责任者说明  空/
            // $f首尾日期 ,
            // $g主体日期(集中出版日期)  自己()
            // $h载体 自己[]
            // $k资料形式 空:
            // $n著作分卷/分节号 .
            // $p著作分卷/分节题名 (在$a$b或者另一个$p后,就是 .。跟在 $n 后面就是 ,)
            // $s版本 .
            // 最后一个子字段尾部要加入一个 .
            {
                MarcSubfield prev_field = null;
                foreach (SubfieldInfo info in infos)
                {
                    switch (info.Subfield.Name)
                    {
                    case "a":
                        if (prev_field != null)
                        {
                            info.LeadingPunctuation = " ";
                        }
                        break;

                    case "b":
                        // 注:如果前一个子字段末尾是 ';',则用 ';'
                        info.LeadingPunctuation = ":";
                        break;

                    case "c":
                        info.LeadingPunctuation = "/";
                        break;

                    case "f":
                        info.LeadingPunctuation = ",";
                        break;

                    case "g":
                        info.Punctuation = "()";
                        break;

                    case "h":
                        info.Punctuation = "[]";
                        break;

                    case "k":
                        info.LeadingPunctuation = ":";
                        break;

                    case "n":
                        info.LeadingPunctuation = ".";
                        break;

                    case "p":
                        if (prev_field != null && prev_field.Name == "n")
                        {
                            info.LeadingPunctuation = ",";
                        }
                        else
                        {
                            info.LeadingPunctuation = ".";
                        }
                        break;

                    case "s":
                        info.LeadingPunctuation = ".";
                        break;
                    }

                    prev_field = info.Subfield;
                }
            }

            string strOld = field.Content;

            {
                SubfieldInfo prev_info = null;
                foreach (SubfieldInfo info in infos)
                {
                    if (prev_info != null)
                    {
                        ResetPunctuation(prev_info.Subfield, prev_info.Punctuation, info.LeadingPunctuation);
                    }

                    prev_info = info;
                }

                // 最后一个子字段加上尾部符号 .
                if (string.IsNullOrEmpty(prev_info.Subfield.Content) == true ||
                    prev_info.Subfield.Content[prev_info.Subfield.Content.Length - 1] != '.')
                {
                    prev_info.Subfield.Content += ".";
                }
            }

            if (strOld != field.Content)
            {
                return(true);
            }

            return(false);
        }
Пример #9
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;
        }
Пример #10
0
        // 根据机内格式的片断字符串,构造若干MarcSubfield对象
        // parameters:
        //      parent      要赋给新创建的MarcSubfield对象的Parent值
        //      strLeadingString   [out] 第一个 31 字符以前的文本部分
        /// <summary>
        /// 根据机内格式 MARC 字符串,创建若干 MarcSubfield 对象
        /// </summary>
        /// <param name="strText">MARC 机内格式字符串</param>
        /// <param name="strLeadingString">返回前导部分字符串。也就是 strText 中第一个子字段符号以前的部分</param>
        /// <returns>新创建的 MarcSubfield 对象集合</returns>
        public static MarcNodeList createSubfields(
            string strText,
            out string strLeadingString)
        {
            strLeadingString = "";
            MarcNodeList results = new MarcNodeList();

            List<string> segments = new List<string>();
            StringBuilder prefix = new StringBuilder(); // 第一个 31 出现以前的一段文字
            StringBuilder segment = new StringBuilder(); // 正在追加的内容段落

            for (int i = 0; i < strText.Length; i++)
            {
                char ch = strText[i];
                if (ch == 31)
                {
                    // 如果先前有累积的,推走
                    if (segment.Length > 0)
                    {
                        segments.Add(segment.ToString());
                        segment.Clear();
                    }

                    segment.Append(ch);
                }
                else
                {
                    if (segment.Length > 0 || segments.Count > 0)
                        segment.Append(ch);
                    else
                        prefix.Append(ch);// 第一个子字段符号以前的内容放在这里
                }
            }

            if (segment.Length > 0)
            {
                segments.Add(segment.ToString());
                segment.Clear();
            }

            if (prefix.Length > 0)
                strLeadingString = prefix.ToString();

            foreach (string s in segments)
            {
                MarcSubfield subfield = new MarcSubfield();
                if (s.Length < 2)
                    subfield.Text = MarcQuery.SUBFLD + "?";  // TODO: 或者可以忽略?
                else
                    subfield.Text = s;
                results.add(subfield);
                // Debug.Assert(subfield.Parent == parent, "");
            }

            return results;
        }
Пример #11
0
 // fangbian diaoyong
 /// <summary>
 /// 在下级节点末尾追加一个子字段节点
 /// </summary>
 /// <param name="subfield">字段节点</param>
 public void add(MarcSubfield subfield)
 {
     this.ChildNodes.add(subfield);
 }