Initial Import
[p5sagit/Excel-Template.git] / lib / Excel / Template / Factory.pm
1 package Excel::Template::Factory;
2
3 use strict;
4
5 BEGIN {
6     use vars qw(%Manifest %isBuildable);
7 }
8
9 %Manifest = (
10
11 # These are the instantiable nodes
12     'IF'        => 'Excel::Template::Container::Conditional',
13     'LOOP'      => 'Excel::Template::Container::Loop',
14     'ROW'       => 'Excel::Template::Container::Row',
15     'SCOPE'     => 'Excel::Template::Container::Scope',
16     'WORKBOOK'  => 'Excel::Template::Container::Workbook',
17     'WORKSHEET' => 'Excel::Template::Container::Worksheet',
18
19     'CELL'      => 'Excel::Template::Element::Cell',
20     'FORMULA'   => 'Excel::Template::Element::Formula',
21     'VAR'       => 'Excel::Template::Element::Var',
22
23     'FORMAT'    => 'Excel::Template::Container::Format',
24
25 # These are all the Format short-cut objects
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
34 # These are the helper objects
35
36     'CONTEXT'    => 'Excel::Template::Context',
37     'ITERATOR'   => 'Excel::Template::Iterator',
38     'TEXTOBJECT' => 'Excel::Template::TextObject',
39
40     'CONTAINER'  => 'Excel::Template::Container',
41     'ELEMENT'    => 'Excel::Template::Element',
42
43     'BASE'       => 'Excel::Template::Base',
44 );
45
46 %isBuildable = map { $_ => 1 } qw(
47     BOLD
48     CELL
49     FORMAT
50     FORMULA
51     IF
52     ITALIC
53     OUTLINE
54     LOOP
55     ROW
56     SHADOW
57     STRIKEOUT
58     VAR
59     WORKBOOK
60     WORKSHEET
61 );
62
63 sub register
64 {
65     my %params = @_;
66
67     my @param_names = qw(name class isa);
68     for (@param_names)
69     {
70         unless ($params{$_})
71         {
72             warn "$_ was not supplied to register()\n";
73             return 0;
74         }
75     }
76
77     my $name = uc $params{name};
78     if (exists $Manifest{$name})
79     {
80         warn "$params{name} already exists in the manifest.\n";
81         return 0;
82     }
83
84     my $isa = uc $params{isa};
85     unless (exists $Manifest{$isa})
86     {
87         warn "$params{isa} does not exist in the manifest.\n";
88         return 0;
89     }
90
91     $Manifest{$name} = $params{class};
92     $isBuildable{$name} = 1;
93
94     {
95         no strict 'refs';
96         unshift @{"$params{class}::ISA"}, $Manifest{$isa};
97     }
98
99     return 1;
100 }
101
102 sub create
103 {
104     my $class = shift;
105     my $name = uc shift;
106
107     return unless exists $Manifest{$name};
108
109     (my $filename = $Manifest{$name}) =~ s!::!/!g;
110  
111     eval {
112         require "$filename.pm";
113     }; if ($@) {
114         die "Cannot find or compile PM file for '$name' ($filename)\n";
115     }
116  
117     return $Manifest{$name}->new(@_);
118 }
119
120 sub create_node
121 {
122     my $class = shift;
123     my $name = uc shift;
124
125     return unless exists $isBuildable{$name};
126
127     return $class->create($name, @_);
128 }
129
130 sub isa
131 {
132     return unless @_ >= 2;
133     exists $Manifest{uc $_[1]}
134         ? UNIVERSAL::isa($_[0], $Manifest{uc $_[1]})
135         : UNIVERSAL::isa(@_)
136 }
137
138 1;
139 __END__
140
141 =head1 NAME
142
143 Excel::Template::Factory
144
145 =head1 PURPOSE
146
147 =head1 NODE NAME
148
149 =head1 INHERITANCE
150
151 =head1 ATTRIBUTES
152
153 =head1 CHILDREN
154
155 =head1 AFFECTS
156
157 =head1 DEPENDENCIES
158
159 =head1 USAGE
160
161 =head1 AUTHOR
162
163 Rob Kinyon (rkinyon@columbus.rr.com)
164
165 =head1 SEE ALSO
166
167 =cut