/// <summary> /// 在 ISSN 字符串中适当的位置插入'-'符号 /// 如果提供的ISBN字符串本来就有978前缀,那么结果仍将保留前缀。如果本来就没有,结果里面也没有。 /// </summary> /// <param name="strISSN">ISSN 字符串</param> /// <param name="strStyle">处理风格。force8/force13/auto/remainverifychar/strict</param> /// <param name="strTarget">返回处理结果</param> /// <param name="strError">返回出错信息</param> /// <returns>-1:出错; 0:未修改校验位; 1:修改了校验位</returns> public static int IssnInsertHyphen( string strISSN, string strStyle, out string strTarget, out string strError) { strTarget = ""; strError = ""; string strSource; // int nSecondLen; // Debug.Assert(false, ""); strSource = strISSN; strSource = strSource.Trim(); bool bHasRemovePrefix977 = false; // 是否有977前缀 bool bForce8 = StringUtil.IsInList("force8", strStyle); bool bForce13 = StringUtil.IsInList("force13", strStyle); bool bAuto = StringUtil.IsInList("auto", strStyle); bool bRemainVerifyChar = StringUtil.IsInList("remainverifychar", strStyle); // 是否不要重新计算校验位 bool bStrict = StringUtil.IsInList("strict", strStyle); // 是否严格要求strISBN输入参数为10或13位 int nCount = 0; if (bForce8 == true) { nCount++; } if (bForce13 == true) { nCount++; } if (bAuto == true) { nCount++; } if (nCount > 1) { strError = "strStyle值 '" + strStyle + "' 中的force8/force13/auto 3种风格是互相排斥,不能同时具备。"; return(-1); } strSource = strSource.Replace("-", ""); strSource = strSource.Replace(" ", ""); bool bAdjustLength = false; // 是否调整过输入的strISSN的长度 if (bStrict == false) { if (strSource.Length == 7) { strSource += '0'; bRemainVerifyChar = false; // 必须要重新计算校验位了 bAdjustLength = true; } else if (strSource.Length == 12) { strSource += '0'; bRemainVerifyChar = false; // 必须要重新计算校验位了 bAdjustLength = true; } } string strPrefix = "977"; // 13位、无-、前缀为977 if (strSource.Length == 13 && strSource.IndexOf("-") == -1 && strSource.Substring(0, 3) == "977" ) { if (strSource.Length >= 3) { strPrefix = strSource.Substring(0, 3); } strSource = strSource.Substring(3, 10); // 丢弃前3位,但不丢弃校验位 bHasRemovePrefix977 = true; } if (strSource.Length != 8 && strSource.Length != 10 && strSource.Length != 13) { strError = "ISSN中(除'-'以外)应为8位、10位或13位有效字符(" + strSource + " " + Convert.ToString(strSource.Length) + ")"; return(-1); } // 2017/9/19 if (strSource.Length == 8) { strSource = strSource.Substring(0, 7); // 丢掉原有 8 位的校验位 } if (strSource.Length < 10) { strSource = strSource.PadRight(10, '0'); } strTarget = strSource; strTarget = strTarget.Insert(4, "-"); strTarget = strTarget.Insert(4 + 3 + 1, "-"); strTarget = strTarget.Insert(7 + 2 + 1 + 1, "-"); if (bForce13 == true) { if (strTarget.Length == 13) { strTarget = strPrefix + "-" + strTarget; } } else if (bAuto == true && bHasRemovePrefix977 == true) { strTarget = strPrefix + "-" + strTarget; } bool bVerifyChanged = false; // 重新计算校验码 // 重新添加ISBN-10的校验位。因为条码号中ISBN-13校验位算法不同。 if (bRemainVerifyChar == false) { if (strTarget.Length == 13) { char old_ver = strTarget[12]; strTarget = strTarget.Substring(0, strTarget.Length - 1 - 4); char v = GetIssn8VerifyChar(strTarget); strTarget += new string(v, 1); if (old_ver != v) { bVerifyChanged = true; } } else if (strTarget.Length == 17) { char old_ver = strTarget[16]; strTarget = strTarget.Substring(0, strTarget.Length - 1); char v = GetIsbn13VerifyChar(strTarget); strTarget += new string(v, 1); if (old_ver != v) { bVerifyChanged = true; } } } if (bHasRemovePrefix977 == true && bForce8 == true) { return(0); // 移走977后,校验位肯定要发生变化。因此不通知这种变化 } if (bAdjustLength == false && bForce13 == true && strISSN.Trim().Replace("-", "").Length == 8) { return(0); // 加入了前缀后,校验位肯定要发生变化,因此不通知这种变化 } if (bVerifyChanged == true) { return(1); } return(0); }
// 观察一个馆藏分配字符串,看看是否在指定用户权限的管辖范围内 // parameters: // return: // -1 出错 // 0 超过管辖范围(至少出现一处超过范围)。strError中有解释 // 1 在管辖范围内 public static int DistributeInControlled(string strDistribute, string strLibraryCodeList, out bool bAllOutOf, out string strError) { strError = ""; bAllOutOf = false; // bNarrow 如果为 true,表示 馆代码 "" 只匹配总馆,不包括各个分馆;如果为 false,表示 馆代码 "" 匹配总馆和所有分馆 bool bNarrow = strLibraryCodeList == "[仅总馆]"; if (strLibraryCodeList == "[仅总馆]") { strLibraryCodeList = ""; } if (bNarrow == false && IsGlobalUser(strLibraryCodeList) == true) { return(1); } // 2018/5/9 if (string.IsNullOrEmpty(strDistribute)) { // 去向分配字符串为空,表示谁都可以控制它。这样便于分馆用户修改。 // 若这种情况返回 0,则分馆用户修改不了,只能等总馆用户才有权限修改 return(1); } LocationCollection locations = new LocationCollection(); int nRet = locations.Build(strDistribute, out strError); if (nRet == -1) { strError = "馆藏分配字符串 '" + strDistribute + "' 格式不正确"; return(-1); } List <string> outof_list = new List <string>(); foreach (Location location in locations) { // 空的馆藏地点被视为不在分馆用户管辖范围内 if (bNarrow == false && string.IsNullOrEmpty(location.Name) == true) { //strError = "馆代码 '' 不在范围 '" + strLibraryCodeList + "' 内"; //return 0; outof_list.Add(""); continue; } // 解析 ParseCalendarName(location.Name, out string strLibraryCode, out string strPureName); if (string.IsNullOrEmpty(strLibraryCode) && string.IsNullOrEmpty(strLibraryCodeList)) { continue; } if (StringUtil.IsInList(strLibraryCode, strLibraryCodeList) == false) { //strError = "馆代码 '" + strLibraryCode + "' 不在范围 '" + strLibraryCodeList + "' 内"; //return 0; outof_list.Add(strLibraryCode); } } if (outof_list.Count > 0) { strError = "馆代码 '" + StringUtil.MakePathList(outof_list) + "' 不在范围 '" + strLibraryCodeList + "' 内"; if (outof_list.Count == locations.Count) { bAllOutOf = true; } return(0); } return(1); }
/// <summary> /// 在 ISBN 字符串中适当的位置插入'-'符号 /// 如果提供的ISBN字符串本来就有978前缀,那么结果仍将保留前缀。如果本来就没有,结果里面也没有。 /// </summary> /// <param name="strISBN">ISBN 字符串</param> /// <param name="strStyle">处理风格。force10/force13/auto/remainverifychar/strict</param> /// <param name="strTarget">返回处理结果</param> /// <param name="strError">返回出错信息</param> /// <returns>-1:出错; 0:未修改校验位; 1:修改了校验位</returns> public int IsbnInsertHyphen( string strISBN, string strStyle, out string strTarget, out string strError) { strTarget = ""; strError = ""; string strSource; // int nFirstLen = 0; int nSecondLen; // Debug.Assert(false, ""); strSource = strISBN; strSource = strSource.Trim(); bool bHasRemovePrefix978 = false; // 是否有978前缀 bool bForce10 = StringUtil.IsInList("force10", strStyle); bool bForce13 = StringUtil.IsInList("force13", strStyle); bool bAuto = StringUtil.IsInList("auto", strStyle); bool bRemainVerifyChar = StringUtil.IsInList("remainverifychar", strStyle); // 是否不要重新计算校验位 bool bStrict = StringUtil.IsInList("strict", strStyle); // 是否严格要求strISBN输入参数为10或13位 int nCount = 0; if (bForce10 == true) { nCount++; } if (bForce13 == true) { nCount++; } if (bAuto == true) { nCount++; } if (nCount > 1) { strError = "strStyle值 '" + strStyle + "' 中的force10/force13/auto 3种风格是互相排斥,不能同时具备。"; return(-1); } strSource = strSource.Replace("-", ""); strSource = strSource.Replace(" ", ""); bool bAdjustLength = false; // 是否调整过输入的strISBN的长度 if (bStrict == false) { if (strSource.Length == 9) { strSource += '0'; bRemainVerifyChar = false; // 必须要重新计算校验位了 bAdjustLength = true; } else if (strSource.Length == 12) { strSource += '0'; bRemainVerifyChar = false; // 必须要重新计算校验位了 bAdjustLength = true; } } string strPrefix = "978"; // 13位、无-、前缀为978 if (strSource.Length == 13 && strSource.IndexOf("-") == -1 && (strSource.Substring(0, 3) == "978" || strSource.Substring(0, 3) == "979") ) { if (strSource.Length >= 3) { strPrefix = strSource.Substring(0, 3); } strSource = strSource.Substring(3, 10); // 丢弃前3位,但不丢弃校验位 bHasRemovePrefix978 = true; } if (strSource.Length != 10 && strSource.Length != 13) { strError = "ISBN中(除'-'以外)应为10位或13位有效字符(" + strSource + " " + Convert.ToString(strSource.Length) + ")"; return(-1); } if (bForce10 && strPrefix == "979") { strError = "979 前缀的 ISBN 不能变为 ISBN-10 形态"; return(-1); } string strFirstPart = ""; // 第一部分 XmlElement hit_prefix = null; XmlNodeList prefix_nodes = dom.DocumentElement.SelectNodes("RegistrationGroups/Group/Prefix"); if (prefix_nodes.Count == 0) { strError = "ISBN 规则文件格式有误,无法选择到任何 RegistrationGroups/Group/Prefix 元素"; return(-1); } string strTemp = strPrefix + strSource; foreach (XmlElement prefix in prefix_nodes) { string strCurrent = prefix.InnerText.Trim().Replace("-", ""); if (strTemp.StartsWith(strCurrent) == true) { hit_prefix = prefix; strFirstPart = strCurrent.Substring(3); // nFirstLen = strFirstPart.Length; break; } } if (hit_prefix == null) { strError = "prefix 部分格式错误"; // 是否需要解释一下? return(-1); } XmlNodeList nodes = hit_prefix.ParentNode.SelectNodes("Rules/Rule"); if (nodes.Count == 0) { strError = "ISBN 数据中 没有找到 prefix='" + strFirstPart + "'的 Rules/Rule 元素 ..."; return(-1); } string strSecondPart = ""; foreach (XmlElement node in nodes) { Range range = GetRangeValue(node); if (range == null) { continue; // TODO: 需要报错 } #if NO if (strLeft.Length != strRight.Length) { strError = "数据节点 " + node.OuterXml + "格式错误, value值'" + strValue + "'中两个数字宽度不等。"; return(-1); } #endif int nWidth = range.Left.Length; if (nWidth == 0) { continue; // 可能数据有错误? } if (nWidth != strSecondPart.Length) { strSecondPart = strSource.Substring(strFirstPart.Length, nWidth); } if (InRange(strSecondPart, range.Left, range.Right) == true) { nSecondLen = nWidth; goto FINISH; } } strError = "第二部分格式错误 nFirstLen=[" + Convert.ToString(strFirstPart.Length) + "]"; return(-1); FINISH: strTarget = strSource; strTarget = strTarget.Insert(strFirstPart.Length, "-"); strTarget = strTarget.Insert(strFirstPart.Length + nSecondLen + 1, "-"); strTarget = strTarget.Insert(9 + 1 + 1, "-"); if (bForce13 == true) { if (strTarget.Length == 13) { strTarget = strPrefix + "-" + strTarget; } } else if (bAuto == true && bHasRemovePrefix978 == true) { strTarget = strPrefix + "-" + strTarget; } bool bVerifyChanged = false; // 重新计算校验码 // 重新添加ISBN-10的校验位。因为条码号中ISBN-13校验位算法不同。 if (bRemainVerifyChar == false) { if (strTarget.Length == 13) { char old_ver = strTarget[12]; strTarget = strTarget.Substring(0, strTarget.Length - 1); char v = GetIsbn10VerifyChar(strTarget); strTarget += new string(v, 1); if (old_ver != v) { bVerifyChanged = true; } } else if (strTarget.Length == 17) { char old_ver = strTarget[16]; strTarget = strTarget.Substring(0, strTarget.Length - 1); char v = GetIsbn13VerifyChar(strTarget); strTarget += new string(v, 1); if (old_ver != v) { bVerifyChanged = true; } } } if (bHasRemovePrefix978 == true && bForce10 == true) { return(0); // 移走978后,校验位肯定要发生变化。因此不通知这种变化 } if (bAdjustLength == false && bForce13 == true && strISBN.Trim().Replace("-", "").Length == 10) { return(0); // 加入了前缀后,校验位肯定要发生变化,因此不通知这种变化 } if (bVerifyChanged == true) { return(1); } return(0); }