line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Template::Plugin::Java; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
($VERSION) = '$ProjectVersion: 0.4 $' =~ /\$ProjectVersion:\s+(\S+)/; |
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
=head1 NAME |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
Template::Plugin::Java - Generate Java Classes from XML description files and templates. |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
=head1 SYNOPSIS |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
From an xml file such as: |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
value2 |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
10 |
18
|
|
|
|
|
|
|
String |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
20 |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
Through the program "tjava": |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
tjava [options] [file.xml ...] |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
Via a template, such as: |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
[% USE Java %] |
31
|
|
|
|
|
|
|
package $package; |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
public class $class { |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
[% FOREACH Java.variables %] |
36
|
|
|
|
|
|
|
$type $name = $initializer; |
37
|
|
|
|
|
|
|
[% END %] |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
//... etc |
40
|
|
|
|
|
|
|
} |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
To generated Java source code in the appropriate directory as determined by the |
43
|
|
|
|
|
|
|
package of the .xml file's directory, specified package option if any, and |
44
|
|
|
|
|
|
|
CLASSPATH. |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
=head1 OPTIONS |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
Any options may be given besides those listed, these are passed directly to the |
49
|
|
|
|
|
|
|
Templates being processed in the stash (the variable table at time of |
50
|
|
|
|
|
|
|
processing). They can be given in the ... section of an XML |
51
|
|
|
|
|
|
|
file (in which case, don't use the -- dashes) as attributes or elements, or on |
52
|
|
|
|
|
|
|
the command line. |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
=over 8 |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
=item B<--template> |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
Name of the template to process. No extension is assumed by default unlike in |
59
|
|
|
|
|
|
|
the previous version. |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
=item B<--package> |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
Destination package to put the generated classes, otherwise will be determined |
64
|
|
|
|
|
|
|
from how the current directory relates to the CLASSPATH. |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
=item B<--class> |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
Class name to use, otherwise will infer from the root tag of the XML file. |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
=item B<--templatePath> |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
Colon separated path where the templates can be found, overrides the |
73
|
|
|
|
|
|
|
environment variable TEMPLATEPATH. This doesn't work right now, so use the |
74
|
|
|
|
|
|
|
TEMPLATEPATH environment variable. |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
=item B<--genContainers> |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
If set to 0, classes for subcontainers will not be generated. This is generally |
79
|
|
|
|
|
|
|
not useful. |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
=item B<--containerTemplate> |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
By default set to F, this is the default template, as well |
84
|
|
|
|
|
|
|
as the template used for sub-containers. |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
=item B<--containerNamePrefix> |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
By default, if generating class Foo that needs to have a sub container wrapped |
89
|
|
|
|
|
|
|
in tag , it's name will be FooBar. This is safe and won't cause collisions |
90
|
|
|
|
|
|
|
with different classes having sub containers of the same name (until some sort |
91
|
|
|
|
|
|
|
of dependency checking code is introduced). To turn this off, set it to the |
92
|
|
|
|
|
|
|
empty string "". |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
=item B<--interface> |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
Interface to add to list of implemented interfaces, can be supplied multiple |
97
|
|
|
|
|
|
|
times. Make sure you append any necessary code to implement any of these |
98
|
|
|
|
|
|
|
interfaces. |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
=item B<--append> |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
Text to insert in the generated class body. |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
=item B<--appendFile> |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
Will insert text read from the file specified into the generated class body. |
107
|
|
|
|
|
|
|
This option and the B<--append> option are mutually exclusive. |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
=item B<--file[s]> |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
The XML file(s) to parse. This is useful for when the Plugin is instantiated |
112
|
|
|
|
|
|
|
from a custom script, not via tjava or inside a template. |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
Any other option will be placed into the stash for the templates to use, making |
115
|
|
|
|
|
|
|
tjava very useful with your custom templates. |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
Anything that's not an option will be assumed to be a file. |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
=back |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
=head1 DESCRIPTION |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
Template::Plugin::Java is a plugin module for the Template toolkit that makes |
124
|
|
|
|
|
|
|
it easier to write templates for generating Java source code, ultimately for |
125
|
|
|
|
|
|
|
transforming XML descriptions into Java language sources. |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
It can be used either directly on the command line, or loaded from a Template |
128
|
|
|
|
|
|
|
with a C<[% USE Java %]> statement, or in many other ways. It tries to be |
129
|
|
|
|
|
|
|
intelligent and figure out what context you are using it in. |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
I'll write more eventually, for now see the examples in the distribution. |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
=head1 METHODS |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
=over 8 |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
=cut |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
require Template::Plugin; |
140
|
|
|
|
|
|
|
@ISA = 'Template::Plugin'; |
141
|
|
|
|
|
|
|
|
142
|
1
|
|
|
1
|
|
1105
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
202
|
|
143
|
1
|
|
|
1
|
|
7
|
use Carp qw/verbose croak/; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
225
|
|
144
|
1
|
|
|
|
|
122
|
use Template::Plugin::Java::Utils qw( |
145
|
|
|
|
|
|
|
parseOptions findPackageDir isNum determinePackage createTemplate |
146
|
|
|
|
|
|
|
parseCmdLine javaTypeName |
147
|
1
|
|
|
1
|
|
863
|
); |
|
1
|
|
|
|
|
3
|
|
148
|
1
|
|
|
1
|
|
5
|
use Template::Plugin::Java::Constants qw/:all/; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
176
|
|
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
=item B |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
This, the constructor, does everything necessary to create a new instance of |
153
|
|
|
|
|
|
|
the Java plugin, based on context. If not given a context, takes control of the |
154
|
|
|
|
|
|
|
command line and then parses any options and files given. This is what the |
155
|
|
|
|
|
|
|
"tjava" utility does. |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
=cut |
158
|
|
|
|
|
|
|
sub new { |
159
|
1
|
|
|
1
|
|
2304
|
use XML::Simple; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
use File::Basename; |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
my $class = shift; |
163
|
|
|
|
|
|
|
my $self = bless {}, ref $class || $class; |
164
|
|
|
|
|
|
|
my $context; |
165
|
|
|
|
|
|
|
my $params = {}; |
166
|
|
|
|
|
|
|
my $arg1 = $_[0]; |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
if (@_ <= 1 && not ref $arg1) { |
169
|
|
|
|
|
|
|
$params->{file} = shift; |
170
|
|
|
|
|
|
|
} elsif (not ref $arg1) { |
171
|
|
|
|
|
|
|
$params = {@_}; |
172
|
|
|
|
|
|
|
} elsif (UNIVERSAL::isa($arg1, 'Template::Context')) { |
173
|
|
|
|
|
|
|
$self->context(shift); |
174
|
|
|
|
|
|
|
} elsif (UNIVERSAL::isa($arg1, 'HASH')) { |
175
|
|
|
|
|
|
|
$params = { %{+shift}, @_ }; |
176
|
|
|
|
|
|
|
} |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
$self->context(delete $params->{context}); |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
my $defaults = delete $params->{defaults} || {}; |
181
|
|
|
|
|
|
|
my $cmd_line = delete $params->{cmdLine} || {}; |
182
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
# Automatically parse the command line unless either explicitly told not to, or |
184
|
|
|
|
|
|
|
# a the object has been created inside a template as an actual plugin. |
185
|
|
|
|
|
|
|
unless ((exists $params->{parseCmdLine} |
186
|
|
|
|
|
|
|
&& (not $params->{parseCmdLine})) |
187
|
|
|
|
|
|
|
|| $self->context) { |
188
|
|
|
|
|
|
|
$cmd_line = { |
189
|
|
|
|
|
|
|
%$cmd_line, |
190
|
|
|
|
|
|
|
parseOptions( parseCmdLine ) |
191
|
|
|
|
|
|
|
}; |
192
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
# Use rest of @ARGV as files. |
194
|
|
|
|
|
|
|
push @{$params->{files}}, @ARGV; |
195
|
|
|
|
|
|
|
@ARGV = (); |
196
|
|
|
|
|
|
|
} |
197
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
unless ($self->context) { |
199
|
|
|
|
|
|
|
$self->template ( |
200
|
|
|
|
|
|
|
createTemplate delete $params->{templateOptions} |
201
|
|
|
|
|
|
|
); |
202
|
|
|
|
|
|
|
} |
203
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
my $files = delete $params->{file} || delete $params->{files}; |
205
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
my @files; |
207
|
|
|
|
|
|
|
if (defined $files) { |
208
|
|
|
|
|
|
|
if (UNIVERSAL::isa($files, 'ARRAY')) { |
209
|
|
|
|
|
|
|
@files = @$files; |
210
|
|
|
|
|
|
|
} else { |
211
|
|
|
|
|
|
|
push @files, $files; |
212
|
|
|
|
|
|
|
} |
213
|
|
|
|
|
|
|
} |
214
|
|
|
|
|
|
|
|
215
|
|
|
|
|
|
|
# The ! eof STDIN is necessary here, because sub-templates will want to create |
216
|
|
|
|
|
|
|
# new instances of this Plugin, when the process still has a redirected STDIN, |
217
|
|
|
|
|
|
|
# just with no data to read. Using eof on a terminal is bad, but this doesn't |
218
|
|
|
|
|
|
|
# happen because of the && short circuit. |
219
|
|
|
|
|
|
|
if (scalar @files == 0 && ! -t STDIN && ! eof STDIN) { |
220
|
|
|
|
|
|
|
push @files, '-'; |
221
|
|
|
|
|
|
|
} |
222
|
|
|
|
|
|
|
|
223
|
|
|
|
|
|
|
for my $file_name (@files) { |
224
|
|
|
|
|
|
|
my $stash; |
225
|
|
|
|
|
|
|
|
226
|
|
|
|
|
|
|
if ($file_name ne '-') { |
227
|
|
|
|
|
|
|
# Prepend ./ if relative path. |
228
|
|
|
|
|
|
|
$file_name =~ s!^([^/-])!./$1!; |
229
|
|
|
|
|
|
|
$stash = XMLin ( |
230
|
|
|
|
|
|
|
$file_name, |
231
|
|
|
|
|
|
|
keyattr => "", |
232
|
|
|
|
|
|
|
keeproot => 1, |
233
|
|
|
|
|
|
|
cache => 'storable' |
234
|
|
|
|
|
|
|
); |
235
|
|
|
|
|
|
|
} else { |
236
|
|
|
|
|
|
|
# Reading from STDIN. |
237
|
|
|
|
|
|
|
my $data; |
238
|
|
|
|
|
|
|
{ |
239
|
|
|
|
|
|
|
local @ARGV = '-'; |
240
|
|
|
|
|
|
|
$data = join '', <>; |
241
|
|
|
|
|
|
|
} |
242
|
|
|
|
|
|
|
$stash = XMLin ( |
243
|
|
|
|
|
|
|
$data, |
244
|
|
|
|
|
|
|
keyattr => "", |
245
|
|
|
|
|
|
|
keeproot => 1, |
246
|
|
|
|
|
|
|
); |
247
|
|
|
|
|
|
|
} |
248
|
|
|
|
|
|
|
|
249
|
|
|
|
|
|
|
my $root = (keys %$stash)[0]; |
250
|
|
|
|
|
|
|
$stash = {%{$stash->{$root}}}; |
251
|
|
|
|
|
|
|
|
252
|
|
|
|
|
|
|
my $context = delete $stash->{'java:'} || {}; |
253
|
|
|
|
|
|
|
|
254
|
|
|
|
|
|
|
$stash = { |
255
|
|
|
|
|
|
|
parseOptions( |
256
|
|
|
|
|
|
|
%$defaults, |
257
|
|
|
|
|
|
|
%$params, |
258
|
|
|
|
|
|
|
%$context, |
259
|
|
|
|
|
|
|
%$cmd_line |
260
|
|
|
|
|
|
|
), |
261
|
|
|
|
|
|
|
variables => $stash |
262
|
|
|
|
|
|
|
}; |
263
|
|
|
|
|
|
|
|
264
|
|
|
|
|
|
|
$stash->{tag} = $root; |
265
|
|
|
|
|
|
|
$stash->{class} ||= ucfirst $root; |
266
|
|
|
|
|
|
|
|
267
|
|
|
|
|
|
|
# Allow nopackage="true" to create a class that isn't in a package. |
268
|
|
|
|
|
|
|
{ |
269
|
|
|
|
|
|
|
# Turn off warnings about comparing uninitialized values. |
270
|
|
|
|
|
|
|
local $^W = undef; |
271
|
|
|
|
|
|
|
|
272
|
|
|
|
|
|
|
if (!$stash->{package} && $stash->{package} ne '0') { |
273
|
|
|
|
|
|
|
$stash->{package} = |
274
|
|
|
|
|
|
|
determinePackage dirname($file_name); |
275
|
|
|
|
|
|
|
} |
276
|
|
|
|
|
|
|
} |
277
|
|
|
|
|
|
|
|
278
|
|
|
|
|
|
|
$stash->{genContainers} ||= TRUE; |
279
|
|
|
|
|
|
|
$stash->{containerTemplate} ||= 'Container'; |
280
|
|
|
|
|
|
|
$stash->{template} ||= $stash->{containerTemplate}; |
281
|
|
|
|
|
|
|
|
282
|
|
|
|
|
|
|
$stash->{containerNamePrefix} = $stash->{class} |
283
|
|
|
|
|
|
|
if not exists $stash->{containerNamePrefix}; |
284
|
|
|
|
|
|
|
|
285
|
|
|
|
|
|
|
if (exists $stash->{appendFile}) { |
286
|
|
|
|
|
|
|
use IO::File; |
287
|
|
|
|
|
|
|
my $file = new IO::File $stash->{appendFile} |
288
|
|
|
|
|
|
|
or die "Could not open $stash->{appendFile}"; |
289
|
|
|
|
|
|
|
local $/ = undef; |
290
|
|
|
|
|
|
|
$stash->{append} .= <$file>; |
291
|
|
|
|
|
|
|
} |
292
|
|
|
|
|
|
|
|
293
|
|
|
|
|
|
|
$self->genClass($stash); |
294
|
|
|
|
|
|
|
} |
295
|
|
|
|
|
|
|
|
296
|
|
|
|
|
|
|
return $self; |
297
|
|
|
|
|
|
|
} |
298
|
|
|
|
|
|
|
|
299
|
|
|
|
|
|
|
=item B |
300
|
|
|
|
|
|
|
|
301
|
|
|
|
|
|
|
Sets the Template of the instance (and therefore the context) when called with |
302
|
|
|
|
|
|
|
a parameter, returns it otherwise. |
303
|
|
|
|
|
|
|
|
304
|
|
|
|
|
|
|
=cut |
305
|
|
|
|
|
|
|
sub template { |
306
|
|
|
|
|
|
|
my ($self, $template) = @_; |
307
|
|
|
|
|
|
|
|
308
|
|
|
|
|
|
|
if ($template) { |
309
|
|
|
|
|
|
|
$self->{template} = $template; |
310
|
|
|
|
|
|
|
$self->context($template->context); |
311
|
|
|
|
|
|
|
} |
312
|
|
|
|
|
|
|
|
313
|
|
|
|
|
|
|
return $self->{template}; |
314
|
|
|
|
|
|
|
} |
315
|
|
|
|
|
|
|
|
316
|
|
|
|
|
|
|
=item B |
317
|
|
|
|
|
|
|
|
318
|
|
|
|
|
|
|
Sets the Template::Context of the instance when called with a parameter, |
319
|
|
|
|
|
|
|
returns it otherwise. |
320
|
|
|
|
|
|
|
|
321
|
|
|
|
|
|
|
=cut |
322
|
|
|
|
|
|
|
sub context { $_[0]->{context} = $_[1] || $_[0]->{context} } |
323
|
|
|
|
|
|
|
|
324
|
|
|
|
|
|
|
=item B |
325
|
|
|
|
|
|
|
|
326
|
|
|
|
|
|
|
Returns an initializer string for a type. |
327
|
|
|
|
|
|
|
|
328
|
|
|
|
|
|
|
=cut |
329
|
|
|
|
|
|
|
sub initializer { |
330
|
|
|
|
|
|
|
my ($self, $type) = @_; |
331
|
|
|
|
|
|
|
$type ||= $self->context->stash->get('type'); |
332
|
|
|
|
|
|
|
|
333
|
|
|
|
|
|
|
# Can check if user defined, for example StringInitializer="null" in xml file |
334
|
|
|
|
|
|
|
# or template, and use that. But only if not called as a static method. |
335
|
|
|
|
|
|
|
if (ref $self) { |
336
|
|
|
|
|
|
|
my $res = $self->context->stash->get($type.'Initializer'); |
337
|
|
|
|
|
|
|
return $res if defined $res && $res ne ""; |
338
|
|
|
|
|
|
|
} |
339
|
|
|
|
|
|
|
|
340
|
|
|
|
|
|
|
return '""' if $self->string($type); |
341
|
|
|
|
|
|
|
|
342
|
|
|
|
|
|
|
return $self->encapsulatePrimitive($type).".MIN_VALUE" |
343
|
|
|
|
|
|
|
if $self->scalar($type); |
344
|
|
|
|
|
|
|
|
345
|
|
|
|
|
|
|
return "new $type(0)" |
346
|
|
|
|
|
|
|
if $type eq 'java.sql.Date' || $type eq 'Date'; |
347
|
|
|
|
|
|
|
|
348
|
|
|
|
|
|
|
return undef if $type =~ /\[\]$/; |
349
|
|
|
|
|
|
|
|
350
|
|
|
|
|
|
|
return "new $type()"; |
351
|
|
|
|
|
|
|
} |
352
|
|
|
|
|
|
|
|
353
|
|
|
|
|
|
|
=item B |
354
|
|
|
|
|
|
|
|
355
|
|
|
|
|
|
|
Returns a list of variable description hashes. |
356
|
|
|
|
|
|
|
|
357
|
|
|
|
|
|
|
=cut |
358
|
|
|
|
|
|
|
sub variables { |
359
|
|
|
|
|
|
|
my ($self, $options) = @_; |
360
|
|
|
|
|
|
|
|
361
|
|
|
|
|
|
|
my $vars = $self->getVariables ( |
362
|
|
|
|
|
|
|
$self->context->stash->get('variables'), |
363
|
|
|
|
|
|
|
$options |
364
|
|
|
|
|
|
|
); |
365
|
|
|
|
|
|
|
|
366
|
|
|
|
|
|
|
return [ map { |
367
|
|
|
|
|
|
|
my $key = $_; |
368
|
|
|
|
|
|
|
my $type = $self->mapType($key, $vars->{$key}); |
369
|
|
|
|
|
|
|
|
370
|
|
|
|
|
|
|
# Returns a hashref for each map iteration: |
371
|
|
|
|
|
|
|
{ |
372
|
|
|
|
|
|
|
name => $key, |
373
|
|
|
|
|
|
|
capName => ucfirst $key, |
374
|
|
|
|
|
|
|
type => $type, |
375
|
|
|
|
|
|
|
typeName=> javaTypeName $type, |
376
|
|
|
|
|
|
|
value => $vars->{$key}, |
377
|
|
|
|
|
|
|
initializer => $self->initializer($type) |
378
|
|
|
|
|
|
|
}; |
379
|
|
|
|
|
|
|
} (sort keys %$vars) ]; |
380
|
|
|
|
|
|
|
} |
381
|
|
|
|
|
|
|
|
382
|
|
|
|
|
|
|
=item B |
383
|
|
|
|
|
|
|
|
384
|
|
|
|
|
|
|
Returns a list of strings such as: |
385
|
|
|
|
|
|
|
String foo |
386
|
|
|
|
|
|
|
int bar |
387
|
|
|
|
|
|
|
... |
388
|
|
|
|
|
|
|
|
389
|
|
|
|
|
|
|
These can be used in a template in this way: |
390
|
|
|
|
|
|
|
function ([% Java.variableDeclarations.join(", ") %]) { |
391
|
|
|
|
|
|
|
... |
392
|
|
|
|
|
|
|
} |
393
|
|
|
|
|
|
|
|
394
|
|
|
|
|
|
|
=cut |
395
|
|
|
|
|
|
|
sub variableDeclarations { |
396
|
|
|
|
|
|
|
my ($self, $options) = @_; |
397
|
|
|
|
|
|
|
|
398
|
|
|
|
|
|
|
my $vars = $self->getVariables ( |
399
|
|
|
|
|
|
|
$self->context->stash->get('variables'), |
400
|
|
|
|
|
|
|
$options |
401
|
|
|
|
|
|
|
); |
402
|
|
|
|
|
|
|
|
403
|
|
|
|
|
|
|
return [ map { |
404
|
|
|
|
|
|
|
my $key = $_; |
405
|
|
|
|
|
|
|
$self->mapType($key, $vars->{$key}).' '.$key; |
406
|
|
|
|
|
|
|
} (sort keys %$vars) ]; |
407
|
|
|
|
|
|
|
} |
408
|
|
|
|
|
|
|
|
409
|
|
|
|
|
|
|
=item B |
410
|
|
|
|
|
|
|
|
411
|
|
|
|
|
|
|
Returns a list of variable names. |
412
|
|
|
|
|
|
|
|
413
|
|
|
|
|
|
|
=cut |
414
|
|
|
|
|
|
|
sub variableNames { |
415
|
|
|
|
|
|
|
my ($self, $options) = @_; |
416
|
|
|
|
|
|
|
|
417
|
|
|
|
|
|
|
return [ |
418
|
|
|
|
|
|
|
keys %{ $self->getVariables ( |
419
|
|
|
|
|
|
|
$self->context->stash->get('variables'), |
420
|
|
|
|
|
|
|
$options |
421
|
|
|
|
|
|
|
)} |
422
|
|
|
|
|
|
|
]; |
423
|
|
|
|
|
|
|
} |
424
|
|
|
|
|
|
|
|
425
|
|
|
|
|
|
|
=item B |
426
|
|
|
|
|
|
|
|
427
|
|
|
|
|
|
|
Returns a hashref of variables, taking a raw variables hash. |
428
|
|
|
|
|
|
|
Takes an optional variable type string. |
429
|
|
|
|
|
|
|
|
430
|
|
|
|
|
|
|
=cut |
431
|
|
|
|
|
|
|
sub getVariables { |
432
|
|
|
|
|
|
|
my ($self, $vars, $options) = @_; |
433
|
|
|
|
|
|
|
$options ||= { type => 'All' }; |
434
|
|
|
|
|
|
|
|
435
|
|
|
|
|
|
|
if ($options->{type} eq 'ScalarArray') { |
436
|
|
|
|
|
|
|
1; |
437
|
|
|
|
|
|
|
} |
438
|
|
|
|
|
|
|
|
439
|
|
|
|
|
|
|
# Don't spew out stuff belonging to our namespace. |
440
|
|
|
|
|
|
|
my @names = grep { !/^java:/ } keys %$vars; |
441
|
|
|
|
|
|
|
my %vars; |
442
|
|
|
|
|
|
|
|
443
|
|
|
|
|
|
|
if ($options->{type} eq 'All') { |
444
|
|
|
|
|
|
|
@vars{@names} = @$vars{@names}; |
445
|
|
|
|
|
|
|
} elsif ($options->{type} eq 'Scalar') { |
446
|
|
|
|
|
|
|
for my $n (@names) { |
447
|
|
|
|
|
|
|
if ($self->scalar($self->mapType($n, $vars->{$n}))) { |
448
|
|
|
|
|
|
|
$vars{$n} = $vars->{$n}; |
449
|
|
|
|
|
|
|
} |
450
|
|
|
|
|
|
|
} |
451
|
|
|
|
|
|
|
} elsif ($options->{type} eq 'Composite') { |
452
|
|
|
|
|
|
|
for my $n (@names) { |
453
|
|
|
|
|
|
|
if (!$self->scalar($self->mapType($n, $vars->{$n})) |
454
|
|
|
|
|
|
|
and |
455
|
|
|
|
|
|
|
!$self->array($self->mapType($n, $vars->{$n})) |
456
|
|
|
|
|
|
|
and |
457
|
|
|
|
|
|
|
$self->mapType($n, $vars->{$n}) !~ /\[\]/) { |
458
|
|
|
|
|
|
|
$vars{$n} = $vars->{$n}; |
459
|
|
|
|
|
|
|
} |
460
|
|
|
|
|
|
|
} |
461
|
|
|
|
|
|
|
} elsif ($options->{type} eq '!Scalar') { |
462
|
|
|
|
|
|
|
for my $n (@names) { |
463
|
|
|
|
|
|
|
if (!$self->scalar($self->mapType($n, $vars->{$n}))){ |
464
|
|
|
|
|
|
|
$vars{$n} = $vars->{$n}; |
465
|
|
|
|
|
|
|
} |
466
|
|
|
|
|
|
|
} |
467
|
|
|
|
|
|
|
} elsif ($options->{type} eq '!Composite') { |
468
|
|
|
|
|
|
|
for my $n (@names) { |
469
|
|
|
|
|
|
|
if ($self->scalar($self->mapType($n, $vars->{$n})) |
470
|
|
|
|
|
|
|
or |
471
|
|
|
|
|
|
|
$self->array($self->mapType($n, $vars->{$n}))) { |
472
|
|
|
|
|
|
|
$vars{$n} = $vars->{$n}; |
473
|
|
|
|
|
|
|
} |
474
|
|
|
|
|
|
|
} |
475
|
|
|
|
|
|
|
} elsif ($options->{type} eq 'ScalarArray') { |
476
|
|
|
|
|
|
|
for my $n (@names) { |
477
|
|
|
|
|
|
|
if ($self->array($self->mapType($n, $vars->{$n})) |
478
|
|
|
|
|
|
|
and |
479
|
|
|
|
|
|
|
$self->scalar($self->arrayType($n, $vars->{$n}))) { |
480
|
|
|
|
|
|
|
$vars{$n} = $vars->{$n}; |
481
|
|
|
|
|
|
|
} |
482
|
|
|
|
|
|
|
} |
483
|
|
|
|
|
|
|
} elsif ($options->{type} eq 'CompositeArray') { |
484
|
|
|
|
|
|
|
for my $n (@names) { |
485
|
|
|
|
|
|
|
if ($self->array($self->mapType($n, $vars->{$n})) |
486
|
|
|
|
|
|
|
and |
487
|
|
|
|
|
|
|
!$self->scalar($self->arrayType($n, $vars->{$n})) |
488
|
|
|
|
|
|
|
and |
489
|
|
|
|
|
|
|
!$self->array($self->arrayType($n, $vars->{$n}))) { |
490
|
|
|
|
|
|
|
$vars{$n} = $vars->{$n}; |
491
|
|
|
|
|
|
|
} |
492
|
|
|
|
|
|
|
} |
493
|
|
|
|
|
|
|
} else { |
494
|
|
|
|
|
|
|
die "Unknown option $options->{type}"; |
495
|
|
|
|
|
|
|
} |
496
|
|
|
|
|
|
|
|
497
|
|
|
|
|
|
|
return \%vars; |
498
|
|
|
|
|
|
|
} |
499
|
|
|
|
|
|
|
|
500
|
|
|
|
|
|
|
=item B |
501
|
|
|
|
|
|
|
|
502
|
|
|
|
|
|
|
Whether or not a java type is a Scalar type. |
503
|
|
|
|
|
|
|
|
504
|
|
|
|
|
|
|
=cut |
505
|
|
|
|
|
|
|
sub scalar { |
506
|
|
|
|
|
|
|
my ($self, $type) = @_; |
507
|
|
|
|
|
|
|
$type ||= $self->context->stash->get('type'); |
508
|
|
|
|
|
|
|
|
509
|
|
|
|
|
|
|
return TRUE if $type =~ /@{[SCALAR]}/; |
510
|
|
|
|
|
|
|
return FALSE; |
511
|
|
|
|
|
|
|
} |
512
|
|
|
|
|
|
|
|
513
|
|
|
|
|
|
|
=item B |
514
|
|
|
|
|
|
|
|
515
|
|
|
|
|
|
|
Whether or not a java type is a String type. |
516
|
|
|
|
|
|
|
|
517
|
|
|
|
|
|
|
=cut |
518
|
|
|
|
|
|
|
sub string { |
519
|
|
|
|
|
|
|
my ($self, $type) = @_; |
520
|
|
|
|
|
|
|
$type ||= $self->context->stash->get('type'); |
521
|
|
|
|
|
|
|
|
522
|
|
|
|
|
|
|
return $1 if $type =~ /@{[STRING]}/; |
523
|
|
|
|
|
|
|
return undef; |
524
|
|
|
|
|
|
|
} |
525
|
|
|
|
|
|
|
|
526
|
|
|
|
|
|
|
=item B |
527
|
|
|
|
|
|
|
|
528
|
|
|
|
|
|
|
Whether or not a java type is an Array type. |
529
|
|
|
|
|
|
|
|
530
|
|
|
|
|
|
|
=cut |
531
|
|
|
|
|
|
|
sub array { |
532
|
|
|
|
|
|
|
my ($self, $type) = @_; |
533
|
|
|
|
|
|
|
$type ||= $self->context->stash->get('type'); |
534
|
|
|
|
|
|
|
|
535
|
|
|
|
|
|
|
return $1 if $type =~ /@{[ARRAY]}/; |
536
|
|
|
|
|
|
|
return undef; |
537
|
|
|
|
|
|
|
} |
538
|
|
|
|
|
|
|
|
539
|
|
|
|
|
|
|
=item B |
540
|
|
|
|
|
|
|
|
541
|
|
|
|
|
|
|
Figures out the type of elements a Vector will take. |
542
|
|
|
|
|
|
|
Parameters: name, arrayref |
543
|
|
|
|
|
|
|
TODO: This should be an aggregate, not merely the type of the first element. |
544
|
|
|
|
|
|
|
|
545
|
|
|
|
|
|
|
=cut |
546
|
|
|
|
|
|
|
sub arrayType { |
547
|
|
|
|
|
|
|
my ($self, $name, $value) = @_; |
548
|
|
|
|
|
|
|
|
549
|
|
|
|
|
|
|
my $type = $self->mapType($name, $value->[0]); |
550
|
|
|
|
|
|
|
|
551
|
|
|
|
|
|
|
if ($self->scalar($type)) { |
552
|
|
|
|
|
|
|
return $self->encapsulatePrimitive($type); |
553
|
|
|
|
|
|
|
} |
554
|
|
|
|
|
|
|
|
555
|
|
|
|
|
|
|
return $type; |
556
|
|
|
|
|
|
|
} |
557
|
|
|
|
|
|
|
|
558
|
|
|
|
|
|
|
=item B |
559
|
|
|
|
|
|
|
|
560
|
|
|
|
|
|
|
Translate int to Integer, long to Long, etc. |
561
|
|
|
|
|
|
|
|
562
|
|
|
|
|
|
|
=cut |
563
|
|
|
|
|
|
|
sub encapsulatePrimitive { |
564
|
|
|
|
|
|
|
my ($self, $type) = @_; |
565
|
|
|
|
|
|
|
|
566
|
|
|
|
|
|
|
if ($type eq 'int') { |
567
|
|
|
|
|
|
|
return 'Integer'; |
568
|
|
|
|
|
|
|
} else { |
569
|
|
|
|
|
|
|
return ucfirst $type; |
570
|
|
|
|
|
|
|
} |
571
|
|
|
|
|
|
|
} |
572
|
|
|
|
|
|
|
|
573
|
|
|
|
|
|
|
{ # Closure over type cache. |
574
|
|
|
|
|
|
|
|
575
|
|
|
|
|
|
|
my %cache; |
576
|
|
|
|
|
|
|
|
577
|
|
|
|
|
|
|
=item B |
578
|
|
|
|
|
|
|
|
579
|
|
|
|
|
|
|
Maps a perl scalar or reference to a Java type. |
580
|
|
|
|
|
|
|
Parameters: name of element, value of element. |
581
|
|
|
|
|
|
|
|
582
|
|
|
|
|
|
|
=cut |
583
|
|
|
|
|
|
|
sub mapType { |
584
|
|
|
|
|
|
|
my $self = shift; |
585
|
|
|
|
|
|
|
my $name = shift || croak "name required"; |
586
|
|
|
|
|
|
|
my $value = shift || croak "value required"; |
587
|
|
|
|
|
|
|
|
588
|
|
|
|
|
|
|
my $type = ref $value; |
589
|
|
|
|
|
|
|
my $result; |
590
|
|
|
|
|
|
|
|
591
|
|
|
|
|
|
|
return $cache{"$name $type"} if exists $cache{"$name $type"}; |
592
|
|
|
|
|
|
|
|
593
|
|
|
|
|
|
|
if (not $type) { # I.E. a scalar. |
594
|
|
|
|
|
|
|
my $is_num = isNum $value; |
595
|
|
|
|
|
|
|
|
596
|
|
|
|
|
|
|
if ($is_num && $value =~ /\./) { |
597
|
|
|
|
|
|
|
$result = 'double'; |
598
|
|
|
|
|
|
|
} elsif ($is_num) { |
599
|
|
|
|
|
|
|
$result = 'int'; |
600
|
|
|
|
|
|
|
} else { |
601
|
|
|
|
|
|
|
$result = 'String'; |
602
|
|
|
|
|
|
|
} |
603
|
|
|
|
|
|
|
} elsif($type eq 'ARRAY') { |
604
|
|
|
|
|
|
|
$result = 'Vector'; |
605
|
|
|
|
|
|
|
} elsif($type eq 'HASH') { |
606
|
|
|
|
|
|
|
my @keys = keys %$value; |
607
|
|
|
|
|
|
|
|
608
|
|
|
|
|
|
|
# Could be pre-mapped to a java type. |
609
|
|
|
|
|
|
|
if (exists $value->{'java:type'}) { |
610
|
|
|
|
|
|
|
$result = $value->{'java:type'}; |
611
|
|
|
|
|
|
|
} else { |
612
|
|
|
|
|
|
|
# Sub-container. |
613
|
|
|
|
|
|
|
my $s = $self->context->stash; |
614
|
|
|
|
|
|
|
if ($s->get('genContainers')) { |
615
|
|
|
|
|
|
|
$result = $self->genClass ({ |
616
|
|
|
|
|
|
|
tag => $name, |
617
|
|
|
|
|
|
|
class => |
618
|
|
|
|
|
|
|
$s->get('containerNamePrefix') |
619
|
|
|
|
|
|
|
."\u$name", |
620
|
|
|
|
|
|
|
template => |
621
|
|
|
|
|
|
|
$s->get('containerTemplate'), |
622
|
|
|
|
|
|
|
variables => $value |
623
|
|
|
|
|
|
|
}); |
624
|
|
|
|
|
|
|
} else { |
625
|
|
|
|
|
|
|
$result = 'Container'; |
626
|
|
|
|
|
|
|
} |
627
|
|
|
|
|
|
|
} |
628
|
|
|
|
|
|
|
} else { |
629
|
|
|
|
|
|
|
die "Cannot map type $type to a Java type"; |
630
|
|
|
|
|
|
|
} |
631
|
|
|
|
|
|
|
|
632
|
|
|
|
|
|
|
$cache{"$name $type"} = $result; |
633
|
|
|
|
|
|
|
|
634
|
|
|
|
|
|
|
return $result; |
635
|
|
|
|
|
|
|
} |
636
|
|
|
|
|
|
|
|
637
|
|
|
|
|
|
|
} # End closure. |
638
|
|
|
|
|
|
|
|
639
|
|
|
|
|
|
|
=item B |
640
|
|
|
|
|
|
|
|
641
|
|
|
|
|
|
|
Generates a container class. |
642
|
|
|
|
|
|
|
|
643
|
|
|
|
|
|
|
Parameters: name of tag to create container from, hashref to gen from. |
644
|
|
|
|
|
|
|
Returns: name of class generated. |
645
|
|
|
|
|
|
|
|
646
|
|
|
|
|
|
|
=cut |
647
|
|
|
|
|
|
|
sub genClass { |
648
|
|
|
|
|
|
|
my ($self, $hash) = @_; |
649
|
|
|
|
|
|
|
|
650
|
|
|
|
|
|
|
my $context = $self->context; |
651
|
|
|
|
|
|
|
|
652
|
|
|
|
|
|
|
my $variables = delete $hash->{variables}; |
653
|
|
|
|
|
|
|
|
654
|
|
|
|
|
|
|
my $v = delete $variables->{'java:'} || {}; |
655
|
|
|
|
|
|
|
$v = { |
656
|
|
|
|
|
|
|
%{$v}, |
657
|
|
|
|
|
|
|
%{$hash} |
658
|
|
|
|
|
|
|
}; |
659
|
|
|
|
|
|
|
|
660
|
|
|
|
|
|
|
$v->{destFile} = $v->{class}.".java"; |
661
|
|
|
|
|
|
|
|
662
|
|
|
|
|
|
|
if (exists $v->{package}) { |
663
|
|
|
|
|
|
|
$v->{destFile} = findPackageDir ( |
664
|
|
|
|
|
|
|
$v->{package} |
665
|
|
|
|
|
|
|
).$v->{destFile}; |
666
|
|
|
|
|
|
|
} |
667
|
|
|
|
|
|
|
|
668
|
|
|
|
|
|
|
$context->localise({ |
669
|
|
|
|
|
|
|
%{$v}, |
670
|
|
|
|
|
|
|
variables => $variables |
671
|
|
|
|
|
|
|
}); |
672
|
|
|
|
|
|
|
|
673
|
|
|
|
|
|
|
# If not using version 2+ of Template, the context needs the output to be |
674
|
|
|
|
|
|
|
# redirected to the appropriate file. |
675
|
|
|
|
|
|
|
if ($Template::VERSION =~ /^[01]/) { |
676
|
|
|
|
|
|
|
# This is necessary for compiling with the newer version, since the |
677
|
|
|
|
|
|
|
# TEMPLATE_OUTPUT constant is gone: |
678
|
|
|
|
|
|
|
my $redirect_constant = |
679
|
|
|
|
|
|
|
&{Template::Constants->can('TEMPLATE_OUTPUT')}(); |
680
|
|
|
|
|
|
|
|
681
|
|
|
|
|
|
|
my $old_output_handle = $context->redirect( |
682
|
|
|
|
|
|
|
$redirect_constant, |
683
|
|
|
|
|
|
|
$v->{destFile} |
684
|
|
|
|
|
|
|
); |
685
|
|
|
|
|
|
|
|
686
|
|
|
|
|
|
|
$context->process($v->{template}); |
687
|
|
|
|
|
|
|
|
688
|
|
|
|
|
|
|
$context->redirect( |
689
|
|
|
|
|
|
|
$redirect_constant, |
690
|
|
|
|
|
|
|
$old_output_handle |
691
|
|
|
|
|
|
|
); |
692
|
|
|
|
|
|
|
} else { |
693
|
|
|
|
|
|
|
# In Template version 2+ process returns the output of processing a template. |
694
|
|
|
|
|
|
|
my $handle = new IO::File "> $v->{destFile}" |
695
|
|
|
|
|
|
|
or croak "Could not write to $v->{destFile}: $!"; |
696
|
|
|
|
|
|
|
|
697
|
|
|
|
|
|
|
print $handle $context->process($v->{template}); |
698
|
|
|
|
|
|
|
$handle->close; |
699
|
|
|
|
|
|
|
} |
700
|
|
|
|
|
|
|
|
701
|
|
|
|
|
|
|
$context->delocalise; |
702
|
|
|
|
|
|
|
|
703
|
|
|
|
|
|
|
# Put variables back. |
704
|
|
|
|
|
|
|
$hash->{variables} = $variables; |
705
|
|
|
|
|
|
|
|
706
|
|
|
|
|
|
|
# Return fully qualified name, or just name. |
707
|
|
|
|
|
|
|
if (exists $v->{package}) { |
708
|
|
|
|
|
|
|
return $v->{package}.".".$v->{class}; |
709
|
|
|
|
|
|
|
} else { |
710
|
|
|
|
|
|
|
return $v->{class}; |
711
|
|
|
|
|
|
|
} |
712
|
|
|
|
|
|
|
} |
713
|
|
|
|
|
|
|
|
714
|
|
|
|
|
|
|
=item B |
715
|
|
|
|
|
|
|
|
716
|
|
|
|
|
|
|
Casts a java String to another type using the appropriate code. |
717
|
|
|
|
|
|
|
Parameters: name of variable to cast, type to cast to. |
718
|
|
|
|
|
|
|
|
719
|
|
|
|
|
|
|
=cut |
720
|
|
|
|
|
|
|
sub castJavaString { |
721
|
|
|
|
|
|
|
my $self = shift; |
722
|
|
|
|
|
|
|
&Template::Plugin::Java::Utils::castJavaString; |
723
|
|
|
|
|
|
|
} |
724
|
|
|
|
|
|
|
|
725
|
|
|
|
|
|
|
1; |
726
|
|
|
|
|
|
|
|
727
|
|
|
|
|
|
|
__END__ |