Class Kwartz::JstlHandler
In: kwartz/binding/jstl.rb
Parent: Handler

directive handler for JSTL

Methods

Included Modules

JstlExpressionParser

Constants

JSTL_DIRECTIVE_PATTERN = /\A(\w+)(?:\s*\(\s*(.*)\))?\z/
JSTL_MAPPING_PATTERN = /\A'([-:\w]+)',\s*(.*)\z/
JSTL_DIRECTIVE_FORMAT = '%s(%s)'

Public Class methods

[Source]

# File kwartz/binding/jstl.rb, line 58
    def initialize(elem_rulesets=[], properties={})
      super
      @jstl_ver = properties[:jstl] || Config::PROPERTY_JSTL
    end

Public Instance methods

[Source]

# File kwartz/binding/jstl.rb, line 80
    def directive_format
      return JSTL_DIRECTIVE_FORMAT
    end

[Source]

# File kwartz/binding/jstl.rb, line 66
    def directive_pattern
      return JSTL_DIRECTIVE_PATTERN
    end

[Source]

# File kwartz/binding/jstl.rb, line 85
    def handle(directive, elem_info, stmt_list)
      ret = super
      return ret if ret

      d_name = directive.name
      d_arg  = directive.arg
      d_str  = directive.str
      e = elem_info

      case d_name

      when :for, :For, :FOR, :list, :List, :LIST
        is_foreach = d_name == :for || d_name == :For || d_name == :FOR
        error_if_empty_tag(elem_info, d_str) unless is_foreach
        unless d_arg =~ /\A(\w+)\s*:\s*(.*)\z/
          raise convert_error("'#{d_str}': invalid argument.", elem_info.stag_info.linenum)
        end
        loopvar = $1 ; looplist = $2
        counter = d_name == :for || d_name == :list ? nil : "#{loopvar}_ctr"
        toggle  = d_name != :FOR && d_name != :LIST ? nil : "#{loopvar}_tgl"
        status  = d_name == :for || d_name == :list ? nil : "#{loopvar}_status"
        foreach_code = "<c:forEach var=\"#{loopvar}\" items=\"${#{looplist}}\""
        foreach_code << " varStatus=\"#{status}\"" if status
        foreach_code << ">"
        code = []
        code << foreach_code
        code << "<c:set var=\"#{counter}\" value=\"${#{status}.count}\"/>" if counter
        if toggle
          if @jstl_ver < 1.2
            code << "<c:choose><c:when test=\"${#{status}.count%2==0}\">"
            code << "<c:set var=\"#{toggle}\" value=\"${self.even}\"/>"
            code << "</c:when><c:otherwise>"
            code << "<c:set var=\"#{toggle}\" value=\"${self.odd}\"/>"
            code << "</c:otherwise></c:choose>"
          else
            code << "<c:set var=\"#{toggle}\" value=\"${#{status}.count%2==0 ? #{self.even} : #{self.odd}}\"/>"
          end
        end
        end_code = "</c:forEach>"
        if is_foreach
          wrap_element_with_native_stmt(elem_info, stmt_list, code, end_code, :set)
        else
          wrap_content_with_native_stmt(elem_info, stmt_list, code, end_code, :set)
        end

      when :while, :loop
        msg = "'#{d_str}': jstl doesn't support '#{d_arg}' directive."
        raise convert_error(msg, elem_info.stag_info.linenum)

      when :set
        unless d_arg =~ /\A(\S+)\s*=\s*(.*)\z/
          raise convert_error("'#{d_str}': invalid argument.", elem_info.stag_info.linenum)
        end
        lhs = $1;  rhs = $2
        code = "<c:set var=\"#{lhs}\" value=\"${#{rhs}}\"/>"
        wrap_element_with_native_stmt(elem_info, stmt_list, code, nil, :set)
        #code = "<c:set var=\"#{lhs}\" value=\"${#{rhs}}\"/>"
        #stmt_list << NativeStatement.new(code, :set)
        #stmt_list << stag_stmt(elem_info)
        #stmt_list.concat(cont_stmts)
        #stmt_list << etag_stmt(elem_info)

      when :if
        start_code = "<c:choose><c:when test=\"${#{d_arg}}\">"
        end_code   = "</c:when></c:choose>"
        wrap_element_with_native_stmt(elem_info, stmt_list, start_code, end_code, :if)
        #stmt_list << NativeStatement.new(start_code, :if)
        #stmt_list << stag_stmt(elem_info)
        #stmt_list.concat(cont_stmts)
        #stmt_list << etag_stmt(elem_info)
        #stmt_list << NativeStatement.new(end, :if)

      when :elseif, :else
        error_when_last_stmt_is_not_if(elem_info, d_str, stmt_list)
        stmt_list.pop    # delete '</c:when></c:choose>'
        if d_name == :else
          kind = :else
          start_code = "</c:when><c:otherwise>"
          end_code   = "</c:otherwise></c:choose>"
        else
          kind = :elseif
          start_code = "</c:when><c:when test=\"${#{d_arg}}\">"
          end_code   = "</c:when></c:choose>"
        end
        wrap_element_with_native_stmt(elem_info, stmt_list, start_code, end_code, kind)
        #stmt_list << NativeStatement.new(start_code, kind)
        #stmt_list << stag_stmt
        #stmt_list.concat(cont_stmts)
        #stmt_list << etag_stmt
        #stmt_list << NativeStatement.new(end_code, kind)

      when :default, :Default, :DEFAULT
        error_if_empty_tag(elem_info, d_str)
        stmt_list << stag_stmt(elem_info)
        flag_escape = d_name == :default ? nil : (d_name == :Default)
        argstr = elem_info.cont_stmts[0].args[0]
        expr_str = directive.dattr == 'id' ? parse_expr_str(d_arg, e.stag_info.linenum) : d_arg
        code =  "<c:out value=\"${#{expr_str}}\""
        code << " escapeXml=\"#{flag_escape}\"" unless flag_escape == nil
        code << " default=\"#{argstr}\"/>"
        stmt_list << NativeStatement.new_without_newline(code)
        stmt_list << etag_stmt(elem_info)

      when :catch
        if d_arg && !d_arg.empty? && d_arg !~ /\A\w+\z/
          raise convert_error("'#{d_str}': invalid varname.", elem_info.stag_info.linenum)
        end
        code = "<c:catch"
        code << " var=\"#{d_arg}\"" if d_arg && !d_arg.empty?
        code << ">"
        stmt_list << NativeStatement.new(code)
        stmt_list.concat(elem_info.cont_stmts)
        stmt_list << NativeStatement.new("</c:catch>")

      when :forEach, :forTokens
        options = eval "{ #{d_arg} }"
        stag, etag = self.__send__ "handle_jstl_#{d_name}", options
        wrap_element_with_native_stmt(elem_info, stmt_list, stag, etag, nil)
        #stmt_list << NativeStatement.new(stag)
        #stmt_list << stag_stmt
        #stmt_list.concat(cont_stmts)
        #stmt_list << etag_stmt
        #stmt_list << NativeStatement.new(etag)

      when :redirect, :import, :url, :remove
        options = eval "{ #{d_arg} }"
        lines = self.__send__ "handle_jstl_#{d_name}", options
        lines.each do |line|
          stmt_list << NativeStatement.new(line.chomp)
        end

      else
        return false

      end #case
      return true

    end

[Source]

# File kwartz/binding/jstl.rb, line 73
    def mapping_pattern
      return JSTL_MAPPING_PATTERN
    end

Protected Instance methods

[Source]

# File kwartz/binding/jstl.rb, line 312
    def _evaluate_options(options={})
      return options
    end

[Source]

# File kwartz/binding/jstl.rb, line 260
    def _handle_jstl_params(tagname, param_list, options)
      stag, etag = _handle_jstl_tag(tagname, param_list, options, true)
      i = 0
      unknown_param_names = options.keys - param_list
      lines = []
      if unknown_param_names.empty?
        lines << stag.sub(/>\z/, '/>')
      else
        lines << stag
        unknown_param_names.each do |name|
          value = options[name]
          if value.is_a?(Symbol)
            lines << " <c:param name=\"#{name}\" value=\"${#{value}}\"/>"
          else
            lines << " <c:param name=\"#{name}\" value=\"#{value}\"/>"
          end
        end
        lines << etag
      end
      return lines.join("\n")
    end

[Source]

# File kwartz/binding/jstl.rb, line 283
    def _handle_jstl_tag(tagname, param_list, options, ignore_unknown_option=false)
      options.each do |name, value|
        next if name.is_a?(String)
        options[name.to_s] = options.delete(name)
      end
      option_names = options.keys
      unless ignore_unknown_option
        unkown_option_names = option_names - param_list
        unless unkown_option_names.empty?
          msg = "'#{unkown_option_names[0]}': unknown option for '#{tagname}' directive."
          raise convert_error(msg, nil)   # TODO
        end
      end
      sb = "<c:#{tagname}"
      (param_list & option_names).each do |name|
        value = options[name]
        if value.is_a?(Symbol)
          sb << " #{name}=\"${#{value}}\""
        else
          sb << " #{name}=\"#{value}\""
        end
      end
      sb << ">"
      stag = sb
      etag = "</c:#{tagname}>"
      return stag, etag
    end

[Source]

# File kwartz/binding/jstl.rb, line 248
    def handle_jstl_forEach(options)
      param_list = %w[var items varStatus begin end step]
      return _handle_jstl_tag('forEach', param_list, options)
    end

[Source]

# File kwartz/binding/jstl.rb, line 254
    def handle_jstl_forTokens(options)
      param_list = %w[items delims var varStatus begin end step]
      return _handle_jstl_tag('forTokens', param_list, options)
    end

[Source]

# File kwartz/binding/jstl.rb, line 233
    def handle_jstl_import(options)
      return _handle_jstl_params('import', %w[url context charEncoding var scope], options)
    end

[Source]

# File kwartz/binding/jstl.rb, line 228
    def handle_jstl_redirect(options)
      return _handle_jstl_params('redirect', %w[url context], options)
    end

[Source]

# File kwartz/binding/jstl.rb, line 243
    def handle_jstl_remove(options)
      return _handle_jstl_params('remove', %w[var scope], options)
    end

[Source]

# File kwartz/binding/jstl.rb, line 238
    def handle_jstl_url(options)
      return _handle_jstl_params('url', %w[value context var scope], options)
    end

[Validate]