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

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

with Dir_Types.MusicXML; use Dir_Types.MusicXML;

package body Music.Directions.MusicXML is

   -- Read direction data from an XML reader.
   function Read_Direction
     (XML : access XML_Reader-- Use this XML reader.
     return Direction_Access   -- The newly created direction.
   is
      Result : Direction_Access;
   begin
      Assert_Attribute_Name(XML, "placement");
      Read_Attribute_Value(XML);
      Result := Create(To_Placement_Enum(Get_Token(XML)));
      Exit_Tag(XML);

      while Descend(XML) loop
         if Found(XML, "direction-type") then
            Set_Dir_Type(Result, Read_Dir_Type(XML));
         elsif Found(XML, "offset") then
            Exit_Tag(XML);
            Set_Offset(Result, To_Number(Read_Data(XML)));
         else
            XML_Expect_Error(XML, "/direction",
              "direction-type" / "offset");
         end if;

         Exit_Element(XML);
      end loop;
      Exit_Tag(XML);

      return Result;
   end Read_Direction;

   -- Write direction data to an XML writer.
   procedure Write_Direction
     (XML  : access XML_Writer-- Use this XML writer.
      This : access Direction)  -- Write this direction.
   is
   begin
      Start_Element(XML, "direction",
        "placement", To_XML(Get_Placement(This)'Img));

      Write_Dir_Type(XML, Get_Dir_Type(This));

      if Get_Offset(This) /= 0 then
         Write_Element(XML, "offset", To_String(Get_Offset(This)));
      end if;

      Close_Element(XML, "direction");
   end Write_Direction;

end Music.Directions.MusicXML;