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; }
public void menuItem_saveOriginRecordToIso2709_Click(object sender, EventArgs e) { string strError = ""; int nRet = 0; if (this.listView_browse.SelectedIndices.Count == 0) { strError = "尚未选定要保存记录的浏览行"; goto ERROR1; } bool bForceFull = false; if (HasSelectionContainBriefRecords() == true) { DialogResult result = MessageBox.Show(this, "即将保存的记录中有Brief(简要)格式的记录,是否在保存前重新获取为Full(完整)格式的记录?\r\n\r\n(Yes: 是,要完整格式的记录; No: 否,依然保存简明格式的记录; Cancel: 取消,放弃整个保存操作", "AmazonSearchForm", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1); if (result == System.Windows.Forms.DialogResult.Cancel) return; if (result == System.Windows.Forms.DialogResult.Yes) bForceFull = true; } Encoding preferredEncoding = Encoding.UTF8; #if NO { // 观察要保存的第一条记录的marc syntax int first_index = this.listView_browse.SelectedIndices[0]; ListViewItem first_item = this.listView_browse.Items[first_index]; preferredEncoding = connection.GetRecordsEncoding( this.m_mainForm, first_record.m_strSyntaxOID); } #endif OpenMarcFileDlg dlg = new OpenMarcFileDlg(); GuiUtil.SetControlFont(dlg, this.Font); dlg.IsOutput = true; dlg.GetEncoding -= new GetEncodingEventHandler(dlg_GetEncoding); dlg.GetEncoding += new GetEncodingEventHandler(dlg_GetEncoding); dlg.FileName = m_mainForm.LastIso2709FileName; dlg.CrLf = m_mainForm.LastCrLfIso2709; dlg.RemoveField998Visible = false; //dlg.RemoveField998 = m_mainForm.LastRemoveField998; dlg.EncodingListItems = Global.GetEncodingList(true); dlg.EncodingName = (String.IsNullOrEmpty(m_mainForm.LastEncodingName) == true ? GetEncodingForm.GetEncodingName(preferredEncoding) : m_mainForm.LastEncodingName); dlg.EncodingComment = "注: 原始编码方式为 " + GetEncodingForm.GetEncodingName(preferredEncoding); dlg.MarcSyntax = "<自动>"; // strPreferedMarcSyntax; dlg.EnableMarcSyntax = false; dlg.ShowDialog(this); if (dlg.DialogResult != DialogResult.OK) return; Encoding targetEncoding = null; if (dlg.EncodingName == "MARC-8") { strError = "暂不能使用这个编码方式保存记录。"; goto ERROR1; } nRet = this.m_mainForm.GetEncoding(dlg.EncodingName, out targetEncoding, out strError); if (nRet == -1) { goto ERROR1; } string strLastFileName = m_mainForm.LastIso2709FileName; string strLastEncodingName = m_mainForm.LastEncodingName; bool bExist = File.Exists(dlg.FileName); bool bAppend = false; if (bExist == true) { DialogResult result = MessageBox.Show(this, "文件 '" + dlg.FileName + "' 已存在,是否以追加方式写入记录?\r\n\r\n--------------------\r\n注:(是)追加 (否)覆盖 (取消)放弃", "AmazonSearchForm", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1); if (result == DialogResult.Yes) bAppend = true; if (result == DialogResult.No) bAppend = false; if (result == DialogResult.Cancel) { strError = "放弃处理..."; goto ERROR1; } } // 检查同一个文件连续存时候的编码方式一致性 if (strLastFileName == dlg.FileName && bAppend == true) { if (strLastEncodingName != "" && strLastEncodingName != dlg.EncodingName) { DialogResult result = MessageBox.Show(this, "文件 '" + dlg.FileName + "' 已在先前已经用 " + strLastEncodingName + " 编码方式存储了记录,现在又以不同的编码方式 " + dlg.EncodingName + " 追加记录,这样会造成同一文件中存在不同编码方式的记录,可能会令它无法被正确读取。\r\n\r\n是否继续? (是)追加 (否)放弃操作", "AmazonSearchForm", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2); if (result == DialogResult.No) { strError = "放弃处理..."; goto ERROR1; } } } m_mainForm.LastIso2709FileName = dlg.FileName; m_mainForm.LastCrLfIso2709 = dlg.CrLf; m_mainForm.LastEncodingName = dlg.EncodingName; // m_mainForm.LastRemoveField998 = dlg.RemoveField998; Stream s = null; try { s = File.Open(m_mainForm.LastIso2709FileName, FileMode.OpenOrCreate); if (bAppend == false) s.SetLength(0); else s.Seek(0, SeekOrigin.End); } catch (Exception ex) { strError = "打开或创建文件 " + m_mainForm.LastIso2709FileName + " 失败,原因: " + ex.Message; goto ERROR1; } try { // 首先获得详细记录 if (bForceFull == true) { ReloadFullElementSet(); bool bError = WaitSearchFinish(); } for (int i = 0; i < this.listView_browse.SelectedIndices.Count; i++) { int index = this.listView_browse.SelectedIndices[i]; byte[] baTimestamp = null; string strSavePath = ""; string strOutStyle = ""; LoginInfo logininfo = null; long lVersion = 0; string strXmlFragment = ""; DigitalPlatform.Z3950.Record record = null; Encoding currentEncoding = null; string strMARC = ""; nRet = this.GetOneRecord( "marc", index, // 即将废止 "index:" + index.ToString(), bForceFull == true ? "force_full" : "", // false, out strSavePath, out strMARC, out strXmlFragment, out strOutStyle, out baTimestamp, out lVersion, out record, out currentEncoding, out logininfo, out strError); if (nRet == -1) goto ERROR1; byte[] baTarget = null; string strMarcSyntax = ""; if (record.m_strSyntaxOID == "1.2.840.10003.5.1") strMarcSyntax = "unimarc"; if (record.m_strSyntaxOID == "1.2.840.10003.5.10") strMarcSyntax = "usmarc"; #if NO if (dlg.RemoveField998 == true) { MarcRecord temp = new MarcRecord(strMARC); temp.select("field[@name='998']").detach(); strMARC = temp.Text; } #endif if (dlg.Mode880 == true && strMarcSyntax == "usmarc") { MarcRecord temp = new MarcRecord(strMARC); MarcQuery.To880(temp); strMARC = temp.Text; } // 将MARC机内格式转换为ISO2709格式 // parameters: // strMarcSyntax "unimarc" "usmarc" // strSourceMARC [in]机内格式MARC记录。 // targetEncoding [in]输出ISO2709的编码方式为 UTF8 codepage-936等等 // baResult [out]输出的ISO2709记录。字符集受nCharset参数控制。 // 注意,缓冲区末尾不包含0字符。 nRet = MarcUtil.CvtJineiToISO2709( strMARC, strMarcSyntax, targetEncoding, out baTarget, out strError); if (nRet == -1) goto ERROR1; s.Write(baTarget, 0, baTarget.Length); if (dlg.CrLf == true) { byte[] baCrLf = targetEncoding.GetBytes("\r\n"); s.Write(baCrLf, 0, baCrLf.Length); } } // if (bAppend == true) m_mainForm.MessageText = this.listView_browse.SelectedIndices.Count.ToString() + "条记录成功追加到文件 " + m_mainForm.LastIso2709FileName + " 尾部"; else m_mainForm.MessageText = this.listView_browse.SelectedIndices.Count.ToString() + "条记录成功保存到新文件 " + m_mainForm.LastIso2709FileName + " 尾部"; } catch (Exception ex) { strError = "写入文件 " + m_mainForm.LastIso2709FileName + " 失败,原因: " + ex.Message; goto ERROR1; } finally { s.Close(); } return; ERROR1: MessageBox.Show(this, strError); }
// 输出 void DoExport(string strProjectName, string strProjectLocate) { string strError = ""; int nRet = 0; Assembly assemblyMain = null; MyFilterDocument filter = null; batchObj = null; m_nRecordCount = -1; // 准备脚本 if (strProjectName != "" && strProjectName != null) { nRet = PrepareScript(strProjectName, strProjectLocate, out assemblyMain, out filter, out batchObj, out strError); if (nRet == -1) goto ERROR1; this.AssemblyMain = assemblyMain; if (filter != null) this.AssemblyFilter = filter.Assembly; else this.AssemblyFilter = null; } // 执行脚本的OnInitial() // 触发Script中OnInitial()代码 // OnInitial()和OnBegin的本质区别, 在于OnInitial()适合检查和设置面板参数 if (batchObj != null) { BatchEventArgs args = new BatchEventArgs(); batchObj.OnInitial(this, args); /* if (args.Continue == ContinueType.SkipBeginMiddle) goto END1; if (args.Continue == ContinueType.SkipMiddle) { strError = "OnInitial()中args.Continue不能使用ContinueType.SkipMiddle.应使用ContinueType.SkipBeginMiddle"; goto ERROR1; } */ if (args.Continue == ContinueType.SkipAll) goto END1; } string strOutputFileName = ""; if (textBox_dbPath.Text == "") { MessageBox.Show(this, "尚未选择源库..."); return; } string[] dbpaths = this.textBox_dbPath.Text.Split(new char[] { ';' }); Debug.Assert(dbpaths.Length != 0, ""); // 如果为单库输出 if (dbpaths.Length == 1) { // 否则移到DoExportFile()函数里面去校验 ResPath respath = new ResPath(dbpaths[0]); channel = this.Channels.GetChannel(respath.Url); string strDbName = respath.Path; // 校验起止号 if (checkBox_verifyNumber.Checked == true) { nRet = VerifyRange(channel, strDbName, out strError); if (nRet == -1) MessageBox.Show(this, strError); } else { if (this.textBox_startNo.Text == "") { strError = "尚未指定起始号"; goto ERROR1; } if (this.textBox_endNo.Text == "") { strError = "尚未指定结束号"; goto ERROR1; } } } else { // 多库输出。修改界面要素,表示针对每个库都是全库处理 this.radioButton_all.Checked = true; this.textBox_startNo.Text = "1"; this.textBox_endNo.Text = "9999999999"; } SaveFileDialog dlg = null; if (checkBox_export_delete.Checked == true) { DialogResult result = MessageBox.Show(this, "确实要(在输出的同时)删除数据库记录?\r\n\r\n---------\r\n(确定)删除 (放弃)放弃批处理", "dp2batch", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2); if (result != DialogResult.OK) { strError = "放弃处理..."; goto ERROR1; } result = MessageBox.Show(this, "在删除记录的同时, 是否将记录输出到文件?\r\n\r\n--------\r\n(是)一边删除一边输出 (否)只删除不输出", "dp2batch", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1); if (result != DialogResult.Yes) goto SKIPASKFILENAME; } // 获得输出文件名 dlg = new SaveFileDialog(); dlg.Title = "请指定要保存的备份文件名"; dlg.CreatePrompt = false; dlg.OverwritePrompt = false; dlg.FileName = strLastOutputFileName; dlg.FilterIndex = nLastOutputFilterIndex; dlg.Filter = "备份文件 (*.dp2bak)|*.dp2bak|XML文件 (*.xml)|*.xml|ISO2709文件 (*.iso;*.mrc)|*.iso;*.mrc|All files (*.*)|*.*"; dlg.RestoreDirectory = true; if (dlg.ShowDialog(this) != DialogResult.OK) { strError = "放弃处理..."; goto ERROR1; } strLastOutputFileName = dlg.FileName; nLastOutputFilterIndex = dlg.FilterIndex; strOutputFileName = dlg.FileName; SKIPASKFILENAME: // 触发Script中OnBegin()代码 // OnBegin()中仍然有修改MainForm面板的自由 if (batchObj != null) { BatchEventArgs args = new BatchEventArgs(); batchObj.OnBegin(this, args); /* if (args.Continue == ContinueType.SkipMiddle) goto END1; if (args.Continue == ContinueType.SkipBeginMiddle) goto END1; */ if (args.Continue == ContinueType.SkipAll) goto END1; } if (dlg == null || dlg.FilterIndex == 1) nRet = DoExportFile( dbpaths, strOutputFileName, ExportFileType.BackupFile, null, out strError); else if (dlg.FilterIndex == 2) nRet = DoExportFile( dbpaths, strOutputFileName, ExportFileType.XmlFile, null, out strError); else if (dlg.FilterIndex == 3) { ResPath respath = new ResPath(dbpaths[0]); string strMarcSyntax = ""; // 从marcdef配置文件中获得marc格式定义 // return: // -1 出错 // 0 没有找到 // 1 找到 nRet = this.SearchPanel.GetMarcSyntax(respath.FullPath, out strMarcSyntax, out strError); if (nRet == 0 || nRet == -1) { strError = "获取数据库 '" + dbpaths[0] + "' 的MARC格式时发生错误: " + strError; goto ERROR1; } // 如果多于一个数据库输出到一个文件,需要关心每个数据库的MARC格式是否相同,给与适当的警告 if (dbpaths.Length > 1) { string strWarning = ""; for (int i = 1; i < dbpaths.Length; i++) { ResPath current_respath = new ResPath(dbpaths[i]); string strPerMarcSyntax = ""; // 从marcdef配置文件中获得marc格式定义 // return: // -1 出错 // 0 没有找到 // 1 找到 nRet = this.SearchPanel.GetMarcSyntax(current_respath.FullPath, out strPerMarcSyntax, out strError); if (nRet == 0 || nRet == -1) { strError = "获取数据库 '" + dbpaths[i] + "' 的MARC格式时发生错误: " + strError; goto ERROR1; } if (strPerMarcSyntax != strMarcSyntax) { if (String.IsNullOrEmpty(strWarning) == false) strWarning += "\r\n"; strWarning += "数据库 '" + dbpaths[i] + "' (" + strPerMarcSyntax + ")"; } } if (String.IsNullOrEmpty(strWarning) == false) { strWarning = "所选择的数据库中,下列数据库的MARC格式和第一个数据库( '" + dbpaths[0] + "' (" + strMarcSyntax + "))的不同: \r\n---\r\n" + strWarning + "\r\n---\r\n\r\n如果把这些不同MARC格式的记录混合输出到一个文件中,可能会造成许多软件以后读取它时发生困难。\r\n\r\n确实要这样混合着转出到一个文件中?"; DialogResult result = MessageBox.Show(this, strWarning, "dp2batch", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2); if (result == DialogResult.No) { strError = "放弃处理..."; goto ERROR1; } } } OpenMarcFileDlg marcdlg = new OpenMarcFileDlg(); MainForm.SetControlFont(marcdlg, this.DefaultFont); marcdlg.IsOutput = true; marcdlg.Text = "请指定要输出的 ISO2709 文件属性"; marcdlg.FileName = strOutputFileName; marcdlg.MarcSyntax = strMarcSyntax; marcdlg.EnableMarcSyntax = false; // 不允许用户选择marc syntax,因为这是数据库配置好了的属性 2007/8/18 marcdlg.CrLf = this.OutputCrLf; marcdlg.AddG01 = this.AddG01; marcdlg.RemoveField998 = this.Remove998; this.AppInfo.LinkFormState(marcdlg, "OpenMarcFileDlg_output_state"); marcdlg.ShowDialog(this); this.AppInfo.UnlinkFormState(marcdlg); if (marcdlg.DialogResult != DialogResult.OK) { strError = "放弃处理..."; goto ERROR1; } if (marcdlg.AddG01 == true) { MessageBox.Show(this, "您选择了在导出的ISO2709记录中加入-01字段。请注意dp2Batch在将来导入这样的ISO2709文件的时候,记录中-01字段***起不到***覆盖定位的作用。“加入-01字段”功能是为了将导出的ISO2709文件应用到dt1000系统而设计的。\r\n\r\n如果您这样做的目的是为了对dp2系统书目库中的数据进行备份,请改用.xml格式或.dp2bak格式。"); } strOutputFileName = marcdlg.FileName; this.CurMarcSyntax = strMarcSyntax; this.OutputCrLf = marcdlg.CrLf; this.AddG01 = marcdlg.AddG01; this.Remove998 = marcdlg.RemoveField998; nRet = DoExportFile( dbpaths, marcdlg.FileName, ExportFileType.ISO2709File, marcdlg.Encoding, out strError); } else { strError = "不支持的文件类型..."; goto ERROR1; } /* if (nRet == 1) goto END2; */ if (nRet == -1) goto ERROR1; END1: // 触发Script的OnEnd()代码 if (batchObj != null) { BatchEventArgs args = new BatchEventArgs(); batchObj.OnEnd(this, args); } // END2: this.AssemblyMain = null; this.AssemblyFilter = null; if (filter != null) filter.Assembly = null; this.MarcFilter = null; if (String.IsNullOrEmpty(strError) == false) MessageBox.Show(this, strError); return; ERROR1: this.AssemblyMain = null; this.AssemblyFilter = null; if (filter != null) filter.Assembly = null; this.MarcFilter = null; MessageBox.Show(this, strError); }
// 将原始记录保存到ISO2709文件 // TODO: 如果记录发生了修改,则不再保存原始记录,而要保存MARC编辑器中的记录? public void SaveOriginRecordToIso2709() { string strError = ""; int nRet = 0; byte[] baTarget = null; // 如果已经是ISO2709格式,则存储在这里 string strMarc = ""; // 如果是MARC机内格式,存储在这里 // 如果不是从Z39.50协议过来的记录,或者记录在MARC编辑窗中已经修改过 if (this.m_currentRecord == null || (this.m_currentRecord != null && this.m_currentRecord.m_baRecord == null) // 2008/4/14 || this.MarcEditor.Changed == true) { // strError = "没有当前记录"; // goto ERROR1; // 从MARC编辑器中取记录 strMarc = this.MarcEditor.Marc; baTarget = null; } else { strMarc = ""; baTarget = this.m_currentRecord.m_baRecord; } Encoding preferredEncoding = this.CurrentEncoding; OpenMarcFileDlg dlg = new OpenMarcFileDlg(); GuiUtil.SetControlFont(dlg, this.Font); dlg.IsOutput = true; dlg.GetEncoding -= new GetEncodingEventHandler(dlg_GetEncoding); dlg.GetEncoding += new GetEncodingEventHandler(dlg_GetEncoding); dlg.FileName = MainForm.LastIso2709FileName; dlg.CrLf = MainForm.LastCrLfIso2709; dlg.RemoveField998 = MainForm.LastRemoveField998; dlg.EncodingListItems = Global.GetEncodingList(true); dlg.EncodingName = (String.IsNullOrEmpty(MainForm.LastEncodingName) == true ? GetEncodingForm.GetEncodingName(preferredEncoding) : MainForm.LastEncodingName); dlg.EncodingComment = "注: 原始编码方式为 " + GetEncodingForm.GetEncodingName(preferredEncoding); dlg.MarcSyntax = "<自动>"; // strPreferedMarcSyntax; dlg.EnableMarcSyntax = false; dlg.ShowDialog(this); if (dlg.DialogResult != DialogResult.OK) return; Encoding targetEncoding = null; if (dlg.EncodingName == "MARC-8" && preferredEncoding.Equals(this.MainForm.Marc8Encoding) == false) { strError = "保存操作无法进行。只有在记录的原始编码方式为 MARC-8 时,才能使用这个编码方式保存记录。"; goto ERROR1; } nRet = this.MainForm.GetEncoding(dlg.EncodingName, out targetEncoding, out strError); if (nRet == -1) { goto ERROR1; } string strLastFileName = MainForm.LastIso2709FileName; string strLastEncodingName = MainForm.LastEncodingName; bool bExist = File.Exists(dlg.FileName); bool bAppend = false; if (bExist == true) { DialogResult result = MessageBox.Show(this, "文件 '" + dlg.FileName + "' 已存在,是否以追加方式写入记录?\r\n\r\n--------------------\r\n注:(是)追加 (否)覆盖 (取消)放弃", "ZSearchForm", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1); if (result == DialogResult.Yes) bAppend = true; if (result == DialogResult.No) bAppend = false; if (result == DialogResult.Cancel) { strError = "放弃处理..."; goto ERROR1; } } // 检查同一个文件连续存时候的编码方式一致性 if (strLastFileName == dlg.FileName && bAppend == true) { if (strLastEncodingName != "" && strLastEncodingName != dlg.EncodingName) { DialogResult result = MessageBox.Show(this, "文件 '" + dlg.FileName + "' 已在先前已经用 " + strLastEncodingName + " 编码方式存储了记录,现在又以不同的编码方式 " + dlg.EncodingName + " 追加记录,这样会造成同一文件中存在不同编码方式的记录,可能会令它无法被正确读取。\r\n\r\n是否继续? (是)追加 (否)放弃操作", "ZSearchForm", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2); if (result == DialogResult.No) { strError = "放弃处理..."; goto ERROR1; } } } MainForm.LastIso2709FileName = dlg.FileName; MainForm.LastCrLfIso2709 = dlg.CrLf; MainForm.LastEncodingName = dlg.EncodingName; MainForm.LastRemoveField998 = dlg.RemoveField998; Stream s = null; try { s = File.Open(MainForm.LastIso2709FileName, FileMode.OpenOrCreate); if (bAppend == false) s.SetLength(0); else s.Seek(0, SeekOrigin.End); } catch (Exception ex) { strError = "打开或创建文件 " + MainForm.LastIso2709FileName + " 失败,原因: " + ex.Message; goto ERROR1; } try { string strMarcSyntax = ""; // 当源和目标编码不同的时候,才需要获得MARC语法参数 if (this.CurrentEncoding.Equals(targetEncoding) == false || strMarc != "") { string strMarcSyntaxOID = this.GetCurrentMarcSyntaxOID(out strError); if (String.IsNullOrEmpty(strMarcSyntaxOID) == true) { strError = "当前MARC syntax OID为空,无法判断MARC具体格式"; goto ERROR1; } if (strMarcSyntaxOID == "1.2.840.10003.5.1") strMarcSyntax = "unimarc"; if (strMarcSyntaxOID == "1.2.840.10003.5.10") strMarcSyntax = "usmarc"; } if (strMarc != "") { Debug.Assert(strMarcSyntax != "", ""); if (dlg.RemoveField998 == true) { MarcRecord temp = new MarcRecord(strMarc); temp.select("field[@name='998']").detach(); strMarc = temp.Text; } if (dlg.Mode880 == true && strMarcSyntax == "usmarc") { MarcRecord temp = new MarcRecord(strMarc); MarcQuery.To880(temp); strMarc = temp.Text; } // 将MARC机内格式转换为ISO2709格式 // parameters: // strSourceMARC [in]机内格式MARC记录。 // strMarcSyntax [in]为"unimarc"或"usmarc" // targetEncoding [in]输出ISO2709的编码方式。为UTF8、codepage-936等等 // baResult [out]输出的ISO2709记录。编码方式受targetEncoding参数控制。注意,缓冲区末尾不包含0字符。 // return: // -1 出错 // 0 成功 nRet = MarcUtil.CvtJineiToISO2709( strMarc, strMarcSyntax, targetEncoding, out baTarget, out strError); if (nRet == -1) goto ERROR1; } else if (this.CurrentEncoding.Equals(targetEncoding) == true) { // source和target编码方式相同,不用转换 // baTarget = this.CurrentRecord.m_baRecord; Debug.Assert(strMarcSyntax == "", ""); // 规范化 ISO2709 物理记录 // 主要是检查里面的记录结束符是否正确,去掉多余的记录结束符 baTarget = MarcUtil.CononicalizeIso2709Bytes(targetEncoding, baTarget); } else { // baTarget = this.CurrentRecord.m_baRecord; Debug.Assert(strMarcSyntax != "", ""); nRet = ZSearchForm.ChangeIso2709Encoding( this.CurrentEncoding, baTarget, targetEncoding, strMarcSyntax, out baTarget, out strError); if (nRet == -1) goto ERROR1; } s.Seek(0, SeekOrigin.End); s.Write(baTarget, 0, baTarget.Length); if (dlg.CrLf == true) { byte[] baCrLf = targetEncoding.GetBytes("\r\n"); s.Write(baCrLf, 0, baCrLf.Length); } if (bAppend == true) MainForm.MessageText = "1条记录成功追加到文件 " + MainForm.LastIso2709FileName + " 尾部"; else MainForm.MessageText = "1条记录成功保存到新文件 " + MainForm.LastIso2709FileName + " 尾部"; } catch (Exception ex) { strError = "写入文件 " + MainForm.LastIso2709FileName + " 失败,原因: " + ex.Message; goto ERROR1; } finally { s.Close(); } return; ERROR1: MessageBox.Show(this, strError); }
/// <summary> /// 构造函数 /// </summary> public Iso2709StatisForm() { InitializeComponent(); _openMarcFileDialog = new OpenMarcFileDlg(); _openMarcFileDialog.IsOutput = false; this.tabPage_source.Padding = new Padding(4, 4, 4, 4); this.tabPage_source.Controls.Add(_openMarcFileDialog.MainPanel); _openMarcFileDialog.MainPanel.Dock = DockStyle.Fill; }
void DoImport(string strProjectName, string strProjectLocate) { string strError = ""; int nRet = 0; Assembly assemblyMain = null; MyFilterDocument filter = null; this.MarcFilter = null; batchObj = null; m_nRecordCount = -1; // 准备脚本 if (strProjectName != "" && strProjectName != null) { nRet = PrepareScript(strProjectName, strProjectLocate, out assemblyMain, out filter, out batchObj, out strError); if (nRet == -1) goto ERROR1; this.AssemblyMain = assemblyMain; if (filter != null) this.AssemblyFilter = filter.Assembly; else this.AssemblyFilter = null; this.MarcFilter = filter; } // 执行脚本的OnInitial() // 触发Script中OnInitial()代码 // OnInitial()和OnBegin的本质区别, 在于OnInitial()适合检查和设置面板参数 if (batchObj != null) { BatchEventArgs args = new BatchEventArgs(); batchObj.OnInitial(this, args); /* if (args.Continue == ContinueType.SkipBeginMiddle) goto END1; if (args.Continue == ContinueType.SkipMiddle) { strError = "OnInitial()中args.Continue不能使用ContinueType.SkipMiddle.应使用ContinueType.SkipBeginMiddle"; goto ERROR1; } */ if (args.Continue == ContinueType.SkipAll) goto END1; } if (this.textBox_import_fileName.Text == "") { strError = "尚未指定输入文件名..."; goto ERROR1; } FileInfo fi = new FileInfo(this.textBox_import_fileName.Text); if (fi.Exists == false) { strError = "文件" + this.textBox_import_fileName.Text + "不存在..."; goto ERROR1; } OpenMarcFileDlg dlg = null; // ISO2709文件需要预先准备条件 if (String.Compare(fi.Extension, ".iso", true) == 0 || String.Compare(fi.Extension, ".mrc", true) == 0) { // 询问encoding和marcsyntax dlg = new OpenMarcFileDlg(); MainForm.SetControlFont(dlg, this.DefaultFont); dlg.Text = "请指定要导入的 ISO2709 文件属性"; dlg.FileName = this.textBox_import_fileName.Text; this.AppInfo.LinkFormState(dlg, "OpenMarcFileDlg_input_state"); dlg.ShowDialog(this); this.AppInfo.UnlinkFormState(dlg); if (dlg.DialogResult != DialogResult.OK) return; this.textBox_import_fileName.Text = dlg.FileName; this.CurMarcSyntax = dlg.MarcSyntax; } // 触发Script中OnBegin()代码 // OnBegin()中仍然有修改MainForm面板的自由 if (batchObj != null) { BatchEventArgs args = new BatchEventArgs(); batchObj.OnBegin(this, args); /* if (args.Continue == ContinueType.SkipMiddle) goto END1; if (args.Continue == ContinueType.SkipBeginMiddle) goto END1; */ if (args.Continue == ContinueType.SkipAll) goto END1; } if (String.Compare(fi.Extension, ".dp2bak", true) == 0) nRet = this.DoImportBackup(this.textBox_import_fileName.Text, out strError); else if (String.Compare(fi.Extension, ".xml", true) == 0) nRet = this.DoImportXml(this.textBox_import_fileName.Text, out strError); else if (String.Compare(fi.Extension, ".iso", true) == 0 || String.Compare(fi.Extension, ".mrc", true) == 0) { this.m_tableMarcSyntax.Clear(); // 2015/5/29 this.CheckTargetDb += new CheckTargetDbEventHandler(CheckTargetDbCallBack); try { nRet = this.DoImportIso2709(dlg.FileName, dlg.MarcSyntax, dlg.Encoding, out strError); } finally { this.CheckTargetDb -= new CheckTargetDbEventHandler(CheckTargetDbCallBack); } } else { strError = "未知的文件类型..."; goto ERROR1; } END1: // 触发Script的OnEnd()代码 if (batchObj != null) { BatchEventArgs args = new BatchEventArgs(); batchObj.OnEnd(this, args); } // END2: this.AssemblyMain = null; this.AssemblyFilter = null; if (filter != null) filter.Assembly = null; if (strError != "") MessageBox.Show(this, strError); this.MarcFilter = null; return; ERROR1: this.AssemblyMain = null; this.AssemblyFilter = null; if (filter != null) filter.Assembly = null; this.MarcFilter = null; MessageBox.Show(this, strError); }
// 将从属的书目记录保存到MARC文件 void menu_saveBiblioRecordToMarcFile_Click(object sender, EventArgs e) { string strError = ""; int nRet = 0; if (this.listView_records.SelectedItems.Count == 0) { strError = "尚未选定要保存的事项"; goto ERROR1; } // 为了书目记录路径去重服务 Hashtable biblio_recpath_table = new Hashtable(); Hashtable rule_name_table = null; bool bTableExists = false; // 将馆藏地点名和编目规则名的对照表装入内存 // return: // -1 出错 // 0 文件不存在 // 1 成功 nRet = LoadRuleNameTable(PathUtil.MergePath(this.MainForm.DataDir, "cataloging_rules.xml"), out rule_name_table, out strError); if (nRet == -1) goto ERROR1; if (nRet == 1) bTableExists = true; Debug.Assert(rule_name_table != null, ""); Encoding preferredEncoding = this.CurrentEncoding; { // 观察要保存的第一条记录的marc syntax } OpenMarcFileDlg dlg = new OpenMarcFileDlg(); MainForm.SetControlFont(dlg, this.Font); dlg.IsOutput = true; dlg.AddG01Visible = false; if (bTableExists == false) { dlg.RuleVisible = true; dlg.Rule = this.LastCatalogingRule; } dlg.FileName = this.LastIso2709FileName; dlg.CrLf = this.LastCrLfIso2709; dlg.EncodingListItems = Global.GetEncodingList(false); dlg.EncodingName = (String.IsNullOrEmpty(this.LastEncodingName) == true ? Global.GetEncodingName(preferredEncoding) : this.LastEncodingName); dlg.EncodingComment = "注: 原始编码方式为 " + Global.GetEncodingName(preferredEncoding); dlg.MarcSyntax = "<自动>"; // strPreferedMarcSyntax; dlg.EnableMarcSyntax = false; dlg.ShowDialog(this); if (dlg.DialogResult != DialogResult.OK) return; string strCatalogingRule = ""; if (bTableExists == false) { strCatalogingRule = dlg.Rule; if (strCatalogingRule == "<无限制>") strCatalogingRule = null; } Encoding targetEncoding = null; nRet = Global.GetEncoding(dlg.EncodingName, out targetEncoding, out strError); if (nRet == -1) { goto ERROR1; } string strLastFileName = this.LastIso2709FileName; string strLastEncodingName = this.LastEncodingName; bool bExist = File.Exists(dlg.FileName); bool bAppend = false; if (bExist == true) { DialogResult result = MessageBox.Show(this, "文件 '" + dlg.FileName + "' 已存在,是否以追加方式写入记录?\r\n\r\n--------------------\r\n注:(是)追加 (否)覆盖 (取消)放弃", this.DbType + "SearchForm", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1); if (result == DialogResult.Yes) bAppend = true; if (result == DialogResult.No) bAppend = false; if (result == DialogResult.Cancel) { strError = "放弃处理..."; goto ERROR1; } } // 检查同一个文件连续存时候的编码方式一致性 if (strLastFileName == dlg.FileName && bAppend == true) { if (strLastEncodingName != "" && strLastEncodingName != dlg.EncodingName) { DialogResult result = MessageBox.Show(this, "文件 '" + dlg.FileName + "' 已在先前已经用 " + strLastEncodingName + " 编码方式存储了记录,现在又以不同的编码方式 " + dlg.EncodingName + " 追加记录,这样会造成同一文件中存在不同编码方式的记录,可能会令它无法被正确读取。\r\n\r\n是否继续? (是)追加 (否)放弃操作", this.DbType + "SearchForm", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2); if (result == DialogResult.No) { strError = "放弃处理..."; goto ERROR1; } } } this.LastIso2709FileName = dlg.FileName; this.LastCrLfIso2709 = dlg.CrLf; this.LastEncodingName = dlg.EncodingName; this.LastCatalogingRule = dlg.Rule; this.EnableControls(false); stop.OnStop += new StopEventHandler(this.DoStop); stop.Initial("正在保存到 MARC 文件 ..."); stop.BeginLoop(); Stream s = null; int nOutputCount = 0; try { s = File.Open(this.LastIso2709FileName, FileMode.OpenOrCreate); if (bAppend == false) s.SetLength(0); else s.Seek(0, SeekOrigin.End); } catch (Exception ex) { strError = "打开或创建文件 " + this.LastIso2709FileName + " 失败,原因: " + ex.Message; goto ERROR1; } try { stop.SetProgressRange(0, this.listView_records.SelectedItems.Count); int i = 0; foreach (ListViewItem item in this.listView_records.SelectedItems) { Application.DoEvents(); // 出让界面控制权 if (stop != null) { if (stop.State != 0) { strError = "用户中断"; goto ERROR1; } } string strRecPath = item.Text; if (String.IsNullOrEmpty(strRecPath) == true) goto CONTINUE; stop.SetMessage("正在准备记录 " + strRecPath + " 的书目记录路径 ..."); stop.SetProgressValue(i); string strItemRecPath = ""; string strBiblioRecPath = ""; string strLocation = ""; if (this.DbType == "item") { Debug.Assert(this.DbType == "item", ""); nRet = SearchTwoRecPathByBarcode( this.stop, this.Channel, "@path:" + strRecPath, out strItemRecPath, out strLocation, out strBiblioRecPath, out strError); } else { nRet = SearchBiblioRecPath( this.stop, this.Channel, this.DbType, strRecPath, out strBiblioRecPath, out strError); } if (nRet == -1) { goto ERROR1; } else if (nRet == 0) { strError = "记录路径 '" + strRecPath + "' 没有找到记录"; goto ERROR1; } else if (nRet == 1) { item.Text = strItemRecPath; } else if (nRet > 1) // 命中发生重复 { strError = "记录路径 '" + strRecPath + "' 命中 " + nRet.ToString() + " 条记录,这是一个严重错误"; } // 去重 if (biblio_recpath_table.ContainsKey(strBiblioRecPath) == true) goto CONTINUE; if (bTableExists == true) { strCatalogingRule = ""; // 根据馆藏地点获得编目规则名 if (this.DbType == "item" && string.IsNullOrEmpty(strLocation) == false) { // strCatalogingRule = (string)rule_name_table[strLocation]; if (string.IsNullOrEmpty(strCatalogingRule) == true) { strCatalogingRule = InputDlg.GetInput( this, null, "请输入馆藏地点 '" + strLocation + "' 所对应的编目规则名称:", "NLC", this.MainForm.DefaultFont); if (strCatalogingRule == null) { DialogResult result = MessageBox.Show(this, "由于您没有指定馆藏地点 '" + strLocation + "' 所对应的编目规则名,此馆藏地点被当作 <无限制> 编目规则来处理。\r\n\r\n是否继续操作? (OK 继续;Cancel 放弃整个导出操作)", this.DbType + "SearchForm", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1); if (result == System.Windows.Forms.DialogResult.Cancel) break; strCatalogingRule = ""; } rule_name_table[strLocation] = strCatalogingRule; // 储存到内存,后面就不再作相同的询问了 } } } string[] results = null; byte[] baTimestamp = null; stop.SetMessage("正在获取书目记录 " + strBiblioRecPath); long lRet = Channel.GetBiblioInfos( stop, strBiblioRecPath, "", new string[] { "xml" }, // formats out results, out baTimestamp, out strError); if (lRet == 0) goto ERROR1; if (lRet == -1) goto ERROR1; if (results == null || results.Length == 0) { strError = "results error"; goto ERROR1; } string strXml = results[0]; string strMARC = ""; string strMarcSyntax = ""; // 将XML格式转换为MARC格式 // 自动从数据记录中获得MARC语法 nRet = MarcUtil.Xml2Marc(strXml, true, null, out strMarcSyntax, out strMARC, out strError); if (nRet == -1) { strError = "XML转换到MARC记录时出错: " + strError; goto ERROR1; } byte[] baTarget = null; Debug.Assert(strMarcSyntax != "", ""); // 按照编目规则过滤 // 获得一个特定风格的 MARC 记录 // parameters: // strStyle 要匹配的style值。如果为null,表示任何$*值都匹配,实际上效果是去除$*并返回全部字段内容 // return: // 0 没有实质性修改 // 1 有实质性修改 nRet = MarcUtil.GetMappedRecord(ref strMARC, strCatalogingRule); if (dlg.RemoveField998 == true) { MarcRecord record = new MarcRecord(strMARC); record.select("field[@name='998']").detach(); strMARC = record.Text; } if (dlg.Mode880 == true && strMarcSyntax == "usmarc") { MarcRecord record = new MarcRecord(strMARC); MarcQuery.To880(record); strMARC = record.Text; } // 将MARC机内格式转换为ISO2709格式 // parameters: // strSourceMARC [in]机内格式MARC记录。 // strMarcSyntax [in]为"unimarc"或"usmarc" // targetEncoding [in]输出ISO2709的编码方式。为UTF8、codepage-936等等 // baResult [out]输出的ISO2709记录。编码方式受targetEncoding参数控制。注意,缓冲区末尾不包含0字符。 // return: // -1 出错 // 0 成功 nRet = MarcUtil.CvtJineiToISO2709( strMARC, strMarcSyntax, targetEncoding, out baTarget, out strError); if (nRet == -1) goto ERROR1; /* Encoding sourceEncoding = connection.GetRecordsEncoding( this.MainForm, record.m_strSyntaxOID); if (sourceEncoding.Equals(targetEncoding) == true) { // source和target编码方式相同,不用转换 baTarget = record.m_baRecord; } else { nRet = ChangeIso2709Encoding( sourceEncoding, record.m_baRecord, targetEncoding, strMarcSyntax, out baTarget, out strError); if (nRet == -1) goto ERROR1; }*/ s.Write(baTarget, 0, baTarget.Length); if (dlg.CrLf == true) { byte[] baCrLf = targetEncoding.GetBytes("\r\n"); s.Write(baCrLf, 0, baCrLf.Length); } stop.SetProgressValue(i + 1); nOutputCount++; CONTINUE: i++; // biblio_recpath_table[strBiblioRecPath] = 1; } } catch (Exception ex) { strError = "写入文件 " + this.LastIso2709FileName + " 失败,原因: " + ex.Message; goto ERROR1; } finally { s.Close(); stop.EndLoop(); stop.OnStop -= new StopEventHandler(this.DoStop); stop.Initial(""); stop.HideProgress(); this.EnableControls(true); } // if (bAppend == true) MainForm.StatusBarMessage = nOutputCount.ToString() + "条记录成功追加到文件 " + this.LastIso2709FileName + " 尾部"; else MainForm.StatusBarMessage = nOutputCount.ToString() + "条记录成功保存到新文件 " + this.LastIso2709FileName + " 尾部"; return; ERROR1: MessageBox.Show(this, strError); }
// 2015/10/10 // 保存到 MARC 文件 void menu_saveToMarcFile_Click(object sender, EventArgs e) { string strError = ""; int nRet = 0; if (this.listView_records.SelectedItems.Count == 0) { strError = "尚未选定要导出的事项"; goto ERROR1; } Encoding preferredEncoding = this.CurrentEncoding; { // 观察要保存的第一条记录的marc syntax } OpenMarcFileDlg dlg = new OpenMarcFileDlg(); MainForm.SetControlFont(dlg, this.Font); dlg.IsOutput = true; dlg.AddG01Visible = false; dlg.RuleVisible = true; dlg.Rule = this.LastCatalogingRule; dlg.FileName = this.LastIso2709FileName; dlg.CrLf = this.LastCrLfIso2709; dlg.RemoveField998 = this.LastRemoveField998; dlg.EncodingListItems = Global.GetEncodingList(false); dlg.EncodingName = (String.IsNullOrEmpty(this.LastEncodingName) == true ? Global.GetEncodingName(preferredEncoding) : this.LastEncodingName); dlg.EncodingComment = "注: 原始编码方式为 " + Global.GetEncodingName(preferredEncoding); dlg.MarcSyntax = "<自动>"; // strPreferedMarcSyntax; dlg.EnableMarcSyntax = false; dlg.ShowDialog(this); if (dlg.DialogResult != DialogResult.OK) return; string strCatalogingRule = dlg.Rule; if (strCatalogingRule == "<无限制>") strCatalogingRule = null; Encoding targetEncoding = null; nRet = Global.GetEncoding(dlg.EncodingName, out targetEncoding, out strError); if (nRet == -1) { goto ERROR1; } string strLastFileName = this.LastIso2709FileName; string strLastEncodingName = this.LastEncodingName; bool bExist = File.Exists(dlg.FileName); bool bAppend = false; if (bExist == true) { DialogResult result = MessageBox.Show(this, "文件 '" + dlg.FileName + "' 已存在,是否以追加方式写入记录?\r\n\r\n--------------------\r\n注:(是)追加 (否)覆盖 (取消)放弃", "BiblioSearchForm", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1); if (result == DialogResult.Yes) bAppend = true; if (result == DialogResult.No) bAppend = false; if (result == DialogResult.Cancel) { strError = "放弃处理..."; goto ERROR1; } } // 检查同一个文件连续存时候的编码方式一致性 if (strLastFileName == dlg.FileName && bAppend == true) { if (strLastEncodingName != "" && strLastEncodingName != dlg.EncodingName) { DialogResult result = MessageBox.Show(this, "文件 '" + dlg.FileName + "' 已在先前已经用 " + strLastEncodingName + " 编码方式存储了记录,现在又以不同的编码方式 " + dlg.EncodingName + " 追加记录,这样会造成同一文件中存在不同编码方式的记录,可能会令它无法被正确读取。\r\n\r\n是否继续? (是)追加 (否)放弃操作", "BiblioSearchForm", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2); if (result == DialogResult.No) { strError = "放弃处理..."; goto ERROR1; } } } this.LastIso2709FileName = dlg.FileName; this.LastCrLfIso2709 = dlg.CrLf; this.LastEncodingName = dlg.EncodingName; this.LastCatalogingRule = dlg.Rule; this.LastRemoveField998 = dlg.RemoveField998; Stream s = null; try { s = File.Open(this.LastIso2709FileName, FileMode.OpenOrCreate); if (bAppend == false) s.SetLength(0); else s.Seek(0, SeekOrigin.End); } catch (Exception ex) { strError = "打开或创建文件 " + this.LastIso2709FileName + " 失败,原因: " + ex.Message; goto ERROR1; } LibraryChannel channel = this.GetChannel(); this.EnableControls(false); stop.OnStop += new StopEventHandler(this.DoStop); stop.Initial("正在导出到 MARC 文件 ..."); stop.BeginLoop(); try { stop.SetProgressRange(0, this.listView_records.SelectedItems.Count); List<ListViewItem> items = new List<ListViewItem>(); foreach (ListViewItem item in this.listView_records.SelectedItems) { if (string.IsNullOrEmpty(item.Text) == true) continue; items.Add(item); } ListViewBiblioLoader loader = new ListViewBiblioLoader(channel, // this.Channel, stop, items, this.m_biblioTable); loader.Prompt -= new MessagePromptEventHandler(loader_Prompt); loader.Prompt += new MessagePromptEventHandler(loader_Prompt); int i = 0; foreach (LoaderItem item in loader) { Application.DoEvents(); // 出让界面控制权 if (stop != null && stop.State != 0) { strError = "用户中断"; goto ERROR1; } #if NO string[] results = null; byte[] baTimestamp = null; stop.SetMessage("正在获取书目记录 " + strRecPath); long lRet = Channel.GetBiblioInfos( stop, strRecPath, "", new string[] { "xml" }, // formats out results, out baTimestamp, out strError); if (lRet == 0) goto ERROR1; if (lRet == -1) goto ERROR1; if (results == null || results.Length == 0) { strError = "results error"; goto ERROR1; } string strXml = results[0]; #endif BiblioInfo info = item.BiblioInfo; string strXml = ""; { if (string.IsNullOrEmpty(info.NewXml) == false) strXml = info.NewXml; else strXml = info.OldXml; } string strMARC = ""; string strMarcSyntax = ""; // 将XML格式转换为MARC格式 // 自动从数据记录中获得MARC语法 nRet = MarcUtil.Xml2Marc(strXml, true, null, out strMarcSyntax, out strMARC, out strError); if (nRet == -1) { strError = "XML转换到MARC记录时出错: " + strError; goto ERROR1; } byte[] baTarget = null; Debug.Assert(strMarcSyntax != "", ""); // 按照编目规则过滤 // 获得一个特定风格的 MARC 记录 // parameters: // strStyle 要匹配的style值。如果为null,表示任何$*值都匹配,实际上效果是去除$*并返回全部字段内容 // return: // 0 没有实质性修改 // 1 有实质性修改 nRet = MarcUtil.GetMappedRecord(ref strMARC, strCatalogingRule); if (dlg.RemoveField998 == true) { MarcRecord record = new MarcRecord(strMARC); record.select("field[@name='998']").detach(); strMARC = record.Text; } if (dlg.Mode880 == true && strMarcSyntax == "usmarc") { MarcRecord record = new MarcRecord(strMARC); MarcQuery.To880(record); strMARC = record.Text; } // 将MARC机内格式转换为ISO2709格式 // parameters: // strSourceMARC [in]机内格式MARC记录。 // strMarcSyntax [in]为"unimarc"或"usmarc" // targetEncoding [in]输出ISO2709的编码方式。为UTF8、codepage-936等等 // baResult [out]输出的ISO2709记录。编码方式受targetEncoding参数控制。注意,缓冲区末尾不包含0字符。 // return: // -1 出错 // 0 成功 nRet = MarcUtil.CvtJineiToISO2709( strMARC, strMarcSyntax, targetEncoding, out baTarget, out strError); if (nRet == -1) goto ERROR1; s.Write(baTarget, 0, baTarget.Length); if (dlg.CrLf == true) { byte[] baCrLf = targetEncoding.GetBytes("\r\n"); s.Write(baCrLf, 0, baCrLf.Length); } stop.SetProgressValue(++i); } } catch (Exception ex) { strError = "写入文件 " + this.LastIso2709FileName + " 失败,原因: " + ex.Message; goto ERROR1; } finally { s.Close(); stop.EndLoop(); stop.OnStop -= new StopEventHandler(this.DoStop); stop.Initial(""); stop.HideProgress(); this.ReturnChannel(channel); this.EnableControls(true); } // if (bAppend == true) MainForm.StatusBarMessage = this.listView_records.SelectedItems.Count.ToString() + "条记录成功追加到文件 " + this.LastIso2709FileName + " 尾部"; else MainForm.StatusBarMessage = this.listView_records.SelectedItems.Count.ToString() + "条记录成功保存到新文件 " + this.LastIso2709FileName; return; ERROR1: MessageBox.Show(this, strError); }
private void button_worToIso_convert_Click(object sender, EventArgs e) { string strError = ""; int nRet = 0; if (string.IsNullOrEmpty(this.textBox_worToIso_worFilename.Text) == true) { strError = "尚未指定工作单文件名"; goto ERROR1; } Encoding encoding = MarcUtil.GetEncoding(this.comboBox_worToIso_encoding.Text); if (encoding == null) encoding = Encoding.GetEncoding(936); Encoding preferredEncoding = Encoding.UTF8; OpenMarcFileDlg dlg = new OpenMarcFileDlg(); GuiUtil.SetControlFont(dlg, this.Font); dlg.Text = "请指定目标 ISO2709 文件名"; dlg.IsOutput = true; dlg.FileName = ""; dlg.CrLf = false; dlg.AddG01Visible = false; dlg.RemoveField998Visible = false; //dlg.RemoveField998 = m_mainForm.LastRemoveField998; dlg.EncodingListItems = Global.GetEncodingList(false); dlg.MarcSyntax = "<自动>"; // strPreferedMarcSyntax; dlg.EnableMarcSyntax = false; this.MainForm.AppInfo.LinkFormState(dlg, "OpenMarcFileDlg_forOutput_state"); dlg.ShowDialog(this); if (dlg.DialogResult != DialogResult.OK) return; Encoding targetEncoding = null; nRet = Global.GetEncoding(dlg.EncodingName, out targetEncoding, out strError); if (nRet == -1) goto ERROR1; bool bExist = File.Exists(dlg.FileName); bool bAppend = false; if (bExist == true) { DialogResult result = MessageBox.Show(this, "文件 '" + dlg.FileName + "' 已存在,是否以追加方式写入记录?\r\n\r\n--------------------\r\n注:(是)追加 (否)覆盖 (取消)放弃", "UtilityForm", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1); if (result == DialogResult.Yes) bAppend = true; if (result == DialogResult.No) bAppend = false; if (result == DialogResult.Cancel) { strError = "放弃处理..."; goto ERROR1; } } EnableControls(false); stop.OnStop += new StopEventHandler(this.DoStop); stop.Initial("正在转换文件格式 ..."); stop.BeginLoop(); try { using (TextReader reader = new StreamReader(this.textBox_worToIso_worFilename.Text, encoding)) using (Stream target = File.Open(dlg.FileName, FileMode.OpenOrCreate)) { if (bAppend == false) target.SetLength(0); else target.Seek(0, SeekOrigin.End); for (int i = 0; ; i++) { stop.SetMessage("正在转换 " + (i + 1).ToString()); Application.DoEvents(); // 出让界面控制权 if (stop != null && stop.State != 0) { strError = "用户中断"; goto ERROR1; } string strMARC = ""; // return: // -2 MARC格式错 // -1 出错 // 0 正确 // 1 结束(当前返回的记录有效) // 2 结束(当前返回的记录无效) nRet = MarcUtil.ReadWorksheetRecord(reader, out strMARC, out strError); if (nRet == -1 || nRet == -2) goto ERROR1; if (nRet == 2) break; if (dlg.Mode880 == true && dlg.MarcSyntax == "usmarc") { MarcRecord record = new MarcRecord(strMARC); MarcQuery.To880(record); strMARC = record.Text; } byte[] baTarget = null; // 将MARC机内格式转换为ISO2709格式 // parameters: // strSourceMARC [in]机内格式MARC记录。 // strMarcSyntax [in]为"unimarc"或"usmarc" // targetEncoding [in]输出ISO2709的编码方式。为UTF8、codepage-936等等 // baResult [out]输出的ISO2709记录。编码方式受targetEncoding参数控制。注意,缓冲区末尾不包含0字符。 // return: // -1 出错 // 0 成功 nRet = MarcUtil.CvtJineiToISO2709( strMARC, dlg.MarcSyntax, targetEncoding, out baTarget, out strError); if (nRet == -1) goto ERROR1; target.Write(baTarget, 0, baTarget.Length); if (dlg.CrLf == true) { byte[] baCrLf = targetEncoding.GetBytes("\r\n"); target.Write(baCrLf, 0, baCrLf.Length); } } } } catch (Exception ex) { strError = "转换过程出现异常: " + ex.Message; goto ERROR1; } finally { stop.EndLoop(); stop.OnStop -= new StopEventHandler(this.DoStop); stop.Initial(""); EnableControls(true); } MessageBox.Show(this, "转换完成。记录已写入文件 "+dlg.FileName+" 中"); return; ERROR1: MessageBox.Show(this, strError); }
// 从 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; }
public void SaveOriginRecordToIso2709() { string strError = ""; int nRet = 0; bool bControl = Control.ModifierKeys == Keys.Control; if (this.listView_browse.SelectedItems.Count == 0) { strError = "尚未选定要保存的记录"; goto ERROR1; } Encoding preferredEncoding = this.CurrentEncoding; string strPreferedMarcSyntax = ""; if (this._linkMarcFile != null) strPreferedMarcSyntax = this._linkMarcFile.MarcSyntax; else { // 观察要保存的第一条记录的marc syntax nRet = GetOneRecordSyntax(0, this.m_bInSearching, out strPreferedMarcSyntax, out strError); if (nRet == -1) goto ERROR1; } OpenMarcFileDlg dlg = new OpenMarcFileDlg(); GuiUtil.SetControlFont(dlg, this.Font); dlg.IsOutput = true; dlg.FileName = MainForm.LastIso2709FileName; dlg.CrLf = MainForm.LastCrLfIso2709; dlg.RemoveField998 = MainForm.LastRemoveField998; dlg.EncodingListItems = Global.GetEncodingList(true); dlg.EncodingName = (String.IsNullOrEmpty(MainForm.LastEncodingName) == true ? GetEncodingForm.GetEncodingName(preferredEncoding) : MainForm.LastEncodingName); dlg.EncodingComment = "注: 原始编码方式为 " + GetEncodingForm.GetEncodingName(preferredEncoding); if (string.IsNullOrEmpty(strPreferedMarcSyntax) == false) dlg.MarcSyntax = strPreferedMarcSyntax; else dlg.MarcSyntax = "<自动>"; // strPreferedMarcSyntax; if (bControl == false) dlg.EnableMarcSyntax = false; dlg.ShowDialog(this); if (dlg.DialogResult != DialogResult.OK) return; Encoding targetEncoding = null; if (dlg.EncodingName == "MARC-8" && preferredEncoding.Equals(this.MainForm.Marc8Encoding) == false) { strError = "保存操作无法进行。只有在记录的原始编码方式为 MARC-8 时,才能使用这个编码方式保存记录。"; goto ERROR1; } nRet = this.MainForm.GetEncoding(dlg.EncodingName, out targetEncoding, out strError); if (nRet == -1) { goto ERROR1; } string strLastFileName = MainForm.LastIso2709FileName; string strLastEncodingName = MainForm.LastEncodingName; bool bExist = File.Exists(dlg.FileName); bool bAppend = false; if (bExist == true) { DialogResult result = MessageBox.Show(this, "文件 '" + dlg.FileName + "' 已存在,是否以追加方式写入记录?\r\n\r\n--------------------\r\n注:(是)追加 (否)覆盖 (取消)放弃", "dp2SearchForm", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1); if (result == DialogResult.Yes) bAppend = true; if (result == DialogResult.No) bAppend = false; if (result == DialogResult.Cancel) { strError = "放弃处理..."; goto ERROR1; } } // 检查同一个文件连续存时候的编码方式一致性 if (strLastFileName == dlg.FileName && bAppend == true) { if (strLastEncodingName != "" && strLastEncodingName != dlg.EncodingName) { DialogResult result = MessageBox.Show(this, "文件 '" + dlg.FileName + "' 已在先前已经用 " + strLastEncodingName + " 编码方式存储了记录,现在又以不同的编码方式 " + dlg.EncodingName + " 追加记录,这样会造成同一文件中存在不同编码方式的记录,可能会令它无法被正确读取。\r\n\r\n是否继续? (是)追加 (否)放弃操作", "dp2SearchForm", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2); if (result == DialogResult.No) { strError = "放弃处理..."; goto ERROR1; } } } MainForm.LastIso2709FileName = dlg.FileName; MainForm.LastCrLfIso2709 = dlg.CrLf; MainForm.LastEncodingName = dlg.EncodingName; MainForm.LastRemoveField998 = dlg.RemoveField998; this.EnableControls(false); stop.OnStop += new StopEventHandler(this.DoStop); stop.Initial("正在保存到MARC文件 ..."); stop.BeginLoop(); Stream s = null; try { s = File.Open(MainForm.LastIso2709FileName, FileMode.OpenOrCreate); if (bAppend == false) s.SetLength(0); else s.Seek(0, SeekOrigin.End); } catch (Exception ex) { strError = "打开或创建文件 " + MainForm.LastIso2709FileName + " 失败,原因: " + ex.Message; goto ERROR1; } int nCount = 0; try { stop.SetProgressRange(0, this.listView_browse.SelectedItems.Count); bool bAsked = false; for (int i = 0; i < this.listView_browse.SelectedItems.Count; i++) { Application.DoEvents(); // 出让界面控制权 if (stop != null) { if (stop.State != 0) { strError = "用户中断"; goto ERROR1; } } string strPath = this.listView_browse.SelectedItems[i].Text; byte[] baTarget = null; string strRecord = ""; string strOutputPath = ""; string strOutStyle = ""; byte[] baTimestamp = null; DigitalPlatform.Z3950.Record record = null; Encoding currrentEncoding; string strXmlFragment = ""; // 获得一条MARC/XML记录 // parameters: // strPath 记录路径。格式为"中文图书/1 @服务器名" // strDirection 方向。为 prev/next/current之一。current可以缺省。 // strOutputPath [out]返回的实际路径。格式和strPath相同。 // return: // -1 error 包括not found // 0 found // 1 为诊断记录 nRet = InternalGetOneRecord( false, "marc", strPath, "current", "", out strRecord, out strXmlFragment, out strOutputPath, out strOutStyle, out baTimestamp, out record, out currrentEncoding, out strError); if (nRet == -1) goto ERROR1; string strMarcSyntax = ""; if (dlg.MarcSyntax == "<自动>") { if (record.m_strSyntaxOID == "1.2.840.10003.5.1") strMarcSyntax = "unimarc"; if (record.m_strSyntaxOID == "1.2.840.10003.5.10") strMarcSyntax = "usmarc"; if (strMarcSyntax == "unimarc" && dlg.Mode880 == true && bAsked == false) { DialogResult result = MessageBox.Show(this, "书目记录 " + strPath + " 的 MARC 格式为 UNIMARC,在保存对话框选择“<自动>”的情况下,在保存前将不会被处理为 880 模式。如果确需在保存前处理为 880 模式,请终止当前操作,重新进行一次保存,注意在保存对话框中明确选择 “USMARC” 格式。\r\n\r\n请问是否继续处理? \r\n\r\n(Yes 继续处理,UNIMARC 格式记录不会处理为 880 模式;\r\nNo 中断整批保存操作)", "BiblioSearchForm", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2); if (result == System.Windows.Forms.DialogResult.No) goto END1; bAsked = true; } } else { strMarcSyntax = dlg.MarcSyntax; // TODO: 检查常用字段名和所选定的 MARC 格式是否矛盾。如果矛盾给出警告 } Debug.Assert(strMarcSyntax != "", ""); if (dlg.RemoveField998 == true) { MarcRecord temp = new MarcRecord(strRecord); temp.select("field[@name='998']").detach(); strRecord = temp.Text; } if (dlg.Mode880 == true && strMarcSyntax == "usmarc") { MarcRecord temp = new MarcRecord(strRecord); MarcQuery.To880(temp); strRecord = temp.Text; } // 将MARC机内格式转换为ISO2709格式 // parameters: // strSourceMARC [in]机内格式MARC记录。 // strMarcSyntax [in]为"unimarc"或"usmarc" // targetEncoding [in]输出ISO2709的编码方式。为UTF8、codepage-936等等 // baResult [out]输出的ISO2709记录。编码方式受targetEncoding参数控制。注意,缓冲区末尾不包含0字符。 // return: // -1 出错 // 0 成功 nRet = MarcUtil.CvtJineiToISO2709( strRecord, strMarcSyntax, targetEncoding, out baTarget, out strError); if (nRet == -1) goto ERROR1; /* Encoding sourceEncoding = connection.GetRecordsEncoding( this.MainForm, record.m_strSyntaxOID); if (sourceEncoding.Equals(targetEncoding) == true) { // source和target编码方式相同,不用转换 baTarget = record.m_baRecord; } else { nRet = ChangeIso2709Encoding( sourceEncoding, record.m_baRecord, targetEncoding, strMarcSyntax, out baTarget, out strError); if (nRet == -1) goto ERROR1; }*/ s.Write(baTarget, 0, baTarget.Length); if (dlg.CrLf == true) { byte[] baCrLf = targetEncoding.GetBytes("\r\n"); s.Write(baCrLf, 0, baCrLf.Length); } nCount++; stop.SetProgressValue(i + 1); } } catch (Exception ex) { strError = "写入文件 " + MainForm.LastIso2709FileName + " 失败,原因: " + ex.Message; goto ERROR1; } finally { s.Close(); stop.EndLoop(); stop.OnStop -= new StopEventHandler(this.DoStop); stop.Initial(""); stop.HideProgress(); this.EnableControls(true); } END1: // if (bAppend == true) MainForm.MessageText = nCount.ToString() + "条记录成功追加到文件 " + MainForm.LastIso2709FileName + " 尾部"; else MainForm.MessageText = nCount.ToString() + "条记录成功保存到新文件 " + MainForm.LastIso2709FileName + " 尾部"; return; ERROR1: MessageBox.Show(this, strError); }
public void menuItem_saveOriginRecordToIso2709_Click(object sender, EventArgs e) { string strError = ""; int nRet = 0; ZConnection connection = this.GetCurrentZConnection(); if (connection == null) { strError = "当前ZConnection为空"; goto ERROR1; } if (connection.VirtualItems.SelectedIndices.Count == 0) { strError = "尚未选定要保存记录的浏览行"; goto ERROR1; } bool bForceFull = false; if (HasSelectionContainBriefRecords() == true) { DialogResult result = MessageBox.Show(this, "即将保存的记录中有Brief(简要)格式的记录,是否在保存前重新获取为Full(完整)格式的记录?\r\n\r\n(Yes: 是,要完整格式的记录; No: 否,依然保存简明格式的记录; Cancel: 取消,放弃整个保存操作", "ZSearchForm", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1); if (result == System.Windows.Forms.DialogResult.Cancel) return; if (result == System.Windows.Forms.DialogResult.Yes) bForceFull = true; } Encoding preferredEncoding = null; // string strPreferedMarcSyntax = ""; { // 观察要保存的第一条记录的marc syntax int first_index = connection.VirtualItems.SelectedIndices[0]; VirtualItem first_item = connection.VirtualItems[first_index]; DigitalPlatform.Z3950.Record first_record = (DigitalPlatform.Z3950.Record)first_item.Tag; /* if (first_record.m_strMarcSyntaxOID == "1.2.840.10003.5.1") strPreferedMarcSyntax = "unimarc"; if (first_record.m_strMarcSyntaxOID == "1.2.840.10003.5.10") strPreferedMarcSyntax = "usmarc"; * */ preferredEncoding = connection.GetRecordsEncoding( this.MainForm, first_record.m_strSyntaxOID); } /* SaveFileDialog dlg = new SaveFileDialog(); dlg.FileName = MainForm.LastIso2709FileName; dlg.RestoreDirectory = true; dlg.CreatePrompt = true; dlg.OverwritePrompt = false; dlg.InitialDirectory = Environment.CurrentDirectory; dlg.Filter = "iso2709 files (*.mrc)|*.mrc|All files (*.*)|*.*"; if (dlg.ShowDialog() != DialogResult.OK) return; MainForm.LastIso2709FileName = dlg.FileName; * */ OpenMarcFileDlg dlg = new OpenMarcFileDlg(); GuiUtil.SetControlFont(dlg, this.Font); dlg.IsOutput = true; dlg.GetEncoding -= new GetEncodingEventHandler(dlg_GetEncoding); dlg.GetEncoding += new GetEncodingEventHandler(dlg_GetEncoding); dlg.FileName = this.MainForm.LastIso2709FileName; dlg.CrLf = this.MainForm.LastCrLfIso2709; dlg.RemoveField998Visible = false; dlg.Mode880Visible = false; // 暂时不支持 880 模式转换 dlg.EncodingListItems = Global.GetEncodingList(true); dlg.EncodingName = (String.IsNullOrEmpty(this.MainForm.LastEncodingName) == true ? GetEncodingForm.GetEncodingName(preferredEncoding) : this.MainForm.LastEncodingName); dlg.EncodingComment = "注: 原始编码方式为 " + GetEncodingForm.GetEncodingName(preferredEncoding); dlg.MarcSyntax = "<自动>"; // strPreferedMarcSyntax; dlg.EnableMarcSyntax = false; dlg.ShowDialog(this); if (dlg.DialogResult != DialogResult.OK) return; Encoding targetEncoding = null; if (dlg.EncodingName == "MARC-8" && preferredEncoding.Equals(this.MainForm.Marc8Encoding) == false) { strError = "保存操作无法进行。只有在记录的原始编码方式为 MARC-8 时,才能使用这个编码方式保存记录。"; goto ERROR1; } nRet = this.MainForm.GetEncoding(dlg.EncodingName, out targetEncoding, out strError); if (nRet == -1) { goto ERROR1; } // targetEncoding = dlg.Encoding; /* strPreferedMarcSyntax = dlg.MarcSyntax; if (strPreferedMarcSyntax == "<自动>") strPreferedMarcSyntax = ""; * */ string strLastFileName = this.MainForm.LastIso2709FileName; string strLastEncodingName = this.MainForm.LastEncodingName; bool bExist = File.Exists(dlg.FileName); bool bAppend = false; if (bExist == true) { DialogResult result = MessageBox.Show(this, "文件 '" + dlg.FileName + "' 已存在,是否以追加方式写入记录?\r\n\r\n--------------------\r\n注:(是)追加 (否)覆盖 (取消)放弃", "ZSearchForm", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1); if (result == DialogResult.Yes) bAppend = true; if (result == DialogResult.No) bAppend = false; if (result == DialogResult.Cancel) { strError = "放弃处理..."; goto ERROR1; } } // 检查同一个文件连续存时候的编码方式一致性 if (strLastFileName == dlg.FileName && bAppend == true) { if (strLastEncodingName != "" && strLastEncodingName != dlg.EncodingName) { DialogResult result = MessageBox.Show(this, "文件 '" + dlg.FileName + "' 已在先前已经用 " + strLastEncodingName + " 编码方式存储了记录,现在又以不同的编码方式 " + dlg.EncodingName + " 追加记录,这样会造成同一文件中存在不同编码方式的记录,可能会令它无法被正确读取。\r\n\r\n是否继续? (是)追加 (否)放弃操作", "ZSearchForm", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2); if (result == DialogResult.No) { strError = "放弃处理..."; goto ERROR1; } } } this.MainForm.LastIso2709FileName = dlg.FileName; this.MainForm.LastCrLfIso2709 = dlg.CrLf; this.MainForm.LastEncodingName = dlg.EncodingName; Stream s = null; try { s = File.Open(this.MainForm.LastIso2709FileName, FileMode.OpenOrCreate); if (bAppend == false) s.SetLength(0); else s.Seek(0, SeekOrigin.End); } catch (Exception ex) { strError = "打开或创建文件 " + this.MainForm.LastIso2709FileName + " 失败,原因: " + ex.Message; goto ERROR1; } try { for (int i = 0; i < connection.VirtualItems.SelectedIndices.Count; i++) { int index = connection.VirtualItems.SelectedIndices[i]; /* VirtualItem item = connection.VirtualItems[index]; DigitalPlatform.Z3950.Record record = (DigitalPlatform.Z3950.Record)item.Tag; */ byte[] baTimestamp = null; string strSavePath = ""; string strOutStyle = ""; LoginInfo logininfo = null; long lVersion = 0; string strXmlFragment = ""; DigitalPlatform.Z3950.Record record = null; Encoding currentEncoding = null; string strMARC = ""; nRet = this.GetOneRecord( "marc", index, // 即将废止 "index:" + index.ToString(), bForceFull == true ? "force_full" : "", // false, out strSavePath, out strMARC, out strXmlFragment, out strOutStyle, out baTimestamp, out lVersion, out record, out currentEncoding, out logininfo, out strError); if (nRet == -1) goto ERROR1; byte[] baTarget = null; Encoding sourceEncoding = connection.GetRecordsEncoding( this.MainForm, record.m_strSyntaxOID); string strMarcSyntax = ""; if (record.m_strSyntaxOID == "1.2.840.10003.5.1") strMarcSyntax = "unimarc"; if (record.m_strSyntaxOID == "1.2.840.10003.5.10") strMarcSyntax = "usmarc"; if (sourceEncoding.Equals(targetEncoding) == true) { // source和target编码方式相同,不用转换 // baTarget = record.m_baRecord; // 规范化 ISO2709 物理记录 // 主要是检查里面的记录结束符是否正确,去掉多余的记录结束符 baTarget = MarcUtil.CononicalizeIso2709Bytes(targetEncoding, record.m_baRecord); } else { nRet = ChangeIso2709Encoding( sourceEncoding, record.m_baRecord, targetEncoding, strMarcSyntax, out baTarget, out strError); if (nRet == -1) goto ERROR1; } s.Write(baTarget, 0, baTarget.Length); if (dlg.CrLf == true) { byte[] baCrLf = targetEncoding.GetBytes("\r\n"); s.Write(baCrLf, 0, baCrLf.Length); } } // if (bAppend == true) this.MainForm.MessageText = connection.VirtualItems.SelectedIndices.Count.ToString() + "条记录成功追加到文件 " + this.MainForm.LastIso2709FileName + " 尾部"; else this.MainForm.MessageText = connection.VirtualItems.SelectedIndices.Count.ToString() + "条记录成功保存到新文件 " + this.MainForm.LastIso2709FileName + " 尾部"; } catch (Exception ex) { strError = "写入文件 " + this.MainForm.LastIso2709FileName + " 失败,原因: " + ex.Message; goto ERROR1; } finally { s.Close(); } return; ERROR1: MessageBox.Show(this, strError); }
// 准备输入 // return: // -1 出错 // 0 正常 // 1 用户放弃 public int Begin( IWin32Window owner, ApplicationInfo appInfo, string strInputFileName, out string strError) { strError = ""; this.m_owner = owner; this.AppInfo = appInfo; this.Index = -1; if (string.IsNullOrEmpty(strInputFileName) == true) { strError = "输入文件名不能为空"; return -1; } if (this.Stream != null) { this.Stream.Close(); this.Stream = null; this.FileName = ""; } string strExt = Path.GetExtension(strInputFileName); if (string.Compare(strExt, ".xml", true) == 0) this.FileType = ExportFileType.XmlFile; else if (string.Compare(strExt, ".dp2bak", true) == 0) this.FileType = ExportFileType.BackupFile; else if (string.Compare(strExt, ".iso", true) == 0 || string.Compare(strExt, ".marc", true) == 0 || string.Compare(strExt, ".mrc", true) == 0) this.FileType = ExportFileType.ISO2709File; else { strError = "无法根据文件扩展名 '" + strExt + "' 判断输入文件的格式"; return -1; } this.FileName = strInputFileName; if (this.FileType == ExportFileType.XmlFile) { this.Stream = File.Open(strInputFileName, FileMode.Open, FileAccess.Read); this._reader = new XmlTextReader(Stream); bool bRet = false; // 移动到根元素 while (true) { bRet = _reader.Read(); if (bRet == false) { strError = "没有根元素"; return -1; } if (_reader.NodeType == XmlNodeType.Element) break; } // 移动到其下级第一个element while (true) { bRet = _reader.Read(); if (bRet == false) { strError = "没有第一个记录元素"; return -1; } if (_reader.NodeType == XmlNodeType.Element) break; } } else if (this.FileType == ExportFileType.BackupFile) { this.Stream = File.Open(strInputFileName, FileMode.Open, FileAccess.Read); } // ISO2709文件需要预先准备条件 else if (this.FileType == ExportFileType.ISO2709File) { // 询问encoding和marcsyntax OpenMarcFileDlg dlg = new OpenMarcFileDlg(); Font font = GuiUtil.GetDefaultFont(); if (font != null) dlg.Font = font; dlg.Text = "请指定要导入的 ISO2709 文件属性"; dlg.FileName = strInputFileName; if (this.AppInfo != null) this.AppInfo.LinkFormState(dlg, "restree_OpenMarcFileDlg_input_state"); dlg.ShowDialog(this.m_owner); if (this.AppInfo != null) this.AppInfo.UnlinkFormState(dlg); if (dlg.DialogResult != DialogResult.OK) { strError = "用户取消"; return 1; } this.FileName = dlg.FileName; this.Stream = File.Open(this.FileName, FileMode.Open, FileAccess.Read); this.MarcSyntax = dlg.MarcSyntax; this.Encoding = dlg.Encoding; } return 0; }