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
|
package SVG::SVG2zinc::Backend;
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU LGPL Libray General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Library General Public License for more details.
#
# You should have received a copy of the GNU Library General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA,
# or refer to http://www.gnu.org/copyleft/lgpl.html
#
##################################################################
# Backend for SVG2zinc
#
# Copyright 2003-2004
# Centre d'?tudes de la Navigation A?rienne
#
# Author: Christophe Mertz <mertz at intuilab dot com>
#
# An abstract class for code generation
# Concrete sub-classes can generate code for perl (script / module), tcl,
# printing, or direct execution
#
# $Id: Backend.pm,v 1.1.1.2 2006-11-16 14:51:45 merlin Exp $
#############################################################################
use strict;
use Carp;
use FileHandle;
use vars qw( $VERSION);
($VERSION) = sprintf("%d.%02d", q$Revision: 1.1.1.2 $ =~ /(\d+)\.(\d+)/);
sub new {
my ($class, %passed_options) = @_;
my $self = {};
bless $self, $class;
$self->_initialize(%passed_options);
return $self;
}
my %new_options = (
-render => 1,
-out => 1,
-in => 1,
-verbose => 1,
);
sub _initialize {
my ($self, %passed_options) = @_;
foreach my $opt (keys (%passed_options))
{
if (defined ($new_options{$opt}))
{
$self->{$opt} = $passed_options{$opt};
}
else
{
carp ("Warning: option $opt unknown for a ".ref($self)."\n");
}
}
$self -> {-render} = 1 unless defined $self -> {-render};
croak("undefined mandatory -in options") unless defined $self -> {-in};
if (defined $self -> {-out} and $self -> {-out})
{
my $out = $self->{-out};
if (ref($out) eq 'GLOB')
{
## nothing to do, the $out is supposed to be open!?
}
else
{
my @reps = split ('::', $out);
my $file = $reps [0];
for (my $i = 0; $i < @reps - 1; $i++)
{
if(not -d $file)
{
if(!mkdir ($file))
{
print STDERR "##### Error creating directory: $file\n";
}
}
$file .= '/'.$reps [$i + 1];
}
print STDERR "writing $file...\n";
my $fh = FileHandle -> new("> " . $file);
if ($fh)
{
$self->{-filehandle} = $fh;
}
else
{
carp ("unable to open " . $out);
}
}
}
return $self;
}
# used by SVG2zinc to know the zinc group in which the svg topgroup
# by default: 1
# currently default need be overriden by PerlClass only, as far as I now!?
sub _topgroup {
my ($self) = @_;
if ($self->{-topgroup}) {
return $self->{-topgroup};
} else {
return 1;
}
}
# returns true if code is put in a file
sub inFile {
my ($self) = @_;
return (defined $self->{-filehandle});
}
sub printLines {
my ($self, @lines) = @_;
if ($self->inFile) {
my $fh = $self->{-filehandle};
foreach my $l (@lines) {
print $fh "$l\n";
}
} else {
carp "printLines cannot print if no outfile has been given\n";
}
}
sub treatLines {
my ($self, @lines) = @_;
if ($self->inFile) {
$self->printLines(@lines);
}
}
## in case of file generation, should print a comment
## the default is to print comment starting with #
sub comment {
my ($self, @lines) = @_;
if ($self->inFile) {
foreach my $l (@lines) {
$self->printLines("## $l");
}
}
}
sub close {
my ($self) = @_;
if ($self->inFile) {
$self->{-filehandle}->close;
}
}
sub fileHeader {
my ($self) = @_;
$self->comment ("", "default Header of SVG::SVG2zinc::Backend", "");
}
sub fileTail {
my ($self) = @_;
$self->comment ("", "default Tail of SVG::SVG2zinc::Backend", "");
$self->close;
}
1;
__END__
=head1 NAME
SVG::SVG2zinc::Backend - a virtual class SVG::SVG2zinc svg reader. Sub-class are specialized for different type of generation
=head1 SYNOPSIS
package SVG::SVG2zinc::Backend::SubClass
use SVG::SVG2zinc::Backend;
## some methods definition
....
## when using a specialized backend:
use SVG::SVG2zinc::Backend::SubClass;
$backend = SVG::SVG2zinc::Backend::SubClass->new(
-out => filename_or_handle,
-in => svgfilename,
-verbose => 0|1,
[otheroptions],
);
$backend->fileHeader();
$backend->treatLines("lineOfCode1", "lineOfCode2",...);
$backend->comment("comment1", "comment2", ...);
$backend->printLines("comment1", "comment2", ...);
$backend->fileTail();
=head1 DESCRIPTION
SVG::SVG2zinc::Backend is a perl virtual class which should be specialized in sub-classes. It defines
a common interface ot classes which can for example generate perl code with Tk::Zinc, display
SVG file in a Tk::Zinc widget, convert svg file in image files (e.g. png) or generate tcl code
to be used with TkZinc etc...
A backend should provide the following methods:
=over
=item B<new>
This creation class method should accept pairs of (-option => value) as well as the following arguments:
=over
=item B<-out>
A filename or a filehandle ready for writing the output. In same rare cases
(e.g. the Display backend which only displays the SVG file on the screen,
this option will not be used)
=item B<-in>
The svg filename. It should be used in comments only in the generated file
=item B<-verbose>
It will be used for letting the backend being verbose
=back
=item B<fileHeader>
Generates the header in the out file, if needed. This method should be called just after creating a backend and prior any treatLines or comment method call.
=item B<treatLines>
Processes the given arguments as lines of code. The arguments are very close to Tk::Zinc perl code. When creating a new backend, using the B<Print> backend can help understanding what are exactly these arguments.
=item B<comment>
Processes the given arguments as comments. Depending on the backend, this method must be redefined so that arguments are treated as comments, or just skipped.
=item B<printLines>
Print in an outfile the given arguments as lines of text. This method should not be re-defined, but used by any Backend which generates code.
=item B<fileTail>
Generate the tail in the out file if needed and closes the out file. This must be the last call.
=back
A backend can use the printLines method to print lines in the generated file.
=head1 SEE ALSO
SVG::SVG2zinc::Backend::Display(3pm), SVG::SVG2zinc::Backend::PerlScript(3pm),
SVG::SVG2zinc::Backend::TclScript(3pm), SVG::SVG2zinc::Backend::PerlClass(3pm) code
as examples of SVG::SVG2zinc::Backend subclasses.
SVG::SVG2zinc(3pm)
=head1 AUTHORS
Christophe Mertz <mertz at intuilab dot com> with some help from Daniel Etienne <etienne at cena dot fr>
=head1 COPYRIGHT
CENA (C) 2003-2004
This program is free software; you can redistribute it and/or modify it under the term of the LGPL licence.
=cut
|