-- $Date: 2004/03/01 01:32:02 $
-- $Revision: 1.12 $
-- $Author: jcrocholl $

with Token_Readers; use Token_Readers;
with Integer_Strings; use Integer_Strings;

package body Clefs.MusicXML is

   -- Read clef data from an XML reader.
   function Read_Clef
     (XML : access XML_Reader-- Use this XML reader.
     return Clef_Access        -- The newly created clef.
   is
      Number : Natural := 0;
      Sign   : Sign_Enum := G;
      Line   : Integer := 0;
   begin
      if not End_Of_Tag(XML) then
         Assert_Attribute_Name(XML, "number");
         Read_Attribute_Value(XML);
         Number := To_Number(Get_Token(XML));
      end if;
      Exit_Tag(XML);

      while Descend(XML) loop
         Exit_Tag(XML);
         if Context_Is(XML, "sign") then
            Sign := To_Sign_Enum(Read_Data(XML));
         elsif Context_Is(XML, "line") then
            Line := To_Number(Read_Data(XML));
         end if;
         Exit_Element(XML);
      end loop;
      Exit_Tag(XML);

      return Create(Number, Sign, Line);
   end Read_Clef;

   -- Write clef data to an XML writer.
   procedure Write_Clef
     (XML  : access XML_Writer-- Use this XML writer.
      This : access Clef)       -- Write this clef.
   is
   begin
      if Get_Number(This) = 0 then
         Start_Element(XML, "clef");
      else
         Start_Element(XML, "clef", "number", To_String(Get_Number(This)));
      end if;
      Write_Element(XML, "sign", Get_Sign(This)'Img);
      Write_Element(XML, "line", To_String(Get_Line(This)));
      Close_Element(XML, "clef");
   end Write_Clef;

end Clefs.MusicXML;