string GetMacroValue(string strMacroName) { // return strMacroName + "--"; string strError = ""; string strResultValue = ""; int nRet = 0; // 2015/7/10 // @accessNo 只能由前端处理 if (strMacroName == "@accessNo") return strMacroName; // 2015/10/10 if (strMacroName.IndexOf("%") != -1) { ParseOneMacroEventArgs e1 = new ParseOneMacroEventArgs(); e1.Macro = strMacroName; e1.Simulate = false; m_macroutil_ParseOneMacro(this, e1); if (e1.Canceled == true) goto CONTINUE; if (string.IsNullOrEmpty(e1.ErrorInfo) == false) { return strMacroName + ":error:" + e1.ErrorInfo; } return e1.Value; } CONTINUE: // 书目记录XML格式 string strXmlBody = ""; if (Global.IsAppendRecPath(this.BiblioRecPath) == true || this.BiblioChanged == true) // 2010/12/5 add { // 如果记录路径表明这是一条待存的新记录,那就需要准备好strXmlBody,以便获取宏的时候使用 nRet = this.GetBiblioXml( "", // 迫使从记录路径中看marc格式 true, // 包含资源ID out strXmlBody, out strError); if (nRet == -1) return strError; } // 获取书目记录的局部 nRet = GetBiblioPart(this.BiblioRecPath, strXmlBody, strMacroName, out strResultValue, out strError); if (nRet == -1) { if (String.IsNullOrEmpty(strResultValue) == true) return strMacroName + ":error:" + strError; return strResultValue; } return strResultValue; }
void m_macroutil_ParseOneMacro(object sender, ParseOneMacroEventArgs e) { // string strError = ""; string strName = StringUtil.Unquote(e.Macro, "%%"); // 去掉百分号 // 函数名: string strFuncName = ""; string strParams = ""; int nRet = strName.IndexOf(":"); if (nRet == -1) { strFuncName = strName.Trim(); } else { strFuncName = strName.Substring(0, nRet).Trim(); strParams = strName.Substring(nRet + 1).Trim(); } if (strName == "username") { e.Value = this.CurrentUserName; return; } string strValue = ""; string strError = ""; // 从marceditor_macrotable.xml文件中解析宏 // return: // -1 error // 0 not found // 1 found nRet = MacroUtil.GetFromLocalMacroTable( Path.Combine(this.MainForm.DataDir, "marceditor_macrotable.xml"), strName, e.Simulate, out strValue, out strError); if (nRet == -1) { e.Canceled = true; e.ErrorInfo = strError; return; } if (nRet == 1) { e.Value = strValue; return; } if (String.Compare(strFuncName, "IncSeed", true) == 0 || String.Compare(strFuncName, "IncSeed+", true) == 0 || String.Compare(strFuncName, "+IncSeed", true) == 0) { // 种次号库名, 指标名, 要填充到的位数 string[] aParam = strParams.Split(new char[] { ',' }); if (aParam.Length != 3 && aParam.Length != 2) { strError = "IncSeed需要2或3个参数。"; goto ERROR1; } bool IncAfter = false; // 是否为先取后加 if (strFuncName[strFuncName.Length - 1] == '+') IncAfter = true; string strZhongcihaoDbName = aParam[0].Trim(); string strEntryName = aParam[1].Trim(); strValue = ""; LibraryChannel channel = this.GetChannel(); try { long lRet = 0; if (e.Simulate == true) { // parameters: // strZhongcihaoGroupName @引导种次号库名 !引导线索书目库名 否则就是 种次号组名 lRet = channel.GetZhongcihaoTailNumber( null, strZhongcihaoDbName, strEntryName, out strValue, out strError); if (lRet == -1) goto ERROR1; if (string.IsNullOrEmpty(strValue) == true) { strValue = "1"; } } else { // parameters: // strZhongcihaoGroupName @引导种次号库名 !引导线索书目库名 否则就是 种次号组名 lRet = channel.SetZhongcihaoTailNumber( null, IncAfter == true ? "increase+" : "increase", strZhongcihaoDbName, strEntryName, "1", out strValue, out strError); if (lRet == -1) goto ERROR1; } } finally { this.ReturnChannel(channel); } // 补足左方'0' if (aParam.Length == 3) { int nWidth = 0; try { nWidth = Convert.ToInt32(aParam[2]); } catch { strError = "第三参数应当为纯数字(表示补足的宽度)"; goto ERROR1; } e.Value = strValue.PadLeft(nWidth, '0'); } else e.Value = strValue; return; } e.Canceled = true; // 不能解释处理 return; ERROR1: e.Canceled = true; e.ErrorInfo = strError; }
// 解析宏 public int Parse( bool bSimulate, string strMacro, out string strResult, out string strError) { strError = ""; int nCurPos = 0; string strPart = ""; strResult = ""; for (; ;) { try { strPart = NextMacro(strMacro, ref nCurPos); } catch (Exception ex) { strError = ExceptionUtil.GetAutoText(ex); return(-1); } if (strPart == "") { break; } if (strPart[0] == '%') { // 兑现宏的内容 ParseOneMacroEventArgs e = new ParseOneMacroEventArgs(); e.Macro = strPart; e.Simulate = bSimulate; this.ParseOneMacro(this, e); if (e.Canceled == true) { // 不能处理的宏 strResult += strPart; continue; } if (String.IsNullOrEmpty(e.ErrorInfo) == false) { strError = "解析宏'" + strPart + "' 时发生错误: '" + e.ErrorInfo + "'"; return(-1); // strResult += "(解析宏'" + strPart + "' 时发生错误: '"+e.ErrorInfo+"')" ; // continue; } strResult += e.Value; } else { strResult += strPart; } } return(1); }
// 解析宏 public int Parse( bool bSimulate, string strMacro, out string strResult, out string strError) { strError = ""; int nCurPos = 0; string strPart = ""; strResult = ""; for (; ; ) { try { strPart = NextMacro(strMacro, ref nCurPos); } catch (Exception ex) { strError = ex.Message; return -1; } if (strPart == "") break; if (strPart[0] == '%') { // 兑现宏的内容 ParseOneMacroEventArgs e = new ParseOneMacroEventArgs(); e.Macro = strPart; e.Simulate = bSimulate; this.ParseOneMacro(this, e); if (e.Canceled == true) { // 不能处理的宏 strResult += strPart; continue; } if (String.IsNullOrEmpty(e.ErrorInfo) == false) { strError = "解析宏'" + strPart + "' 时发生错误: '" + e.ErrorInfo + "'"; return -1; // strResult += "(解析宏'" + strPart + "' 时发生错误: '"+e.ErrorInfo+"')" ; // continue; } strResult += e.Value; } else { strResult += strPart; } } return 1; }
// 解析每一个宏 void m_macroutil_ParseOneMacro(object sender, ParseOneMacroEventArgs e) { string strError = ""; string strName = Unquote(e.Macro); // 去掉百分号 // 函数名: string strFuncName = ""; string strParams = ""; int nRet = strName.IndexOf(":"); if (nRet == -1) { strFuncName = strName.Trim(); } else { strFuncName = strName.Substring(0, nRet).Trim(); strParams = strName.Substring(nRet + 1).Trim(); } if (String.Compare(strFuncName, "IncSeed", true) == 0) { string[] aParam = strParams.Split(new char[] {','}); if (aParam.Length != 3 && aParam.Length != 2) { strError = "IncSeed需要2或3个参数。"; goto ERROR1; } ResPath respath = new ResPath(textBox_recPath.Text); m_seedmanager.Initial(this.MainForm.SearchPanel, respath.Url, aParam[0].Trim()); string strValue = ""; if (e.Simulate == true) { nRet = m_seedmanager.GetSeed( aParam[1].Trim(), out strValue, out strError); if (nRet == 0) { nRet = 1; strValue = "1"; } } else { nRet = m_seedmanager.IncSeed( aParam[1].Trim(), "1", out strValue, out strError); } if (nRet == -1) goto ERROR1; // 补足左方'0' if (aParam.Length == 3) { int nWidth = 0; try { nWidth = Convert.ToInt32(aParam[2]); } catch { strError = "第三参数应当为纯数字(表示补足的宽度)"; goto ERROR1; } e.Value = strValue.PadLeft(nWidth, '0'); } else e.Value = strValue; return; } e.Canceled = true; // 不能解释处理 return; ERROR1: e.ErrorInfo = strError; }
void m_macroutil_ParseOneMacro(object sender, ParseOneMacroEventArgs e) { string strError = ""; string strName = Unquote(e.Macro); // 去掉百分号 // 函数名: string strFuncName = ""; string strParams = ""; int nRet = strName.IndexOf(":"); if (nRet == -1) { strFuncName = strName.Trim(); } else { strFuncName = strName.Substring(0, nRet).Trim(); strParams = strName.Substring(nRet + 1).Trim(); } if (strName == "username" && String.IsNullOrEmpty(this.SavePath) == false) { e.Value = this.CurrentUserName; return; } string strValue = ""; // 从marceditor_macrotable.xml文件中解析宏 // return: // -1 error // 0 not found // 1 found nRet = MacroUtil.GetFromLocalMacroTable(PathUtil.MergePath(this.MainForm.DataDir, "marceditor_macrotable.xml"), strName, e.Simulate, out strValue, out strError); if (nRet == -1) { e.Canceled = true; e.ErrorInfo = strError; return; } if (nRet == 1) { e.Value = strValue; return; } ERROR1: e.Canceled = true; // 不能解释处理 return; }