-- $Date: 2004/02/14 09:40:45 $
-- $Revision: 1.2 $
-- $Author: jcrocholl $

with Readers; use Readers;
with Token_Readers; use Token_Readers;

with Encodings.MusicXML; use Encodings.MusicXML;
with String_String_Hash_Tables; use String_String_Hash_Tables;

package body Identifications.MusicXML is

   -- Read a creator type and name from an XML reader.
   procedure Read_Creator
     (XML  : access XML_Reader;        -- Use this XML reader.
      This : access Identification) is -- Add creator to this identification.
   begin
      Assert_Attribute_Name(XML, "type");
      Read_Attribute_Value(XML);
      Add_Creator(This, Get_Token(XML), Read_Data(XML));
      Exit_Tag(XML);
      Assert_Tag(XML, "/creator");
   end Read_Creator;

   -- Write the creators of a score to an XML writer.
   procedure Write_Creators
     (XML  : access XML_Writer-- Use this XML writer.
      This : access Hash_Table-- Write these creators.
   is
      Index : Natural := 0;
   begin
      loop
         Next(This, Index);
         exit when Index = 0;
         Write_Element(XML, "creator",
           "type", Get_Key(This, Index),
           Get_Item(This, Index));
      end loop;
   end Write_Creators;

   -- Read identification data from an XML reader.
   function Read_Identification
     (XML : access XML_Reader)    -- Use this XML reader.
     return Identification_Access -- The newly created identification.
   is
      Result : Identification_Access := Create;
   begin
      while Descend(XML) loop
         if not Found(XML, "creator") then
            Exit_Tag(XML);
         end if;

         if Found(XML, "creator") then
            Read_Creator(XML, Result);
         elsif Found(XML, "encoding") then
            Set_Encoding(Result, Read_Encoding(XML));
         elsif Found(XML, "rights") then
            Set_Rights(Result, Read_Data(XML));
            Exit_Element(XML);
         elsif Found(XML, "source") then
            Set_Source(Result, Read_Data(XML));
            Exit_Element(XML);
         else
            XML_Expect_Error(XML, "/identification",
              "encoding" / "rights" / "source");
         end if;
      end loop;
      Exit_Tag(XML);

      return Result;
   end Read_Identification;

   -- Write identification data to an XML writer.
   procedure Write_Identification
     (XML  : access XML_Writer;     -- Use this XML writer.
      This : access Identification-- Write this identification.
   is
   begin
      Start_Element(XML, "identification");

      Write_Creators(XML, Get_Creators(This));

      if Get_Rights(This) /= "" then
         Write_Element(XML, "rights", Get_Rights(This));
      end if;

      if Get_Encoding(This) /= null then
         Write_Encoding(XML, Get_Encoding(This));
      end if;

      Close_Element(XML, "identification");
   end Write_Identification;

end Identifications.MusicXML;