Xmldoom/Schema/SAXHandler.pm untested

filter: Statementstmt 0.0% 0 / 96 Branchbran 0.0% 0 / 66 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 198 CC 34 SCAR 60.9
CC
34
Cov
0%
start_element
702.0
end_element
30.0
_bool
12.0
CRAP 439.8
1  
2 package Xmldoom::Schema::SAXHandler;
3 use base qw(XML::SAX::Base);
4  
5 use strict;
6  
7 our $DATABASE_NS = "http://gna.org/projects/xmldoom/database";
8 our $DATABASE_PERL_NS = "http://gna.org/projects/xmldoom/database-perl";
9  
10 sub _bool
11 {
12 my $text = shift;
13 if ( $text eq '1' or $text eq 'true' )
14 {
15 return 1;
16 }
17 elsif ( $text eq '0' or $text eq 'false' )
18 {
19 return 0;
20 }
21  
22 return undef;
23 }
24  
25 sub new
26 {
27 my $class = shift;
28 my $args = shift;
29  
30 my $parser;
31  
32 if ( ref($args) eq 'HASH' )
33 {
34 $parser = $args->{parser};
35 }
36 else
37 {
38 $parser = $args;
39 }
40  
41 my $self = {
42 parser => $parser,
43 table => undef,
44 foreign_table => undef,
45  
46 # state-ness
47 column_args => undef,
48 in_option => 0,
49 buffer => "",
50 };
51  
52 bless $self, $class;
53 return $self;
54 }
55  
56 sub start_document
57 {
58 my ($self, $doc) = @_;
59 }
60  
61 sub end_document
62 {
63 my ($self, $doc) = @_;
64 }
65  
66 sub start_element
67 {
68 my ($self, $el) = @_;
69  
70 # simple aliases
71 my $name = $el->{'LocalName'};
72 my $attrs = $el->{'Attributes'};
73  
74 if ( $name eq "database" )
75 {
76 my $args = { };
77  
78 if ( defined $attrs->{'{}name'} )
79 {
80 $args->{name} = $attrs->{'{}name'}->{Value};
81 }
82 if ( defined $attrs->{'{}defaultIdMethod'} )
83 {
84 $args->{defaultIdMethod} = $attrs->{'{}defaultIdMethod'}->{Value};
85 }
86  
87 $self->{parser}->setup_database($args);
88 }
89 elsif ( $name eq "table" )
90 {
91 if ( defined $self->{table} )
92 {
93 die "Cannot nest table declarations";
94 }
95  
96 my $args = {
97 name => $attrs->{'{}name'}->{Value}
98 };
99  
100 if ( defined $attrs->{'{}description'} )
101 {
102 $args->{description} = $attrs->{'{}description'}->{Value};
103 }
104  
105 # store for column adding hot action
106 $self->{table} = $self->{parser}->add_table($args);
107 }
108 elsif ( $name eq "column" )
109 {
110 if ( not defined $self->{table} )
111 {
112 die "Column must be defined inside of a <table/> tag.";
113 }
114  
115 my $args = { };
116  
117 if ( defined $attrs->{'{}name'} )
118 {
119 $args->{name} = $attrs->{'{}name'}->{Value};
120 }
121 if ( defined $attrs->{'{}required'} )
122 {
123 $args->{required} = _bool( $attrs->{'{}required'}->{Value} );
124 }
125 if ( defined $attrs->{'{}primaryKey'} )
126 {
127 $args->{primary_key} = _bool( $attrs->{'{}primaryKey'}->{Value} );
128 }
129 if ( defined $attrs->{'{}auto_increment'} )
130 {
131 $args->{auto_increment} = _bool( $attrs->{'{}auto_increment'}->{Value} );
132 }
133 if ( defined $attrs->{"{$DATABASE_PERL_NS}idGenerator"} )
134 {
135 $args->{id_generator} = $attrs->{"{$DATABASE_PERL_NS}idGenerator"}->{Value};
136 }
137 if ( defined $attrs->{'{}type'} )
138 {
139 $args->{type} = $attrs->{'{}type'}->{Value};
140 }
141 if ( defined $attrs->{'{}size'} )
142 {
143 $args->{size} = $attrs->{'{}size'}->{Value};
144 }
145 if ( defined $attrs->{'{}description'} )
146 {
147 $args->{description} = $attrs->{'{}description'}->{Value};
148 }
149 if ( defined $attrs->{'{}default'} )
150 {
151 $args->{default} = $attrs->{'{}default'}->{Value};
152 }
153 if ( defined $attrs->{'{}timestamp'} )
154 {
155 $args->{timestamp} = $attrs->{'{}timestamp'}->{Value};
156 }
157  
158 $self->{column_args} = $args;
159 }
160 elsif ( $name eq 'options' )
161 {
162 if ( not defined $self->{column_args} or defined $self->{column_args}->{options} )
163 {
164 die "<options/> can be defined once only inside of <column/>";
165 }
166  
167 $self->{column_args}->{options} = [ ];
168 }
169 elsif ( $name eq 'option' )
170 {
171 $self->{in_option} = 1;
172 }
173 elsif ( $name eq 'foreign-key' )
174 {
175 if ( not defined $self->{table} or defined $self->{foreign_key} )
176 {
177 die "<foreign-key/> can only be defined inside of <table/>";
178 }
179  
180 $self->{foreign_key} = {
181 local_columns => [ ],
182 foreign_columns => [ ],
183 foreign_table => $attrs->{'{}foreignTable'}->{Value},
184 };
185 }
186 elsif ( $name eq 'reference' )
187 {
188 if ( not defined $self->{foreign_key} )
189 {
190 die "<reference/> tag must be inside of a <foreign-key> tag with a valid foreignTable attribute";
191 }
192  
193 # store these new values
194 push @{$self->{foreign_key}->{local_columns}}, $attrs->{'{}local'}->{Value};
195 push @{$self->{foreign_key}->{foreign_columns}}, $attrs->{'{}foreign'}->{Value};
196 }
197 }
198  
199 sub characters
200 {
201 my ($self, $h) = @_;
202  
203 # simple alias
204 my $text = $h->{'Data'};
205  
206 if ( $self->{in_option} )
207 {
208 $self->{buffer} .= $text;
209 }
210 }
211  
212 sub end_element
213 {
214 my ($self, $el) = @_;
215  
216 # simple alias
217 my $name = $el->{'LocalName'};
218  
219 if ( $name eq "table" )
220 {
221 # mark that we have left the table
222 $self->{parser}->finish_table($self->{table});
223 $self->{table} = undef;
224 }
225 elsif ( $name eq 'column' )
226 {
227 # add it
228 $self->{parser}->add_column($self->{table}, $self->{column_args});
229  
230 # clear state
231 $self->{column_args} = undef;
232 }
233 elsif ( $name eq 'option' )
234 {
235 my $option = $self->{buffer};
236  
237 # strip the blank space from the string
238 $option =~ s/^\s*//;
239 $option =~ s/\s*$//;
240  
241 # add it.
242 push @{$self->{column_args}->{options}}, $option;
243  
244 # clear the buffer.
245 $self->{buffer} = '';
246 }
247 elsif ( $name eq 'foreign-key' )
248 {
249 $self->{parser}->add_foreign_key($self->{table}, $self->{foreign_key});
250
251 # mark that we have left the foreign key
252 $self->{foreign_key} = undef;
253 }
254 }
255  
256 1;
257