
(********************************************************************)
(*                                                                  *)
(*  xml_ent.s7i   XML entity handling library                       *)
(*  Copyright (C) 2009  Thomas Mertes                               *)
(*                                                                  *)
(*  This file is part of the Seed7 Runtime Library.                 *)
(*                                                                  *)
(*  The Seed7 Runtime Library is free software; you can             *)
(*  redistribute it and/or modify it under the terms of the GNU     *)
(*  Lesser General Public License as published by the Free Software *)
(*  Foundation; either version 2.1 of the License, or (at your      *)
(*  option) any later version.                                      *)
(*                                                                  *)
(*  The Seed7 Runtime Library is distributed in the hope that it    *)
(*  will be useful, but WITHOUT ANY WARRANTY; without even the      *)
(*  implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR *)
(*  PURPOSE.  See the GNU Lesser General Public License for more    *)
(*  details.                                                        *)
(*                                                                  *)
(*  You should have received a copy of the GNU Lesser General       *)
(*  Public License along with this program; if not, write to the    *)
(*  Free Software Foundation, Inc., 51 Franklin Street,             *)
(*  Fifth Floor, Boston, MA  02110-1301, USA.                       *)
(*                                                                  *)
(********************************************************************)


const type: xmlEntityHashType is hash [string] string;

const func xmlEntityHashType: genPredeclaredXmlEntities is func
  result
    var xmlEntityHashType: result is xmlEntityHashType.value;
  begin
    result @:= ["quot"] "\"";
    result @:= ["amp"]  "&";
    result @:= ["apos"] "'";
    result @:= ["lt"]   "<";
    result @:= ["gt"]   ">";
  end func;

const xmlEntityHashType: predeclaredXmlEntities is genPredeclaredXmlEntities;


(**
 *  Return a string with all XML entities of the argument replaced by
 *  the corresponding characters.
 *)
const func string: decodeXmlEntities (in string: stri, in xmlEntityHashType: xmlEntityHash) is func
  result
    var string: result is "";
  local
    var integer: old_pos is 1;
    var integer: new_pos is 0;
    var integer: semicol_pos is 0;
    var string: entityName is "";
    var string: entityValue is "";
  begin
    new_pos := pos(stri, '&');
    while new_pos <> 0 do
      semicol_pos := pos(stri, ';', succ(new_pos));
      if semicol_pos <> 0 then
        entityName := stri[succ(new_pos) .. pred(semicol_pos)];
        if length(entityName) >= 2 and entityName[1] = '#' and
            isDigitString(entityName[2 ..]) then
          entityValue := str(chr(integer parse (entityName[2 ..])));
          result &:= stri[old_pos .. pred(new_pos)];
          result &:= entityValue;
          old_pos := succ(semicol_pos);
        elsif length(entityName) >= 3 and entityName[1 .. 2] = "#x" and
            isDigitString(entityName[3 ..], 16) then
          entityValue := str(chr(toInt(entityName[3 ..], 16)));
          result &:= stri[old_pos .. pred(new_pos)];
          result &:= entityValue;
          old_pos := succ(semicol_pos);
        elsif entityName in xmlEntityHash then
          entityValue := xmlEntityHash[entityName];
          result &:= stri[old_pos .. pred(new_pos)];
          result &:= entityValue;
          old_pos := succ(semicol_pos);
        else
          result &:= stri[old_pos .. new_pos];
          old_pos := succ(new_pos);
        end if;
      else
        result &:= stri[old_pos .. new_pos];
        old_pos := succ(new_pos);
      end if;
      new_pos := pos(stri, '&', old_pos);
    end while;
    result &:= stri[old_pos ..];
  end func;
