static string BuildHeaderHtml(bool bModified, string strField) { if (string.IsNullOrEmpty(strField) == true) { // return "\r\n<td class='content' colspan='3'></td>"; return("<td class='fieldname'></td>" + "<td class='indicator'></td>" + "<td class='content'></td>"); } // string strLineClass = ""; string strFieldName = ""; string strIndicatior = ""; string strContent = ""; // strLineClass = "header"; strField = strField.Replace(' ', '_'); strContent = MarcUtil.GetHtmlFieldContent(strField, false); string strTypeClass = ""; if (bModified) { strTypeClass = " modified"; } // return("\r\n<td class='fieldname" + strTypeClass + "'>" + strFieldName + "</td>" + "<td class='indicator" + strTypeClass + "'>" + strIndicatior + "</td>" + "<td class='content" + strTypeClass + "'>" + strContent + "</td>"); }
/// <summary> /// 根据一个 Field 对象的内容创建一个新的 SubfieldCollection 对象 /// </summary> /// <param name="container">Field 对象,也被当作要创建的 SubfieldCollection 对象的容器</param> /// <returns>新创建的 SubfieldCollection 对象</returns> public static SubfieldCollection BuildSubfields(Field container) { SubfieldCollection subfields = new SubfieldCollection(); string strField = container.Text; for (int i = 0; ; i++) { string strSubfield = ""; string strNextSubfieldName = ""; // 从字段或子字段组中得到一个子字段 // parameters: // strText 字段内容,或者子字段组内容。 // textType 表示strText中包含的是字段内容还是组内容。若为ItemType.Field,表示strText参数中为字段;若为ItemType.Group,表示strText参数中为子字段组。 // strSubfieldName 子字段名,内容为1位字符。如果==null,表示任意子字段 // 形式为'a'这样的。 // nIndex 想要获得同名子字段中的第几个。从0开始计算。 // strSubfield [out]输出子字段。子字段名(1字符)、子字段内容。 // strNextSubfieldName [out]下一个子字段的名字,内容一个字符 // return: // -1 出错 // 0 所指定的子字段没有找到 // 1 找到。找到的子字段返回在strSubfield参数中 int nRet = MarcUtil.GetSubfield( strField, ItemType.Field, null, i, out strSubfield, out strNextSubfieldName); if (nRet == -1 || nRet == 0) { break; } Subfield subfield = new Subfield(); subfield.Text = strSubfield; subfield.Container = subfields; subfields.Add(subfield); } subfields.Container = container; return(subfields); }
// 将 MARC 机内格式字符串写入。也就是执行转换的意思。调用后, XML 结果需要从 TextWriter 中取出 public int WriteRecord( string strMARC, out string strError) { strError = ""; int nRet = 0; string[] saField = null; nRet = MarcUtil.ConvertMarcToFieldArray(strMARC, out saField, out strError); if (nRet == -1) { return(-1); } return(WriteRecord( saField, out strError)); }
// 从 ISO2709 文件中读出第一条记录。返回的是机内格式 // return: // -1 出错 // 0 正常 /*public*/ static int LoadFirstRecord(string strMarcFileName, Encoding encoding, out string strMARC, out string strError) { strError = ""; strMARC = ""; try { using (Stream stream = File.Open(strMarcFileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { // 从ISO2709文件中读入一条MARC记录 // return: // -2 MARC格式错 // -1 出错 // 0 正确 // 1 结束(当前返回的记录有效) // 2 结束(当前返回的记录无效) int nRet = MarcUtil.ReadMarcRecord(stream, encoding, true, // bRemoveEndCrLf, true, // bForce, out strMARC, out strError); if (nRet == 0 || nRet == 1) { return(0); // 正常 } strError = "读入MARC记录时出错: " + strError; return(-1); } } catch (Exception ex) { strError = "异常: " + ex.Message; return(-1); } }
/// <summary> /// 获得符合条件的若干个字段对象 /// </summary> /// <param name="strFieldName">字段名</param> /// <param name="strIndicatorMatch">字段指示符筛选条件。缺省为 "**"</param> /// <returns>字段对象集合</returns> public FieldCollection GetFields(string strFieldName, string strIndicatorMatch = "**") { FieldCollection fields = new FieldCollection(); foreach (Field field in this) { if (field.m_strName == strFieldName) { if (strIndicatorMatch != "**" && string.IsNullOrEmpty(field.m_strIndicator) == false && MarcUtil.MatchIndicator(strIndicatorMatch, field.m_strIndicator) == false) { continue; } // 这里要特别注意 fields.Add(field); } } return(fields); }
void DisplayFirstRecord(string strFileName, Encoding encoding) { if (string.IsNullOrEmpty(strFileName) == true) { goto ERROR1; } if (File.Exists(strFileName) == false) { goto ERROR1; } if (encoding == null) { goto ERROR1; } string strMARC = ""; string strError = ""; // return: // -1 出错 // 0 正常 int nRet = LoadFirstRecord(strFileName, encoding, out strMARC, out strError); if (nRet == -1) { goto ERROR1; } if (this.Mode880 == true && (this.comboBox_marcSyntax.Text == "USMARC" || this.comboBox_marcSyntax.Text == "<自动>")) { if (this.IsOutput == false) { MarcRecord temp = new MarcRecord(strMARC); MarcQuery.ToParallel(temp); strMARC = temp.Text; } } string strHead = @"<head> <style type='text/css'> BODY { FONT-FAMILY: Microsoft YaHei, Verdana, 宋体; FONT-SIZE: 8pt; } TABLE.marc { font-size: 8pt; width: auto; } TABLE.marc TD { vertical-align:text-top; } TABLE.marc TR.header { background-color: #eeeeee; } TABLE.marc TR.datafield { } TABLE.marc TD.fieldname { border: 0px; border-top: 1px; border-style: dotted; border-color: #cccccc; } TABLE.marc TD.fieldname, TABLE.marc TD.indicator, TABLE.marc TR.header TD.content, TABLE.marc SPAN { font-family: Courier New, Tahoma, Arial, Helvetica, sans-serif; font-weight: bold; } TABLE.marc TD.indicator { padding-left: 4px; padding-right: 4px; border: 0px; border-left: 1px; border-right: 1px; border-style: dotted; border-color: #eeeeee; } TABLE.marc SPAN.subfield { margin: 2px; margin-left: 0px; line-height: 140%; border: 1px; border-style: solid; border-color: #cccccc; padding-top: 1px; padding-bottom: 1px; padding-left: 3px; padding-right: 3px; font-weight: bold; color: Blue; background-color: Yellow; } TABLE.marc SPAN.fieldend { margin: 2px; margin-left: 4px; border: 1px; border-style: solid; border-color: #cccccc; padding-top: 1px; padding-bottom: 1px; padding-left: 3px; padding-right: 3px; font-weight: bold; color: White; background-color: #cccccc; } </style> </head>"; string strHtml = "<html>" + strHead + "<body>" + MarcUtil.GetHtmlOfMarc(strMARC, false) + "</body></html>"; AppendHtml(this.webBrowser1, strHtml, true); if (this.IsOutput == false && this.Visible == true) { HideMessageTip(); ShowMessageTip(); } return; ERROR1: ClearHtml(); }
// return: // 0 成功 // -1 出错 public int WriteRecord( string[] saField, out string strError) { string strFieldName = null; // int nRet; strError = ""; // long lStart = writer.BaseStream.Position; // Debug.Assert(writer.BaseStream.CanSeek == true, "writer.BaseStream.CanSeek != true"); //根据WriteMarcPrefix的值,确定是否对元素record加命名空间 if (WriteMarcPrefix == false) { _writer.WriteStartElement("record"); } else { _writer.WriteStartElement(MarcPrefix, "record", MarcNameSpaceUri); } if (String.IsNullOrEmpty(_writer.LookupPrefix("dprms")) == true) { // dprms名字空间 2010/11/15 _writer.WriteAttributeString("xmlns", "dprms", null, DpNs.dprms); } //循环,写头标区及每个子段 for (int i = 0; i < saField.Length; i++) { string strLine = saField[i]; string strInd1 = null; string strInd2 = null; string strContent = null; // 头标区 if (i == 0) { // 多截少添 if (strLine.Length > 24) { strLine = strLine.Substring(0, 24); } else { while (strLine.Length < 24) { strLine += " "; } } if (WriteMarcPrefix == false) { _writer.WriteElementString("leader", strLine); } else { _writer.WriteElementString("leader", MarcNameSpaceUri, strLine); } continue; } Debug.Assert(strLine != null, ""); // 不合法的字段,不算数 if (strLine.Length < 3) { continue; } strFieldName = strLine.Substring(0, 3); if (strLine.Length >= 3) { strContent = strLine.Substring(3); } else { strContent = ""; } // control field 001-009 没有子字段 /* * if ( (String.Compare(strFieldName, "001") >= 0 * && String.Compare(strFieldName, "009") <= 0 ) || String.Compare(strFieldName, "-01") == 0) * */ if (MarcUtil.IsControlFieldName(strFieldName) == true) { if (WriteMarcPrefix == false) { _writer.WriteStartElement("controlfield"); } else { _writer.WriteStartElement(MarcPrefix, "controlfield", MarcNameSpaceUri); } _writer.WriteAttributeString("tag", strFieldName); _writer.WriteString(strContent); _writer.WriteEndElement(); continue; } if (strLine.Length == 3) { strInd1 = " "; strInd2 = " "; strContent = ""; } //字段长度等于4的情况,这样做是为了防止越界 else if (strLine.Length == 4) { strInd1 = strContent[0].ToString(); strInd2 = " "; strContent = ""; } else { strInd1 = strContent[0].ToString(); strInd2 = strContent[1].ToString(); strContent = strContent.Substring(2); } // 普通字段 if (WriteMarcPrefix == false) { _writer.WriteStartElement("datafield"); } else { _writer.WriteStartElement(MarcPrefix, "datafield", MarcNameSpaceUri); } _writer.WriteAttributeString("tag", strFieldName); _writer.WriteAttributeString("ind1", strInd1); _writer.WriteAttributeString("ind2", strInd2); // 得到子字段数组 string[] aSubfield = strContent.Split(new char[] { (char)31 }); if (aSubfield == null) { // 不太可能发生 continue; } // 循环写子字段 for (int j = 0; j < aSubfield.Length; j++) { string strValue = aSubfield[j]; string strSubfieldName = ""; string strSubfieldContent = ""; if (j == 0) { // 第一个空字符串要被跳过。其余的,将来返还时会用来产生一个单独的 31 字符 if (string.IsNullOrEmpty(aSubfield[0]) == true) { continue; } strSubfieldName = null; // 表示后面不要创建code属性 strSubfieldContent = strValue; } else { if (string.IsNullOrEmpty(strValue) == false) { strSubfieldName = strValue.Substring(0, 1); } if (string.IsNullOrEmpty(strValue) == false) { strSubfieldContent = strValue.Substring(1); } } if (WriteMarcPrefix == false) { _writer.WriteStartElement("subfield"); } else { _writer.WriteStartElement(MarcPrefix, "subfield", MarcNameSpaceUri); } if (strSubfieldName != null) { _writer.WriteAttributeString("code", strSubfieldName); } _writer.WriteString(strSubfieldContent); //注意这里是否有越界的危险 _writer.WriteEndElement(); } _writer.WriteEndElement(); } _writer.WriteEndElement(); return(0); }
// 创建一个字段的 HTML 局部 三个 <td> static string BuildFieldHtml( ChangeType type, string strField, bool bSubfieldReturn = false) { if (string.IsNullOrEmpty(strField) == true) { return("\r\n<td class='content' colspan='3'></td>"); } string strLineClass = ""; string strFieldName = ""; string strIndicatior = ""; string strContent = ""; // 取字段名 if (strField.Length < 3) { strFieldName = strField; strField = ""; } else { strFieldName = strField.Substring(0, 3); strField = strField.Substring(3); } // 取指示符 if (MarcUtil.IsControlFieldName(strFieldName) == true) { strLineClass = "controlfield"; strField = strField.Replace(' ', '_'); } else { if (strField.Length < 2) { strIndicatior = strField; strField = ""; } else { strIndicatior = strField.Substring(0, 2); strField = strField.Substring(2); } strIndicatior = strIndicatior.Replace(' ', '_'); strLineClass = "datafield"; // 1XX字段有定长内容 if (strFieldName.Length >= 1 && strFieldName[0] == '1') { strField = strField.Replace(' ', '_'); strLineClass += " fixedlengthsubfield"; } } strContent = MarcUtil.GetHtmlFieldContent(strField, bSubfieldReturn); string strTypeClass = ""; if (type == ChangeType.Modified) { strTypeClass = " modified"; } else if (type == ChangeType.Inserted) { strTypeClass = " inserted"; } else if (type == ChangeType.Deleted) { strTypeClass = " deleted"; } else if (type == ChangeType.Imaginary) { strTypeClass = " imaginary"; } else if (type == ChangeType.Unchanged) { strTypeClass = " unchanged"; } // return("\r\n<td class='fieldname" + strTypeClass + "'>" + strFieldName + "</td>" + "<td class='indicator" + strTypeClass + "'>" + strIndicatior + "</td>" + "<td class='content" + strTypeClass + "'>" + strContent + "</td>"); }
// 把byte[]类型的MARC记录转换为机内格式 // return: // -2 MARC格式错 // -1 一般错误 // 0 正常 public static int ConvertByteArrayToMarcRecord(byte[] baRecord, Encoding encoding, bool bForce, out string strMarc, out string strError) { strError = ""; strMarc = ""; int nRet = 0; bool bUcs2 = false; if (encoding.Equals(Encoding.Unicode) == true) { bUcs2 = true; } List <byte[]> aField = new List <byte[]>(); if (bForce == true || bUcs2 == true) { nRet = MarcUtil.ForceCvt2709ToFieldArray( ref encoding, baRecord, out aField, out strError); Debug.Assert(nRet != -2, ""); /* * if (bUcs2 == true) * { * // 转换后,编码方式已经变为UTF8 * Debug.Assert(encoding.Equals(Encoding.UTF8), ""); * } * */ } else { //??? nRet = MarcUtil.Cvt2709ToFieldArray( encoding, baRecord, out aField, out strError); } if (nRet == -1) { return(-1); } if (nRet == -2) //marc出错 { return(-2); } string[] saField = null; GetMarcRecordString(aField, encoding, out saField); if (saField.Length > 0) { string strHeader = saField[0]; if (strHeader.Length > 24) { strHeader = strHeader.Substring(0, 24); } else { strHeader = saField[0].PadRight(24, '*'); } StringBuilder text = new StringBuilder(1024); text.Append(strHeader); for (int i = 1; i < saField.Length; i++) { text.Append(saField[i] + new string(FLDEND, 1)); } strMarc = text.ToString().Replace("\r", "*").Replace("\n", "*"); // 2012/3/16 return(0); } return(0); }