Class: Kwartz::JstlHandler

Inherits:
Handler
  • Object
show all
Includes:
JstlExpressionParser
Defined in:
lib/kwartz/binding/jstl.rb

Overview

directive handler for JSTL

Direct Known Subclasses

StrutsHandler

Constant Summary collapse

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

Instance Attribute Summary

Attributes inherited from Handler

#even, #filename, #odd

Instance Method Summary collapse

Methods included from JstlExpressionParser

#parse_expr_str, #parse_expr_str!

Methods inherited from Handler

#_elem_info_table, #_import_element_info_from_handler, #apply_rulesets, #extract, get_class, #get_element_info, #get_ruleset, #handle_directives, register_class, #register_element_info

Methods included from Expander

#expand_element_info, #expand_statement, #expand_statements, #get_element_info, #get_ruleset

Methods included from Assertion

assert

Methods included from HandlerHelper

#_last_stmt_kind, #add_foreach_stmts, #add_native_code, #add_native_expr_with_default, #build_print_args, #build_print_expr_stmt, #build_print_stmt, #create_text_print_stmt, #error_if_empty_tag, #error_when_last_stmt_is_not_if, #etag_stmt, #expand_attr_vars, #expand_attr_vars_in_native_expr, #expand_attr_vars_in_native_stmt, #stag_stmt, #wrap_content_with_native_stmt, #wrap_element_with_native_stmt

Methods included from ConvertErrorHelper

#convert_error

Constructor Details

#initialize(elem_rulesets = [], properties = {}) ⇒ JstlHandler

Returns a new instance of JstlHandler.



58
59
60
61
# File 'lib/kwartz/binding/jstl.rb', line 58

def initialize(elem_rulesets=[], properties={})
  super
  @jstl_ver = properties[:jstl] || Config::PROPERTY_JSTL
end

Instance Method Details

#directive_formatObject



80
81
82
# File 'lib/kwartz/binding/jstl.rb', line 80

def directive_format
  return JSTL_DIRECTIVE_FORMAT
end

#directive_patternObject



66
67
68
# File 'lib/kwartz/binding/jstl.rb', line 66

def directive_pattern
  return JSTL_DIRECTIVE_PATTERN
end

#handle(directive, elem_info, stmt_list) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/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

#mapping_patternObject



73
74
75
# File 'lib/kwartz/binding/jstl.rb', line 73

def mapping_pattern
  return JSTL_MAPPING_PATTERN
end