-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMicroLIFFormatParser.cs
More file actions
112 lines (95 loc) · 3.95 KB
/
MicroLIFFormatParser.cs
File metadata and controls
112 lines (95 loc) · 3.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
namespace MicroLIFParser
{
public class MicroLIFFormatParser
{
readonly StreamReader reader;
public MicroLIFFormatParser()
{
var filePath = Directory.GetCurrentDirectory() + @"\LIFs\SampleLif2";
reader = new StreamReader(File.Open(filePath, FileMode.Open));
}
public MarcRecord Current
{
get;
private set;
}
private MarcRecord ParseNextRecord()
{
try
{
string currentLine;
var currentMarc = new MarcRecord();
bool eor;
var firstLine = reader.ReadLine();
var secondLine = reader.ReadLine();
do
{
if (firstLine != null && !firstLine.StartsWith("LDR")) //If not LDR then it is probably Software name and version so need to skip
{
eor = false;
firstLine = null;
continue;
}
if (!string.IsNullOrEmpty(firstLine))
currentLine = firstLine;
else
currentLine = reader.ReadLine();
if (currentLine == null)
{
reader.Dispose();
currentMarc = null;
break;
}
eor = currentLine.EndsWith('`'); //end of record
if (currentLine.StartsWith("LDR"))
currentMarc.Leader = currentLine.Substring(3).TrimEnd('`').TrimEnd('^');
else //control or MARC fields
{
var currentTag = currentLine.Substring(0, 3);
if (int.Parse(currentTag) < 10) //control fields
currentMarc.ControlFields.Add(currentTag, currentLine.Substring(3).TrimEnd('`').TrimEnd('^'));
else //Marc fields
{
var subFieldValues = currentLine.Substring(3).TrimEnd('`').TrimEnd('^');
var indicators = subFieldValues.Substring(0, 2);
var marcField = new MarcField();
marcField.Tag = int.Parse(currentTag);
marcField.Ind1 = indicators[0];
marcField.Ind2 = indicators[1];
var subFields = subFieldValues.Substring(2).Split('_').Where(x => !string.IsNullOrEmpty(x)).ToList();
foreach (var subfield in subFields)
marcField.Subfields.Add(new MarcSubfield { Code = subfield.First(), Data = subfield.Substring(1).Trim() });
//currentMarc.MarcFields[marcField.Tag].Add(marcField);
if (currentMarc.MarcFields.ContainsKey(marcField.Tag))
currentMarc.MarcFields[marcField.Tag].Add(marcField);
else
{
List<MarcField> newTagCollection = new List<MarcField> { marcField };
currentMarc.MarcFields[marcField.Tag] = newTagCollection;
}
}
}
if (firstLine != null) //making sure that first line is clean
firstLine = null;
} while (!eor);
return currentMarc;
}
catch (Exception e)
{
throw;
}
}
public bool MoveNext()
{
Current = ParseNextRecord();
if (Current == null)
return false;
return true;
}
}
}