kwartz 3.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (169) hide show
  1. data/COPYING +340 -0
  2. data/ChangeLog +103 -0
  3. data/README.txt +37 -0
  4. data/bin/kwartz +12 -0
  5. data/doc-api/classes/Kwartz.html +218 -0
  6. data/doc-api/classes/Kwartz/Assertion.html +140 -0
  7. data/doc-api/classes/Kwartz/AssertionError.html +148 -0
  8. data/doc-api/classes/Kwartz/AttrInfo.html +320 -0
  9. data/doc-api/classes/Kwartz/BaseError.html +206 -0
  10. data/doc-api/classes/Kwartz/BaseTranslator.html +331 -0
  11. data/doc-api/classes/Kwartz/CharacterType.html +212 -0
  12. data/doc-api/classes/Kwartz/CommandOptionError.html +154 -0
  13. data/doc-api/classes/Kwartz/CommandOptions.html +374 -0
  14. data/doc-api/classes/Kwartz/Config.html +150 -0
  15. data/doc-api/classes/Kwartz/ConvertError.html +191 -0
  16. data/doc-api/classes/Kwartz/Converter.html +252 -0
  17. data/doc-api/classes/Kwartz/CssStyleParser.html +483 -0
  18. data/doc-api/classes/Kwartz/DocumentRuleset.html +369 -0
  19. data/doc-api/classes/Kwartz/ElementExpander.html +325 -0
  20. data/doc-api/classes/Kwartz/ElementInfo.html +312 -0
  21. data/doc-api/classes/Kwartz/ElementRuleset.html +582 -0
  22. data/doc-api/classes/Kwartz/EperlHandler.html +338 -0
  23. data/doc-api/classes/Kwartz/EperlTranslator.html +167 -0
  24. data/doc-api/classes/Kwartz/ErubisHandler.html +113 -0
  25. data/doc-api/classes/Kwartz/ErubisTranslator.html +168 -0
  26. data/doc-api/classes/Kwartz/ErubyHandler.html +337 -0
  27. data/doc-api/classes/Kwartz/ErubyTranslator.html +167 -0
  28. data/doc-api/classes/Kwartz/ExpandStatement.html +227 -0
  29. data/doc-api/classes/Kwartz/Expression.html +119 -0
  30. data/doc-api/classes/Kwartz/Handler.html +558 -0
  31. data/doc-api/classes/Kwartz/JstlHandler.html +657 -0
  32. data/doc-api/classes/Kwartz/JstlTranslator.html +226 -0
  33. data/doc-api/classes/Kwartz/KwartzError.html +146 -0
  34. data/doc-api/classes/Kwartz/Main.html +384 -0
  35. data/doc-api/classes/Kwartz/NativeExpression.html +236 -0
  36. data/doc-api/classes/Kwartz/NativeStatement.html +254 -0
  37. data/doc-api/classes/Kwartz/Node.html +156 -0
  38. data/doc-api/classes/Kwartz/ParseError.html +148 -0
  39. data/doc-api/classes/Kwartz/PhpHandler.html +333 -0
  40. data/doc-api/classes/Kwartz/PhpTranslator.html +194 -0
  41. data/doc-api/classes/Kwartz/PresentationLogicParser.html +830 -0
  42. data/doc-api/classes/Kwartz/PrintStatement.html +221 -0
  43. data/doc-api/classes/Kwartz/RailsHandler.html +587 -0
  44. data/doc-api/classes/Kwartz/RailsTranslator.html +167 -0
  45. data/doc-api/classes/Kwartz/RubyStyleParser.html +558 -0
  46. data/doc-api/classes/Kwartz/Ruleset.html +117 -0
  47. data/doc-api/classes/Kwartz/Statement.html +119 -0
  48. data/doc-api/classes/Kwartz/StrutsTranslator.html +190 -0
  49. data/doc-api/classes/Kwartz/TagInfo.html +314 -0
  50. data/doc-api/classes/Kwartz/TextConverter.html +270 -0
  51. data/doc-api/classes/Kwartz/Translator.html +318 -0
  52. data/doc-api/classes/Test.html +107 -0
  53. data/doc-api/classes/Test/Unit.html +101 -0
  54. data/doc-api/created.rid +1 -0
  55. data/doc-api/files/__/README_txt.html +150 -0
  56. data/doc-api/files/kwartz/assert_rb.html +114 -0
  57. data/doc-api/files/kwartz/binding/eperl_rb.html +116 -0
  58. data/doc-api/files/kwartz/binding/erubis_rb.html +116 -0
  59. data/doc-api/files/kwartz/binding/eruby_rb.html +115 -0
  60. data/doc-api/files/kwartz/binding/jstl_rb.html +116 -0
  61. data/doc-api/files/kwartz/binding/php_rb.html +116 -0
  62. data/doc-api/files/kwartz/binding/rails_rb.html +115 -0
  63. data/doc-api/files/kwartz/binding/struts_rb.html +117 -0
  64. data/doc-api/files/kwartz/config_rb.html +107 -0
  65. data/doc-api/files/kwartz/converter_rb.html +119 -0
  66. data/doc-api/files/kwartz/error_rb.html +107 -0
  67. data/doc-api/files/kwartz/main_rb.html +124 -0
  68. data/doc-api/files/kwartz/node_rb.html +114 -0
  69. data/doc-api/files/kwartz/parser_rb.html +117 -0
  70. data/doc-api/files/kwartz/translator_rb.html +115 -0
  71. data/doc-api/files/kwartz/util/assert-text-equal_rb.html +115 -0
  72. data/doc-api/files/kwartz/util/testcase-helper_rb.html +115 -0
  73. data/doc-api/files/kwartz_rb.html +120 -0
  74. data/doc-api/fr_class_index.html +75 -0
  75. data/doc-api/fr_file_index.html +45 -0
  76. data/doc-api/fr_method_index.html +216 -0
  77. data/doc-api/index.html +24 -0
  78. data/doc-api/rdoc-style.css +208 -0
  79. data/doc/docstyle.css +188 -0
  80. data/doc/p-pattern.html +1207 -0
  81. data/doc/reference.html +3396 -0
  82. data/doc/users-guide.html +1670 -0
  83. data/examples/breadcrumbs1/Makefile +15 -0
  84. data/examples/breadcrumbs1/breadcrumbs.eruby.plogic +27 -0
  85. data/examples/breadcrumbs1/breadcrumbs.html +12 -0
  86. data/examples/breadcrumbs1/breadcrumbs.jstl.plogic +28 -0
  87. data/examples/breadcrumbs1/breadcrumbs.php.plogic +26 -0
  88. data/examples/breadcrumbs1/main.php +12 -0
  89. data/examples/breadcrumbs1/main.rb +12 -0
  90. data/examples/breadcrumbs2/Makefile +15 -0
  91. data/examples/breadcrumbs2/breadcrumbs.eruby.plogic +22 -0
  92. data/examples/breadcrumbs2/breadcrumbs.html +14 -0
  93. data/examples/breadcrumbs2/breadcrumbs.jstl.plogic +24 -0
  94. data/examples/breadcrumbs2/breadcrumbs.php.plogic +23 -0
  95. data/examples/breadcrumbs2/main.php +12 -0
  96. data/examples/breadcrumbs2/main.rb +12 -0
  97. data/examples/pagelayout/Makefile +47 -0
  98. data/examples/pagelayout/content.eruby.plogic +44 -0
  99. data/examples/pagelayout/content.jstl.plogic +36 -0
  100. data/examples/pagelayout/content.php.plogic +37 -0
  101. data/examples/pagelayout/content1.html +36 -0
  102. data/examples/pagelayout/content2.html +29 -0
  103. data/examples/pagelayout/design.css +40 -0
  104. data/examples/pagelayout/layout.html +50 -0
  105. data/examples/pagelayout/main.php +55 -0
  106. data/examples/pagelayout/main.rb +77 -0
  107. data/examples/pagelayout/menu.eruby.plogic +14 -0
  108. data/examples/pagelayout/menu.html +13 -0
  109. data/examples/pagelayout/menu.jstl.plogic +14 -0
  110. data/examples/pagelayout/menu.php.plogic +14 -0
  111. data/examples/rails1/Makefile +36 -0
  112. data/examples/rails1/README +19 -0
  113. data/examples/rails1/application_helper.rb +31 -0
  114. data/examples/rails1/edit.html +28 -0
  115. data/examples/rails1/edit.plogic +10 -0
  116. data/examples/rails1/form.html +52 -0
  117. data/examples/rails1/form.plogic +33 -0
  118. data/examples/rails1/layout.plogic +15 -0
  119. data/examples/rails1/link_to.plogic +19 -0
  120. data/examples/rails1/list.html +48 -0
  121. data/examples/rails1/list.plogic +28 -0
  122. data/examples/rails1/new.html +27 -0
  123. data/examples/rails1/new.plogic +10 -0
  124. data/examples/rails1/reader.plogic +29 -0
  125. data/examples/rails1/show.html +40 -0
  126. data/examples/rails1/show.plogic +4 -0
  127. data/examples/rails1/writer.plogic +8 -0
  128. data/examples/table1/Makefile +15 -0
  129. data/examples/table1/main.php +11 -0
  130. data/examples/table1/main.rb +11 -0
  131. data/examples/table1/table1.eruby.plogic +21 -0
  132. data/examples/table1/table1.html +16 -0
  133. data/examples/table1/table1.jstl.plogic +21 -0
  134. data/examples/table1/table1.php.plogic +22 -0
  135. data/kwartz.gemspec +55 -0
  136. data/lib/kwartz.rb +13 -0
  137. data/lib/kwartz/assert.rb +31 -0
  138. data/lib/kwartz/binding/eperl.rb +166 -0
  139. data/lib/kwartz/binding/erubis.rb +61 -0
  140. data/lib/kwartz/binding/eruby.rb +164 -0
  141. data/lib/kwartz/binding/jstl.rb +334 -0
  142. data/lib/kwartz/binding/php.rb +167 -0
  143. data/lib/kwartz/binding/rails.rb +295 -0
  144. data/lib/kwartz/binding/struts.rb +109 -0
  145. data/lib/kwartz/config.rb +28 -0
  146. data/lib/kwartz/converter.rb +920 -0
  147. data/lib/kwartz/error.rb +41 -0
  148. data/lib/kwartz/main.rb +464 -0
  149. data/lib/kwartz/node.rb +454 -0
  150. data/lib/kwartz/parser.rb +903 -0
  151. data/lib/kwartz/translator.rb +153 -0
  152. data/lib/kwartz/util/assert-text-equal.rb +44 -0
  153. data/lib/kwartz/util/testcase-helper.rb +112 -0
  154. data/setup.rb +1331 -0
  155. data/test/test-compile.rb +36 -0
  156. data/test/test-compile.yaml +178 -0
  157. data/test/test-converter.rb +34 -0
  158. data/test/test-converter.yaml +127 -0
  159. data/test/test-directives.rb +32 -0
  160. data/test/test-directives.yaml +1411 -0
  161. data/test/test-main.rb +464 -0
  162. data/test/test-parser.rb +54 -0
  163. data/test/test-parser.yaml +394 -0
  164. data/test/test-rails.rb +28 -0
  165. data/test/test-rails.yaml +301 -0
  166. data/test/test-ruleset.rb +36 -0
  167. data/test/test-ruleset.yaml +804 -0
  168. data/test/test.rb +44 -0
  169. metadata +236 -0
@@ -0,0 +1,334 @@
1
+ ###
2
+ ### $Rev: 116 $
3
+ ### $Release: 3.0.0 $
4
+ ### copyright(c) 2004-2006 kuwata-lab.com all rights reserved
5
+ ###
6
+
7
+ require 'kwartz/assert'
8
+ require 'kwartz/converter'
9
+ require 'kwartz/translator'
10
+
11
+
12
+
13
+ module Kwartz
14
+
15
+
16
+
17
+ ##
18
+ ## directive handler for JSTL
19
+ ##
20
+ class JstlHandler < Handler
21
+
22
+
23
+ def initialize(elem_rulesets=[], properties={})
24
+ super
25
+ @jstl_ver = properties[:jstl] || 1.2
26
+ end
27
+
28
+
29
+ JSTL_DIRECTIVE_PATTERN = /\A(\w+)(?:\s*\(\s*(.*)\))?\z/
30
+
31
+ def directive_pattern
32
+ return JSTL_DIRECTIVE_PATTERN
33
+ end
34
+
35
+
36
+ JSTL_MAPPING_PATTERN = /\A'([-:\w]+)',\s*(.*)\z/
37
+
38
+ def mapping_pattern
39
+ return JSTL_MAPPING_PATTERN
40
+ end
41
+
42
+
43
+ JSTL_DIRECTIVE_FORMAT = '%s(%s)'
44
+
45
+ def directive_format
46
+ return JSTL_DIRECTIVE_FORMAT
47
+ end
48
+
49
+
50
+ def handle(directive_name, directive_arg, directive_str, stag_info, etag_info, cont_stmts, attr_info, append_exprs, stmt_list)
51
+ ret = super
52
+ return ret if ret
53
+
54
+ d_name = directive_name
55
+ d_arg = directive_arg
56
+ d_str = directive_str
57
+
58
+ case directive_name
59
+
60
+ when :for, :For, :FOR, :list, :List, :LIST
61
+ is_foreach = d_name == :for || d_name == :For || d_name == :FOR
62
+ unless d_arg =~ /\A(\w+)\s*:\s*(.*)\z/
63
+ raise convert_error("'#{d_str}': invalid argument.", stag_info.linenum)
64
+ end
65
+ loopvar = $1 ; looplist = $2
66
+ counter = d_name == :for || d_name == :list ? nil : "#{loopvar}_ctr"
67
+ toggle = d_name != :FOR && d_name != :LIST ? nil : "#{loopvar}_tgl"
68
+ status = d_name == :for || d_name == :list ? nil : "#{loopvar}_status"
69
+ stmt_list << build_print_stmt(stag_info, attr_info, append_exprs) if !is_foreach
70
+ sb = "<c:forEach var=\"#{loopvar}\" items=\"${#{looplist}}\""
71
+ sb << " varStatus=\"#{status}\"" if status
72
+ sb << ">"
73
+ stmt_list << NativeStatement.new(sb, :foreach)
74
+ if counter
75
+ stmt_list << NativeStatement.new("<c:set var=\"#{counter}\" value=\"${#{status}.count}\" />")
76
+ end
77
+ if toggle
78
+ if @jstl_ver < 1.2
79
+ stmt_list << NativeStatement.new("<c:choose><c:when test=\"${#{status}.count%2==0}\">")
80
+ stmt_list << NativeStatement.new("<c:set var=\"#{toggle}\" value=\"${self.even}\"/>")
81
+ stmt_list << NativeStatement.new("</c:when><c:otherwise>")
82
+ stmt_list << NativeStatement.new("<c:set var=\"#{toggle}\" value=\"${self.odd}\"/>")
83
+ stmt_list << NativeStatement.new("</c:otherwise></c:choose>")
84
+ else
85
+ sb = "<c:set var=\"#{toggle}\" value=\"${#{status}.count%2==0 ? #{self.even} : #{self.odd}}\" />"
86
+ stmt_list << NativeStatement.new(sb)
87
+ end
88
+ end
89
+ stmt_list << build_print_stmt(stag_info, attr_info, append_exprs) if is_foreach
90
+ stmt_list.concat(cont_stmts)
91
+ stmt_list << build_print_stmt(etag_info, nil, nil) if is_foreach
92
+ stmt_list << NativeStatement.new("</c:forEach>", :foreach)
93
+ stmt_list << build_print_stmt(etag_info, nil, nil) if !is_foreach
94
+
95
+ when :while, :loop
96
+ raise convert_error("'#{d_str}': jstl doesn't support '#{d_arg}' directive.", stag_info.linenum)
97
+
98
+ when :set
99
+ unless d_arg =~ /\A(\S+)\s*=\s*(.*)\z/
100
+ raise convert_error("'#{d_str}': invalid argument.", stag_info.linenum)
101
+ end
102
+ lhs = $1; rhs = $2
103
+ sb = "<c:set var=\"#{lhs}\" value=\"${#{rhs}}\" />"
104
+ stmt_list << NativeStatement.new(sb, :set)
105
+ stmt_list << build_print_stmt(stag_info, attr_info, append_exprs)
106
+ stmt_list.concat(cont_stmts)
107
+ stmt_list << build_print_stmt(etag_info, nil, nil)
108
+
109
+ when :if
110
+ sb = "<c:choose><c:when test=\"${#{d_arg}}\">"
111
+ stmt_list << NativeStatement.new(sb, :if)
112
+ stmt_list << build_print_stmt(stag_info, attr_info, append_exprs)
113
+ stmt_list.concat(cont_stmts)
114
+ stmt_list << build_print_stmt(etag_info, nil, nil)
115
+ stmt_list << NativeStatement.new("</c:when></c:choose>", :if)
116
+
117
+ when :elseif, :else
118
+ unless !stmt_list.empty? && (st=stmt_list[-1]).is_a?(NativeStatement) && (st.kind == :if || st.kind == :elseif)
119
+ raise convert_error("'#{d_str}': previous statement should be 'if' or 'elseif'.", stag_info.linenum)
120
+ end
121
+ stmt_list.pop # delete '</c:when></c:choose>'
122
+ if d_name == :else
123
+ kind = :else
124
+ sb = "</c:when><c:otherwise>"
125
+ stmt_list << NativeStatement.new(sb, :else)
126
+ sb = "</c:otherwise></c:choose>"
127
+ else
128
+ kind = :elseif
129
+ sb = "</c:when><c:when test=\"${#{d_arg}}\">"
130
+ stmt_list << NativeStatement.new(sb, :elseif)
131
+ sb = "</c:when></c:choose>"
132
+ end
133
+ stmt_list << build_print_stmt(stag_info, attr_info, append_exprs)
134
+ stmt_list.concat(cont_stmts)
135
+ stmt_list << build_print_stmt(etag_info, nil, nil)
136
+ stmt_list << NativeStatement.new(sb, kind)
137
+
138
+ when :default, :Default, :DEFAULT
139
+ error_if_empty_tag(stag_info, etag_info, d_name, d_arg)
140
+ stmt_list << build_print_stmt(stag_info, attr_info, append_exprs)
141
+ flag_escape = d_name == :default ? nil : (d_name == :Default)
142
+ argstr = cont_stmts[0].args[0]
143
+ sb = "<c:out value=\"${#{d_arg}}\""
144
+ sb << " escapeXml=\"#{flag_escape}\"" unless flag_escape == nil
145
+ sb << " default=#{argstr.dump} />"
146
+ stmt_list << NativeStatement.new_without_newline(sb)
147
+ stmt_list << build_print_stmt(etag_info, nil, nil)
148
+
149
+ when :catch
150
+ if d_arg && !d_arg.empty? && d_arg !~ /\A\w+\z/
151
+ raise convert_error("'#{d_str}': invalid varname.", stag_info.linenum)
152
+ end
153
+ sb = "<c:catch"
154
+ sb << " var=\"#{d_arg}\"" if d_arg && !d_arg.empty?
155
+ sb << ">"
156
+ stmt_list << NativeStatement.new(sb)
157
+ stmt_list.concat(cont_stmts)
158
+ stmt_list << NativeStatement.new("</c:catch>")
159
+
160
+ when :forEach, :forTokens
161
+ stag, etag = eval "handle_jstl_#{d_name}(#{d_arg})"
162
+ stmt_list << NativeStatement.new(stag)
163
+ stmt_list << build_print_stmt(stag_info, attr_info, append_exprs)
164
+ stmt_list.concat(cont_stmts)
165
+ stmt_list << build_print_stmt(etag_info, nil, nil)
166
+ stmt_list << NativeStatement.new(etag)
167
+
168
+ when :redirect, :import, :url, :remove
169
+ lines = eval "handle_jstl_#{d_name}(#{d_arg})"
170
+ lines.each do |line|
171
+ stmt_list << NativeStatement.new(line)
172
+ end
173
+
174
+ else
175
+ return false
176
+
177
+ end #case
178
+ return true
179
+
180
+ end #def
181
+
182
+
183
+ protected
184
+
185
+
186
+ def handle_jstl_redirect(options)
187
+ return _handle_jstl_params('redirect', %w[url context], options)
188
+ end
189
+
190
+
191
+ def handle_jstl_import(options)
192
+ return _handle_jstl_params('import', %w[url context charEncoding var scope], options)
193
+ end
194
+
195
+
196
+ def handle_jstl_url(options)
197
+ return _handle_jstl_params('url', %w[value context var scope], options)
198
+ end
199
+
200
+
201
+ def handle_jstl_remove(options)
202
+ return _handle_jstl_params('remove', %w[var scope], options)
203
+ end
204
+
205
+
206
+ def handle_jstl_forEach(options)
207
+ param_list = %w[var items varStatus begin end step]
208
+ return _handle_jstl_tag('forEach', param_list, options)
209
+ end
210
+
211
+
212
+ def handle_jstl_forTokens(options)
213
+ param_list = %w[items delims var varStatus begin end step]
214
+ return _handle_jstl_tag('forTokens', param_list, options)
215
+ end
216
+
217
+
218
+ def _handle_jstl_params(tagname, param_list, options)
219
+ stag, etag = _handle_jstl_tag(tagname, param_list, options)
220
+ lines = [stag]
221
+ i = 0
222
+ options.each do |name, value|
223
+ i += 1
224
+ if value.is_a?(Symbol)
225
+ lines << " <c:param name=\"#{name}\" value=\"${#{value}}\"/>"
226
+ else
227
+ #lines << " <c:param name=\"#{name}\" value=\"#{value}\"/>"
228
+ end
229
+ end
230
+ if i == 0
231
+ stag.sub!(/>\z/, '/>')
232
+ else
233
+ lines << etag
234
+ end
235
+ return lines
236
+ end
237
+
238
+
239
+ def _handle_jstl_tag(tagname, param_list, options)
240
+ sb = "<c:#{tagname}"
241
+ param_list.each do |param|
242
+ key = nil
243
+ if options.key?(param.intern) ; key = param.intern
244
+ elsif options.key?(param) ; key = param
245
+ end
246
+ next if key == nil
247
+ value = options.delete(key)
248
+ if value.is_a?(Symbol)
249
+ sb << " #{param}=\"${#{value}}\""
250
+ else
251
+ sb << " #{param}=\"#{value}\""
252
+ end
253
+ end
254
+ sb << ">"
255
+ stag = sb
256
+ etag = "</c:#{tagname}>"
257
+ return stag, etag
258
+ end
259
+
260
+
261
+ def _evaluate_options(options={})
262
+ return options
263
+ end
264
+
265
+
266
+ end #class
267
+ Handler.register_class('jstl', JstlHandler)
268
+
269
+
270
+
271
+ ##
272
+ ## translator for php
273
+ ##
274
+ class JstlTranslator < BaseTranslator
275
+
276
+
277
+ JSTL11_EMBED_PATTERNS = [
278
+ '', '', # statement
279
+ '<c:out value="${', '}" escapeXml="false"/>', # expression
280
+ '<c:out value="${', '}"/>' # escaped expression
281
+ ]
282
+
283
+
284
+ JSTL12_EMBED_PATTERNS = [
285
+ '', '', # statement
286
+ '<c:out value="${', '}" escapeXml="false"/>', # expression
287
+ '${', '}' # escaped expression
288
+ ]
289
+
290
+
291
+ def initialize(properties={})
292
+ jstl_ver = properties[:jstl] || 1.2
293
+ super(jstl_ver < 1.2 ? JSTL11_EMBED_PATTERNS : JSTL12_EMBED_PATTERNS, properties)
294
+ @jstl_ver = jstl_ver
295
+ unless self.header
296
+ sb = ''
297
+ if charset = properties[:charset]
298
+ sb << "<%@ page contentType=\"text/html; charset=#{charset}\" %>" << @nl
299
+ else
300
+ #sb << "<%@ page contentType=\"text/html\" %>" << @nl
301
+ end
302
+ if @jstl_ver < 1.2
303
+ sb << '<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>' << @nl
304
+ else
305
+ sb << '<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>' << @nl
306
+ sb << '<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>' << @nl
307
+ end
308
+ self.header = sb
309
+ end
310
+ end
311
+
312
+
313
+ def translate_native_expr(expr)
314
+ assert unless expr.is_a?(NativeExpression)
315
+ if expr.code =~ /\A"(.*)"\z/ || expr.code =~ /\A'(.*)'\z/
316
+ @sb << $1
317
+ else
318
+ flag_escape = expr.escape?
319
+ flag_escape = @escape if flag_escape.nil?
320
+ if flag_escape == false
321
+ @sb << @expr_l << expr.code << @expr_r # ex. <c:out value="${expr}" escapeXml="false"/>
322
+ else
323
+ @sb << @escape_l << expr.code << @escape_r # ex. <c:out value="${expr}"/>
324
+ end
325
+ end
326
+ end
327
+
328
+
329
+ end
330
+ Translator.register_class('jstl', JstlTranslator)
331
+
332
+
333
+
334
+ end #module
@@ -0,0 +1,167 @@
1
+ ###
2
+ ### $Rev: 116 $
3
+ ### $Release: 3.0.0 $
4
+ ### copyright(c) 2004-2006 kuwata-lab.com all rights reserved
5
+ ###
6
+
7
+ require 'kwartz/assert'
8
+ require 'kwartz/converter'
9
+ require 'kwartz/translator'
10
+
11
+
12
+
13
+ module Kwartz
14
+
15
+
16
+
17
+ ##
18
+ ## directive handler for PHP
19
+ ##
20
+ class PhpHandler < Handler
21
+
22
+
23
+ PHP_DIRECTIVE_PATTERN = /\A(\w+)(?:\s*\(\s*(.*)\))?\z/
24
+
25
+ def directive_pattern
26
+ return PHP_DIRECTIVE_PATTERN
27
+ end
28
+
29
+
30
+ PHP_MAPPING_PATTERN = /\A'([-:\w]+)',\s*(.*)\z/
31
+
32
+ def mapping_pattern
33
+ return PHP_MAPPING_PATTERN
34
+ end
35
+
36
+
37
+ PHP_DIRECTIVE_FORMAT = '%s(%s)'
38
+
39
+ def directive_format
40
+ return PHP_DIRECTIVE_FORMAT
41
+ end
42
+
43
+
44
+ def handle(directive_name, directive_arg, directive_str, stag_info, etag_info, cont_stmts, attr_info, append_exprs, stmt_list)
45
+ ret = super
46
+ return ret if ret
47
+
48
+ d_name = directive_name
49
+ d_arg = directive_arg
50
+ d_str = directive_str
51
+
52
+ case directive_name
53
+
54
+ when :foreach, :Foreach, :FOREACH, :list, :List, :LIST
55
+ is_foreach = d_name == :foreach || d_name == :Foreach || d_name == :FOREACH
56
+ unless d_arg =~ /\A.*\s+as\s+(\$\w+)(?:\s*=>\s*\$\w+)?\z/
57
+ raise convert_error("'#{d_str}': invalid argument.", stag_info.linenum)
58
+ end
59
+ loopvar = $1
60
+ counter = d_name == :foreach || d_name == :list ? nil : "#{loopvar}_ctr"
61
+ toggle = d_name != :FOREACH && d_name != :LIST ? nil : "#{loopvar}_tgl"
62
+ stmt_list << build_print_stmt(stag_info, attr_info, append_exprs) if !is_foreach
63
+ stmt_list << NativeStatement.new("#{counter} = 0;") if counter
64
+ stmt_list << NativeStatement.new("foreach (#{d_arg}) {", :foreach)
65
+ stmt_list << NativeStatement.new(" #{counter}++;") if counter
66
+ stmt_list << NativeStatement.new(" #{toggle} = #{counter}%2==0 ? #{self.even} : #{self.odd};") if toggle
67
+ stmt_list << build_print_stmt(stag_info, attr_info, append_exprs) if is_foreach
68
+ stmt_list.concat(cont_stmts)
69
+ stmt_list << build_print_stmt(etag_info, nil, nil) if is_foreach
70
+ stmt_list << NativeStatement.new("}", :foreach)
71
+ stmt_list << build_print_stmt(etag_info, nil, nil) if !is_foreach
72
+
73
+ when :while, :loop
74
+ is_while = d_name == :while
75
+ stmt_list << build_print_stmt(stag_info, attr_info, append_exprs) if !is_while
76
+ stmt_list << NativeStatement.new("while (#{d_arg}) {", :while)
77
+ stmt_list << build_print_stmt(stag_info, attr_info, append_exprs) if is_while
78
+ stmt_list.concat(cont_stmts)
79
+ stmt_list << build_print_stmt(etag_info, nil, nil) if is_while
80
+ stmt_list << NativeStatement.new("}", :while)
81
+ stmt_list << build_print_stmt(etag_info, nil, nil) if !is_while
82
+
83
+ when :set
84
+ stmt_list << NativeStatement.new("#{d_arg};", :set)
85
+ stmt_list << build_print_stmt(stag_info, attr_info, append_exprs)
86
+ stmt_list.concat(cont_stmts)
87
+ stmt_list << build_print_stmt(etag_info, nil, nil)
88
+
89
+ when :if
90
+ stmt_list << NativeStatement.new("if (#{d_arg}) {", :if)
91
+ stmt_list << build_print_stmt(stag_info, attr_info, append_exprs)
92
+ stmt_list.concat(cont_stmts)
93
+ stmt_list << build_print_stmt(etag_info, nil, nil)
94
+ stmt_list << NativeStatement.new("}", :if)
95
+
96
+ when :elseif, :else
97
+ unless !stmt_list.empty? && (st=stmt_list[-1]).is_a?(NativeStatement) && (st.kind == :if || st.kind == :elseif)
98
+ raise convert_error("'#{d_str}': previous statement should be 'if' or 'elsif'.", stag_info.linenum)
99
+ end
100
+ stmt_list.pop # delete 'end'
101
+ if d_name == :else
102
+ kind = :else
103
+ stmt_list << NativeStatement.new("} else {", :else)
104
+ else
105
+ kind = :elseif
106
+ stmt_list << NativeStatement.new("} elseif (#{d_arg}) {", :elseif)
107
+ end
108
+ stmt_list << build_print_stmt(stag_info, attr_info, append_exprs)
109
+ stmt_list.concat(cont_stmts)
110
+ stmt_list << build_print_stmt(etag_info, nil, nil)
111
+ stmt_list << NativeStatement.new("}", kind)
112
+
113
+ when :default, :Default, :DEFAULT
114
+ error_if_empty_tag(stag_info, etag_info, d_name, d_arg)
115
+ stmt_list << build_print_stmt(stag_info, attr_info, append_exprs)
116
+ stmt_list << NativeStatement.new_without_newline("if (#{d_arg}) {", :if)
117
+ flag_escape = d_name == :default ? nil : (d_name == :Default)
118
+ stmt_list << PrintStatement.new([ NativeExpression.new(d_arg, flag_escape) ])
119
+ stmt_list << NativeStatement.new_without_newline("} else {", :else)
120
+ stmt_list.concat(cont_stmts)
121
+ stmt_list << NativeStatement.new_without_newline("}", :else)
122
+ stmt_list << build_print_stmt(etag_info, nil, nil)
123
+
124
+ else
125
+ return false
126
+
127
+ end #case
128
+ return true
129
+
130
+ end #def
131
+
132
+
133
+ end #class
134
+ Handler.register_class('php', PhpHandler)
135
+
136
+
137
+
138
+ ##
139
+ ## translator for php
140
+ ##
141
+ class PhpTranslator < BaseTranslator
142
+
143
+
144
+ PHP_EMBED_PATTERNS = [
145
+ '<?php ', ' ?>', # statement
146
+ '<?php echo ', '; ?>', # expression
147
+ '<?php echo htmlspecialchars(', '); ?>', # escaped expression
148
+ ]
149
+
150
+
151
+ def initialize(properties={})
152
+ super(PHP_EMBED_PATTERNS, properties)
153
+ end
154
+
155
+
156
+ def translate_string(str)
157
+ str.gsub!(/<\?xml/, '<<?php ?>?xml')
158
+ super(str)
159
+ end
160
+
161
+
162
+ end
163
+ Translator.register_class('php', PhpTranslator)
164
+
165
+
166
+
167
+ end #module