MetaGenerator.pm untested

filter: Statementstmt 0.0% 0 / 59 Branchbran 0.0% 0 / 6 Conditioncond 0.0% 0 / 10 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 11 totaltotal 0.0% 0 / 107 CC 4 SCAR 14.1
CC
4
Cov
0%
validate
12.0
get_instance
6.0
BEGIN
2.0
CRAP 4.1
1 #
2 # MetaGenerator.pm - Object that parses schemes, collects and validates input and generates
3 # output.
4 #
5 #
6 # Author: Riccardo Cambiassi <brujah@infodrome.net>
7 #
8 #
9 # This program is free software; you can redistribute it and/or modify
10 # it under the same terms as perl itself.
11 #
12 package XML::MetaGenerator;
13  
14  
15 use strict;
16 use vars qw($form @contest $valids $missings $invalids);
17  
18 use XML::Parser;
19  
20 BEGIN {
21 $XML::MetaGenerator::VERSION = '0.03';
22 @XML::MetaGenerator::ISA = qw();
23 }
24  
25 =pod
26  
27 =head1 NAME
28  
29 XML::MetaGenerator - Collects user input, validates input and generates output in a number of ways based on the defined grammar.
30  
31 =head1 SYNOPSIS
32  
33 use XML::MetaGenerator;
34 use XML::MetaGenerator::Language::Formula;
35 use XML::MetaGenerator::Language::Formula::Collector::ReadLine;
36 use XML::MetaGenerator::Language::Formula::Generator::HTML;
37  
38 my $wow = XML::MetaGenerator->get_instance();
39 $wow->setObject('user');
40 $wow->setLanguage(XML::MetaGenerator::Language::Formula->new());
41  
42 my $input = XML::MetaGenerator::Language::Formula::Collector::ReadLine->new();
43 $wow->setCollector($input);
44  
45 my $generator = XML::MetaGenerator::Language::Formula::Generator::HTML->new();
46 $wow->setGenerator($generator);
47  
48 # now collect the data
49 $wow->collect();
50  
51 # validate it
52 my ($valids, $missings, $invalids)= $wow->validate();
53  
54 # generate a document from the collected data;
55 my $page = $wow->generate();
56  
57 =head1 DESCRIPTION
58  
59 This object will work with many kinds of XML specification languages (RELAX, xml-scheme, Formula and so on), using them to catch ('collect') input data from different sources (cgi, command line, files ...), 'validate' it and eventually transforming it into the desired format (e.g. HTML, XML).
60 I use this in order to have a common API for applications that require multiple media access (e.g. WWW and console).
61  
62 =cut
63  
64 sub _new_instance {
65 my $proto = shift;
66 my $class = ref $proto || $proto;
67 my $formula_key = shift;
68 my $repository = "/usr/local/share/formulae/";
69 my $p = XML::Parser->new();
70 bless {
71 formula_key => $formula_key,
72 repository => $repository,
73 p => $p
74 }, $class;
75 }
76  
77 =pod
78  
79 =head1 Object methods
80  
81 These are the public methods available from MetaGenerator objects
82  
83 =over 4
84  
85 =item get_instance
86  
87 returns the singleton MetaGenerator object
88  
89 =cut
90  
91 sub get_instance
92 {
93 my ($class) = shift;
94 no strict 'refs';
95 # we use CodeWitch::_instance, so if any application inherit from
96 # CodeWitch, the instace is still unique.
97  
98 my $instance = \${__PACKAGE__ . '::_instance'};
99  
100 defined $$instance
101 ? $$instance
102 : ($$instance = $class->_new_instance());
103 }
104  
105 =pod
106  
107 =item setCollector
108  
109 This let the user choose what kind of collector will be used.
110 The only parameter is a XML::MetaGenerator::Collector object.
111 XML::MetaGenerator comes with three prepackaged collectors:
112 Apache - read data from $r params
113 Environment - read data from %ENV
114 ReadLine - read data from terminal using Term::ReadLine
115  
116 =cut
117  
118 sub setCollector {
119 my ($self, $collector) = @_;
120 $self->{collector} = $collector;
121 }
122  
123 =pod
124  
125 =item setGenerator
126  
127 This let the user choose what kind of output to generate.
128 The parameter is a XML::MetaGenerator::Generator object.
129 The package comes with two prepackaged generator:
130 SimpleHTML - generate a html document consisting of a table with one row for every element in the formula; Also, if the data was validated before calling the generator, invalid elements will be highlighted.
131 XML - generate a basic xml document with XML::Writer.
132  
133 =cut
134  
135 sub setGenerator {
136 my ($self, $generator) = @_;
137 $self->{generator} = $generator;
138 }
139  
140 =pod
141  
142 =item setLanguage
143  
144 This method lets the user choose what kind of grammar to
145 use for validation.
146 Currently XML::MetaGenerator supports just the formula language, for more information on it, refer to XML::MetaGenerator::Formula(3).
147 I hope to be able to add more languages soon (e.g. RELAX and xml-schema).
148  
149 =cut
150  
151 sub setLanguage {
152 my ($self, $validator) = @_;
153 $self->{validator} = $validator;
154 }
155  
156 =pod
157  
158 =item setObject
159  
160 This set the type of objects that MetaGenerator is going to manipulate.
161 The formulae directory holds some sample objects defined
162 through the formula markup language.
163  
164 =cut
165 sub setObject {
166 my ($self, $obj) = @_;
167 $self->{formula_key} = $obj;
168 }
169  
170 =pod
171  
172 =item collect
173  
174 This is the input method for MetaGenerator.
175 It uses the Collector object to get the data from the right input source
176  
177 =cut
178  
179 sub collect {
180 my ($self) = shift;
181 my $formula = $self->{formula_key};
182 $self->{p}->setHandlers(@{$self->{collector}->{handlers}});
183 $self->{p} ->parsefile($self->{repository}.$formula.".xml");
184 }
185  
186 =pod
187  
188 =item template
189  
190 This method outputs a dummy object. It's useful for testing
191 or when you have to show how that object will look like.
192  
193 =cut
194  
195 sub template {
196 my ($self) = shift;
197 my ($init) = shift || 0;
198 }
199  
200 =pod
201  
202 =item generate
203  
204 This method is used to parse the collected input and to
205 output the desired object in the desired format.
206  
207 =cut
208  
209 sub generate {
210 my ($self) = shift;
211 my ($validate) = shift || 0;
212 my ($formula) = $self->{formula_key};
213 $self->{p}->setHandlers(@{$self->{generator}->{handlers}});
214 $self->{p} ->parsefile($self->{repository}.$formula.".xml");
215 my ($buffer) = $self->{generator}->{buffer};
216 return $buffer;
217 }
218  
219 =pod
220  
221 =item validate
222  
223 This method validates the input and returns an hash containing
224 valids, invalids and missing elements.
225  
226 =cut
227  
228 sub validate {
229 my ($self) = shift;
230 my $formula = (defined($self->{form}->{formula}) && $self->{form}->{formula} ne '')?$self->{form}->{formula}:$self->{formula_key};
231 print STDERR "Validating a $formula\n";
232 return unless ($formula);
233  
234 local ($valids) = {};
235 local ($missings) = [];
236 local ($invalids) = [];
237 # $self->{p}->setHandlers(Start => undef, End => undef, Char => undef);
238 $self->{p}->setHandlers(@{$self->{validator}->{handlers}});
239 $self->{p} ->parsefile($self->{repository}.$formula.".xml");
240 return ($self->{validator}->{valids}, $self->{validator}->{missings}, $self->{validator}->{invalids});
241 }
242  
243 =pod
244  
245 =back
246  
247 =cut
248  
249 1;
250 __END__