File: Factory.pm

package info (click to toggle)
libpdf-fromhtml-perl 0.34-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 576 kB
  • sloc: perl: 4,876; makefile: 15
file content (149 lines) | stat: -rw-r--r-- 3,671 bytes parent folder | download
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
package PDF::FromHTML::Template::Factory;

use strict;

BEGIN {
    use vars qw(%Manifest %isBuildable);
}

%Manifest = (

# These are the instantiable nodes
    'ALWAYS'      => 'PDF::FromHTML::Template::Container::Always',
    'CONDITIONAL' => 'PDF::FromHTML::Template::Container::Conditional',
    'FONT'        => 'PDF::FromHTML::Template::Container::Font',
    'IF'          => 'PDF::FromHTML::Template::Container::Conditional',
    'LOOP'        => 'PDF::FromHTML::Template::Container::Loop',
    'PAGEDEF'     => 'PDF::FromHTML::Template::Container::PageDef',
    'PDFTEMPLATE' => 'PDF::FromHTML::Template::Container::PdfTemplate',
    'ROW'         => 'PDF::FromHTML::Template::Container::Row',
    'SCOPE'       => 'PDF::FromHTML::Template::Container::Scope',
    'SECTION'     => 'PDF::FromHTML::Template::Container::Section',
    'HEADER'      => 'PDF::FromHTML::Template::Container::Header',
    'FOOTER'      => 'PDF::FromHTML::Template::Container::Footer',

    'BOOKMARK'    => 'PDF::FromHTML::Template::Element::Bookmark',
    'CIRCLE'      => 'PDF::FromHTML::Template::Element::Circle',
    'HR'          => 'PDF::FromHTML::Template::Element::HorizontalRule',
    'IMAGE'       => 'PDF::FromHTML::Template::Element::Image',
    'PAGEBREAK'   => 'PDF::FromHTML::Template::Element::PageBreak',
    'LINE'        => 'PDF::FromHTML::Template::Element::Line',
    'TEXTBOX'     => 'PDF::FromHTML::Template::Element::TextBox',
    'VAR'         => 'PDF::FromHTML::Template::Element::Var',
    'WEBLINK'     => 'PDF::FromHTML::Template::Element::Weblink',

# These are the helper objects

    'TEXTOBJECT' => 'PDF::FromHTML::Template::TextObject',
    'CONTEXT'    => 'PDF::FromHTML::Template::Context',
    'ITERATOR'   => 'PDF::FromHTML::Template::Iterator',

    'MARGIN'     => 'PDF::FromHTML::Template::Container::Margin',

    'CONTAINER'  => 'PDF::FromHTML::Template::Container',
    'ELEMENT'    => 'PDF::FromHTML::Template::Element',

    'BASE'       => 'PDF::FromHTML::Template::Base',
);

%isBuildable = map { $_ => 1 } qw(
    ALWAYS
    BOOKMARK
    CIRCLE
    CONDITIONAL
    FONT
    FOOTER
    HEADER
    HR
    IF
    IMAGE
    LINE
    LOOP
    PAGEBREAK
    PAGEDEF
    PDFTEMPLATE
    ROW
    SCOPE
    SECTION
    TEXTBOX
    VAR
    WEBLINK
);

sub register
{
    my %params = @_;

    my @param_names = qw(name class isa);
    for (@param_names)
    {
        unless ($params{$_})
        {
            warn "$_ was not supplied to register()\n";
            return 0;
        }
    }

    my $name = uc $params{name};
    if (exists $Manifest{$name})
    {
        warn "$params{name} already exists in the manifest.\n";
        return 0;
    }

    my $isa = uc $params{isa};
    unless (exists $Manifest{$isa})
    {
        warn "$params{isa} does not exist in the manifest.\n";
        return 0;
    }

    $Manifest{$name} = $params{class};
    $isBuildable{$name} = 1;

    {
        no strict 'refs';
        unshift @{"$params{class}::ISA"}, $Manifest{$isa};
    }

    return 1;
}

sub create
{
    my $class = shift;
    my $name = uc shift;

    return unless exists $Manifest{$name};

    (my $filename = $Manifest{$name}) =~ s!::!/!g;

    eval {
        require "$filename.pm";
    }; if ($@) {
        die "Cannot find or compile PM file for '$name' ($filename)\n";
    }

    return $Manifest{$name}->new(@_);
}

sub create_node
{
    my $class = shift;
    my $name = uc shift;

    return unless exists $isBuildable{$name};

    return $class->create($name, @_);
}

sub isa
{
    return UNIVERSAL::isa($_[0], $Manifest{uc $_[1]})
        if @_ >= 2 && exists $Manifest{uc $_[1]};

    UNIVERSAL::isa(@_)
}

1;
__END__