コード例 #1
0
ファイル: ISO2709StatisForm.cs プロジェクト: zgren/dp2
        // 注意:上级函数RunScript()已经使用了BeginLoop()和EnableControls()
        // 对每个Iso2709Statis记录进行循环
        // return:
        //      0   普通返回
        //      1   要全部中断
        int DoLoop(out string strError)
        {
            strError = "";
            // int nRet = 0;
            // long lRet = 0;
            Encoding encoding = null;

            if (string.IsNullOrEmpty(this._openMarcFileDialog.EncodingName) == true)
            {
                strError = "尚未选定 ISO2709 文件的编码方式";
                return(-1);
            }

            if (StringUtil.IsNumber(this._openMarcFileDialog.EncodingName) == true)
            {
                encoding = Encoding.GetEncoding(Convert.ToInt32(this._openMarcFileDialog.EncodingName));
            }
            else
            {
                encoding = Encoding.GetEncoding(this._openMarcFileDialog.EncodingName);
            }

#if NO
            // 清除错误信息窗口中残余的内容
            if (this.ErrorInfoForm != null)
            {
                try
                {
                    this.ErrorInfoForm.HtmlString = "<pre>";
                }
                catch
                {
                }
            }
#endif
            ClearErrorInfoForm();

            string strInputFileName = "";

            try
            {
                strInputFileName = this._openMarcFileDialog.FileName;

                Stream file = null;

                try
                {
                    file = File.Open(strInputFileName,
                                     FileMode.Open,
                                     FileAccess.Read);
                }
                catch (Exception ex)
                {
                    strError = "打开文件 " + strInputFileName + " 失败: " + ex.Message;
                    return(-1);
                }

                this.progressBar_records.Minimum = 0;
                this.progressBar_records.Maximum = (int)file.Length;
                this.progressBar_records.Value   = 0;

                /*
                 * stop.OnStop += new StopEventHandler(this.DoStop);
                 * stop.Initial("正在获取ISO2709记录 ...");
                 * stop.BeginLoop();
                 *
                 * EnableControls(false);
                 * */

                try
                {
                    int nCount = 0;

                    for (int i = 0; ; i++)
                    {
                        Application.DoEvents(); // 出让界面控制权

                        if (stop != null)
                        {
                            if (stop.State != 0)
                            {
                                DialogResult result = MessageBox.Show(this,
                                                                      "准备中断。\r\n\r\n确实要中断全部操作? (Yes 全部中断;No 中断循环,但是继续收尾处理;Cancel 放弃中断,继续操作)",
                                                                      "Iso2709StatisForm",
                                                                      MessageBoxButtons.YesNoCancel,
                                                                      MessageBoxIcon.Question,
                                                                      MessageBoxDefaultButton.Button3);

                                if (result == DialogResult.Yes)
                                {
                                    strError = "用户中断";
                                    return(-1);
                                }
                                if (result == DialogResult.No)
                                {
                                    return(0);   // 假装loop正常结束
                                }
                                stop.Continue(); // 继续循环
                            }
                        }

                        // 从ISO2709文件中读入一条MARC记录
                        // return:
                        //	-2	MARC格式错
                        //	-1	出错
                        //	0	正确
                        //	1	结束(当前返回的记录有效)
                        //	2	结束(当前返回的记录无效)
                        int nRet = MarcUtil.ReadMarcRecord(file,
                                                           encoding,
                                                           true, // bRemoveEndCrLf,
                                                           true, // bForce,
                                                           out string strMARC,
                                                           out strError);
                        if (nRet == -2 || nRet == -1)
                        {
                            DialogResult result = MessageBox.Show(this,
                                                                  "读入MARC记录(" + nCount.ToString() + ")出错: " + strError + "\r\n\r\n确实要中断当前批处理操作?",
                                                                  "Iso2709StatisForm",
                                                                  MessageBoxButtons.YesNo,
                                                                  MessageBoxIcon.Question,
                                                                  MessageBoxDefaultButton.Button2);
                            if (result == DialogResult.Yes)
                            {
                                break;
                            }
                            else
                            {
                                strError = "读入MARC记录(" + nCount.ToString() + ")出错: " + strError;
                                GetErrorInfoForm().WriteHtml(strError + "\r\n");
                                continue;
                            }
                        }

                        if (nRet != 0 && nRet != 1)
                        {
                            return(0);   // 结束
                        }
                        stop.SetMessage("正在获取第 " + (i + 1).ToString() + " 个 ISO2709 记录");
                        this.progressBar_records.Value = (int)file.Position;

                        // 跳过太短的记录
                        if (string.IsNullOrEmpty(strMARC) == true ||
                            strMARC.Length <= 24)
                        {
                            continue;
                        }

                        if (this._openMarcFileDialog.Mode880 == true &&
                            (this._openMarcFileDialog.MarcSyntax == "usmarc" || this._openMarcFileDialog.MarcSyntax == "<自动>"))
                        {
                            MarcRecord temp = new MarcRecord(strMARC);
                            MarcQuery.ToParallel(temp);
                            strMARC = temp.Text;
                        }

                        // 触发Script中OnRecord()代码
                        if (objStatis != null)
                        {
                            objStatis.MARC = strMARC;
                            objStatis.CurrentRecordIndex = i;

                            StatisEventArgs args = new StatisEventArgs();
                            objStatis.OnRecord(this, args);
                            if (args.Continue == ContinueType.SkipAll)
                            {
                                return(1);
                            }
                            if (args.Continue == ContinueType.Error)
                            {
                                strError = args.ParamString;
                                return(-1);
                            }
                        }

                        nCount++;
                    }

                    /*
                     * Global.WriteHtml(this.webBrowser_batchAddItemPrice,
                     *  "处理结束。共增补价格字符串 " + nCount.ToString() + " 个。\r\n");
                     * */

                    return(0);
                }
                finally
                {
                    /*
                     * EnableControls(true);
                     *
                     * stop.EndLoop();
                     * stop.OnStop -= new StopEventHandler(this.DoStop);
                     * stop.Initial("");
                     * */

                    if (file != null)
                    {
                        file.Close();
                    }
                }
            }
            finally
            {
            }

            // return 0;
        }
コード例 #2
0
        // 注意:上级函数RunScript()已经使用了BeginLoop()和EnableControls()
        // 对每个XML记录进行循环
        // return:
        //      0   普通返回
        //      1   要全部中断
        int DoLoop(out string strError)
        {
            strError = "";
            // int nRet = 0;
            // long lRet = 0;

#if NO
            // 清除错误信息窗口中残余的内容
            if (this.ErrorInfoForm != null)
            {
                try
                {
                    this.ErrorInfoForm.HtmlString = "<pre>";
                }
                catch
                {
                }
            }
#endif
            // 清除错误信息窗口中残余的内容
            ClearErrorInfoForm();

            string strInputFileName = "";

            try
            {
                strInputFileName = this.textBox_inputXmlFilename.Text;

                Stream file = null;

                try
                {
                    file = File.Open(strInputFileName,
                                     FileMode.Open,
                                     FileAccess.Read);
                }
                catch (Exception ex)
                {
                    strError = "打开文件 " + strInputFileName + " 失败: " + ex.Message;
                    return(-1);
                }

                XmlTextReader reader = new XmlTextReader(file);

                this.progressBar_records.Minimum = 0;
                this.progressBar_records.Maximum = (int)file.Length;
                this.progressBar_records.Value   = 0;

                /*
                 * stop.OnStop += new StopEventHandler(this.DoStop);
                 * stop.Initial("正在获取XML记录 ...");
                 * stop.BeginLoop();
                 *
                 * EnableControls(false);
                 * */

                bool bRet = false;

                while (true)
                {
                    bRet = reader.Read();
                    if (bRet == false)
                    {
                        strError = "没有根元素";
                        return(-1);
                    }
                    if (reader.NodeType == XmlNodeType.Element)
                    {
                        break;
                    }
                }

                try
                {
                    int nCount = 0;

                    for (int i = 0; ; i++)
                    {
                        Application.DoEvents(); // 出让界面控制权

                        if (stop != null)
                        {
                            if (stop.State != 0)
                            {
                                DialogResult result = MessageBox.Show(this,
                                                                      "准备中断。\r\n\r\n确实要中断全部操作? (Yes 全部中断;No 中断循环,但是继续收尾处理;Cancel 放弃中断,继续操作)",
                                                                      "ReaderStatisForm",
                                                                      MessageBoxButtons.YesNoCancel,
                                                                      MessageBoxIcon.Question,
                                                                      MessageBoxDefaultButton.Button3);

                                if (result == DialogResult.Yes)
                                {
                                    strError = "用户中断";
                                    return(-1);
                                }
                                if (result == DialogResult.No)
                                {
                                    return(0);   // 假装loop正常结束
                                }
                                stop.Continue(); // 继续循环
                            }
                        }


                        while (true)
                        {
                            bRet = reader.Read();
                            if (bRet == false)
                            {
                                return(0);
                            }
                            if (reader.NodeType == XmlNodeType.Element)
                            {
                                break;
                            }
                        }

                        if (bRet == false)
                        {
                            return(0);   // 结束
                        }
                        string strXml = reader.ReadOuterXml();

                        stop.SetMessage("正在获取第 " + (i + 1).ToString() + " 个XML记录");
                        this.progressBar_records.Value = (int)file.Position;

                        // strXml中为XML记录
                        XmlDocument dom = new XmlDocument();
                        try
                        {
                            dom.LoadXml(strXml);
                        }
                        catch (Exception ex)
                        {
                            strError = "XML记录装入DOM发生错误: " + ex.Message;
                            GetErrorInfoForm().WriteHtml(strError + "\r\n");
                            continue;
                        }

                        // 触发Script中OnRecord()代码
                        if (objStatis != null)
                        {
                            objStatis.Xml                = strXml;
                            objStatis.RecordDom          = dom;
                            objStatis.CurrentRecordIndex = i;

                            StatisEventArgs args = new StatisEventArgs();
                            objStatis.OnRecord(this, args);
                            if (args.Continue == ContinueType.SkipAll)
                            {
                                return(1);
                            }
                        }

                        nCount++;
                    }

                    /*
                     * Global.WriteHtml(this.webBrowser_batchAddItemPrice,
                     *  "处理结束。共增补价格字符串 " + nCount.ToString() + " 个。\r\n");
                     * */
                }
                finally
                {
                    /*
                     * EnableControls(true);
                     *
                     * stop.EndLoop();
                     * stop.OnStop -= new StopEventHandler(this.DoStop);
                     * stop.Initial("");
                     * */

                    if (file != null)
                    {
                        file.Close();
                    }
                }
            }
            finally
            {
            }

            // return 0;
        }