--
-- @filename xml_writer.adb
-- @author Julien Burdy & Vincent Decorges
-- @date 22.4.02
-- @brief Sub-programm to creat xml file for AdaDoc
--
-- History  :
-- Date     Modification               Author
--
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
with Ada.Strings.Fixed;

with Ada.Integer_Text_IO;

package body Xml_Writer is

   --Write an attribute of the dictionary
   procedure Write_Attribute( Name : in String; Value : in String) is

   begin
      Put(Validate(Name) & "=""" & Validate(Value) & """ ");
   end Write_Attribute;

   --Run through the dictionary and write the attribute
   procedure Write_Attributes is new Dict_Tda.Run_Through(Write_Attribute);


   -- Name    : Begin_File
   -- Purpose : Writing the beginning of the XML file
   --
   -- in      : File : File to write to
   -- in      : Package_Name
   --
   procedure Begin_File (File : in File_Type;
                         Attributes : in Dict_Tda.Dict_Type) is

   begin

      --Redefine the output to the file
      Set_Output(File);
      --Xml Header
      Put_Line("<?xml version=""1.0"" encoding=""ISO-8859-1"" ?>");
      --The root of the Xml file is the tag Package
      Put_Line("<Package ");
      Write_Attributes(Attributes);
      Put_Line(" xsi:noNamespaceSchemaLocation=""AdaDocV2.xsd""" &
               " xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"">");
      Set_Output(Standard_Output);

   end Begin_File;

   -- Name    : Create_Tage
   -- Purpose : Open and Write tag
   --
   -- in      : File : File to write to
   -- in      : Name : Name of the tag
   -- in      : Attributes : Array of attributes
   -- in      : Contents : Content of the array
   --
   procedure Create_Tag (File : in File_Type;
                         Name : in String;
                         Attributes : in Dict_Tda.Dict_Type;
                         Content : in String := "") is

   begin

      Set_Output(File);
      Put("<" & Name & " ");
      Write_Attributes(Attributes);
      Put_Line(">" & Content);
      Set_Output(Standard_Output);

   end Create_Tag;

   -- Name    : Close_Tag
   -- Purpose : Close a tag
   --
   -- in      : File : File to write to
   -- in      : Name : Name of the tag
   --
   procedure Close_Tag (File : in File_Type;
                        Name : in String) is

   begin

      Put_Line(File, "</" & Name & ">");

   end Close_Tag;

   -- Name    : End_File
   -- Purpose : Writing the end of the XML file
   --
   -- in      : File : File to write to
   --
   procedure End_File (File : in File_Type) is


   begin

      --Close the main tag
      Put_Line(File, "</Package>");

   end End_File;

   -- Name    : Validate
   -- Purpose : Validate a string to xml standard
   --
   -- in      : Element : the string to validate
   -- return  : The Element validated
   --
   function Validate (Element : String) return String is

      Element_Tmp : Unbounded_String := To_Unbounded_String(Element);

      -- Value of the XML symbols
      Amp  : constant String := "&amp;"  ;
      Lt   : constant String := "&lt;"   ;
      Gt   : constant String := "&gt;"   ;
      Apos : constant String := "&apos;" ;
      Quot : constant String := "&quot;" ;

      -- Dummy Amp value because & is in Amp!
      Dummy_Amp : constant String := "!amp;";


   begin -- Replacement of the whole symbols

      while Index(Element_Tmp,"&") /= 0 loop
         Replace_Slice (Element_Tmp,Index(Element_Tmp,"&"), Index(Element_Tmp,"&"),
                        Dummy_Amp);
      end loop;

      --Replace Dummy_Amp
      while Index(Element_Tmp, Dummy_Amp) /= 0 loop
         Replace_Slice (Element_Tmp,Index(Element_Tmp,Dummy_Amp),
                        Index(Element_Tmp,Dummy_Amp) + Dummy_Amp'Length,
                        Amp);
      end loop;

      while Index(Element_Tmp,"<") /= 0 loop
         Replace_Slice (Element_Tmp,Index(Element_Tmp,"<"), Index(Element_Tmp,"<"),Lt);
      end loop;

      while Index(Element_Tmp,"<") /= 0 loop
         Replace_Slice (Element_Tmp,Index(Element_Tmp,"<"), Index(Element_Tmp,"<"),Lt);
      end loop;

      while Index(Element_Tmp,">") /= 0 loop
         Replace_Slice (Element_Tmp,Index(Element_Tmp,">"), Index(Element_Tmp,">"),Gt);
      end loop;

      while Index(Element_Tmp,"'") /= 0 loop
         Replace_Slice (Element_Tmp,Index(Element_Tmp,"'"), Index(Element_Tmp,"'"),Apos);
      end loop;

      while Index(Element_Tmp,"""") /= 0 loop
         Replace_Slice (Element_Tmp,Index(Element_Tmp,""""), Index(Element_Tmp,""""),
         Quot);
      end loop;

      -- Return after converting
      return To_String(Element_Tmp);

   end Validate;

end Xml_Writer;