-- $Date: 2004/02/14 06:21:48 $
-- $Revision: 1.18 $
-- $Author: jcrocholl $

with XML_Readers; use XML_Readers;
with XML_Writers; use XML_Writers;
with Writers; use Writers;

with Scores.MusicXML; use Scores.MusicXML;

package body MusicXML is

   -------------------
   -- Document headers
   -------------------

   -- Read the <?xml> tag.
   procedure Read_XML_Header
     (XML : access XML_Reader) is -- Use this XML reader.
   begin
      Assert_Tag_Name(XML, "?xml");
      while not End_Of_Tag(XML) loop
         Read_Attribute_Name(XML);
         Read_Attribute_Value(XML);
      end loop;
      Exit_Tag(XML);
   end Read_XML_Header;

   -- Write the <?xml> tag.
   procedure Write_XML_Header
     (XML : access XML_Writer) is -- Use this XML writer.
   begin
      Start_Tag(XML, "?xml");
      Write_Attribute(XML, "version", "1.0");
      Write_Attribute(XML, "encoding", "UTF-8");
      Write_Attribute(XML, "standalone", "no");
      Close_Tag(XML, "?");
      New_Line(XML);
   end Write_XML_Header;

   -- Read the <!DOCTYPE> tag.
   procedure Read_Document_Header
     (XML : access XML_Reader) is -- Use this XML reader.
   begin
      Assert_Tag_Name(XML, "!DOCTYPE");
      while not End_Of_Tag(XML) loop
         Read_Attribute_Name(XML);
      end loop;
      Exit_Tag(XML);
   end Read_Document_Header;

   -- Write the <!DOCTYPE> tag.
   procedure Write_Document_Header
     (XML : access XML_Writer) is -- Use this XML writer.
   begin
      Start_Tag(XML, "!DOCTYPE");
      Write_Attribute_Name(XML, "score-partwise");
      Write_Attribute_Name(XML, "PUBLIC");
      -- New_Line(XML); Put(XML, " ");
      Write_Attribute_Name(XML, """-//Recordare//DTD MusicXML 1.0 Partwise//EN""");
      -- New_Line(XML); Put(XML, " ");
      Write_Attribute_Name(XML, """http://www.musicxml.org/dtds/partwise.dtd""");
      Close_Tag(XML);
      New_Line(XML);
   end Write_Document_Header;

   ----------
   -- Reading
   ----------

   -- Read a score from an XML reader.
   function Read
     (XML : access XML_Reader-- Use this XML reader.
     return Score_Access is    -- The newly created score.
   begin
      Read_XML_Header(XML);
      Read_Document_Header(XML);
      return Read_Score(XML);
   end Read;

   -- Read a score from current input (stdin).
   function Read
     return Score_Access -- The newly created score.
   is
      XML    : XML_Reader_Access;
      Result : Score_Access;
   begin
      XML := Current_Input;
      Result := Read(XML);
      Close(XML);
      return Result;
   end Read;

   -- Read a score from a MusicXML file.
   function Read_File
     (Filename : in String-- Read from this file.
     return Score_Access    -- The newly created score.
   is
      XML    : XML_Reader_Access;
      Result : Score_Access;
   begin
      XML := Open(Filename);
      Result := Read(XML);
      Close(XML);
      return Result;
   end Read_File;

   ----------
   -- Writing
   ----------

   -- Write a score to an XML writer.
   procedure Write
     (XML  : access XML_Writer-- Use this XML writer.
      This : access Score) is   -- Write this score.
   begin
      Write_XML_Header(XML);
      Write_Document_Header(XML);
      Write_Score_Partwise(XML, This);
   end Write;

   -- Write a score to current output (stdout).
   procedure Write
     (This : access Score-- Write this score.
   is
      XML : XML_Writer_Access;
   begin
      XML := Current_Output;
      Write(XML, This);
      Close(XML);
   end Write;

   -- Write a score to a MusicXML file.
   procedure Write_File
     (This     : access Score-- Write this score.
      Filename : in String)    -- Write to this file.
   is
      XML : XML_Writer_Access;
   begin
      XML := Create(Filename);
      Write(XML, This);
      Close(XML);
   end Write_File;

end MusicXML;