Reader/Writer for reading metadata stored in METS files
This relies heavily upon the METS Sec ReaderWriters to read the dmdSec and amdSec portions of the METS file.
Inheritance: XML_Writing_Base_Type, iMetadata_File_ReaderWriter
        /// <summary> Read the metadata file and enrich the existing bibliographic package </summary>
        /// <param name="Data_File">Generic (unspecified) metadata file</param>
        /// <param name="thisPackage">Bibliographic package to enrich</param>
        public void Read(string Data_File, SobekCM_Item thisPackage)
        {
            // Many readers output an error message
            string errorMessage;

            // Does this filename have '.info.xml' in it?
            if (Data_File.ToUpper().IndexOf(".INFO.XML") > 0)
            {
                INFO_File_ReaderWriter readInfo = new INFO_File_ReaderWriter();
                readInfo.Read_Metadata(Data_File, thisPackage, null, out errorMessage);
                return;
            }

            // Does this file have '.mets' in it?
            if ((Data_File.ToUpper().IndexOf(".METS") > 0) || (Data_File.ToUpper().IndexOf(".PMETS") > 0))
            {
                METS_File_ReaderWriter readInfo = new METS_File_ReaderWriter();
                readInfo.Read_Metadata(Data_File, thisPackage, null, out errorMessage);
                return;
            }

            // If it made it here, it may be METS or MXF
            if (Data_File.ToUpper().IndexOf(".XML") > 0)
            {
                // Read first couple lines
                StreamReader reader = new StreamReader(Data_File);
                string thisLine = reader.ReadLine();
                while (thisLine != null)
                {
                    // Is this MXF?
                    if (thisLine.ToUpper().Trim() == "<MXF>")
                    {
                        // Close the current connection
                        reader.Close();

                        // Read in the MXF file
                        MXF_File_ReaderWriter readInfo = new MXF_File_ReaderWriter();
                        readInfo.Read_Metadata(Data_File, thisPackage, null, out errorMessage);
                        return;
                    }

                    // Is this a METS declaration?
                    if (thisLine.ToUpper().IndexOf("<METS:") > 0)
                    {
                        // Close the current connection
                        reader.Close();

                        // Read in the METS file
                        METS_File_ReaderWriter readInfo = new METS_File_ReaderWriter();
                        readInfo.Read_Metadata(Data_File, thisPackage, null, out errorMessage);
                        return;
                    }

                    // Read the next line
                    thisLine = reader.ReadLine();
                }
            }
        }
        /// <summary> Read from an existing METS file and returns a new object</summary>
        /// <param name="fileName">Name of the METS file</param>
        /// <returns>Built SobekCM_Item object</returns>
        public static SobekCM_Item Read_METS(string fileName)
        {
            SobekCM_Item returnVal = new SobekCM_Item();

            // Save this to the METS file format
            METS_File_ReaderWriter writer = new METS_File_ReaderWriter();
            string Error_Message;
            writer.Read_Metadata(fileName, returnVal, null, out Error_Message);

            return returnVal;
        }
 /// <summary> Saves this resource as a SobekCM Service METS file </summary>
 /// <remarks> The SobekCM Service METS file tries to keep the file size as small as possible and also includes service information (image properties) that the standard SobekCM METS file does not include </remarks>
 public void Save_SobekCM_METS()
 {
     // Save this to the METS file format
     METS_File_ReaderWriter writer = new METS_File_ReaderWriter();
     string Error_Message;
     writer.Write_Metadata( Source_Directory + "\\" + BibID + "_" + VID + ".mets.xml", this, null, out Error_Message);
 }
        /// <summary> Save this resource as a METS file </summary>
        /// <remarks>Will use either the default namespace, or the same namespace which was used
        /// in any previously read METS files.</remarks>
        public void Save_METS(string Destination_File)
        {
            METS_Header.ObjectID = BibID + "_" + VID;

            // If there is an interview date, use that to calculate the sort safe date
            // Ensure this metadata module extension exists and has data
            Oral_Interview_Info oralInfo = Get_Metadata_Module("OralInterview") as Oral_Interview_Info;
            if ((oralInfo != null) && (oralInfo.Interview_Date.Length > 0) && (BIBInfo.SortDate > 0))
            {
                BIBInfo.SortDate = BIBInfo.SortSafeDate(oralInfo.Interview_Date);
            }

            // Save this to the METS file format
            METS_File_ReaderWriter writer = new METS_File_ReaderWriter();
            string Error_Message;
            writer.Write_Metadata(Destination_File, this, null, out Error_Message);
        }
        /// <summary> Save this resource as a METS file </summary>
        /// <remarks>Will use either the default namespace, or the same namespace which was used
        /// in any previously read METS files.</remarks>
        public void Save_METS(bool include_daitss)
        {
            METS_Header.ObjectID = BibID + "_" + VID;

            string FileName = Source_Directory + "/" + BibID + "_" + VID + ".mets";

            // If this is juat a project XML, do this differently
            if (Bib_Info.SobekCM_Type == TypeOfResource_SobekCM_Enum.Project)
            {
                Bib_Info.Main_Title.Title = "Project level metadata for '" + BibID + "'";
                FileName = Source_Directory + "/" + BibID + ".pmets";
                METS_Header.ObjectID = BibID;
            }

            // If there is an interview date, use that to calculate the sort safe date
            // Ensure this metadata module extension exists and has data
            Oral_Interview_Info oralInfo = Get_Metadata_Module("OralInterview") as Oral_Interview_Info;
            if ((oralInfo != null) && (oralInfo.Interview_Date.Length > 0) && (BIBInfo.SortDate > 0))
            {
                BIBInfo.SortDate = BIBInfo.SortSafeDate(oralInfo.Interview_Date);
            }

            // Save this to the METS file format
            METS_File_ReaderWriter writer = new METS_File_ReaderWriter();
            string Error_Message;
            writer.Write_Metadata(FileName, this, null, out Error_Message);
        }
 /// <summary> Read from an existing METS file into the current object</summary>
 /// <param name="fileName">Name of the METS file</param>
 public void Read_From_METS(string fileName)
 {
     // Save this to the METS file format
     METS_File_ReaderWriter writer = new METS_File_ReaderWriter();
     string Error_Message;
     writer.Read_Metadata(fileName, this, null, out Error_Message);
 }
        private SobekCM_Item Build_Item_From_METS(string METS_URL, string METS_Name, Custom_Tracer Tracer)
        {
            try
            {
                if (Tracer != null)
                {
                    Tracer.Add_Trace("SobekCM_METS_Based_ItemBuilder.Build_Item_From_METS", "Open http web request stream to METS file ( <a href=\"" + METS_URL + "\">" + METS_Name + "</a> )");
                }

                SobekCM_Item thisPackage = new SobekCM_Item();
                if (METS_URL.IndexOf("http:") >= 0)
                {
                    WebRequest objRequest = WebRequest.Create(METS_URL);
                    objRequest.Timeout = 5000;
                    WebResponse objResponse = objRequest.GetResponse();

                    if (Tracer != null)
                    {
                        Tracer.Add_Trace("SobekCM_METS_Based_ItemBuilder.Build_Item_From_METS", "Read the METS file from the stream");
                    }

                    // Read the METS file and create the package
                    METS_File_ReaderWriter reader = new METS_File_ReaderWriter();
                    string errorMessage;
                    reader.Read_Metadata(objResponse.GetResponseStream(), thisPackage, null, out errorMessage);
                    objResponse.Close();
                }
                else
                {
                    if (File.Exists(METS_URL.Replace("/", "\\")))
                    {
                        // Read the METS file and create the package
                        METS_File_ReaderWriter reader = new METS_File_ReaderWriter();
                        string errorMessage;
                        reader.Read_Metadata(METS_URL.Replace("/", "\\"), thisPackage, null, out errorMessage);
                    }
                    else
                    {
                        return null;
                    }
                }

                return thisPackage;
            }
            catch
            {
                return null;
            }
        }
        private void show_preview(TextWriter Output, string Preview_Mode, Custom_Tracer Tracer)
        {
            Tracer.Add_Trace("Edit_Item_Metadata_MySobekViewer.show_preview", String.Empty);

            Output.WriteLine("<script language=\"JavaScript\">");

            // Get the URL to use for forwarding
            string current_submode = currentMode.My_Sobek_SubMode;
            currentMode.My_Sobek_SubMode = "ZZZZZ";
            string redirect_url = currentMode.Redirect_URL();
            currentMode.My_Sobek_SubMode = current_submode;

            Output.WriteLine("  function preview1() {if (document.itemNavForm.pickme) location='" + redirect_url.Replace("ZZZZZ", "preview") + "#template'}");
            Output.WriteLine("  function preview2() {if (document.itemNavForm.pickme) location='" + redirect_url.Replace("ZZZZZ", "marc") + "#template'}");
            Output.WriteLine("  function preview3() {if (document.itemNavForm.pickme) location='" + redirect_url.Replace("ZZZZZ", "mets") + "#template'}");
            Output.WriteLine("</script>");

            Output.WriteLine("<center>");
            Output.WriteLine(Preview_Mode == "preview"
                                 ? "<input type=\"radio\" name=\"sbkPreviewType\" id=\"sbkTypeOfPreview\" checked=\"checked\" /><label for=\"sbkTypeOfPreview\">Standard View</label> &nbsp; &nbsp; "
                                 : "<input type=\"radio\" name=\"sbkPreviewType\" id=\"sbkTypeOfPreview\" onchange=\"location='" + redirect_url.Replace("ZZZZZ", "preview") + "#template';\" /><label for=\"sbkTypeOfPreview\">Standard View</label> &nbsp; &nbsp; ");

            Output.WriteLine(Preview_Mode == "marc"
                                 ? "<input type=\"radio\" name=\"sbkPreviewType\" id=\"sbkTypeOfMarc\" checked=\"checked\" /><label for=\"sbkTypeOfMarc\">MARC View</label> &nbsp; &nbsp; "
                                 : "<input TYPE=\"radio\" name=\"sbkPreviewType\" id=\"sbkTypeOfMarc\" onchange=\"location='" + redirect_url.Replace("ZZZZZ", "marc") + "#template';\" /><label for=\"sbkTypeOfMarc\">MARC View</label> &nbsp; &nbsp; ");

            Output.WriteLine(Preview_Mode == "mets"
                                 ? "<input type=\"radio\" name=\"sbkPreviewType\" id=\"sbkTypeOfMets\" checked=\"checked\" /><label for=\"sbkTypeOfMets\">METS View</label>"
                                 : "<input type=\"radio\" name=\"sbkPreviewType\" id=\"sbkTypeOfMets\" onchange=\"location='" + redirect_url.Replace("ZZZZZ", "mets") + "#template';\" /><label for=\"sbkTypeOfMets\">METS View</label>");

            Output.WriteLine("</center>");
            Output.WriteLine("<br />");

            switch (Preview_Mode)
            {
                case "marc":
                    Output.WriteLine("<div class=\"sbkEimv_Citation\">");
                    Citation_ItemViewer marcViewer = new Citation_ItemViewer(Translator, codeManager, false)
                                                         {CurrentItem = item, CurrentMode = currentMode};
                    Output.WriteLine(marcViewer.MARC_String("735px", Tracer));
                    break;

                case "mets":
                    Output.WriteLine("<div class=\"sbkEimv_Citation\" >");
                  //  Output.WriteLine("<table width=\"950px\"><tr><td width=\"950px\">");
                    StringBuilder mets_builder = new StringBuilder(2000);
                    StringWriter mets_output = new StringWriter(mets_builder);

                    METS_File_ReaderWriter metsWriter = new METS_File_ReaderWriter();

                    string errorMessage;
                    metsWriter.Write_Metadata(mets_output, item, null, out errorMessage);
                    string mets_string = mets_builder.ToString();
                    string header = mets_string.Substring(0, mets_string.IndexOf("<METS:mets"));
                    string remainder = mets_string.Substring(header.Length);
                    Output.WriteLine(header.Replace("<?", "&AAA;span style=&QQQ;color:Blue;&QQQ;&ZZZ;&lt?").Replace("?>", "?&gt;&AAA;/span&ZZZ;").Replace("<!--", "&AAA;span style=&QQQ;color:Blue;&QQQ;&ZZZ;&lt!--&AAA;/span&ZZZ;&AAA;span style=&QQQ;color:Gray;&QQQ;&ZZZ;").Replace("-->", "&AAA;/span&ZZZ;&AAA;span style=&QQQ;color:Blue;&QQQ;&ZZZ;--&gt;&AAA;/span&ZZZ;").Replace("\r", "<br />").Replace("&AAA;", "<").Replace("&ZZZ;", ">").Replace("&QQQ;", "\""));
                    Output.WriteLine(remainder.Replace("<?", "&AAA;span style=&QQQ;color:Blue;&QQQ;&ZZZ;&lt?").Replace("?>", "?&gt;&AAA;/span&ZZZ;").Replace("<!--", "&AAA;span style=&QQQ;color:Blue;&QQQ;&ZZZ;&lt!--&AAA;/span&ZZZ;&AAA;span style=&QQQ;color:Gray;&QQQ;&ZZZ;").Replace("-->", "&AAA;/span&ZZZ;&AAA;span style=&QQQ;color:Blue;&QQQ;&ZZZ;--&gt;&AAA;/span&ZZZ;").Replace("</", "&AAA;span style=&QQQ;color:Blue;&QQQ;&ZZZ;&lt;/&AAA;/span&ZZZ;&AAA;span style=&QQQ;color:Maroon;&QQQ;&ZZZ;").Replace("<", "&AAA;span style=&QQQ;color:Blue;&QQQ;&ZZZ;&lt;&AAA;/span&ZZZ;&AAA;span style=&QQQ;color:Maroon;&QQQ;&ZZZ;").Replace("=\"", "&AAA;/span&ZZZ;&AAA;span style=&QQQ;color:Blue;&QQQ;&ZZZ;=&quot;&AAA;/span&ZZZ;").Replace("\">", "&AAA;span style=&QQQ;color:Blue;&QQQ;&ZZZ;&quot;&gt;&AAA;/span&ZZZ;").Replace("\"", "&AAA;span style=&QQQ;color:Blue;&QQQ;&ZZZ;&quot;&AAA;/span&ZZZ;&AAA;span style=&QQQ;color:Maroon;&QQQ;&ZZZ;").Replace("/>", "&AAA;/span&ZZZ;&AAA;span style=&QQQ;color:Blue;&QQQ;&ZZZ;/&gt;&AAA;/span&ZZZ;").Replace(">", "&AAA;/span&ZZZ;&AAA;span style=&QQQ;color:Blue;&QQQ;&ZZZ;&gt;&AAA;/span&ZZZ;").Replace("\r", "<br />").Replace("&AAA;", "<").Replace("&ZZZ;", ">").Replace("&QQQ;", "\""));
                 //   Output.WriteLine("</td></tr></table>");
                    break;

                default:
                    Output.WriteLine("<div class=\"sbkEimv_Citation\">");
                    Citation_ItemViewer citationViewer = new Citation_ItemViewer(Translator, codeManager, false)
                                                             {CurrentItem = item, CurrentMode = currentMode};
                    Output.WriteLine(citationViewer.Standard_Citation_String(false, Tracer));
                    break;

            }
            Output.WriteLine("</div><br />");
        }