/// <summary> DEMO 3: Read the resulting demo2.dat file, change the title, publisher, and add /// a subject field and then save it again</summary> private static void demo3() { Console.WriteLine("Performing demo3"); // Create the marc21 exchange reader MARC21_Exchange_Format_Parser parser1 = new MARC21_Exchange_Format_Parser(); // Read the record MARC_Record record = parser1.Parse("demo2.dat"); // If this is null, say so if (record == null) { Console.WriteLine("Unable to read the demo2.dat in the 3rd demo portion"); return; } // Change the title field ( 245 ) record[245][0].Add_NonRepeatable_Subfield('a', "New Title"); // Also change the creator field (110 in this case) record[110][0].Add_NonRepeatable_Subfield('a', "Corn Maze Production, Incorporated"); // Add a new field to record MARC_Field newSubject = record.Add_Field(650, ' ', '0'); newSubject.Add_Subfield('a', "Soils"); newSubject.Add_Subfield('x', "Phosphorous content"); newSubject.Add_Subfield('z', "Indonesia"); // Save this as XML and also as Marc21 record.Save_MARC_XML("demo3.xml"); record.Save_MARC21("demo3.dat"); }
/// <summary> Writes the formatted metadata from the provided item to a TextWriter (usually to an output stream) </summary> /// <param name="Output_Stream"></param> /// <param name="Item_To_Save"> Package with all the metadata to save </param> /// <param name="Options"> Dictionary of any options which this metadata reader/writer may utilize </param> /// <param name="Error_Message">[OUTPUT] Explanation of the error, if an error occurs during write </param> /// <returns>TRUE if successful, otherwise FALSE </returns> /// <remarks>This writer accepts one option value. 'MarcXML_File_ReaderWriter:Additional_Tags' is a List of additional /// <see cref="MARC_Field"/> tags which should be added to the standard tags written (Default is NULL).</remarks> public bool Write_Metadata(TextWriter Output_Stream, SobekCM_Item Item_To_Save, Dictionary <string, object> Options, out string Error_Message) { // Set default error outpt message Error_Message = String.Empty; // Try to pull some values from the options string cataloging_source_code = String.Empty; if ((Options.ContainsKey("MarcXML_File_ReaderWriter:MARC Cataloging Source Code")) && (Options["MarcXML_File_ReaderWriter:MARC Cataloging Source Code"] != null)) { cataloging_source_code = Options["MarcXML_File_ReaderWriter:MARC Cataloging Source Code"].ToString(); } string location_code = String.Empty; if ((Options.ContainsKey("MarcXML_File_ReaderWriter:MARC Location Code")) && (Options["MarcXML_File_ReaderWriter:MARC Location Code"] != null)) { location_code = Options["MarcXML_File_ReaderWriter:MARC Location Code"].ToString(); } string reproduction_agency = String.Empty; if ((Options.ContainsKey("MarcXML_File_ReaderWriter:MARC Reproduction Agency")) && (Options["MarcXML_File_ReaderWriter:MARC Reproduction Agency"] != null)) { reproduction_agency = Options["MarcXML_File_ReaderWriter:MARC Reproduction Agency"].ToString(); } string reproduction_place = String.Empty; if ((Options.ContainsKey("MarcXML_File_ReaderWriter:MARC Reproduction Place")) && (Options["MarcXML_File_ReaderWriter:MARC Reproduction Place"] != null)) { reproduction_place = Options["MarcXML_File_ReaderWriter:MARC Reproduction Place"].ToString(); } string system_name = String.Empty; if ((Options.ContainsKey("MarcXML_File_ReaderWriter:System Name")) && (Options["MarcXML_File_ReaderWriter:System Name"] != null)) { system_name = Options["MarcXML_File_ReaderWriter:System Name"].ToString(); } string system_abbreviation = String.Empty; if ((Options.ContainsKey("MarcXML_File_ReaderWriter:System Abbreviation")) && (Options["MarcXML_File_ReaderWriter:System Abbreviation"] != null)) { system_abbreviation = Options["MarcXML_File_ReaderWriter:System Abbreviation"].ToString(); } string thumbnail_base = String.Empty; if ((Options.ContainsKey("MarcXML_File_ReaderWriter:Image_Base")) && (Options["MarcXML_File_ReaderWriter:Image_Base"] != null)) { thumbnail_base = Options["MarcXML_File_ReaderWriter:Image_Base"].ToString(); } // Get all the standard tags MARC_Record tags = Item_To_Save.To_MARC_Record(cataloging_source_code, location_code, reproduction_agency, reproduction_place, system_name, system_abbreviation, thumbnail_base); // Look for extra tags to add in the OPTIONS if (Options.ContainsKey("MarcXML_File_ReaderWriter:Additional_Tags")) { object add_tags_obj = Options["MarcXML_File_ReaderWriter:Additional_Tags"]; if (add_tags_obj != null) { try { List <MARC_Field> add_tags = (List <MARC_Field>)add_tags_obj; foreach (MARC_Field thisTag in add_tags) { tags.Add_Field(thisTag); } } catch { Error_Message = "Unable to cast provided option ( MarcXML_File_ReaderWriter:Additional_Tags ) to List<MARC_Field> object"; return(false); } } } // Start to build the XML result Output_Stream.WriteLine("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>"); Output_Stream.WriteLine("<collection xmlns=\"http://www.loc.gov/MARC21/slim\">"); Output_Stream.WriteLine(" <record>"); // Add the leader Output_Stream.WriteLine(" <leader>" + tags.Leader + "</leader>"); foreach (MARC_Field thisTag in tags.Sorted_MARC_Tag_List) { if ((thisTag.Tag >= 1) && (thisTag.Tag <= 8)) { Output_Stream.WriteLine(" <controlfield tag=\"" + thisTag.Tag.ToString().PadLeft(3, '0') + "\">" + Convert_String_To_XML_Safe(thisTag.Control_Field_Value).Replace("&bar;", "|") + "</controlfield>"); } else { Output_Stream.WriteLine(" <datafield tag=\"" + thisTag.Tag.ToString().PadLeft(3, '0') + "\" ind1=\"" + thisTag.Indicators[0] + "\" ind2=\"" + thisTag.Indicators[1] + "\">"); string[] splitter = thisTag.Control_Field_Value.Split("|".ToCharArray()); foreach (string subfield in splitter) { if (subfield.Length > 2) { Output_Stream.WriteLine(" <subfield code=\"" + subfield[0] + "\">" + Convert_String_To_XML_Safe(subfield.Substring(2).Trim()).Replace("&bar;", "|") + "</subfield>"); } } Output_Stream.WriteLine(" </datafield>"); } } Output_Stream.WriteLine(" </record>"); Output_Stream.WriteLine("</collection>"); return(true); }
/// <summary> Writes a digital resource in MARC format, with additional considerations for displaying within a HTML page </summary> /// <param name="package"> Digital resource to write in MARC format </param> /// <param name="Width"> Width of the resulting HTML-formatted MARC record </param> /// <param name="add_tags"> Any additional MARC tags which should be included besides the standard MARC tags </param> /// <returns> MARC record formatted for HTML, returned as a string </returns> public string MARC_HTML(SobekCM_Item package, string Width, List <MARC_Field> add_tags) { // Get all the standard tags MARC_Record tags = package.To_MARC_Record(); // Add any additional tags included here if (add_tags != null) { foreach (MARC_Field thisTag in add_tags) { tags.Add_Field(thisTag); } } // Start to build the HTML result StringBuilder results = new StringBuilder(); results.Append("<table border=\"0\" align=\"center\" width=\"" + Width + "\">\n"); // Add the LEADER results.Append(" <tr class=\"trGenContent\">\n"); results.Append(" <td width=\"33\" valign=\"top\">LDR</td>\n"); results.Append(" <td width=\"26\" valign=\"top\"> </td>\n"); results.Append(" <td>" + tags.Leader.Replace(" ", "^") + "</td>\n"); results.Append(" </tr>"); // Add all the FIELDS foreach (MARC_Field thisTag in tags.Sorted_MARC_Tag_List) { results.Append(" <tr class=\"trGenContent\">\n"); results.Append(" <td width=\"33\" valign=\"top\">" + thisTag.Tag.ToString().PadLeft(3, '0') + "</td>\n"); results.Append(" <td width=\"26\" valign=\"top\"><font color=\"green\">" + thisTag.Indicators.Replace(" ", " ") + "</font></td>\n"); results.Append(" <td>"); if ((thisTag.Tag == 8) || (thisTag.Tag == 7) || (thisTag.Tag == 6)) { results.Append(base.Convert_String_To_XML_Safe(thisTag.Control_Field_Value.Replace(" ", "^"))); } else { results.Append(base.Convert_String_To_XML_Safe(thisTag.Control_Field_Value).Replace("|a", "<font color=\"blue\">|a</font>"). Replace("|b", "<font color=\"blue\">|b</font>"). Replace("|c", "<font color=\"blue\">|c</font>"). Replace("|d", "<font color=\"blue\">|d</font>"). Replace("|e", "<font color=\"blue\">|e</font>"). Replace("|g", "<font color=\"blue\">|g</font>"). Replace("|x", "<font color=\"blue\">|x</font>"). Replace("|y", "<font color=\"blue\">|y</font>"). Replace("|z", "<font color=\"blue\">|z</font>"). Replace("|v", "<font color=\"blue\">|v</font>"). Replace("|h", "<font color=\"blue\">|h</font>"). Replace("|u", "<font color=\"blue\">|u</font>"). Replace("|f", "<font color=\"blue\">|f</font>"). Replace("|n", "<font color=\"blue\">|n</font>"). Replace("|2", "<font color=\"blue\">|2</font>"). Replace("|3", "<font color=\"blue\">|3</font>"). Replace("|w", "<font color=\"blue\">|w</font>"). Replace("|t", "<font color=\"blue\">|t</font>"). Replace("|q", "<font color=\"blue\">|q</font>"). Replace("|o", "<font color=\"blue\">|o</font>"). Replace("|i", "<font color=\"blue\">|i</font>"). Replace("|4", "<font color=\"blue\">|4</font>")); } results.Append("</td>\n"); results.Append(" </tr>"); } results.Append("</table>\n"); return(results.ToString().Replace("&bar;", "|")); }
/// <summary> Writes a digital resource in MARC format, with additional considerations for displaying within a HTML page </summary> /// <param name="Package"> Digital resource to write in MARC format </param> /// <param name="Width"> Width of the resulting HTML-formatted MARC record </param> /// <param name="Options"> Dictionary of any options which this metadata reader/writer may utilize </param> /// <returns> MARC record formatted for HTML, returned as a string </returns> public string MARC_HTML(SobekCM_Item Package, string Width, Dictionary <string, object> Options) { // Try to pull some values from the options string cataloging_source_code = String.Empty; if ((Options.ContainsKey("MarcXML_File_ReaderWriter:MARC Cataloging Source Code")) && (Options["MarcXML_File_ReaderWriter:MARC Cataloging Source Code"] != null)) { cataloging_source_code = Options["MarcXML_File_ReaderWriter:MARC Cataloging Source Code"].ToString(); } string location_code = String.Empty; if ((Options.ContainsKey("MarcXML_File_ReaderWriter:MARC Location Code")) && (Options["MarcXML_File_ReaderWriter:MARC Location Code"] != null)) { location_code = Options["MarcXML_File_ReaderWriter:MARC Location Code"].ToString(); } string reproduction_agency = String.Empty; if ((Options.ContainsKey("MarcXML_File_ReaderWriter:MARC Reproduction Agency")) && (Options["MarcXML_File_ReaderWriter:MARC Reproduction Agency"] != null)) { reproduction_agency = Options["MarcXML_File_ReaderWriter:MARC Reproduction Agency"].ToString(); } string reproduction_place = String.Empty; if ((Options.ContainsKey("MarcXML_File_ReaderWriter:MARC Reproduction Place")) && (Options["MarcXML_File_ReaderWriter:MARC Reproduction Place"] != null)) { reproduction_place = Options["MarcXML_File_ReaderWriter:MARC Reproduction Place"].ToString(); } string system_name = String.Empty; if ((Options.ContainsKey("MarcXML_File_ReaderWriter:System Name")) && (Options["MarcXML_File_ReaderWriter:System Name"] != null)) { system_name = Options["MarcXML_File_ReaderWriter:System Name"].ToString(); } string system_abbreviation = String.Empty; if ((Options.ContainsKey("MarcXML_File_ReaderWriter:System Abbreviation")) && (Options["MarcXML_File_ReaderWriter:System Abbreviation"] != null)) { system_abbreviation = Options["MarcXML_File_ReaderWriter:System Abbreviation"].ToString(); } string thumbnail_base = String.Empty; if ((Options.ContainsKey("MarcXML_File_ReaderWriter:Image_Base")) && (Options["MarcXML_File_ReaderWriter:Image_Base"] != null)) { thumbnail_base = Options["MarcXML_File_ReaderWriter:Image_Base"].ToString(); } // Get all the standard tags MARC_Record tags = Package.To_MARC_Record(cataloging_source_code, location_code, reproduction_agency, reproduction_place, system_name, system_abbreviation, thumbnail_base); // Look for extra tags to add in the OPTIONS if (Options.ContainsKey("MarcXML_File_ReaderWriter:Additional_Tags")) { object add_tags_obj = Options["MarcXML_File_ReaderWriter:Additional_Tags"]; if (add_tags_obj != null) { try { List <MARC_Field> add_tags = (List <MARC_Field>)add_tags_obj; foreach (MARC_Field thisTag in add_tags) { tags.Add_Field(thisTag); } } catch { // Do nothing in this case } } } // Start to build the HTML result StringBuilder results = new StringBuilder(); results.Append("<table style=\"border:none; text-align:left; width:" + Width + ";\">\n"); // Add the LEADER results.Append(" <tr class=\"trGenContent\">\n"); results.Append(" <td style=\"width:33px;vertical-align:top;\">LDR</td>\n"); results.Append(" <td style=\"width:26px;vertical-align:top;\"> </td>\n"); results.Append(" <td>" + tags.Leader.Replace(" ", "^") + "</td>\n"); results.Append(" </tr>"); // Add all the FIELDS foreach (MARC_Field thisTag in tags.Sorted_MARC_Tag_List) { results.Append(" <tr class=\"trGenContent\">\n"); results.Append(" <td>" + thisTag.Tag.ToString().PadLeft(3, '0') + "</td>\n"); results.Append(" <td style=\"color: green;\">" + thisTag.Indicators.Replace(" ", " ") + "</td>\n"); results.Append(" <td>"); if ((thisTag.Tag == 8) || (thisTag.Tag == 7) || (thisTag.Tag == 6)) { results.Append(Convert_String_To_XML_Safe(thisTag.Control_Field_Value.Replace(" ", "^"))); } else { results.Append(Convert_String_To_XML_Safe(thisTag.Control_Field_Value).Replace("|a", "<span style=\"color:blue;\">|a</span>"). Replace("|b", "<span style=\"color:blue;\">|b</span>"). Replace("|c", "<span style=\"color:blue;\">|c</span>"). Replace("|d", "<span style=\"color:blue;\">|d</span>"). Replace("|e", "<span style=\"color:blue;\">|e</span>"). Replace("|g", "<span style=\"color:blue;\">|g</span>"). Replace("|x", "<span style=\"color:blue;\">|x</span>"). Replace("|y", "<span style=\"color:blue;\">|y</span>"). Replace("|z", "<span style=\"color:blue;\">|z</span>"). Replace("|v", "<span style=\"color:blue;\">|v</span>"). Replace("|h", "<span style=\"color:blue;\">|h</span>"). Replace("|u", "<span style=\"color:blue;\">|u</span>"). Replace("|f", "<span style=\"color:blue;\">|f</span>"). Replace("|n", "<span style=\"color:blue;\">|n</span>"). Replace("|2", "<span style=\"color:blue;\">|2</span>"). Replace("|3", "<span style=\"color:blue;\">|3</span>"). Replace("|w", "<span style=\"color:blue;\">|w</span>"). Replace("|t", "<span style=\"color:blue;\">|t</span>"). Replace("|q", "<span style=\"color:blue;\">|q</span>"). Replace("|o", "<span style=\"color:blue;\">|o</span>"). Replace("|i", "<span style=\"color:blue;\">|i</span>"). Replace("|4", "<span style=\"color:blue;\">|4</span>")); } results.Append("</td>\n"); results.Append(" </tr>"); } results.Append("</table>\n"); return(results.ToString().Replace("&bar;", "|")); }