blib/lib/blx/xsdsql/xsd_parser.pm

filter: Statementstmt 94.8% 37 / 39 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 13 / 13 Podpod n/a totaltotal 96.1% 50 / 52 CC 1 SCAR 0.2
CC
1
Cov
95%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« blib/lib/blx/xsdsql/schema_repository.pm ↑ Summary t/test.pl »
1 package blx::xsdsql::xsd_parser;
2  
3 1 ▶use strict; # use strict is for PBP
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Filter::Include;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 include blx::xsdsql::include;
6 #line 7
7  
8 1 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 0 ▶use XML::Parser;
Subroutine BEGIN: called CC=1 SCAR=2.6
10  
11 1 ▶use blx::xsdsql::xsd_parser::node;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use blx::xsdsql::ut::ut qw(nvl ev);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use blx::xsdsql::xsd_parser::schema;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use blx::xsdsql::schema_repository::extra_tables;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶ 
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use base qw(blx::xsdsql::ut::common_interfaces blx::xsdsql::ios::debuglogger Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶ 
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶ 
Subroutine BEGIN: called CC=1 SCAR=0.0
19 use constant {
20 1 ▶ USER_SCHEMA_CLASS => 'blx::xsdsql::xsd_parser::schema'
Subroutine BEGIN: called CC=1 SCAR=0.0
21 };
22  
23 my @ATTRIBUTE_KEYS:Constant(qw(
24 OUTPUT_NAMESPACE
25 DB_NAMESPACE
26 DEBUG
27 EXTRA_TABLES
28 TABLE_CLASS
29 COLUMN_CLASS
30 )
31 );
32  
33 my @ATTRIBUTE_KEYS_RESERVED:Constant(qw(
34 STACK
35 SCHEMA_OBJECT
36 DICTIONARIES
37 SCHEMA_OBJECT
38 )
39 );
40  
41 my %t=( overload => [ qw ( USER_SCHEMA_CLASS )]);
42  
43 our %EXPORT_TAGS=( all => [ map { @{$t{$_}} } keys %t ],%t);
44 our @EXPORT_OK=( @{$EXPORT_TAGS{all}} );
45 our @EXPORT=qw( );
46  
47 our %_ATTRS_R:Constant(
48 map { my $a=$_;($a,sub { croak $a.": this attribute is reserved"}) } @ATTRIBUTE_KEYS_RESERVED
49 );
50  
51 our %_ATTRS_W:Constant(
52 (
53 map { my $a=$_;($a,sub { croak $a.": this attribute is not writeable"}) } @ATTRIBUTE_KEYS
54 ,map { my $a=$_;($a,sub { croak $a.": this attribute is reserved"}) } @ATTRIBUTE_KEYS_RESERVED
55 )
56 );
57  
58  
59 sub _get_attrs_w { return \%_ATTRS_W; }
60 sub _get_attrs_r { return \%_ATTRS_R; }
61  
62  
63 sub _push {
64 my ($self,$v,%params)=@_;
65 push @{$self->{STACK}},$v;
66 return $v;
67 }
68  
69 sub _pop {
70 my ($self,%params)=@_;
71 affirm { scalar(@{$self->{STACK}}) > 0 } "empty stack";
72 pop @{$self->{STACK}};
73 return scalar(@{$self->{STACK}}) == 0 ? undef : $self->{STACK}->[-1];
74 }
75  
76 sub _get_stack {
77 my ($self,%params)=@_;
78 affirm { scalar(@{$self->{STACK}}) > 0 } "empty stack";
79 my $s=$self->{STACK}->[-1];
80 return $s;
81 }
82  
83  
84 sub _to_obj {
85 my ($self,$tag,%params)=@_;
86 return blx::xsdsql::xsd_parser::node::factory_object($tag,%params);
87 }
88  
89 sub _decode {
90 my $self=shift;
91 return $_[0] if scalar(@_) <= 1;
92 return @_;
93 }
94  
95 my %H=(
96 Start => sub {
97 my ($expect,$node,%attrs)=@_;
98 my $self=$expect->{LOAD_INSTANCE};
99 my @params=(%{$expect->{PARAMS}},ATTRIBUTES => \%attrs);
100 push @params,map { ($_,$self->{$_}) } grep(ref($self->{$_}) eq '',keys %$self);
101 my $stack=$self->get_attrs_value(qw(STACK));
102 my $obj=$self->_to_obj($node,@params,STACK => $stack,EXTRA_TABLES => $self->{EXTRA_TABLES});
103 $self->_debug(__LINE__,'> (start path)',$obj->get_attrs_value(qw(PATH))," with type ",ref($obj));
104 $obj->trigger_at_start_node(%{$expect->{PARAMS}},PARSER => $self);
105 if (ref($obj) =~/::schema$/) {
106 $stack->[1]=$obj;
107 }
108 else {
109 $self->_push($obj);
110 }
111 undef;
112 }
113 ,End => sub {
114 my ($expect,$node,%attrs)=@_;
115 my $self=$expect->{LOAD_INSTANCE};
116 my $obj=$self->_get_stack;
117 $self->_debug(__LINE__,'< (end path)',$obj->get_attrs_value(qw(PATH))," with type ",ref($obj));
118 $obj->trigger_at_end_node;
119 if (ref($obj) =~ /::schema$/) {
120 $obj->set_attrs_value(
121 XMLDECL => $self->get_attrs_value(qw(STACK))->[0]
122 );
123 $self->{SCHEMA_OBJECT}=$obj;
124 }
125 else {
126 if (ref($obj)=~/Type$/ && (defined (my $name=$obj->get_attrs_value(qw(name))))) {
127 $self->_debug(__LINE__,"type '$name' add to know types");
128 $self->get_attrs_value(qw(STACK))->[1]->add_types($obj);
129 }
130 $self->_pop;
131 }
132 undef;
133 }
134 ,XMLDecl => sub {
135 my ($expect,@decl)=@_;
136 my $self=$expect->{LOAD_INSTANCE};
137 $self->_push(\@decl);
138 }
139 # ,Char => sub { x("Char",@_); }
140 # ,Proc => sub { x_("Proc",@_); }
141 # ,Comment => sub { x("Comment",@_); }
142 # ,CdataStart => sub { x_("CdataStart",@_); }
143 # ,CdataEnd => sub { x_("CdataEnd",@_); }
144 # ,Default => sub { x("Default",@_); }
145 # ,Unparsed => sub { x_("Unparsed",@_); }
146 # ,Notation => sub { x_("Notation",@_); }
147 # ,ExternEnt => sub { x_("ExternEnt",@_); }
148 # ,ExternEntFin => sub { x_("ExternEntFin",@_); }
149 # ,Entity => sub { x_("Entity",@_); }
150 # ,Element => sub { x_("Element",@_); }
151 # ,Attlist => sub { x_("Attlist",@_); }
152 # ,Doctype => sub { x_("Doctype",@_); }
153 # ,DoctypeFin => sub { x_("DoctypeFin",@_); }
154 );
155  
156 sub _resolve_postposted_types {
157 my ($self,$tables,$types,%params)=@_;
158 $self->_debug(__LINE__,'start resolve postposted types');
159 for my $t(@$tables) {
160 my $child_tables=$t->get_child_tables;
161 $self->_resolve_postposted_types($child_tables,$types,%params);
162 for my $c($t->get_columns) {
163 next if $c->is_pk || $c->is_sys_attributes;
164 my $ctype=$c->get_attrs_value(qw(TYPE));
165 affirm { defined $ctype } $c->get_full_name.": column without type";
166 next if defined $ctype->resolve_type($types);
167 my $type_fullname=$ctype->get_attrs_value(qw(FULLNAME));
168 $self->_debug(__LINE__,'column ',$c->get_full_name,' with type ',$type_fullname);
169 if (defined (my $new_ctype=$ctype->resolve_external_type($params{SCHEMA}))) {
170 $new_ctype->link_to_column($c,%params,TABLE => $t,DEBUG => $self->get_attrs_value(qw(DEBUG)));
171 }
172 else {
173 my $new_ctype=$self->_resolve_recursive_external_type($ctype,$params{ROOT_SCHEMA});
174 affirm { defined $new_ctype } "$type_fullname: failed the external resolution";
175 $new_ctype->link_to_column($c,%params,TABLE => $t,DEBUG => $self->get_attrs_value(qw(DEBUG)));
176 }
177 }
178 }
179 return $self;
180 }
181  
182 sub _recursive_resolution {
183 my ($self,$schema,%params)=@_;
184 for my $h($schema->get_childs_schema) {
185 $self->_recursive_resolution($h->{SCHEMA},%params);
186 }
187 $self->_resolve_postposted_ref($schema,%params,NO_ATTRIBUTE_GROUP => 1);
188 my $types_name=$schema->get_types_name;
189 my $types=[values(%$types_name)];
190 $self->_resolve_postposted_ref($schema,%params,NO_ATTRIBUTE_GROUP => 0);
191 $self->_resolve_postposted_types($types,$types_name,%params,SCHEMA => $schema);
192 $self->_resolve_postposted_types([$schema->get_root_table],$types_name,%params,SCHEMA => $schema);
193 return $self;
194 }
195  
196 sub _resolve_postposted_ref {
197 my ($self,$schema,%params)=@_;
198 my $list=$schema->get_attrs_value(qw(POST_POSTED_REF));
199 return $self unless @$list;
200 for my $c(@$list) {
201 if ($c->get_attrs_value(qw(ATTRIBUTE_GROUP))) {
202 next if $params{NO_ATTRIBUTE_GROUP};
203 my $table=$schema->get_attrs_value(qw(ATTRIBUTES_GROUP))->{$c->get_name};
204 unless (defined $table) {
205 my ($uri,$name)=$c->get_attrs_value(qw(URI NAME));
206 $uri=$schema->get_attrs_value(qw(URI)) unless defined $uri;
207 $table=$self->_resolve_recursive_external_ref($c,$params{ROOT_SCHEMA},$uri,%params);
208 affirm { defined $table } "($uri,$name): failed ref resolution";
209 }
210 my $parent_table=$c->get_attrs_value(qw(TABLE_NAME));
211 my @columns=$parent_table->reset_columns;
212 my @new_cols=();
213 my $fl=0;
214 for my $col(@columns) {
215 if ($col->get_name eq $c->get_name) {
216 push @new_cols,map {
217 my $c=$_->clone;
218 $c->{TYPE}=$_->{TYPE};
219 affirm { defined $c->get_attrs_value(qw(TYPE)) }
220 $c->get_full_name.': not TYPE attribute set';
221 $c;
222 } grep { ! $_->get_attrs_value(qw(SYS_ATTRIBUTES)) } $table->get_columns;
223 $fl=1;
224 }
225 else {
226 push @new_cols,$col;
227 }
228 }
229 $self->_debug(undef,$c->get_name,': not column added') unless defined $fl;
230 # affirm { $fl } "not columns added";
231 $parent_table->add_columns(@new_cols);
232 }
233 else {
234 my ($uri,$name)=$c->get_attrs_value(qw(URI NAME));
235 $uri=$schema->get_attrs_value(qw(URI)) unless defined $uri;
236 my $new_c=$self->_resolve_recursive_external_ref($c,$params{ROOT_SCHEMA},$uri,%params);
237 affirm { defined $new_c } "($uri,$name): failed ref resolution";
238 }
239 }
240 return $self;
241 }
242  
243  
244 sub _resolve_recursive_external_type {
245 my ($self,$ctype,$schema,%params)=@_;
246 if ($schema->get_attrs_value(qw(URI)) eq $ctype->get_attrs_value(qw(URI))) {
247 my $types=$schema->get_attrs_value(qw(TYPES));
248 my %type_node_names=map { ($_->get_attrs_value(qw(name)),$_); } @$types;
249 my $name=$ctype->get_attrs_value(qw(NAME));
250 if (defined (my $t=$type_node_names{$name})) {
251 $self->_debug(__LINE__,'factory type from object type ',ref($t));
252 my $new_ctype=$t->factory_type($t,\%type_node_names,%params);
253 return $new_ctype if defined $new_ctype;
254 }
255 }
256 for my $h($schema->get_childs_schema) {
257 my $new_ctype=$self->_resolve_recursive_external_type($ctype,$h->{SCHEMA},%params);
258 return $new_ctype if defined $new_ctype;
259 }
260 $self->_debug(__LINE__,$ctype->get_attrs_value(qw(FULLNAME)).': failed the external resolution');
261 undef;
262 }
263  
264 sub _resolve_recursive_external_ref {
265 my ($self,$ref,$schema,$ns,%params)=@_;
266 if (nvl($schema->get_attrs_value(qw(URI))) eq nvl($ns)) {
267 if ($ref->get_attrs_value(qw(ATTRIBUTE))) {
268 my $name=$ref->get_attrs_value(qw(NAME));
269 my $ty=$schema->get_global_attr($name,%params);
270 if (defined $ty) {
271 $ref->set_attrs_value(
272 REF => 0
273 ,TYPE => $ty
274 ,ELEMENT_FORM => 'Q' #must be qualified because ref to external
275 );
276 return $ref;
277 }
278 }
279 elsif ($ref->get_attrs_value(qw(ATTRIBUTE_GROUP))) {
280 affirm { 0 } "not implemented";
281 }
282 else { #is an element ref
283 for my $col($schema->get_root_table->get_columns) {
284 if ($ref->get_name eq $col->get_name) {
285 $ref->set_attrs_value(
286 TYPE => $col->get_attrs_value(qw(TYPE))
287 ,REF => 0
288 ,ELEMENT_FORM => 'Q' #must be qualified because ref to external
289 );
290 if (defined (my $path_ref=$col->get_path_reference)) {
291 $ref->set_attrs_value(
292 PATH_REFERENCE => $path_ref
293 );
294 }
295 return $ref;
296 }
297 }
298 }
299 }
300 for my $h($schema->get_childs_schema) {
301 my $new_ref=$self->_resolve_recursive_external_ref($ref,$h->{SCHEMA},$ns,%params);
302 return $new_ref if defined $new_ref;
303 }
304 $self->_debug(__LINE__,$ref.': failed the external resolution');
305 undef;
306 }
307  
308 sub _recursive_mapping_path {
309 my ($self,$schema,%params)=@_;
310 for my $h($schema->get_childs_schema) {
311 $self->_recursive_mapping_path($h->{SCHEMA},%params);
312 }
313 my $type_table_paths=$schema->get_types_path;
314 $schema->mapping_paths($type_table_paths,%params);
315 return $self;
316 }
317  
318 sub _recursive_change_schema_class {
319 my ($self,$schema,%params)=@_;
320 for my $h($schema->get_childs_schema) {
321 $self->_recursive_change_schema_class($h->{SCHEMA},%params);
322 }
323 $schema->set_attrs_value(%{$params{DICTIONARIES}});
324 bless $schema,USER_SCHEMA_CLASS;
325 return $self;
326 }
327  
328  
329 sub _parse {
330 my ($self,%params)=@_;
331 $params{PARSER}->setHandlers(%H);
332 $self->{STACK}=[];
333 $params{PARSER}->parse($params{FD},LOAD_INSTANCE => $self,PARAMS => \%params);
334 delete $self->{STACK};
335 return delete $self->{SCHEMA_OBJECT};
336 }
337  
338 sub _parsefile {
339 my ($self,$file_name,%params)=@_;
340 affirm { defined $file_name } "1^ param not set";
341 my $p=$self->_fusion_params(%params);
342 for my $k(qw(TABLE_CLASS COLUMN_CLASS)) {
343 $p->{$k}=$self->{$k};
344 }
345 for my $k(qw(TABLE_PREFIX VIEW_PREFIX)) {
346 $p->{$k}='' unless defined $p->{$k};
347 }
348
349 $p->{TABLENAME_LIST}={} unless ref($p->{TABLENAME_LIST}) eq 'HASH';
350 $p->{CONSTRAINT_LIST}={} unless ref($p->{CONSTRAINT_LIST}) eq 'HASH';
351  
352 my $fd=sub {
353 if (defined $file_name && $file_name ne '-') {
354 open(my $fd,"<",$file_name) or croak "$file_name: open error $!\n";
355 return $fd;
356 }
357 else {
358 return *STDIN;
359 }
360 }->();
361
362  
363 $p->{PARSER}=XML::Parser->new;
364 my $schema=$self->_parse(%$p,FD => $fd);
365 close $fd if defined $file_name && $file_name ne '-';
366 delete $p->{PARSER};
367  
368 unless ($p->{CHILD_SCHEMA_}) {
369 $self->_recursive_resolution($schema,%$p,ROOT_SCHEMA => $schema);
370 $self->_recursive_mapping_path($schema,%$p);
371 }
372 else {
373 $self->_debug(__LINE__,nvl($schema->get_attrs_value(qw(URI))).': the resolution of external names is postposted because is a child schema');
374 }
375 return $schema;
376 }
377  
378 sub _search_schema_file {
379 my ($self,$file_name,%params)=@_;
380 affirm { defined $file_name } "1^ param not set";
381 return $file_name if File::Spec->file_name_is_absolute($file_name);
382 my $schema_path=$params{SCHEMA_PATH};
383 affirm { defined $schema_path } "param SCHEMA_PATH not set";
384 my @dirs=split(':',$schema_path);
385 for my $dir(@dirs) {
386 next unless length($dir);
387 my $f=File::Spec->catfile($dir,$file_name);
388 return $f if -e $f && ! -d $f;
389 }
390 undef;
391 }
392  
393 sub parsefile {
394 my ($self,$file_name,%params)=@_;
395 $file_name='-' if !$params{CHILDS_SCHEMA_} && !defined $file_name;
396 affirm { defined $file_name } "1^ param not set";
397 my $schema_path=$params{SCHEMA_PATH};
398 unless (defined $schema_path) {
399 affirm { !$params{CHILD_SCHEMA_} } " param SCHEMA_PATH not set";
400 $schema_path=$ENV{SCHEMA_PATH};
401 $schema_path=dirname($file_name) unless defined $schema_path;
402 $params{SCHEMA_PATH}=$schema_path;
403 }
404 my $f=$params{CHILD_SCHEMA_} ? $self->_search_schema_file($file_name,%params) : $file_name;
405 croak "$file_name: not found in SCHEMA_PATH\n" unless defined $f;
406 my $schema=$self->_parsefile($f,%params);
407 unless ($params{CHILD_SCHEMA_}) {
408 my %p=map { ($_,$schema->get_attrs_value($_)); } $self->{EXTRA_TABLES}->get_extra_table_types;
409 $self->_recursive_change_schema_class($schema,%params,DICTIONARIES => \%p);
410 $schema->set_attrs_value(
411 OUTPUT_NAMESPACE => $self->get_attrs_value(qw(OUTPUT_NAMESPACE))
412 ,DB_NAMESPACE => $self->get_attrs_value(qw(DB_NAMESPACE))
413 );
414 }
415 return $schema;
416 }
417  
418  
419 sub new {
420 my ($class,%params)=@_;
421 $params{OUTPUT_NAMESPACE}='sql' unless defined $params{OUTPUT_NAMESPACE};
422 affirm { defined $params{DB_NAMESPACE} } 'param DB_NAMESPACE not set';
423 affirm { !defined $params{EXTRA_TABLES} } 'the param EXTRA_TABLES is reserved';
424 $params{EXTRA_TABLES}=blx::xsdsql::schema_repository::extra_tables::factory_instance(
425 map { ($_,$params{$_} ) } (qw(OUTPUT_NAMESPACE DB_NAMESPACE DEBUG))
426 );
427  
428 for my $cl(qw(catalog table column )) {
429 my $k=uc($cl).'_CLASS';
430 $params{$k}=$params{EXTRA_TABLES}->get_attrs_value($k);
431 }
432 return bless \%params,$class;
433 }
434  
435  
436 1;
437  
438  
439  
440 __END__
« blib/lib/blx/xsdsql/schema_repository.pm ↑ Summary t/test.pl »