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

with Integer_Strings; use Integer_Strings;
with Enum_Strings; use Enum_Strings;
with Readers; use Readers;
with Token_Readers; use Token_Readers;

package body Lyrics.MusicXML is

   function Read_Special
     (XML : access XML_Reader-- Use this XML reader.
     return Special_Enum
   is
      Result : Special_Enum := None;
   begin
      if not Descend(XML) then
         Exit_Tag(XML);
      else
         if Found(XML, "extend") then Result := Extend;
         elsif Found(XML, "laughing") then Result := Laughing;
         elsif Found(XML, "humming") then Result := Humming;
         else
            XML_Expect_Error(XML, "/lyric",
              "extend" / "laughing" / "humming");
         end if;
         Exit_Tag(XML);
         Exit_Element(XML);
      end if;
      return Result;
   end Read_Special;

   -- Read lyric data from an XML reader.
   function Read_Lyric
     (XML : access XML_Reader-- Use this XML reader.
     return Lyric_Access       -- The newly created lyric.
   is
      Number   : Natural;
      Syllabic : Syllabic_Enum;
      Special  : Special_Enum;
   begin
      Assert_Attribute_Name(XML, "number");
      Read_Attribute_Value(XML);
      Number := To_Number(Get_Token(XML));
      Exit_Tag(XML);

      Assert_Tag(XML, "syllabic");
      Syllabic := To_Syllabic_Enum(Read_Data(XML));
      Assert_Tag(XML, "/syllabic");

      Assert_Tag(XML, "text");
      declare
         Text : String := Read_Data(XML);
      begin
         Assert_Tag(XML, "/text");

         Special := Read_Special(XML);
         return Create(Number, Syllabic, Text, Special);
      end;
   end Read_Lyric;

   -- Write lyric data to an XML writer.
   procedure Write_Lyric
     (XML  : access XML_Writer-- Use this XML writer.
      This : access Lyric)      -- Write this lyric.
   is
   begin
      Start_Element(XML, "lyric", "number", To_String(Get_Number(This)));
      Write_Element(XML, "syllabic", To_XML(Get_Syllabic(This)'Img));
      Write_Element(XML, "text", Get_Text(This));
      if Get_Special(This) /= None then
         Write_Element(XML, To_XML(Get_Special(This)'Img));
      end if;
      Close_Element(XML, "lyric");
   end Write_Lyric;

end Lyrics.MusicXML;