public LoaderItem(BiblioInfo info, ListViewItem item) { this.BiblioInfo = info; this.ListViewItem = item; }
public IEnumerator GetEnumerator() { Debug.Assert(m_loader != null, ""); Hashtable dup_table = new Hashtable(); // 确保 recpaths 中不会出现重复的路径 List<string> recpaths = new List<string>(); // 缓存中没有包含的那些记录 foreach (ListViewItem item in this.Items) { string strRecPath = item.Text; Debug.Assert(string.IsNullOrEmpty(strRecPath) == false, ""); #if NO BiblioInfo info = (BiblioInfo)this.CacheTable[strRecPath]; if (info == null || string.IsNullOrEmpty(info.OldXml) == true) recpaths.Add(strRecPath); #endif if (dup_table.ContainsKey(strRecPath) == true) continue; BiblioInfo info = (BiblioInfo)this.CacheTable[strRecPath]; if (info == null || string.IsNullOrEmpty(info.OldXml) == true) { recpaths.Add(strRecPath); dup_table[strRecPath] = true; } } // 注: Hashtable 在这一段时间内不应该被修改。否则会破坏 m_loader 和 items 之间的锁定对应关系 m_loader.RecPaths = recpaths; var enumerator = m_loader.GetEnumerator(); // 开始循环 foreach (ListViewItem item in this.Items) { string strRecPath = item.Text; Debug.Assert(string.IsNullOrEmpty(strRecPath) == false, ""); BiblioInfo info = (BiblioInfo)this.CacheTable[strRecPath]; if (info == null || string.IsNullOrEmpty(info.OldXml) == true) { if (m_loader.Stop != null) { m_loader.Stop.SetMessage("正在获取书目记录 " + strRecPath); } bool bRet = enumerator.MoveNext(); if (bRet == false) { Debug.Assert(false, "还没有到结尾, MoveNext() 不应该返回 false"); // TODO: 这时候也可以采用返回一个带没有找到的错误码的元素 yield break; } BiblioItem biblio = (BiblioItem)enumerator.Current; Debug.Assert(biblio.RecPath == strRecPath, "m_loader 和 items 的元素之间 记录路径存在严格的锁定对应关系"); // 需要放入缓存 if (info == null) { info = new BiblioInfo(); info.RecPath = biblio.RecPath; } info.OldXml = biblio.Content; info.Timestamp = biblio.Timestamp; this.CacheTable[strRecPath] = info; yield return new LoaderItem(info, item); } else yield return new LoaderItem(info, item); } }
int GetXmlHtml(BiblioInfo info, out string strXml1, out string strXml2, out string strMarcHtml, out string strBiblioHtml, out string strError) { strError = ""; strXml1 = ""; strXml2 = ""; strMarcHtml = ""; strBiblioHtml = ""; int nRet = 0; strXml1 = info.OldXml; strXml2 = info.NewXml; string strMarcSyntax = ""; string strOldMARC = ""; string strOldFragmentXml = ""; if (string.IsNullOrEmpty(strXml1) == false) { string strOutMarcSyntax = ""; // 将XML格式转换为MARC格式 // 自动从数据记录中获得MARC语法 nRet = MarcUtil.Xml2Marc(strXml1, MarcUtil.Xml2MarcStyle.Warning | MarcUtil.Xml2MarcStyle.OutputFragmentXml, "", out strOutMarcSyntax, out strOldMARC, out strOldFragmentXml, out strError); if (nRet == -1) { strError = "XML转换到MARC记录时出错: " + strError; return -1; } strMarcSyntax = strOutMarcSyntax; } string strNewMARC = ""; string strNewFragmentXml = ""; if (string.IsNullOrEmpty(strXml2) == false) { string strOutMarcSyntax = ""; // 将XML格式转换为MARC格式 // 自动从数据记录中获得MARC语法 nRet = MarcUtil.Xml2Marc(strXml2, MarcUtil.Xml2MarcStyle.Warning | MarcUtil.Xml2MarcStyle.OutputFragmentXml, "", out strOutMarcSyntax, out strNewMARC, out strNewFragmentXml, out strError); if (nRet == -1) { strError = "XML转换到MARC记录时出错: " + strError; return -1; } strMarcSyntax = strOutMarcSyntax; } string strMARC = ""; if (string.IsNullOrEmpty(strOldMARC) == false && string.IsNullOrEmpty(strNewMARC) == false) { // 创建展示两个 MARC 记录差异的 HTML 字符串 // return: // -1 出错 // 0 成功 nRet = MarcDiff.DiffHtml( strOldMARC, strOldFragmentXml, "", strNewMARC, strNewFragmentXml, "", out strMarcHtml, out strError); if (nRet == -1) return -1; strMARC = strNewMARC; } else if (string.IsNullOrEmpty(strOldMARC) == false && string.IsNullOrEmpty(strNewMARC) == true) { strMarcHtml = MarcUtil.GetHtmlOfMarc(strOldMARC, strOldFragmentXml, "", false); strMARC = strOldMARC; } else if (string.IsNullOrEmpty(strOldMARC) == true && string.IsNullOrEmpty(strNewMARC) == false) { strMarcHtml = MarcUtil.GetHtmlOfMarc(strNewMARC, strNewFragmentXml, "", false); strMARC = strNewMARC; } // return: // -1 出错 // 0 .fltx 文件没有找到 // 1 成功 nRet = this.MainForm.BuildMarcHtmlText( MarcDetailForm.GetSyntaxOID(strMarcSyntax), strMARC, out strBiblioHtml, out strError); if (nRet == -1) strBiblioHtml = strError.Replace("\r\n", "<br/>"); return 0; }
int GetBiblioInfo( bool bCheckSearching, ListViewItem item, out BiblioInfo info, out string strError) { strError = ""; info = null; if (this.m_biblioTable == null) return 0; string strRecPath = item.Text; if (string.IsNullOrEmpty(strRecPath) == true) return 0; // 存储所获得书目记录 XML info = (BiblioInfo)this.m_biblioTable[strRecPath]; if (info == null || string.IsNullOrEmpty(info.OldXml) == true) { if (bCheckSearching == true && this._linkMarcFile == null) { if (this.InSearching == true) return 0; } // 解析记录路径 string strServerName = ""; string strPurePath = ""; ParseRecPath(strRecPath, out strServerName, out strPurePath); // 获得server url dp2Server server = this.dp2ResTree1.Servers.GetServerByName(strServerName); if (server == null) { strError = "名为 '" + strServerName + "' 的服务器在检索窗中尚未定义..."; return -1; } string strServerUrl = server.Url; this.Channel = this.Channels.GetChannel(strServerUrl); string[] results = null; byte[] baTimestamp = null; // 获得书目记录 long lRet = Channel.GetBiblioInfos( stop, strPurePath, "", new string[] { "xml" }, // formats out results, out baTimestamp, out strError); if (lRet == 0) return -1; // 是否设定为特殊状态? if (lRet == -1) return -1; if (results == null || results.Length == 0) { strError = "results error"; return -1; } string strXml = results[0]; // 滞后创建新对象,避免在 hashtable 中存在一个尚未初始化的对象,而被其他线程抢先使用了 if (info == null) { info = new BiblioInfo(); info.RecPath = strRecPath; this.m_biblioTable[strRecPath] = info; } info.OldXml = strXml; info.Timestamp = baTimestamp; info.RecPath = strRecPath; } return 1; }
// 将刚刚 paste 进入的新行,进行处理,以便达到功能完满的水平 // TODO: 其实当前浏览列表中是应该允许 UNIMARC 和 USMARC 两类记录并存的 void ConvertPastedLines() { string strError = ""; int nRet = 0; AmazonSearchForm amazon_searchform = this.MainForm.GetAmazonSearchForm(); foreach (ListViewItem item in this.listView_browse.SelectedItems) { if (item.Tag is AmazonSearchForm.ItemInfo) { AmazonSearchForm.ItemInfo origin = (AmazonSearchForm.ItemInfo)item.Tag; string strRecPath = ListViewUtil.GetItemText(item, 0); string strMARC = ""; nRet = amazon_searchform.GetItemInfo(origin, out strMARC, out strError); if (nRet == -1) goto ERROR1; string strXml = ""; nRet = MarcUtil.Marc2Xml(strMARC, "unimarc", out strXml, out strError); if (nRet == -1) goto ERROR1; strRecPath = strRecPath + " @mem"; BiblioInfo info = new BiblioInfo(); info.RecPath = strRecPath; this.m_biblioTable[strRecPath] = info; info.OldXml = strXml; info.Timestamp = null; info.RecPath = strRecPath; string strSytaxOID = "1.2.840.10003.5.1"; string strBrowseText = ""; nRet = BuildMarcBrowseText( strSytaxOID, strMARC, out strBrowseText, out strError); string[] cols = strBrowseText.Split(new char[] { '\t' }); // 修改浏览行 ChangeCols( item, strRecPath, cols); } } 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; }
// 是否为尚未初始化的 BiblioInfo static bool IsNullBiblioInfo(BiblioInfo info) { if (string.IsNullOrEmpty(info.OldXml) == true && string.IsNullOrEmpty(info.NewXml) == true) return true; return false; }