blib/lib/blx/xsdsql/xsd_parser/schema.pm untested

filter: Statementstmt 0.0% 0 / 295 Branchbran 0.0% 0 / 104 Conditioncond 0.0% 0 / 42 MC/DCmcdc 0.0% 0 / 28 Subroutinesub 0.0% 0 / 32 Podpod 0.0% 0 / 26 totaltotal 0.0% 0 / 527 CC 47 SCAR 45.0
CC
47
Cov
0%
_parse_group_ref
342.0
_resolve_custom_types
42.0
set_types
42.0
CRAP 90.3
1 package blx::xsdsql::xsd_parser::schema;
2  
3 use strict; # use strict is for PBP
4 use Filter::Include;
5 include blx::xsdsql::include;
6 #line 7
7 use File::Basename;
8  
9 use base qw(blx::xsdsql::ios::debuglogger blx::xsdsql::ut::common_interfaces);
10 use blx::xsdsql::ut::ut qw(nvl);
11 use blx::xsdsql::xsd_parser::path_map;
12  
13  
14 my @ATTRIBUTE_KEYS:Constant( qw(
15 CATALOG_NAME
16 SCHEMA_CODE
17 LOCATION
18 URI
19 ELEMENT_FORM_DEFAULT
20 ATTRIBUTE_FORM_DEFAULT
21 ROOT_SCHEMA
22 ENCODING
23 )
24 );
25  
26  
27 our %_ATTRS_R:Constant(());
28  
29  
30 our %_ATTRS_W:Constant(
31 CHILDS_SCHEMA_LIST => sub { croak "use add_child_schema to add a child schema"; }
32 ,MAPPING_PATH => sub { croak "MAPPING_PATH: this attribute is read_only"; }
33 ,POST_POSTED_REF => sub { croak "the attribute POST_POSTED_REF is reserved" }
34 ,TYPES => sub { croak "use add_types to add types"; }
35 ,ATTRIBUTES => sub { croak "use add_attrs to add attributes"; }
36 ,NO_FLAT_GROUPS => sub { croak "NO_FLAT_GROUPS: this attribute is read_only"}
37 ,ELEMENT_FORM_DEFAULT => sub {
38 my ($self,$value)=@_;
39 return unless defined $value;
40 return 'Q' if $value eq 'qualified' || $value eq 'Q';
41 return 'U' if $value eq 'unqualified' || $value eq 'U';
42 croak "$value: invalid value for ELEMENT_FORM_DEFAULT";
43 }
44 ,ATTRIBUTE_FORM_DEFAULT => sub {
45 my ($self,$value)=@_;
46 return unless defined $value;
47 return 'Q' if $value eq 'qualified' || $value eq 'Q';
48 return 'U' if $value eq 'unqualified' || $value eq 'U';
49 croak "$value: invalid value for ATTRIBUTE_FORM_DEFAULT";
50 }
51
52 );
53  
54  
55 sub _get_attrs_w { return \%_ATTRS_W; }
56 sub _get_attrs_r { return \%_ATTRS_R; }
57  
58 sub _adj_element_ref {
59 my ($self,$c,%params)=@_;
60 for my $col($self->get_root_table->get_columns) {
61 if ($c->get_name eq $col->get_name) {
62 my $t=$col->get_attrs_value(qw(TYPE));
63 $c->set_attrs_value(
64 TYPE => $t
65 ,REF => 0
66 ,ELEMENT_FORM => 'Q' #force qualified because is a reference
67 );
68 if (defined (my $path_ref=$col->get_path_reference)) {
69 $c->set_attrs_value(
70 PATH_REFERENCE => $path_ref
71 );
72 }
73 return $c;
74 }
75 }
76 $self->_debug(undef,$c->get_name.": element ref is postposted");
77 push @{$self->{POST_POSTED_REF}},$c;
78 undef;
79 }
80  
81 sub _adj_attr_ref {
82 my ($self,$c,%params)=@_;
83 my $name=nvl($params{NAME},$c->get_attrs_value(qw(NAME)));
84 if (defined (my $ty=$self->get_global_attr($name,%params))) {
85 $c->set_attrs_value(
86 REF => 0
87 ,TYPE => $ty
88 ,ELEMENT_FORM => 'Q' #force qualified because is a reference
89 );
90 return $c;
91 }
92 $self->_debug(undef,$c->get_name.": attribute ref is postposted");
93 push @{$self->{POST_POSTED_REF}},$c;
94 undef;
95 }
96  
97 sub _adj_ref {
98 my ($self,$c,%params)=@_;
99 if (!$params{FORCE_ADJ} && (defined (my $uri=$c->get_URI))) {
100 my $name=$c->get_attrs_value(qw(NAME));
101 $self->_debug(__LINE__,$uri.':'.$name," the resolution of this external ref is post posted\n");
102 push @{$self->{POST_POSTED_REF}},$c;
103 return;
104 }
105 return $c->get_attrs_value(qw(ATTRIBUTE)) ? $self->_adj_attr_ref($c,%params) : $self->_adj_element_ref($c,%params);
106 }
107  
108  
109 sub _parse_group_ref { # flat the columns of groups table into $table
110 my ($self,$table,$type_node_names,%params)=@_;
111 if ($params{START_FLAG}) {
112 $params{PATH}={};
113 my $z=-1;
114 $params{MAX_XSD_SEQ}=\$z;
115 $params{START_TABLE}=$table;
116 }
117 my $max_xsd_seq=${$params{MAX_XSD_SEQ}};
118 my $pred_xsd_seq=!$params{START_FLAG} && $params{CHOICE} ? $max_xsd_seq : undef;
119 my $fl=0;
120 my @newcols=();
121 for my $c($table->get_columns) {
122 next if ($c->is_pk || $c->is_sys_attributes) && ! $params{START_FLAG}; #bypass the column if is the primary keys or sysattrs col and not a start table
123 my $p=$c->get_path;
124 my $nc=$params{START_FLAG} ? $c : $c->shallow_clone;
125  
126 if (defined ( my $xsd_seq=$nc->get_xsd_seq)) { # change xsd_seq
127 if (defined $pred_xsd_seq && $xsd_seq == $pred_xsd_seq) {
128 $xsd_seq=$max_xsd_seq;
129 }
130 else {
131 $pred_xsd_seq=$xsd_seq;
132 $xsd_seq=++$max_xsd_seq;
133 }
134 $nc->set_attrs_value(XSD_SEQ => $xsd_seq);
135 unless ($params{START_FLAG}) {
136 $nc->set_attrs_value(CHOICE => $params{CHOICE});
137 $nc->set_attrs_value(MINOCCURS => 0) if $params{CHOICE};
138 }
139 }
140  
141 if (!$params{START_FLAG} && defined (my $cpath=$nc->get_path)) { #change the path of the column
142 my $path=$params{START_TABLE}->is_unpath ? $params{START_TABLE}->get_parent_path : $params{START_TABLE}->get_path;
143 $path.='/'.basename($cpath) unless $nc->is_group_reference;
144 $self->_debug(__LINE__,' change path of column ',$nc->get_full_name," from '$cpath' to '$path'");
145 $nc->set_attrs_value(PATH => $path);
146 $p=$path;
147 }
148  
149  
150 if (defined $p && !$nc->is_group_reference) { #register new path
151 if (defined (my $col=$params{PATH}->{$p})) {
152 $self->_debug(__LINE__,$p,': path already register for column ',$nc->get_full_name,' - pred column is ',$col->get_full_name);
153 unless ($params{START_FLAGS}) { # a column into a group has priority to a column with same path
154 $col->set_attrs_value(DELETE => 1); # the pred column is marked for deletion
155 }
156 else {
157 $self->_debug(__LINE__,$p,' the column ',$nc->get_full_name, ' is bypassed');
158 next;
159 }
160 }
161 $params{PATH}->{$p}=$nc;
162 }
163  
164  
165 if ($nc->is_group_reference && $nc->get_max_occurs <= 1) { #flat the columns of ref table into $table
166 ++$fl;
167 my $ty=$nc->get_attrs_value(qw(TYPE))->get_attrs_value(qw(NAME));
168 my $ref=$type_node_names->{$ty}->get_attrs_value(qw(TABLE));
169 affirm { defined $ref } "no such table ref for column ".$c->get_full_name."(type '$ty')";
170 $self->_debug(__LINE__,$nc->get_full_name,": the columun ref table group '",$ref->get_sql_name,"' with maxoccurs <=1 - flating the columns of table !!");
171 ${$params{MAX_XSD_SEQ}}=$max_xsd_seq;
172 my @cols=$self->_parse_group_ref($ref,$type_node_names,%params,START_FLAG => 0,CHOICE => $nc->is_choice);
173 $max_xsd_seq=${$params{MAX_XSD_SEQ}};
174 push @newcols,@cols;
175 }
176 else {
177 push @newcols,$nc
178 }
179 } #for
180 ${$params{MAX_XSD_SEQ}}=$max_xsd_seq;
181 return @newcols unless $params{START_FLAG};
182 return unless $fl; # no group ref column
183 $table->reset_columns;
184 $table->add_columns(grep(!$_->get_attrs_value(qw(DELETE)),@newcols));
185 undef;
186 }
187  
188  
189 sub _resolve_custom_types {
190 my ($self,$tables,$types,%params)=@_;
191 $self->_debug(__LINE__,'start resolve custom types');
192 for my $t(@$tables) {
193 affirm { ref($t) =~/::table$/ } ref($t).": not a table class";
194 my $child_tables=$t->get_child_tables;
195 $self->_resolve_custom_types($child_tables,$types,%params);
196 $self->_parse_group_ref($t,$types,%params,START_FLAG => 1) unless $self->get_attrs_value(qw(NO_FLAT_GROUPS));
197 for my $c($t->get_columns) {
198 next if $c->is_pk || $c->is_sys_attributes;
199 $self->_adj_ref($c,%params) if $c->get_attrs_value(qw(REF));
200 if (defined (my $ctype=$c->get_attrs_value(qw(TYPE)))) {
201 if (defined (my $new_ctype=$ctype->resolve_type($types))) {
202 $self->_debug(__LINE__,'col ',$c->get_full_name,' with type of type ',ref($new_ctype));
203 $new_ctype->link_to_column($c,%params,TABLE => $t,SCHEMA => $self,DEBUG => $self->get_attrs_value(qw(DEBUG)));
204 }
205 else {
206 $self->_debug(__LINE__," the resolution of type '",$ctype->get_attrs_value(qw(FULLNAME)),"' for column '",$c->get_full_name,"' is post posted");
207 }
208 }
209 else {
210 $self->_debug(__LINE__,$c->get_full_name.": column without type");
211 }
212 }
213 }
214 return $self;
215 }
216  
217  
218 sub _find_schemas_from_namespace {
219 my ($self,$namespace,%params)=@_;
220 my @schemas=();
221 for my $h($self->get_childs_schema) {
222 my ($ns,$child_schema)=map { $h->{$_} } qw(NAMESPACE SCHEMA);
223 push @schemas,$child_schema if nvl($ns) eq nvl($namespace);
224 push @schemas,$child_schema->_find_schemas_from_namespace($namespace,%params,);
225 }
226 return wantarray ? @schemas : \@schemas;
227 }
228  
229  
230 sub get_root_table {
231 my ($self,%params)=@_;
232 return $self->get_attrs_value(qw(TABLE));
233 }
234  
235  
236 sub get_types_name {
237 my ($self,%params)=@_;
238 my $types=$self->{TYPE_NAMES};
239 return unless defined $types;
240 return wantarray ? %$types : $types;
241 }
242  
243  
244 sub get_dictionary_data {
245 my ($self,$dictionary_type,%params)=@_;
246 affirm { defined $dictionary_type } "1^ param not set";
247 affirm { $dictionary_type eq 'SCHEMA_DICTIONARY' } "1^ param must be set to 'SCHEMA_DICTIONARY' value";
248 my %data=map { ($_ eq 'URI' ? $_ : lc($_),$self->get_attrs_value($_)); } @ATTRIBUTE_KEYS;
249 for my $k(qw(CATALOG_NAME SCHEMA_CODE)) {
250 affirm { defined $params{$k} && length($params{$k})} "$k: param not set";
251 my $k1=lc($k);
252 affirm { exists $data{$k1} } "$k: key not found in data";
253 $data{$k1}=$params{$k};
254 }
255 $data{location}=$params{LOCATION};
256 return wantarray ? %data : \%data;
257 }
258  
259 sub factory_from_dictionary_data {
260 my ($data,%params)=@_;
261 affirm { ref($data) eq 'ARRAY' } "the 1^ param must be array";
262 affirm { scalar(@$data) == scalar(@ATTRIBUTE_KEYS) } "the attributes number is not equal to keys number";
263 my %data=map { ($ATTRIBUTE_KEYS[$_],$data->[$_]) } (0..scalar(@$data) - 1);
264 return __PACKAGE__->new(%params,%data);
265 }
266  
267  
268 sub get_childs_schema {
269 my ($self,%params)=@_;
270 my $a=$self->{CHILDS_SCHEMA_LIST};
271 return wantarray ? @$a : $a;
272 }
273  
274 sub get_global_attr {
275 my ($self,$name,%params)=@_;
276 return $self->{ATTRIBUTES}->{$name};
277 }
278  
279 sub add_attrs {
280 my $self=shift;
281 for my $col(@_) {
282 my ($name,$type)=map { $col->get_attrs_value($_); }(qw(NAME TYPE));
283 next if defined $self->{ATTRIBUTES}->{$name};
284 $self->{ATTRIBUTES}->{$name}=$type;
285 }
286 $self;
287 }
288  
289 sub add_attributes_group {
290 my $self=shift;
291 for my $table(@_) {
292 my $name=$table->get_name;
293 affirm { $table->get_attrs_value(qw(ATTRIBUTE_GROUP)) } "$name: this table is not an ATTRIBUTE_GROUP table";
294 $self->{ATTRIBUTES_GROUP}->{$name}=$table;
295 }
296 $self;
297 }
298  
299 sub resolve_attributes {
300 my ($self,$table,$nsprefixes,@attrnames)=@_;
301 $self->{MAPPING_PATH}->resolve_attributes($table,$nsprefixes,@attrnames);
302 }
303  
304 sub find_schemas_from_namespace {
305 my ($self,$namespace,%params)=@_;
306 my @schemas=();
307 push @schemas,$self if nvl($self->get_attrs_value(qw(URI))) eq nvl($namespace);
308 push @schemas,$self->_find_schemas_from_namespace($namespace,%params);
309 $self->_debug(undef,nvl($namespace,'<global_namespace>').": not find schemas from this namespace") unless scalar(@schemas);
310 return wantarray ? @schemas : \@schemas;
311 }
312  
313 sub add_child_schema {
314 my ($self,$child_schema,$ns,$location)=@_;
315 affirm { defined $child_schema } "1^ param not set";
316 affirm { defined $location } "3^ param not set";
317 unless (defined $child_schema->get_attrs_value(qw(SCHEMA_CODE))) {
318 my $schema_code=$self->get_attrs_value(qw(SCHEMA_CODE));
319 affirm { defined $schema_code } "attribute SCHEMA_CODE not set";
320 $schema_code .='_'.scalar(@{$self->{CHILDS_SCHEMA_LIST}});
321 $child_schema->set_attrs_value(SCHEMA_CODE => $schema_code);
322 }
323 $child_schema->set_attrs_value(CHILD_SCHEMA => 1);
324 $self->_debug(__LINE__,'add child_schema from location ',$location,' and namespace ',$ns);
325 push @{$self->{CHILDS_SCHEMA_LIST}},{ SCHEMA => $child_schema,NAMESPACE => $ns,LOCATION => $location };
326 return $self;
327 }
328  
329 sub add_types {
330 my $self=shift;
331 push @{$self->{TYPES}},@_;
332 return $self;
333 }
334  
335  
336  
337 sub mapping_paths {
338 my ($self,$type_paths,%params)=@_;
339 affirm { ref($type_paths) eq 'HASH' } "1^ param must be a hash";
340 my $root=$self->get_root_table;
341 affirm { ref($root)=~/::table$/ } ref($root).": is not a table class";
342 $self->{MAPPING_PATH}->mapping_paths($root,$type_paths,%params);
343 return $self;
344 }
345  
346  
347 sub set_types {
348 my ($self,%params)=@_;
349 my $types=$self->get_attrs_value(qw(TYPES));
350 my @type_tables=map { my $t=$_->get_attrs_value(qw(TABLE)); defined $t ? $t : (); } @$types;
351  
352 my %type_node_names=map {
353 affirm { defined $_ } "element not set";
354 my $name=$_->get_attrs_value(qw(name));
355 $name=$_->get_attrs_value(qw(NAME)) unless defined $name;
356 affirm { defined $name } "name not set ".ref($_);
357 ($name,$_);
358 } @$types;
359
360 $self->_resolve_custom_types(\@type_tables,\%type_node_names,%params);
361 $self->_resolve_custom_types([$self->get_root_table],\%type_node_names,%params);
362 $self->_resolve_custom_types([values(%{$self->{ATTRIBUTES_GROUP}})],\%type_node_names,%params);
363  
364 my %type_table_paths=map { my $path=$_->get_attrs_value(qw(PATH)); defined $path ? ($path,$_) : (); } @type_tables;
365 $self->{TYPE_NAMES}={ map { my $name=$_->get_attrs_value(qw(NAME)); defined $name ? ($name,$_) : (); } @type_tables };
366 $self->{TYPE_PATHS}={ map { my $path=$_->get_attrs_value(qw(PATH)); defined $path ? ($path,$_) : (); } @type_tables };
367 $self->{TYPE_NODE_NAMES}=\%type_node_names;
368 return $self;
369 }
370  
371 sub get_types_path {
372 my ($self,%params)=@_;
373 my $types=$self->{TYPE_PATHS};
374 return unless defined $types;
375 return wantarray ? %$types : $types;
376 }
377  
378  
379  
380 sub resolve_path {
381 my ($self,$path,%params)=@_;
382 $self->{MAPPING_PATH}->resolve_path($path,%params);
383 }
384  
385  
386 sub resolve_column_link {
387 my ($self,$t1,$t2,%params)=@_;
388 $self->{MAPPING_PATH}->resolve_column_link($t1,$t2,%params);
389 }
390  
391 sub new {
392 my ($class,%params)=@_;
393 my $self=bless {
394 CHILDS_SCHEMA_LIST => []
395 ,MAPPING_PATH => blx::xsdsql::xsd_parser::path_map->new(DEBUG => $params{DEBUG})
396 ,POST_POSTED_REF => []
397 ,TYPES => []
398 ,ATTRIBUTES => {}
399 ,NO_FLAT_GROUPS => delete $params{NO_FLAT_GROUPS}
400 },$class;
401 $self->set_attrs_value(%params);
402 }
403  
404  
405  
406 1;
407  
408 __END__