Commit | Line | Data |
d0eafc11 |
1 | package Excel::Template::Factory; |
2 | |
3 | use strict; |
4 | |
d01e4722 |
5 | my %Manifest = ( |
d0eafc11 |
6 | |
7 | # These are the instantiable nodes |
8 | 'IF' => 'Excel::Template::Container::Conditional', |
9 | 'LOOP' => 'Excel::Template::Container::Loop', |
10 | 'ROW' => 'Excel::Template::Container::Row', |
11 | 'SCOPE' => 'Excel::Template::Container::Scope', |
12 | 'WORKBOOK' => 'Excel::Template::Container::Workbook', |
13 | 'WORKSHEET' => 'Excel::Template::Container::Worksheet', |
14 | |
37513eae |
15 | 'BACKREF' => 'Excel::Template::Element::Backref', |
d0eafc11 |
16 | 'CELL' => 'Excel::Template::Element::Cell', |
17 | 'FORMULA' => 'Excel::Template::Element::Formula', |
d3018037 |
18 | 'IMAGE' => 'Excel::Template::Element::Image', |
37513eae |
19 | 'RANGE' => 'Excel::Template::Element::Range', |
d0eafc11 |
20 | 'VAR' => 'Excel::Template::Element::Var', |
21 | |
22 | 'FORMAT' => 'Excel::Template::Container::Format', |
23 | |
24 | # These are all the Format short-cut objects |
37513eae |
25 | # They are also instantiable |
d0eafc11 |
26 | 'BOLD' => 'Excel::Template::Container::Bold', |
27 | 'HIDDEN' => 'Excel::Template::Container::Hidden', |
28 | 'ITALIC' => 'Excel::Template::Container::Italic', |
29 | 'LOCKED' => 'Excel::Template::Container::Locked', |
30 | 'OUTLINE' => 'Excel::Template::Container::Outline', |
31 | 'SHADOW' => 'Excel::Template::Container::Shadow', |
32 | 'STRIKEOUT' => 'Excel::Template::Container::Strikeout', |
33 | |
8fd01531 |
34 | 'KEEP_LEADING_ZEROS' => 'Excel::Template::Container::KeepLeadingZeros', |
35 | |
d0eafc11 |
36 | # These are the helper objects |
37513eae |
37 | # They are also in here to make E::T::Factory::isa() work. |
d0eafc11 |
38 | 'CONTEXT' => 'Excel::Template::Context', |
39 | 'ITERATOR' => 'Excel::Template::Iterator', |
40 | 'TEXTOBJECT' => 'Excel::Template::TextObject', |
41 | |
42 | 'CONTAINER' => 'Excel::Template::Container', |
43 | 'ELEMENT' => 'Excel::Template::Element', |
44 | |
45 | 'BASE' => 'Excel::Template::Base', |
46 | ); |
47 | |
d01e4722 |
48 | my %isBuildable = map { $_ => ~~1 } qw( |
49 | WORKBOOK WORKSHEET |
50 | FORMAT BOLD HIDDEN ITALIC LOCKED OUTLINE SHADOW STRIKEOUT |
8fd01531 |
51 | IF ROW LOOP SCOPE KEEP_LEADING_ZEROS |
d3018037 |
52 | CELL FORMULA IMAGE |
d01e4722 |
53 | VAR BACKREF RANGE |
d0eafc11 |
54 | ); |
55 | |
d0eafc11 |
56 | { |
d01e4722 |
57 | my %Loaded; |
58 | sub _load_class |
59 | { |
60 | my $self = shift; |
61 | my ($class) = @_; |
62 | |
63 | unless ( exists $Loaded{$class} ) |
64 | { |
65 | (my $filename = $class) =~ s!::!/!g; |
66 | eval { |
67 | require "$filename.pm"; |
68 | }; if ($@) { |
69 | die "Cannot find or compile PM file for '$class' ($filename)\n"; |
70 | } |
71 | |
72 | $Loaded{$class} = ~~1; |
73 | } |
e976988f |
74 | |
d01e4722 |
75 | return ~~1; |
76 | } |
e976988f |
77 | } |
78 | |
79 | { |
d0eafc11 |
80 | my @param_names = qw(name class isa); |
e976988f |
81 | sub register |
d0eafc11 |
82 | { |
e976988f |
83 | my $self = shift; |
84 | my %params = @_; |
85 | |
86 | for (@param_names) |
d0eafc11 |
87 | { |
e976988f |
88 | unless ($params{$_}) |
89 | { |
90 | warn "$_ was not supplied to register()\n" if $^W; |
91 | return; |
92 | } |
d0eafc11 |
93 | } |
d0eafc11 |
94 | |
e976988f |
95 | my $name = uc $params{name}; |
96 | if (exists $Manifest{$name}) |
97 | { |
98 | warn "$params{name} already exists in the manifest.\n" if $^W; |
99 | return; |
100 | } |
d0eafc11 |
101 | |
e976988f |
102 | my $isa = uc $params{isa}; |
103 | unless (exists $Manifest{$isa}) |
104 | { |
105 | warn "$params{isa} does not exist in the manifest.\n" if $^W; |
106 | return; |
107 | } |
d0eafc11 |
108 | |
e976988f |
109 | { |
110 | no strict 'refs'; |
111 | unshift @{"$params{class}::ISA"}, $Manifest{$isa}; |
112 | } |
d0eafc11 |
113 | |
e976988f |
114 | $self->_load_class( $Manifest{$isa} ); |
115 | $self->_load_class( $params{class} ); |
d0eafc11 |
116 | |
e976988f |
117 | $Manifest{$name} = $params{class}; |
118 | $isBuildable{$name} = ~~1; |
119 | |
120 | return ~~1; |
121 | } |
d0eafc11 |
122 | } |
123 | |
6dd4c89d |
124 | sub _create |
d0eafc11 |
125 | { |
e976988f |
126 | my $self = shift; |
d0eafc11 |
127 | my $name = uc shift; |
128 | |
129 | return unless exists $Manifest{$name}; |
130 | |
e976988f |
131 | $self->_load_class( $Manifest{$name} ); |
d0eafc11 |
132 | |
133 | return $Manifest{$name}->new(@_); |
134 | } |
135 | |
6dd4c89d |
136 | sub _create_node |
d0eafc11 |
137 | { |
e976988f |
138 | my $self = shift; |
d0eafc11 |
139 | my $name = uc shift; |
140 | |
141 | return unless exists $isBuildable{$name}; |
142 | |
e976988f |
143 | return $self->_create($name, @_); |
d0eafc11 |
144 | } |
145 | |
146 | sub isa |
147 | { |
148 | return unless @_ >= 2; |
149 | exists $Manifest{uc $_[1]} |
150 | ? UNIVERSAL::isa($_[0], $Manifest{uc $_[1]}) |
151 | : UNIVERSAL::isa(@_) |
152 | } |
153 | |
37513eae |
154 | sub is_embedded |
155 | { |
156 | return unless @_ >= 1; |
157 | |
6dd4c89d |
158 | isa( $_[0], $_ ) && return ~~1 for qw( VAR BACKREF RANGE ); |
37513eae |
159 | return; |
160 | } |
161 | |
d0eafc11 |
162 | 1; |
163 | __END__ |
164 | |
165 | =head1 NAME |
166 | |
167 | Excel::Template::Factory |
168 | |
169 | =head1 PURPOSE |
170 | |
37513eae |
171 | To provide a common way to instantiate Excel::Template nodes |
d0eafc11 |
172 | |
37513eae |
173 | =head1 USAGE |
d0eafc11 |
174 | |
37513eae |
175 | =head2 register() |
d0eafc11 |
176 | |
37513eae |
177 | Use this to register your own nodes. |
d0eafc11 |
178 | |
37513eae |
179 | Example forthcoming. |
d0eafc11 |
180 | |
6dd4c89d |
181 | =head1 METHODS |
182 | |
183 | =head2 isa |
184 | |
185 | This is a customized isa() wrapper for syntactic sugar |
186 | |
187 | =head2 is_embedded |
188 | |
d0eafc11 |
189 | =head1 AUTHOR |
190 | |
c09684ff |
191 | Rob Kinyon (rob.kinyon@gmail.com) |
d0eafc11 |
192 | |
193 | =head1 SEE ALSO |
194 | |
195 | =cut |