1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
|
#-*- perl -*-
#
# Copyright (C) 2006 Ken'ichi Fukamachi
# All rights reserved. This program is free software; you can
# redistribute it and/or modify it under the same terms as Perl itself.
#
# $FML: Project.pm,v 1.5 2006/10/08 09:10:27 fukachan Exp $
#
package FML::Demo::Project;
use strict;
use vars qw(@ISA @EXPORT @EXPORT_OK $AUTOLOAD);
use Carp;
my $global_language = "Japanese";
=head1 NAME
FML::Demo::Project - generate pseudo Gantt chart (demonstration module).
=head1 SYNOPSIS
=head1 DESCRIPTION
=head1 METHODS
=head2 C<new()>
constructor.
=cut
# Descriptions: constuctor.
# Arguments: OBJ($self)
# Side Effects: none
# Return Value: OBJ
sub new
{
my ($self) = @_;
my ($type) = ref($self) || $self;
my $me = {};
return bless $me, $type;
}
# Descriptions: parse file.
# Arguments: OBJ($self) STR($file)
# Side Effects: save data at $self->{ _data }.
# Return Value: none
sub parse
{
my ($self, $file) = @_;
my ($data) = [];
use FileHandle;
my $rh = new FileHandle $file;
if (defined $rh) {
my ($buf, $line, $level, $date_list, $status, $comment);
LINE:
while ($buf = <$rh>) {
next LINE if $buf =~ /^\#/o;
$level = 0;
$date_list = [];
$status = '';
$comment = '';
if ($buf =~ /^\%format/) {
$self->_parse_format($buf);
next LINE;
}
if ($buf =~ /^\%alias/) {
$self->_parse_alias($buf);
next LINE;
}
if ($buf =~ /^\%date_range/) {
$self->_parse_date_range($buf);
next LINE;
}
if ($buf =~ /^\S+/) { $level = 1;}
if ($buf =~ /^\t{1}\S+/) { $level = 2;}
if ($buf =~ /^\t{2}\S+/) { $level = 3;}
if ($buf =~ /^\t{3}\S+/) { $level = 4;}
$line++;
$data->[ $line ] = {};
$buf =~ s/^\s*//;
my ($title, @_data) = split(/\s+/, $buf);
DATA:
for my $s (@_data) {
if ($s =~ /^[-\d+\/]+$/) {
$date_list = $self->_get_canonical_date_list($s);
next DATA;
}
elsif ($s =~ /^(DONE|WAIT)$/) {
$status = $s;
next DATA;
}
else {
$comment .= $s;
}
}
$data->[ $line ]->{ level } = $level;
$data->[ $line ]->{ title } = $title;
$data->[ $line ]->{ date_list } = $date_list;
$data->[ $line ]->{ status } = $status;
$data->[ $line ]->{ comment } = $comment;
}
$rh->close();
}
$self->{ _data } = $data;
return $data;
}
# Descriptions: parse the given string and return the date list as ARRAY_REF.
# Arguments: OBJ($self) STR($s)
# Side Effects: none
# Return Value: ARRAY_REF
sub _get_canonical_date_list
{
my ($self, $s) = @_;
if ($s =~ /^([\/\d]+)-([\/\d]+)$/) {
my $first = $self->_canonical_date($1);
my $last = $self->_canonical_date($2);
return $self->_expand_date_list( $first, $last );
}
else {
my $d = $self->_canonical_date($s);
return [ $d ];
}
}
# Descriptions: return date list from $first to $last.
# Arguments: OBJ($self) STR($first) STR($last)
# Side Effects: none
# Return Value: ARRAY_REF
sub _expand_date_list
{
my ($self, $first, $last) = @_;
my $r = [];
use Time::ParseDate;
my $first_sec = parsedate($first);
my $last_sec = parsedate($last);
for (my $sec = $first_sec; $sec <= $last_sec; $sec += 86400) {
use Mail::Message::Date;
my $date = new Mail::Message::Date $sec;
my $yyyy = $date->YYYYxMMxDD($sec);
push(@$r, $yyyy);
}
return $r;
}
# Descriptions: return canonicalized date string.
# Arguments: OBJ($self) STR($date)
# Side Effects: none
# Return Value: STR
sub _canonical_date
{
my ($self, $date) = @_;
my ($year, $month, $day);
my ($_sec,$_min,$_hour,$_mday,$_mon,$_year,$_wday) = localtime(time);
$_year += 1900;
if ($date =~ /^(\d+)\/(\d+)$/) {
($year, $month, $day) = ($_year, $1, $2);
}
elsif ($date =~ /^(\d+)\/(\d+)\/(\d+)$/) {
($year, $month, $day) = ($1, $2, $3);
}
return sprintf("%04d/%02d/%02d", $year, $month, $day);
}
# Descriptions: parse %format line and build in-object data.
# Arguments: OBJ($self) STR($format)
# Side Effects: update $self->{ _format } field.
# Return Value: none
sub _parse_format
{
my ($self, $format) = @_;
$format =~ s/^\%format\s+//;
my (@format) = split(/\s+/, $format);
$self->{ _format } = \@format || [];
}
# Descriptions: parse %alias line and build in-object data.
# Arguments: OBJ($self) STR($alias)
# Side Effects: update $self->{ _alias } field.
# Return Value: none
sub _parse_alias
{
my ($self, $alias) = @_;
$alias =~ s/^\%alias\s+//;
my ($src, $dst) = split(/\s+/, $alias);
$self->{ _alias }->{ $src } = $dst;
}
# Descriptions: parse %date_range line and build in-object data.
# Arguments: OBJ($self) STR($date)
# Side Effects: update $self->{ _alias } field.
# Return Value: none
sub _parse_date_range
{
my ($self, $date) = @_;
$date =~ s/^\%date_range\s+//;
my ($d0, $d1) = split(/\s+/, $date);
$self->{ _date_range } = [ $d0, $d1 ];
}
# Descriptions: build data object.
# Arguments: OBJ($self)
# Side Effects: none
# Return Value: none
sub build
{
my ($self) = @_;
my ($data) = $self->{ _data };
my ($format) = $self->{ _format } || [];
my ($alias) = $self->{ _alias } || {};
my ($line) = 0;
my ($_data);
# $data->[ $line ]->{ level } = $level;
# $data->[ $line ]->{ title } = $title;
# $data->[ $line ]->{ date } = $date;
# $data->[ $line ]->{ comment } = $comment;
use FML::Demo::Chart;
my $chart = new FML::Demo::Chart;
my $max_line = $#$data;
LINE:
for (my $line = 1; $line < $max_line; $line++) {
my $level = $data->[ $line ]->{ level } || 1;
my $title = $data->[ $line ]->{ title } || '';
my $date_list = $data->[ $line ]->{ date_list } || [];
my $status = $data->[ $line ]->{ status } || '';
my $comment = $data->[ $line ]->{ comment } || '';
if ($level == 1) {
$chart->add($line, "item1", $title);
}
elsif ($level == 2) {
$chart->add($line, "item2", $title);
}
else {
$chart->add($line, "item1", "");
}
if (@$date_list) {
my $mark = $self->get_mark_nl();
for my $day (@$date_list) {
$chart->add($line, $day, $mark);
}
# save information for GanttProject XML format.
my $duration = $#$date_list + 1;
$chart->add($line, "duration", $duration);
my $range_start = $self->{ _date_range }->[0];
$range_start = $self->_canonical_date($range_start);
$chart->add($line, "start_time", $date_list->[0] || $range_start);
}
else {
my $range_start = $self->{ _date_range }->[0];
$range_start = $self->_canonical_date($range_start);
$chart->add($line, "start_time", $date_list->[0] || $range_start);
}
if ($status) {
$chart->add($line, "status", $status);
}
if ($comment) {
$chart->add($line, "misc", $comment);
}
}
$chart->set_format($format);
$chart->set_alias($alias);
my $range = $self->{ _date_range };
$chart->set_date_range(@$range);
$self->{ _chart } = $chart;
}
# Descriptions: print as HTML TABLE format.
# Arguments: OBJ($self)
# Side Effects: none
# Return Value: none
sub print_as_html_table
{
my ($self) = @_;
my $chart = $self->{ _chart };
$chart->print_as_html_table();
}
# Descriptions: print as CSV format.
# Arguments: OBJ($self)
# Side Effects: none
# Return Value: none
sub print_as_csv
{
my ($self) = @_;
my $chart = $self->{ _chart };
$chart->print_as_csv();
}
# Descriptions: print as GanntProject XML format.
# Arguments: OBJ($self)
# Side Effects: none
# Return Value: none
sub print_as_xml
{
my ($self) = @_;
my $chart = $self->{ _chart };
$chart->print_as_xml();
}
=head1 Japanese Specific Methods
=head2 get_mark_nl()
=cut
# Descriptions: return mark.
# Arguments: OBJ($self)
# Side Effects: none
# Return Value: STR
sub get_mark_nl
{
my ($self) = @_;
my $base_class = "FML::Demo::Language";
my $module = sprintf("%s::%s", $base_class, $global_language);
my $mark = 'O';
eval qq{
use $module;
my \$lang = new $module;
\$mark = \$lang->get_mark();
};
croak($@) if $@;
return( $mark || 'O' );
}
if ($0 eq __FILE__) {
my $file = shift @ARGV;
my $proj = new FML::Demo::Project;
$proj->parse($file);
$proj->build();
$proj->print_as_xml();
}
=head1 CODING STYLE
See C<http://www.fml.org/software/FNF/> on fml coding style guide.
=head1 AUTHOR
Ken'ichi Fukamachi
=head1 COPYRIGHT
Copyright (C) 2006 Ken'ichi Fukamachi
All rights reserved. This program is free software; you can
redistribute it and/or modify it under the same terms as Perl itself.
=head1 HISTORY
FML::Demo::Project appeared in fml8 mailing list driver package.
See C<http://www.fml.org/> for more details.
=cut
1;
|