コード例 #1
0
ファイル: VolumeInfo.cs プロジェクト: renyh1013/dp2
        /*
        // 构造表达一个册所在的当年期号、总期号、卷号的字符串
        public static string BuildItemVolumeString(string strIssue,
            string strZong,
            string strVolume)
        {
            string strResult = "";
            if (String.IsNullOrEmpty(strIssue) == false)
                strResult += "no." + strIssue;

            if (String.IsNullOrEmpty(strZong) == false)
            {
                if (strResult != "")
                    strResult += ", ";
                strResult += "总." + strZong;
            }

            if (String.IsNullOrEmpty(strVolume) == false)
            {
                if (strResult != "")
                    strResult += ", ";
                strResult += "v." + strVolume;
            }

            return strResult;
        }
         * */

        // 解析no.序列
        public static int ExpandNoString(string strText,
            string strDefaultYear,
            out List<VolumeInfo> infos,
            out string strError)
        {
            strError = "";
            infos = new List<VolumeInfo>();

            string strCurrentYear = strDefaultYear;

            string[] no_parts = strText.Split(new char[] { ',', ':', ';', ',', ':', ';' });    // ':' ';' 是为了兼容某个阶段的临时用法 2001:no.1-2;2002:no.1-12
            for (int i = 0; i < no_parts.Length; i++)
            {
                string strPart = no_parts[i].Trim();
                if (String.IsNullOrEmpty(strPart) == true)
                    continue;
                if (StringUtil.IsNumber(strPart) == true
                    && strPart.Length == 4)
                {
                    strCurrentYear = strPart;
                    continue;
                }

                // 去掉"no."部分
                if (StringUtil.HasHead(strPart, "no.") == true)
                {
                    strPart = strPart.Substring(3).Trim();
                }

                // TODO: 没有"no."开头的,是否警告?

                if (String.IsNullOrEmpty(strPart) == true)
                    continue;

                List<string> nos = null;

                try
                {
                    nos = ExpandSequence(strPart);
                }
                catch (Exception ex)
                {
                    strError = "序列 '" + strPart + "' 格式错误:" + ex.Message;
                    return -1;
                }

                for (int j = 0; j < nos.Count; j++)
                {
                    string strNo = nos[j];

                    if (String.IsNullOrEmpty(strCurrentYear) == true)
                    {
                        strError = "当遇到 '" + strNo + "' 的时候,没有必要的年份信息,无法解析no.序列信息";
                        return -1;
                    }

                    VolumeInfo info = new VolumeInfo();
                    info.IssueNo = strNo;
                    info.Year = strCurrentYear;
                    infos.Add(info);
                }

            }

            return 0;
        }
コード例 #2
0
        /*
         * // 构造表达一个册所在的当年期号、总期号、卷号的字符串
         * public static string BuildItemVolumeString(string strIssue,
         *  string strZong,
         *  string strVolume)
         * {
         *  string strResult = "";
         *  if (String.IsNullOrEmpty(strIssue) == false)
         *      strResult += "no." + strIssue;
         *
         *  if (String.IsNullOrEmpty(strZong) == false)
         *  {
         *      if (strResult != "")
         *          strResult += ", ";
         *      strResult += "总." + strZong;
         *  }
         *
         *  if (String.IsNullOrEmpty(strVolume) == false)
         *  {
         *      if (strResult != "")
         *          strResult += ", ";
         *      strResult += "v." + strVolume;
         *  }
         *
         *  return strResult;
         * }
         * */

        // 解析no.序列
        public static int ExpandNoString(string strText,
                                         string strDefaultYear,
                                         out List <VolumeInfo> infos,
                                         out string strError)
        {
            strError = "";
            infos    = new List <VolumeInfo>();

            string strCurrentYear = strDefaultYear;

            string[] no_parts = strText.Split(new char[] { ',', ':', ';', ',', ':', ';' });    // ':' ';' 是为了兼容某个阶段的临时用法 2001:no.1-2;2002:no.1-12
            for (int i = 0; i < no_parts.Length; i++)
            {
                string strPart = no_parts[i].Trim();
                if (String.IsNullOrEmpty(strPart) == true)
                {
                    continue;
                }
                if (StringUtil.IsNumber(strPart) == true &&
                    strPart.Length == 4)
                {
                    strCurrentYear = strPart;
                    continue;
                }

                // 去掉"no."部分
                if (StringUtil.HasHead(strPart, "no.") == true)
                {
                    strPart = strPart.Substring(3).Trim();
                }

                // TODO: 没有"no."开头的,是否警告?

                if (String.IsNullOrEmpty(strPart) == true)
                {
                    continue;
                }

                List <string> nos = null;

                try
                {
                    nos = ExpandSequence(strPart);
                }
                catch (Exception ex)
                {
                    strError = "序列 '" + strPart + "' 格式错误:" + ex.Message;
                    return(-1);
                }

                for (int j = 0; j < nos.Count; j++)
                {
                    string strNo = nos[j];

                    if (String.IsNullOrEmpty(strCurrentYear) == true)
                    {
                        strError = "当遇到 '" + strNo + "' 的时候,没有必要的年份信息,无法解析no.序列信息";
                        return(-1);
                    }

                    VolumeInfo info = new VolumeInfo();
                    info.IssueNo = strNo;
                    info.Year    = strCurrentYear;
                    infos.Add(info);
                }
            }

            return(0);
        }
コード例 #3
0
        // 2016/10/6
        // 从实体记录中获得 定位期 的检索式字符串
        // 如果是合订册,则可能返回多个字符串。普通册返回(最多)一个字符串
        // return:
        //      空集合
        //      其他
        public static List <IssueString> GetIssueQueryStringFromItemXml(XmlDocument dom)
        {
            if (dom == null || dom.DocumentElement == null)
            {
                return(new List <IssueString>());
            }

            // 看看记录中是否有 binding/item 元素

            /*
             * - <binding>
             * <item publishTime="20160101" volume="2016,no.1, 总.100, v.10" refID="1e651c7d-bcce-442a-b574-ceee7b4b81e0" price="CNY12" />
             * <item publishTime="20160201" volume="2016,no.2, 总.101, v.10" refID="64e4cc27-36df-42ce-a90b-9d4cfa6631dd" price="CNY12" />
             * <item publishTime="20160301" volume="2016,no.3, 总.102, v.10" refID="574b3639-a033-4bbe-8ac3-804251cb13de" price="CNY12" />
             * <item publishTime="20160401" volume="2016,no.4, 总.103, v.10" refID="" missing="true" />
             * <item publishTime="20160501" volume="2016,no.5, 总.104, v.10" refID="9d0a2975-2c88-4f29-9b62-2f864a850259" price="CNY12" />
             * </binding>
             *
             * */
            bool bAttr = false;  // 是否用属性方式存储字段
            List <XmlElement> items = new List <XmlElement>();
            XmlNodeList       nodes = dom.DocumentElement.SelectNodes("binding/item");

            if (nodes.Count == 0)
            {
                items.Add(dom.DocumentElement);
                bAttr = false;
            }
            else
            {
                foreach (XmlElement item in nodes)
                {
                    items.Add(item);
                }
                bAttr = true;
            }

            List <IssueString> results = new List <IssueString>();

            foreach (XmlElement item in items)
            {
                string strVolumeString = GetField(item, "volume", bAttr);

                if (string.IsNullOrEmpty(strVolumeString) == true)
                {
                    continue;
                }

                string strYear   = "";
                string strIssue  = "";
                string strZong   = "";
                string strVolume = "";

                // 解析当年期号、总期号、卷号的字符串
                VolumeInfo.ParseItemVolumeString(strVolumeString,
                                                 out strYear,
                                                 out strIssue,
                                                 out strZong,
                                                 out strVolume);

                // 若 volume 元素中不包含年份,则从 publishTime 元素中取
                if (string.IsNullOrEmpty(strYear))
                {
                    string strPublishTime = GetField(item, "publishTime", bAttr);
                    strYear = dp2StringUtil.GetYearPart(strPublishTime);
                }

                results.Add(new IssueString(strVolumeString, strYear + "|" + strIssue + "|" + strZong + "|" + strVolume));
            }

            return(results);
        }
コード例 #4
0
        // 解析卷期范围序列。例如“2001,no.1-12=总.101-112=v.25*12”
        public static int BuildVolumeInfos(string strText,
                                           out List <VolumeInfo> infos,
                                           out string strError)
        {
            int nRet = 0;

            strError = "";
            infos    = new List <VolumeInfo>();

            string strYearString   = "";
            string strNoString     = "";
            string strVolumeString = "";
            string strZongString   = "";

            List <string> notdef_segments = new List <string>();

            string[] segments = strText.Split(new char[] { '=' });
            for (int i = 0; i < segments.Length; i++)
            {
                string strSegment = segments[i].Trim();
                if (String.IsNullOrEmpty(strSegment) == true)
                {
                    continue;
                }
                if (strSegment.IndexOf("y.") != -1)
                {
                    strYearString = strSegment;
                }
                else if (strSegment.IndexOf("no.") != -1)
                {
                    strNoString = strSegment;
                }
                else if (strSegment.IndexOf("v.") != -1)
                {
                    strVolumeString = strSegment;
                }
                else if (strSegment.IndexOf("总.") != -1)
                {
                    strZongString = strSegment;
                }
                else
                {
                    notdef_segments.Add(strSegment);
                }
            }

            // 2012/4/25
            // 当年期号序列很重要,如果缺了,光有总期号和卷号是不行的
            if (string.IsNullOrEmpty(strNoString) == true &&
                (string.IsNullOrEmpty(strZongString) == false || string.IsNullOrEmpty(strVolumeString) == false))
            {
                strError = "当年期号序列不能省却。'" + strText + "'";
                if (notdef_segments.Count > 0)
                {
                    strError += "。字符串中出现了无法识别的序列: " + StringUtil.MakePathList(notdef_segments, "=");
                }
                return(-1);
            }

            if (String.IsNullOrEmpty(strNoString) == false)
            {
                // 去掉"y."部分
                if (StringUtil.HasHead(strYearString, "y.") == true)
                {
                    strYearString = strYearString.Substring(2).Trim();
                }

                // 解析no.序列
                nRet = ExpandNoString(strNoString,
                                      strYearString,
                                      out infos,
                                      out strError);
                if (nRet == -1)
                {
                    strError = "解析序列 '" + strNoString + "' (年份'" + strYearString + "')时发生错误: " + strError;
                    return(-1);
                }
            }

            // 去掉"总."部分
            if (StringUtil.HasHead(strZongString, "总.") == true)
            {
                strZongString = strZongString.Substring(2).Trim();
            }

            if (String.IsNullOrEmpty(strZongString) == false)
            {
                List <string> zongs = null;

                try
                {
                    zongs = ExpandSequence(strZongString);
                }
                catch (Exception ex)
                {
                    strError = "总. 序列 '" + strZongString + "' 格式错误:" + ex.Message;
                    return(-1);
                }

                for (int i = 0; i < infos.Count; i++)
                {
                    VolumeInfo info = infos[i];
                    if (i < zongs.Count)
                    {
                        info.Zong = zongs[i];
                    }
                    else
                    {
                        break;
                    }
                }
            }

            // 去掉"v."部分
            if (StringUtil.HasHead(strVolumeString, "v.") == true)
            {
                strVolumeString = strVolumeString.Substring(2).Trim();
            }

            if (String.IsNullOrEmpty(strVolumeString) == false)
            {
                List <string> volumes = null;

                try
                {
                    volumes = ExpandSequence(strVolumeString);
                }
                catch (Exception ex)
                {
                    strError = "v.序列 '" + strVolumeString + "' 格式错误:" + ex.Message;
                    return(-1);
                }

                string strLastValue = "";
                for (int i = 0; i < infos.Count; i++)
                {
                    VolumeInfo info = infos[i];
                    if (i < volumes.Count)
                    {
                        info.Volume  = volumes[i];
                        strLastValue = info.Volume; // 记忆最后一个
                    }
                    else
                    {
                        info.Volume = strLastValue; // 沿用最后一个
                    }
                }
            }

            // 2015/5/8 如果 strText 内容为“绿笔采风”之类的,就无法分析出部件
            if (infos.Count == 0 && notdef_segments.Count > 0)
            {
                strError += "卷期范围字符串中出现了无法识别的序列: " + StringUtil.MakePathList(notdef_segments, "=");
                return(-1);
            }

            return(0);
        }