public int LinkMarcFile() { OpenMarcFileDlg dlg = new OpenMarcFileDlg(); GuiUtil.SetControlFont(dlg, this.Font); dlg.IsOutput = false; dlg.GetEncoding -= new GetEncodingEventHandler(dlg_GetEncoding); dlg.GetEncoding += new GetEncodingEventHandler(dlg_GetEncoding); dlg.FileName = this.MainForm.LinkedMarcFileName; // dlg.CrLf = MainForm.LastCrLfIso2709; dlg.EncodingListItems = Global.GetEncodingList(true); // dlg.EncodingName = ""; GetEncodingForm.GetEncodingName(preferredEncoding); // dlg.EncodingComment = "注: 原始编码方式为 " + GetEncodingForm.GetEncodingName(preferredEncoding); // dlg.MarcSyntax = "<自动>"; // strPreferedMarcSyntax; dlg.EnableMarcSyntax = true; if (String.IsNullOrEmpty(this.MainForm.LinkedEncodingName) == false) dlg.EncodingName = this.MainForm.LinkedEncodingName; if (String.IsNullOrEmpty(this.MainForm.LinkedMarcSyntax) == false) dlg.MarcSyntax = this.MainForm.LinkedMarcSyntax; this.MainForm.AppInfo.LinkFormState(dlg, "OpenMarcFileDlg_state"); dlg.ShowDialog(this); if (dlg.DialogResult != DialogResult.OK) return 0; // 储存用过的文件名 // 2009/9/21 this.MainForm.LinkedMarcFileName = dlg.FileName; this.MainForm.LinkedEncodingName = dlg.EncodingName; this.MainForm.LinkedMarcSyntax = dlg.MarcSyntax; string strError = ""; this.linkMarcFile = new LinkMarcFile(); int nRet = this.linkMarcFile.Open(dlg.FileName, out strError); if (nRet == -1) goto ERROR1; this.linkMarcFile.Encoding = dlg.Encoding; this.linkMarcFile.MarcSyntax = dlg.MarcSyntax; string strMarc = ""; byte[] baRecord = null; // 2 结束(当前返回的记录无效) nRet = this.linkMarcFile.NextRecord(out strMarc, out baRecord, out strError); if (nRet == -1) goto ERROR1; // 2013/5/26 if (nRet == 2) goto ERROR1; if (this.linkMarcFile.MarcSyntax == "<自动>" || this.linkMarcFile.MarcSyntax.ToLower() == "<auto>") { // 自动识别MARC格式 string strOutMarcSyntax = ""; // 探测记录的MARC格式 unimarc / usmarc / reader // return: // 0 没有探测出来。strMarcSyntax为空 // 1 探测出来了 nRet = MarcUtil.DetectMarcSyntax(strMarc, out strOutMarcSyntax); this.linkMarcFile.MarcSyntax = strOutMarcSyntax; // 有可能为空,表示探测不出来 if (String.IsNullOrEmpty(this.linkMarcFile.MarcSyntax) == true) { MessageBox.Show(this, "软件无法确定此MARC文件的MARC格式"); } } if (dlg.Mode880 == true && linkMarcFile.MarcSyntax == "usmarc") { MarcRecord temp = new MarcRecord(strMarc); MarcQuery.ToParallel(temp); strMarc = temp.Text; } LoadLinkedMarcRecord(strMarc, baRecord); return 0; ERROR1: MessageBox.Show(this, strError); return -1; }
// 从 MARC 文件中导入 void menu_importFromMarcFile_Click(object sender, EventArgs e) { OpenMarcFileDlg dlg = new OpenMarcFileDlg(); GuiUtil.SetControlFont(dlg, this.Font); dlg.IsOutput = false; dlg.GetEncoding -= new GetEncodingEventHandler(dlg_GetEncoding); dlg.GetEncoding += new GetEncodingEventHandler(dlg_GetEncoding); dlg.FileName = this.MainForm.LinkedMarcFileName; // dlg.CrLf = MainForm.LastCrLfIso2709; dlg.EncodingListItems = Global.GetEncodingList(true); // dlg.EncodingName = ""; GetEncodingForm.GetEncodingName(preferredEncoding); // dlg.EncodingComment = "注: 原始编码方式为 " + GetEncodingForm.GetEncodingName(preferredEncoding); // dlg.MarcSyntax = "<自动>"; // strPreferedMarcSyntax; dlg.EnableMarcSyntax = true; if (String.IsNullOrEmpty(this.MainForm.LinkedEncodingName) == false) dlg.EncodingName = this.MainForm.LinkedEncodingName; if (String.IsNullOrEmpty(this.MainForm.LinkedMarcSyntax) == false) dlg.MarcSyntax = this.MainForm.LinkedMarcSyntax; dlg.ShowDialog(this); if (dlg.DialogResult != DialogResult.OK) return; // 储存用过的文件名 // 2009/9/21 this.MainForm.LinkedMarcFileName = dlg.FileName; this.MainForm.LinkedEncodingName = dlg.EncodingName; this.MainForm.LinkedMarcSyntax = dlg.MarcSyntax; string strError = ""; _linkMarcFile = new LinkMarcFile(); int nRet = _linkMarcFile.Open(dlg.FileName, out strError); if (nRet == -1) goto ERROR1; _linkMarcFile.Encoding = dlg.Encoding; _linkMarcFile.MarcSyntax = dlg.MarcSyntax; ClearListViewItems(); stop.Style = StopStyle.EnableHalfStop; stop.OnStop += new StopEventHandler(this.DoStop); stop.Initial("正在从 MARC 文件导入 ..."); stop.BeginLoop(); this.listView_browse.BeginUpdate(); try { ListViewUtil.ClearSortColumns(this.listView_browse); stop.SetProgressRange(0, _linkMarcFile.Stream.Length); bool bEOF = false; for (int i = 0; bEOF == false; i++) { Application.DoEvents(); // 出让界面控制权 if (stop != null && stop.State != 0) { strError = "用户中断"; goto ERROR1; } string strMARC = ""; byte[] baRecord = null; // 获得下一条记录 // return: // -1 error // 0 succeed // 1 reach end(当前返回的记录有效) // 2 结束(当前返回的记录无效) nRet = _linkMarcFile.NextRecord(out strMARC, out baRecord, out strError); if (nRet == -1) goto ERROR1; if (nRet == 1) bEOF = true; if (nRet == 2) break; if (_linkMarcFile.MarcSyntax == "<自动>" || _linkMarcFile.MarcSyntax.ToLower() == "<auto>") { // 自动识别MARC格式 string strOutMarcSyntax = ""; // 探测记录的MARC格式 unimarc / usmarc / reader // return: // 0 没有探测出来。strMarcSyntax为空 // 1 探测出来了 nRet = MarcUtil.DetectMarcSyntax(strMARC, out strOutMarcSyntax); _linkMarcFile.MarcSyntax = strOutMarcSyntax; // 有可能为空,表示探测不出来 if (String.IsNullOrEmpty(_linkMarcFile.MarcSyntax) == true) { MessageBox.Show(this, "软件无法确定此 MARC 文件的 MARC 格式"); } } if (dlg.Mode880 == true && _linkMarcFile.MarcSyntax == "usmarc") { MarcRecord temp = new MarcRecord(strMARC); MarcQuery.ToParallel(temp); strMARC = temp.Text; } string strXml = ""; nRet = MarcUtil.Marc2XmlEx(strMARC, _linkMarcFile.MarcSyntax, ref strXml, out strError); if (nRet == -1) goto ERROR1; string strRecPath = i.ToString() + " @file"; BiblioInfo info = new BiblioInfo(); info.RecPath = strRecPath; this.m_biblioTable[strRecPath] = info; info.OldXml = strXml; info.Timestamp = null; info.RecPath = strRecPath; string strSytaxOID = ""; if (_linkMarcFile.MarcSyntax == "unimarc") strSytaxOID = "1.2.840.10003.5.1"; // unimarc else if (_linkMarcFile.MarcSyntax == "usmarc") strSytaxOID = "1.2.840.10003.5.10"; // usmarc string strBrowseText = ""; if (strSytaxOID == "1.2.840.10003.5.1" // unimarc || strSytaxOID == "1.2.840.10003.5.10") // usmarc { nRet = BuildMarcBrowseText( strSytaxOID, strMARC, out strBrowseText, out strError); if (nRet == -1) strBrowseText = strError; } string[] cols = strBrowseText.Split(new char[] { '\t' }); // 创建浏览行 NewLine( this.listView_browse, strRecPath, cols); stop.SetMessage(i.ToString()); stop.SetProgressValue(_linkMarcFile.Stream.Position); } } finally { this.listView_browse.EndUpdate(); stop.EndLoop(); stop.OnStop -= new StopEventHandler(this.DoStop); stop.Initial(""); stop.HideProgress(); stop.Style = StopStyle.None; _linkMarcFile.Close(); } return; ERROR1: MessageBox.Show(this, strError); _linkMarcFile = null; }