예제 #1
0
        // 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);
        }
예제 #2
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>");
        }