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

## 
# Rem: Cette classe est chargée et instancie dans la méthode initialize de 
#      XML::Script
#

require 'xml/script/base'
require 'js'

module XML
  class Script
    class Compiler < XML::Script::BaseCompiler
      def initialize
         ## Const: Language name
        @LanguageName        = 'JavaScript'
  
         ## Const: Separateur d'instructions
        @Separator           = '; '
  
         ## Const: Display
        @DisplayOpen         = 'document.write('
        @DisplayClose        = ')'
        @DisplayQuoteOpen    = '"'
        @DisplayQuoteClose   = '"'
        @DisplayNoQuoteOpen  = ''
        @DisplayNoQuoteClose = ''
        @DisplayConcat       = ', '

         ## Const: Line End
        @LineEnd             = '"\n"'
      end

      def quoteString( xString )
        xString.gsub( /"/, "\\\"" )
      end

      def codeBegin
		xJSScript = <<-EOM
function write() {return arguments.join('')}
function Document() { this.write = write }
var document = new Document;
		EOM
	    
		return( xJSScript )
	  end

	  def codeEnd
	    return ""
	  end
	  
      def compile( xCode )
		puts JS::evaluate( xCode )
      end
    end
  end
end
