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