« blib/lib/blx/xsdsql/schema_repository/loader.pm
↑ Summary
blib/lib/blx/xsdsql/schema_repository/xml.pm »
| 1 | package blx::xsdsql::xsd_parser::node; | ||
| 2 | |||
| 3 | use strict; # use strict is for PBP | ||
| 4 | use Filter::Include; | ||
| 5 | include blx::xsdsql::include; | ||
| 6 | #line 7 | ||
| 7 | |||
| 8 | use blx::xsdsql::ut::ut qw(ev nvl); | ||
| 9 | use blx::xsdsql::ut::posix; | ||
| 10 | |||
| 11 | use base qw(blx::xsdsql::ios::debuglogger blx::xsdsql::ut::common_interfaces); | ||
| 12 | |||
| 13 | our %_ATTRS_W:Constant(()); | ||
| 14 | our %_ATTRS_R:Constant(()); | ||
| 15 | |||
| 16 | sub _get_attrs_w { return \%_ATTRS_W; } | ||
| 17 | sub _get_attrs_r { return \%_ATTRS_R; } | ||
| 18 | |||
| 19 | |||
| 20 | use constant { | ||
| 21 | UNBOUNDED => blx::xsdsql::ut::posix::get_int_max | ||
| 22 | }; | ||
| 23 | |||
| 24 | |||
| 25 | sub _resolve_maxOccurs { | ||
| 26 | my ($self,%params)=@_; | ||
| 27 | my $n=exists $params{VALUE} ? $params{VALUE} : $self->get_attrs_value(qw(maxOccurs)); | ||
| 28 | $n=nvl($n,1); | ||
| 29 | $n=UNBOUNDED if $n eq 'unbounded'; | ||
| 30 | return $n; | ||
| 31 | } | ||
| 32 | |||
| 33 | sub _resolve_minOccurs { | ||
| 34 | my ($self,%params)=@_; | ||
| 35 | return 0 if $params{CHOICE}; | ||
| 36 | my $n=exists $params{VALUE} ? $params{VALUE} : $self->get_attrs_value(qw(minOccurs)); | ||
| 37 | return nvl($n,1); | ||
| 38 | } | ||
| 39 | |||
| 40 | sub _resolve_form { | ||
| 41 | my ($self,%params)=@_; | ||
| 42 | my $form=$self->get_attrs_value(qw(form)); | ||
| 43 | return $form unless defined $form; | ||
| 44 | $form='Q' if $form eq 'qualified'; | ||
| 45 | $form='U' if $form eq 'unqualified'; | ||
| 46 | $form; | ||
| 47 | } | ||
| 48 | |||
| 49 | sub _split_tag_name { # split tag into namespace/name | ||
| 50 | my ($name,%params)=@_; | ||
| 51 | my @a=$name=~/^([^:]+):([^:]+)$/; | ||
| 52 | @a=('',$name) unless scalar(@a); # name without namespace prefix | ||
| 53 | return { | ||
| 54 | FULLNAME => $name | ||
| 55 | ,NAMESPACE => $a[0] #namespace abbr | ||
| 56 | ,NAME => $a[1] | ||
| 57 | }; | ||
| 58 | } | ||
| 59 | |||
| 60 | |||
| 61 | sub _resolve_boolean { | ||
| 62 | my ($self,$value,%params)=@_; | ||
| 63 | return 0 unless defined $value; | ||
| 64 | return $value eq 'true' || $value eq '1' ? 1 : 0; | ||
| 65 | } | ||
| 66 | |||
| 67 | sub _is_into_a_mixed { | ||
| 68 | my ($self,%params)=@_; | ||
| 69 | my $stack=$self->get_attrs_value(qw(STACK)); | ||
| 70 | for my $s(reverse @$stack) { | ||
| 71 | if (ref($s)=~/::complexType$/) { | ||
| 72 | return $s->get_attrs_value(qw(MIXED)); | ||
| 73 | } | ||
| 74 | } | ||
| 75 | return 0; | ||
| 76 | } | ||
| 77 | |||
| 78 | sub _new { | ||
| 79 | my ($class,%params)=@_; | ||
| 80 | return bless \%params,$class; | ||
| 81 | } | ||
| 82 | |||
| 83 | sub _construct_path { | ||
| 84 | my ($self,$name,%params)=@_; | ||
| 85 | my $parent=nvl($params{PARENT},$self->get_attrs_value(qw(STACK))->[-1]); | ||
| 86 | my $path=$parent->get_attrs_value(qw(PATH)); | ||
| 87 | return $path unless defined $path; | ||
| 88 | if (defined $name) { | ||
| 89 | $path.='/' unless $path eq '/'; | ||
| 90 | $path.=$name; | ||
| 91 | } | ||
| 92 | return $path; | ||
| 93 | } | ||
| 94 | |||
| 95 | sub _get_parent_table { | ||
| 96 | my ($self,%params)=@_; | ||
| 97 | my $i=-1; | ||
| 98 | while(1) { | ||
| 99 | my $parent=$self->get_attrs_value(qw(STACK))->[$i]; | ||
| 100 | last unless defined $parent; | ||
| 101 | if (defined (my $parent_table=$parent->get_attrs_value(qw(TABLE)))) { | ||
| 102 | return $parent_table; | ||
| 103 | } | ||
| 104 | $i--; | ||
| 105 | } | ||
| 106 | affirm { 0 } "no such parent table"; | ||
| 107 | undef; | ||
| 108 | } | ||
| 109 | |||
| 110 | sub _get_parent_path { | ||
| 111 | my ($self,%params)=@_; | ||
| 112 | my $i=-1; | ||
| 113 | my $stack=$self->get_attrs_value(qw(STACK)); | ||
| 114 | while(1) { | ||
| 115 | my $parent=$stack->[$i]; | ||
| 116 | last unless defined $parent; | ||
| 117 | if (defined(my $path=$parent->get_attrs_value(qw(PATH)))) { | ||
| 118 | return $path; | ||
| 119 | } | ||
| 120 | $i--; | ||
| 121 | } | ||
| 122 | affirm { 0 } "no such parent path"; | ||
| 123 | undef; | ||
| 124 | } | ||
| 125 | |||
| 126 | |||
| 127 | sub _resolve_simple_type { | ||
| 128 | my ($self,$t,$types,$out,%params)=@_; | ||
| 129 | if (ref($t)=~/::union/) { | ||
| 130 | $out->{base}='string'; | ||
| 131 | return $self; | ||
| 132 | } | ||
| 133 | if (defined (my $base=$t->get_attrs_value(qw(base)))) { | ||
| 134 | my $t=blx::xsdsql::xsd_parser::type::factory($base,%params); | ||
| 135 | if (ref($t)=~/::type::simple/) { | ||
| 136 | $out->{base}=$t->get_attrs_value(qw(NAME)); | ||
| 137 | } | ||
| 138 | elsif (defined $types) { | ||
| 139 | my $t=$types->{$base}; | ||
| 140 | unless (defined $t) { | ||
| 141 | $self->_debug(undef,"$base: type not found"); | ||
| 142 | return; | ||
| 143 | } | ||
| 144 | $self->_resolve_simple_type($t,$types,$out,%params); | ||
| 145 | } | ||
| 146 | else { | ||
| 147 | $out->{base}=$t; | ||
| 148 | } | ||
| 149 | } | ||
| 150 | if (defined (my $v=$t->get_attrs_value(qw(value)))) { | ||
| 151 | my $r=ref($t); | ||
| 152 | my ($b)=$r=~/::([^:]+)$/; | ||
| 153 | affirm { defined $b } "$r: is not a class"; | ||
| 154 | if ($b eq 'enumeration') { | ||
| 155 | $out->{$b}=[] unless defined $out->{$b}; | ||
| 156 | $self->_debug(__LINE__,$v); | ||
| 157 | push @{$out->{$b}},$v; | ||
| 158 | } | ||
| 159 | else { | ||
| 160 | $out->{$b}=$v; | ||
| 161 | } | ||
| 162 | } | ||
| 163 | |||
| 164 | if (defined (my $child=$t->get_attrs_value(qw(CHILD)))) { | ||
| 165 | for my $c(@$child) { | ||
| 166 | $self->_resolve_simple_type($c,$types,$out,%params); | ||
| 167 | } | ||
| 168 | } | ||
| 169 | return $self; | ||
| 170 | } | ||
| 171 | |||
| 172 | sub _dynamic_create { | ||
| 173 | my ($tag,%params)=@_; | ||
| 174 | my $split=_split_tag_name($tag,%params); | ||
| 175 | if (defined (my $name=$split->{NAME})) { | ||
| 176 | my $class='blx::xsdsql::xsd_parser::node::'.$name; | ||
| 177 | ev("use $class"); | ||
| 178 | my $attrs=delete $params{ATTRIBUTES}; | ||
| 179 | my $obj=$class->new( | ||
| 180 | %params | ||
| 181 | ,%$attrs | ||
| 182 | ,%$split | ||
| 183 | ,DEBUG_NAME => undef | ||
| 184 | ); | ||
| 185 | unless ($name eq 'schema') { | ||
| 186 | if (defined (my $path=$obj->_get_parent_path(%params))) { | ||
| 187 | if (defined (my $name=$attrs->{name})) { | ||
| 188 | $path.='/' if $path ne '/'; | ||
| 189 | $path.=$name; | ||
| 190 | } | ||
| 191 | $obj->set_attrs_value(PATH => $path); | ||
| 192 | } | ||
| 193 | else { | ||
| 194 | affirm { 0 } "path not set"; | ||
| 195 | } | ||
| 196 | } | ||
| 197 | return $obj; | ||
| 198 | } | ||
| 199 | else { | ||
| 200 | affirm { 0 } "$tag: NAME not set"; | ||
| 201 | } | ||
| 202 | undef; | ||
| 203 | } | ||
| 204 | |||
| 205 | sub factory_object { | ||
| 206 | my ($tag,%params)=@_; | ||
| 207 | affirm { defined $params{STACK} } "STACK param not set"; | ||
| 208 | affirm { defined $params{ATTRIBUTES} } "ATTRIBUTES param not set"; | ||
| 209 | affirm { defined $params{EXTRA_TABLES} } "EXTRA_TABLES param not set"; | ||
| 210 | affirm { !defined $params{CHILDS_SCHEMA_LIST} } "CHILDS_SCHEMA_LIST param is reserved"; | ||
| 211 | my $obj=_dynamic_create($tag,%params); | ||
| 212 | return $obj; | ||
| 213 | } | ||
| 214 | |||
| 215 | sub trigger_at_start_node { | ||
| 216 | my ($self,%params)=@_; | ||
| 217 | undef; | ||
| 218 | } | ||
| 219 | |||
| 220 | sub trigger_at_end_node { | ||
| 221 | my ($self,%params)=@_; | ||
| 222 | undef; | ||
| 223 | } | ||
| 224 | |||
| 225 | |||
| 226 | 1; | ||
| 227 | |||
| 228 | __END__ |