/// <summary> /// Decodes the raw MARC record into a <see cref="MARC.Record"/> at the specified index./// /// </summary> /// <param name="index">The index of the record to retrieve.</param> /// <returns></returns> private Record Decode(int index) { XElement record = _rawSource[index]; Record marcXml = new Record(); //First we get the leader marcXml.Leader = record.Elements().First(e => e.Name.LocalName == "leader").Value; //Now we get the control fields foreach (XElement controlField in record.Elements().Where(e => e.Name.LocalName == "controlfield")) { ControlField newField = new ControlField(controlField.Attribute("tag").Value, controlField.Value); marcXml.Fields.Add(newField); } //Now we get the data fields foreach (XElement dataField in record.Elements().Where(e => e.Name.LocalName == "datafield")) { DataField newField = new DataField(dataField.Attribute("tag").Value, new List <Subfield>(), dataField.Attribute("ind1").Value[0], dataField.Attribute("ind2").Value[0]); foreach (XElement subfield in dataField.Elements().Where(e => e.Name.LocalName == "subfield")) { Subfield newSubfield = new Subfield(subfield.Attribute("code").Value[0], subfield.Value); newField.Subfields.Add(newSubfield); } marcXml.Fields.Add(newField); } return(marcXml); }
/// <summary> /// Inserts the subfield into position before the first subfield found with a higher code. /// Numbers always get sorted after letters /// This assumes the subfield has already been sorted. /// </summary> /// <param name="newSubfield">The new subfield.</param> public void InsertSubfield(Subfield newSubfield) { int rowNum = 0; foreach (Subfield subfield in subfields) { int x; if (!Int32.TryParse(subfield.Code.ToString(), out x) && !Int32.TryParse(newSubfield.Code.ToString(), out x) && subfield.Code.CompareTo(newSubfield.Code) > 0) { subfields.Insert(rowNum, newSubfield); return; } else if (Int32.TryParse(subfield.Code.ToString(), out x) && !Int32.TryParse(newSubfield.Code.ToString(), out x)) { subfields.Insert(rowNum, newSubfield); return; } else if (Int32.TryParse(subfield.Code.ToString(), out x) && subfield.Code.CompareTo(newSubfield.Code) > 0) { subfields.Insert(rowNum, newSubfield); return; } rowNum++; } //Insert at the end subfields.Add(newSubfield); }
/// <summary> /// Gets the first <see cref="MARC.Subfield"/> with the specified code. /// </summary> /// <value>The first matching subfield or null if not found</value> public Subfield this[char code] { get { Subfield foundSubfield = null; foreach (Subfield subfield in subfields) { if (subfield.Code == code) { foundSubfield = subfield; break; } } return(foundSubfield); } }
public void CloneTest() { char code = 'a'; string data = "Test Data"; Subfield target = new Subfield(code, data); Subfield expected = target; Subfield actual; actual = target.Clone(); Assert.AreNotEqual(expected, actual); target.Code = 'b'; string expectedString = "a"; string actualString; actualString = actual.Code.ToString(); Assert.AreEqual(expectedString, actualString); target.Data = "New Test Data"; expectedString = data; actualString = actual.Data; Assert.AreEqual(expectedString, actualString); }
public void CloneTest() { string tag = "100"; List<Subfield> subfields = new List<Subfield>(); Subfield subfield = new Subfield('a', "It's a book!"); subfields.Add(subfield); subfield = new Subfield('b', "Anne Author"); subfields.Add(subfield); Field target = new DataField(tag, subfields); Field actual; actual = target.Clone(); Assert.AreNotEqual(target, actual); target.Tag = "200"; string expectedString = tag; string actualString; actualString = actual.Tag; Assert.AreEqual(expectedString, actualString); ((DataField)target).Indicator1 = '1'; ((DataField)target).Indicator2 = '2'; expectedString = " "; actualString = ((DataField)actual).Indicator1.ToString() + ((DataField)actual).Indicator2.ToString(); Assert.AreEqual(expectedString, actualString); ((DataField)target)['a'].Code = 'c'; ((DataField)target)['c'].Data = "It's a NEW book!"; expectedString = "aIt's a book!"; actualString = ((DataField)actual)['a'].Code.ToString() + ((DataField)actual)['a'].Data; Assert.AreEqual(expectedString, actualString); //This next line will fail as there's no subfield c! ((DataField)actual)['c'].Code = 'a'; }
public void ToStringTest() { string tag = "100"; List<Subfield> subfields = new List<Subfield>(); Subfield subfield = new Subfield('a', "It's a book!"); subfields.Add(subfield); subfield = new Subfield('b', "Anne Author"); subfields.Add(subfield); DataField target = new DataField(tag, subfields); string expected = "100 [a]: It's a book!" + Environment.NewLine + " [b]: Anne Author"; string actual; actual = target.ToString(); Assert.AreEqual(expected, actual); }
public void DataFieldConstructorTest3() { string tag = "100"; List<Subfield> subfields = new List<Subfield>(); Subfield subfield = new Subfield('a', "It's a book!"); subfields.Add(subfield); subfield = new Subfield('b', "Anne Author"); subfields.Add(subfield); char ind1 = '1'; DataField target = new DataField(tag, subfields, ind1); { char expected = '1'; char actual = target.Indicator1; Assert.AreEqual(expected, actual); expected = ' '; actual = target.Indicator2; Assert.AreEqual(expected, actual); expected = 'a'; actual = target['a'].Code; Assert.AreEqual(expected, actual); expected = 'b'; actual = target['b'].Code; Assert.AreEqual(expected, actual); } { string expected = "100"; string actual = target.Tag; Assert.AreEqual(expected, actual); expected = "It's a book!"; actual = target['a'].Data; Assert.AreEqual(expected, actual); expected = "Anne Author"; actual = target['b'].Data; Assert.AreEqual(expected, actual); } { int expected = 2; int actual = target.Subfields.Count; Assert.AreEqual(expected, actual); } }
public void SubfieldsTest() { string tag = "100"; List<Subfield> subfields = new List<Subfield>(); Subfield subfield = new Subfield('a', "It's a book!"); subfields.Add(subfield); subfield = new Subfield('b', "Anne Author"); subfields.Add(subfield); DataField target = new DataField(tag, subfields); List<Subfield> expected = new List<Subfield>(); subfield = new Subfield('a', "It's another book!"); expected.Add(subfield); subfield = new Subfield('b', "Anne Otter Author"); expected.Add(subfield); List<Subfield> actual; target.Subfields = expected; actual = target.Subfields; Assert.AreEqual(expected.Count, actual.Count); for (int i = 0; i < expected.Count; i++) { Assert.AreEqual(expected[i].Code, actual[i].Code); Assert.AreEqual(expected[i].Data, actual[i].Data); } }
public void ToRawTest() { string tag = "100"; List<Subfield> subfields = new List<Subfield>(); Subfield subfield = new Subfield('a', "It's a book!"); subfields.Add(subfield); subfield = new Subfield('b', "Anne Author"); subfields.Add(subfield); DataField target = new DataField(tag, subfields); string expected = " " + FileMARC.SUBFIELD_INDICATOR.ToString() + "aIt's a book!" + FileMARC.SUBFIELD_INDICATOR.ToString() + "bAnne Author" + FileMARC.END_OF_FIELD.ToString(); string actual; actual = target.ToRaw(); Assert.AreEqual(expected, actual); }
public void IsEmptyTest() { string tag = "100"; DataField target = new DataField(tag); bool expected = true; bool actual; actual = target.IsEmpty(); Assert.AreEqual(expected, actual); List<Subfield> subfields = new List<Subfield>(); Subfield subfield = new Subfield('a', "It's a book!"); subfields.Add(subfield); subfield = new Subfield('b', "Anne Author"); subfields.Add(subfield); subfield = new Subfield('b', "Anne Otter Author"); subfields.Add(subfield); target = new DataField(tag, subfields); expected = false; actual = target.IsEmpty(); Assert.AreEqual(expected, actual); }
public void ItemTest() { string tag = "100"; List<Subfield> subfields = new List<Subfield>(); Subfield subfield = new Subfield('a', "It's a book!"); subfields.Add(subfield); subfield = new Subfield('b', "Anne Author"); subfields.Add(subfield); DataField target = new DataField(tag, subfields); char code = 'a'; Subfield expected = new Subfield('a', "It's a book!"); Subfield actual; actual = target[code]; Assert.AreEqual(expected.Code, actual.Code); Assert.AreEqual(expected.Data, actual.Data); }
public void CodeTest() { char code = 'a'; string data = "Test Data"; Subfield target = new Subfield(code, data); char expected = 'b'; char actual; target.Code = expected; actual = target.Code; Assert.AreEqual(expected, actual); }
public void FormatFieldTest1() { string tag = "600"; List<Subfield> subfields = new List<Subfield>(); Subfield subfield = new Subfield('a', "It's a book!"); subfields.Add(subfield); subfield = new Subfield('b', "Anne Author"); subfields.Add(subfield); subfield = new Subfield('c', "Some text"); subfields.Add(subfield); subfield = new Subfield('d', "Some more text"); subfields.Add(subfield); subfield = new Subfield('v', "Some fancy text"); subfields.Add(subfield); subfield = new Subfield('z', "Some more fancy text"); subfields.Add(subfield); DataField target = new DataField(tag, subfields); string expected = "It's a book! Anne Author Some text Some more text -- Some fancy text -- Some more fancy text"; string actual; actual = target.FormatField(); Assert.AreEqual(expected, actual); }
public void DataTest() { char code = 'a'; string data = "Test Data"; Subfield target = new Subfield(code, data); string expected = "Test Data Again"; string actual; target.Data = expected; actual = target.Data; Assert.AreEqual(expected, actual); }
public void ToStringTest() { char code = 'a'; string data = "Test Data"; Subfield target = new Subfield(code, data); string expected = "[a]: Test Data"; string actual; actual = target.ToString(); Assert.AreEqual(expected, actual); }
public void ToRawTest() { char code = 'a'; string data = "Test Data"; Subfield target = new Subfield(code, data); string expected = FileMARC.SUBFIELD_INDICATOR.ToString() + "aTest Data"; string actual; actual = target.ToRaw(); Assert.AreEqual(expected, actual); }
public void SubfieldConstructorTest() { char code = 'a'; string data = "Test Data"; Subfield target = new Subfield(code, data); Assert.IsInstanceOfType(target, typeof(Subfield)); { char expected = 'a'; char actual = target.Code; Assert.AreEqual(expected, actual); } { string expected = "Test Data"; string actual = target.Data; Assert.AreEqual(expected, actual); } }
public void IsEmptyTest() { char code = 'a'; string data = "Test Data"; Subfield target = new Subfield(code, data); bool expected = false; bool actual; actual = target.IsEmpty(); Assert.AreEqual(expected, actual); target.Data = null; expected = true; actual = target.IsEmpty(); Assert.AreEqual(expected, actual); target.Data = string.Empty; actual = target.IsEmpty(); Assert.AreEqual(expected, actual); }
/// <summary> /// Decodes the raw MARC record into a <see cref="MARC.Record"/> at the specified index./// /// </summary> /// <param name="index">The index of the record to retrieve.</param> /// <returns></returns> private Record Decode(int index) { XElement record = _rawSource[index]; Record marcXml = new Record(); //First we get the leader marcXml.Leader = record.Elements().First(e => e.Name.LocalName == "leader").Value; //Now we get the control fields foreach (XElement controlField in record.Elements().Where(e => e.Name.LocalName == "controlfield")) { ControlField newField = new ControlField(controlField.Attribute("tag").Value, controlField.Value); marcXml.Fields.Add(newField); } //Now we get the data fields foreach (XElement dataField in record.Elements().Where(e => e.Name.LocalName == "datafield")) { DataField newField = new DataField(dataField.Attribute("tag").Value, new List<Subfield>(), dataField.Attribute("ind1").Value[0], dataField.Attribute("ind2").Value[0]); foreach (XElement subfield in dataField.Elements().Where(e => e.Name.LocalName == "subfield")) { Subfield newSubfield = new Subfield(subfield.Attribute("code").Value[0], subfield.Value); newField.Subfields.Add(newSubfield); } marcXml.Fields.Add(newField); } return marcXml; }
/// <summary> /// Make a deep clone of this instance. /// </summary> /// <returns></returns> public Subfield Clone() { Subfield clone = new Subfield(code, data); return(clone); }