# Copyright (C) 2001 Tobias Peters <t-peters@berlios.de>
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Library General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This 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
# Library General Public License for more details.
#
# You should have received a copy of the GNU Library General Public
# License along with this library; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA  02111-1307, USA.

# I'm cheating in this file. It is assumed, DOMException is the only existing
# exception

class Idl_Exception
  attr_reader :name
  attr_reader :attributes
  attr_reader :idl_module

  def c_value_name
    "rbdom_exception_" + name
  end

  def to_s
    @name
  end

  def initialize (idl_module, remaining_lines)
    @idl_module = idl_module
    match =
      /\s*exception\s+([^\s]+)\s*\{/.match(remaining_lines[0])
    @name = match[1]
    @attributes = []
    puts "exception #{@name}" if ($verbose)
    until(remaining_lines[0] =~ /}/)
      case remaining_lines[0]
      when /;/
	remaining_lines[0] = " attribute " + remaining_lines[0]
	@attributes << Idl_Attribute.new(@idl_module, self, remaining_lines)
      else 
        remaining_lines.delete_at(0)
      end
    end
    remaining_lines.delete_at(0)
  end

  ####################
  def create_ruby_code()
    @idl_module.add_ruby_code("values",
			      "static VALUE #{rbdom_class_name(self)};\n")

    @idl_module.add_ruby_code("methods", <<-END_OF_METHOD )
    static VALUE rbdom_exception_code(VALUE self)
    {
      VALUE code = rb_funcall(self, rb_intern("message"), 0);
      return rb_funcall(code, rb_intern("to_i"), 0);
    }
    END_OF_METHOD

    @idl_module.add_ruby_code("init_func", <<-END_OF_INIT_FUNC_CODE)
    #{rbdom_class_name(self)} =
    rb_define_class_under(#{@idl_module.get_c_value_name},
			  "#{@name}", rb_eException);
    rb_define_method(#{rbdom_class_name(self)}, "code",
		     &rbdom_exception_code, 0);
    END_OF_INIT_FUNC_CODE
  end

  ###################
  def create_cpp_code(gdome_cpp_hh, gdome_cpp_cc)
    # struct and constructor are created statically for DOMException
  end
end
