lib/PurpleWiki/View/Driver.pm untested

filter: Statementstmt 0.0% 0 / 84 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 146 CC 15 SCAR 28.9
CC
15
Cov
0%
AUTOLOAD
42.0
recurse
30.0
traverse
12.0
CRAP 18.1
1 # PurpleWiki::View::Driver.pm
2 #
3 # $Id: Driver.pm 366 2004-05-19 19:22:17Z eekim $
4 #
5 # Copyright (c) Blue Oxen Associates 2002-2004. All rights reserved.
6 #
7 # This file is part of PurpleWiki. PurpleWiki is derived from:
8 #
9 # UseModWiki v0.92 (c) Clifford A. Adams 2000-2001
10 # AtisWiki v0.3 (c) Markus Denker 1998
11 # CVWiki CVS-patches (c) Peter Merel 1997
12 # The Original WikiWikiWeb (c) Ward Cunningham
13 #
14 # PurpleWiki is free software; you can redistribute it and/or modify
15 # it under the terms of the GNU General Public License as published by
16 # the Free Software Foundation; either version 2 of the License, or
17 # (at your option) any later version.
18 #
19 # This program is distributed in the hope that it will be useful,
20 # but WITHOUT ANY WARRANTY; without even the implied warranty of
21 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 # GNU General Public License for more details.
23 #
24 # You should have received a copy of the GNU General Public License
25 # along with this program; if not, write to the
26 # Free Software Foundation, Inc.
27 # 59 Temple Place, Suite 330
28 # Boston, MA 02111-1307 USA
29  
30 package PurpleWiki::View::Driver;
31 use 5.005;
32 use strict;
33 use warnings;
34 use Carp;
35 use PurpleWiki::Config;
36 use PurpleWiki::Tree;
37  
38 ######## Package Globals ########
39  
40 our $VERSION;
41 $VERSION = sprintf("%d", q$Id: Driver.pm 366 2004-05-19 19:22:17Z eekim $ =~ /\s(\d+)\s/);
42  
43 # This probably belongs in StructuralNode.pm
44 our @structuralNodeTypes = qw(document section indent ul ol dl h p li dd dt
45 pre sketch);
46  
47 # This probably belongs in InlineNode.pm
48 our @inlineNodeTypes = qw(b i tt text nowiki transclusion link url wikiword
49 freelink image);
50  
51 # I don't know where this belongs, but here is as good a place as any.
52 our @allNodeTypes = (@structuralNodeTypes, @inlineNodeTypes);
53  
54 # Used to quickly see if a node type is valid, we need to do this in AUTOLOAD
55 # and so we make this a package global so as to incur the cost only once.
56 our %lookupTable = map { $_ => 1 } @allNodeTypes;
57  
58  
59 ######## Public Methods ########
60  
61 # Create a new driver.
62 sub new {
63 my $proto = shift;
64 my $self = { @_ };
65 my $class = ref($proto) || $proto;
66  
67 # Make sure we have a PurpleWiki::Config object.
68 $self->{config} = PurpleWiki::Config->instance();
69 croak "PurpleWiki::Config object not found" unless $self->{config};
70  
71 # Object state.
72 $self->{depth} = 0;
73  
74 bless($self, $class);
75 return $self;
76 }
77  
78 # View starts the processing of the PurpleWiki::Tree and returns the
79 # finished string.
80 sub view {
81 my ($self, $wikiTree) = @_;
82 $self->processNode($wikiTree->root) if defined $wikiTree->root;
83 }
84  
85 # Recurse decends down the PurpleWiki::Tree depth first. Structural nodes
86 # have two kinds of children: Inline and Structural, so we need to process
87 # the Inline children of a Structural Node seperately.
88 sub recurse {
89 my ($self, $nodeRef) = @_;
90  
91 # recurse() should never be called on an undefined node.
92 if (not defined $nodeRef) {
93 carp "Warning: tried to recurse on an undefined node\n";
94 return;
95 }
96  
97 if ($nodeRef->isa('PurpleWiki::StructuralNode')) {
98 $self->traverse($nodeRef->content) if defined $nodeRef->content;
99 }
100  
101 $self->traverse($nodeRef->children) if defined $nodeRef->children;
102 }
103  
104 # Traverse goes through a list of nodes calling their pre, main, and post
105 # handlers (in that order). Recursion is depth first because the default
106 # main handler is recurse().
107 sub traverse {
108 my ($self, $nodeListRef) = @_;
109  
110 # traverse() should never be called on an undefined node.
111 if (not defined $nodeListRef) {
112 carp "Warning: tried to traverse on an undefined node list\n";
113 return;
114 }
115
116 foreach my $nodeRef (@{$nodeListRef}) {
117 $self->processNode($nodeRef) if defined $nodeRef;
118 }
119 }
120  
121 # Call the pre, main, and post handlers for a specific node, as well as the
122 # generic structural/inline pre, main, and post handlers.
123 sub processNode {
124 my ($self, $nodeRef) = @_;
125  
126 # processNode() should never be called on an undefined node.
127 if (not defined $nodeRef) {
128 carp "Warning: tried to process an undefined node\n";
129 return;
130 }
131  
132 # We have to construct each method name. The method names are of the form
133 # fooPre, fooMain, and fooPost where foo = $node->type.
134 my $nodePre = $nodeRef->type."Pre";
135 my $nodeMain = $nodeRef->type."Main";
136 my $nodePost = $nodeRef->type."Post";
137  
138 $self->{depth}++;
139  
140 # Run all the handlers
141 $self->Pre($nodeRef);
142 $self->$nodePre($nodeRef);
143  
144 $self->Main($nodeRef);
145 $self->$nodeMain($nodeRef);
146  
147 $self->$nodePost ($nodeRef);
148 $self->Post($nodeRef);
149  
150 $self->{depth}--;
151 }
152  
153 # Noop == No Operation. Just a little stub since it's used by most of
154 # the handlers. Most handlers are a noop by default.
155 sub noop {
156 return;
157 }
158  
159 ######## Private Methods ########
160  
161 # The AUTOLOAD function captures calls to non-existant methods. This allows
162 # us to define a default behavior for a whole set of methods without having
163 # to declare each one individually.
164 #
165 # AUTOLOAD is passed in the method name that was called, but not found. Two
166 # checks are done to resolve the method name and if they both fail the method
167 # is considered unfound. The checks are as follow:
168 #
169 # 1) See if method name is is exactly equal to "Pre", "Main", or "Post"
170 # and if it is call the noop method, which is the default behavior
171 # for these methods.
172 #
173 # 2) Pattern match the method name pulling out the nodeType and opType
174 # (opType is one of Pre, Main, or Post). If the pattern match was
175 # successful we just call the noop method for Pre and Post handlers,
176 # and we call the recurse() method for Main handlers.
177 sub AUTOLOAD {
178 our $AUTOLOAD;
179 my $self = shift;
180 my $method = $AUTOLOAD;
181  
182 # Remove all but the method name
183 $method =~ s/(.*)://g; # Reduces Foo::Bar::Baz::Quz::method to "method"
184  
185 # Bail on DESTROY, otherwise we'll cause an infinite loop when our object
186 # is garbage collected.
187 return if $method =~ /DESTROY/;
188  
189 # The generic Pre, Main, and Post handlers apply to every single node
190 # and are noops by default, we just provide them for ease of use.
191 if ($method eq "Pre" or $method eq "Main" or $method eq "Post") {
192 $self->noop(@_);
193 return;
194 }
195
196 # Do a pattern match to see if $method is a node specific handler, and
197 # extract out the nodeType and the opType if it is.
198 if ($method =~ /^([a-z]+)(Pre|Main|Post)$/) {
199 my ($nodeType, $opType) = ($1, $2);
200  
201 goto notFound if not exists $lookupTable{$nodeType};
202  
203 # Invoke the default behavior for undefined methods.
204 if ($opType eq "Main") {
205 $self->recurse(@_); # fooMain handlers recurse by default
206 } else {
207 $self->noop(@_); # fooPre/Post handlers are noops by default.
208 }
209  
210 return;
211 }
212  
213 notFound:
214 croak "Could not locate $AUTOLOAD.\n";
215 }
216 1;
217 __END__