| 1 | package XML::ED::Bare; | ||
| 2 | |||
| 3 | use Carp; | ||
| 4 | use strict; | ||
| 5 | use vars qw( @ISA @EXPORT @EXPORT_OK $VERSION ); | ||
| 6 | use utf8; | ||
| 7 | require Exporter; | ||
| 8 | require DynaLoader; | ||
| 9 | @ISA = qw(DynaLoader); | ||
| 10 | |||
| 11 | $VERSION = "0.0.2"; | ||
| 12 | |||
| 13 | use vars qw($VERSION *AUTOLOAD); | ||
| 14 | |||
| 15 | *AUTOLOAD = \&XML::ED::Bare::AUTOLOAD; | ||
| 16 | bootstrap XML::ED::Bare $VERSION; | ||
| 17 | |||
| 18 | =head1 NAME | ||
| 19 | |||
| 20 | XML::ED::Bare - Minimal XML parser implemented via a C state engine | ||
| 21 | |||
| 22 | =head1 VERSION | ||
| 23 | |||
| 24 | 0.45 | ||
| 25 | |||
| 26 | =cut | ||
| 27 | |||
| 28 | sub new { | ||
| 29 | my $class = shift; | ||
| 30 | my $self = { @_ }; | ||
| 31 | |||
| 32 | if( $self->{ 'text' } ) { | ||
| 33 | XML::ED::Bare::c_parse( $self->{'text'} ); | ||
| 34 | } | ||
| 35 | else { | ||
| 36 | my $res = open( XML, $self->{ 'file' } ); | ||
| 37 | if( !$res ) { | ||
| 38 | $self->{ 'xml' } = 0; | ||
| 39 | return 0; | ||
| 40 | } | ||
| 41 | { | ||
| 42 | local $/ = undef; | ||
| 43 | $self->{'text'} = <XML>; | ||
| 44 | } | ||
| 45 | close( XML ); | ||
| 46 | XML::ED::Bare::c_parse( $self->{'text'} ); | ||
| 47 | } | ||
| 48 | bless $self, $class; | ||
| 49 | return $self if( !wantarray ); | ||
| 50 | return ( $self, $self->parse() ); | ||
| 51 | } | ||
| 52 | |||
| 53 | sub DESTROY { | ||
| 54 | my $self = shift; | ||
| 55 | undef $self->{'xml'}; | ||
| 56 | } | ||
| 57 | |||
| 58 | # Load a file using XML::ED::DOM, convert it to a hash, and return the hash | ||
| 59 | sub parse { | ||
| 60 | my $self = shift; | ||
| 61 | |||
| 62 | my $res = XML::ED::Bare::xml2obj(); | ||
| 63 | $self->{'structroot'} = XML::ED::Bare::get_root(); | ||
| 64 | $self->free_tree(); | ||
| 65 | |||
| 66 | if( defined( $self->{'scheme'} ) ) { | ||
| 67 | $self->{'xbs'} = new XML::ED::Bare( %{ $self->{'scheme'} } ); | ||
| 68 | } | ||
| 69 | if( defined( $self->{'xbs'} ) ) { | ||
| 70 | my $xbs = $self->{'xbs'}; | ||
| 71 | my $ob = $xbs->parse(); | ||
| 72 | $self->{'xbso'} = $ob; | ||
| 73 | readxbs( $ob ); | ||
| 74 | } | ||
| 75 | |||
| 76 | if( $res < 0 ) { croak "Error at ".$self->lineinfo( -$res ); } | ||
| 77 | $self->{ 'xml' } = $res; | ||
| 78 | |||
| 79 | if( defined( $self->{'xbso'} ) ) { | ||
| 80 | my $ob = $self->{'xbso'}; | ||
| 81 | my $cres = $self->check( $res, $ob ); | ||
| 82 | croak( $cres ) if( $cres ); | ||
| 83 | } | ||
| 84 | |||
| 85 | return $self->{ 'xml' }; | ||
| 86 | } | ||
| 87 | |||
| 88 | sub lineinfo { | ||
| 89 | my $self = shift; | ||
| 90 | my $res = shift; | ||
| 91 | my $line = 1; | ||
| 92 | my $j = 0; | ||
| 93 | for( my $i=0;$i<$res;$i++ ) { | ||
| 94 | my $let = substr( $self->{'text'}, $i, 1 ); | ||
| 95 | if( ord($let) == 10 ) { | ||
| 96 | $line++; | ||
| 97 | $j = $i; | ||
| 98 | } | ||
| 99 | } | ||
| 100 | my $part = substr( $self->{'text'}, $res, 10 ); | ||
| 101 | $part =~ s/\n//g; | ||
| 102 | $res -= $j; | ||
| 103 | if( $self->{'offset'} ) { | ||
| 104 | my $off = $self->{'offset'}; | ||
| 105 | $line += $off; | ||
| 106 | return "$off line $line char $res \"$part\""; | ||
| 107 | } | ||
| 108 | return "line $line char $res \"$part\""; | ||
| 109 | } | ||
| 110 | |||
| 111 | # xml bare schema | ||
| 112 | sub check { | ||
| 113 | my ( $self, $node, $scheme, $parent ) = @_; | ||
| 114 | |||
| 115 | my $fail = ''; | ||
| 116 | if( ref( $scheme ) eq 'ARRAY' ) { | ||
| 117 | for my $one ( @$scheme ) { | ||
| 118 | my $res = $self->checkone( $node, $one, $parent ); | ||
| 119 | return 0 if( !$res ); | ||
| 120 | $fail .= "$res\n"; | ||
| 121 | } | ||
| 122 | } | ||
| 123 | else { return $self->checkone( $node, $scheme, $parent ); } | ||
| 124 | return $fail; | ||
| 125 | } | ||
| 126 | |||
| 127 | sub checkone { | ||
| 128 | my ( $self, $node, $scheme, $parent ) = @_; | ||
| 129 | |||
| 130 | for my $key ( keys %$node ) { | ||
| 131 | next if( substr( $key, 0, 1 ) eq '_' || $key eq '_att' || $key eq 'comment' ); | ||
| 132 | if( $key eq 'value' ) { | ||
| 133 | my $val = $node->{ 'value' }; | ||
| 134 | my $regexp = $scheme->{'value'}; | ||
| 135 | if( $regexp ) { | ||
| 136 | if( $val !~ m/^($regexp)$/ ) { | ||
| 137 | my $linfo = $self->lineinfo( $node->{'_i'} ); | ||
| 138 | return "Value of '$parent' node ($val) does not match /$regexp/ [$linfo]"; | ||
| 139 | } | ||
| 140 | } | ||
| 141 | next; | ||
| 142 | } | ||
| 143 | my $sub = $node->{ $key }; | ||
| 144 | my $ssub = $scheme->{ $key }; | ||
| 145 | if( !$ssub ) { #&& ref( $schemesub ) ne 'HASH' | ||
| 146 | my $linfo = $self->lineinfo( $sub->{'_i'} ); | ||
| 147 | return "Invalid node '$key' in xml [$linfo]"; | ||
| 148 | } | ||
| 149 | if( ref( $sub ) eq 'HASH' ) { | ||
| 150 | my $res = $self->check( $sub, $ssub, $key ); | ||
| 151 | return $res if( $res ); | ||
| 152 | } | ||
| 153 | if( ref( $sub ) eq 'ARRAY' ) { | ||
| 154 | my $asub = $ssub; | ||
| 155 | if( ref( $asub ) eq 'ARRAY' ) { | ||
| 156 | $asub = $asub->[0]; | ||
| 157 | } | ||
| 158 | if( $asub->{'_t'} ) { | ||
| 159 | my $max = $asub->{'_max'} || 0; | ||
| 160 | if( $#$sub >= $max ) { | ||
| 161 | my $linfo = $self->lineinfo( $sub->[0]->{'_i'} ); | ||
| 162 | return "Too many nodes of type '$key'; max $max; [$linfo]" | ||
| 163 | } | ||
| 164 | my $min = $asub->{'_min'} || 0; | ||
| 165 | if( ($#$sub+1)<$min ) { | ||
| 166 | my $linfo = $self->lineinfo( $sub->[0]->{'_i'} ); | ||
| 167 | return "Not enough nodes of type '$key'; min $min [$linfo]" | ||
| 168 | } | ||
| 169 | } | ||
| 170 | for( @$sub ) { | ||
| 171 | my $res = $self->check( $_, $ssub, $key ); | ||
| 172 | return $res if( $res ); | ||
| 173 | } | ||
| 174 | } | ||
| 175 | } | ||
| 176 | if( my $dem = $scheme->{'_demand'} ) { | ||
| 177 | for my $req ( @{$scheme->{'_demand'}} ) { | ||
| 178 | my $ck = $node->{ $req }; | ||
| 179 | if( !$ck ) { | ||
| 180 | my $linfo = $self->lineinfo( $node->{'_i'} ); | ||
| 181 | return "Required node '$req' does not exist [$linfo]" | ||
| 182 | } | ||
| 183 | if( ref( $ck ) eq 'ARRAY' ) { | ||
| 184 | my $linfo = $self->lineinfo( $node->{'_i'} ); | ||
| 185 | return "Required node '$req' is empty array [$linfo]" if( $#$ck == -1 ); | ||
| 186 | } | ||
| 187 | } | ||
| 188 | } | ||
| 189 | return 0; | ||
| 190 | } | ||
| 191 | |||
| 192 | |||
| 193 | sub readxbs { # xbs = xml bare schema | ||
| 194 | my $node = shift; | ||
| 195 | my @demand; | ||
| 196 | for my $key ( keys %$node ) { | ||
| 197 | next if( substr( $key, 0, 1 ) eq '_' || $key eq '_att' || $key eq 'comment' ); | ||
| 198 | if( $key eq 'value' ) { | ||
| 199 | my $val = $node->{'value'}; | ||
| 200 | delete $node->{'value'} if( $val =~ m/^\W*$/ ); | ||
| 201 | next; | ||
| 202 | } | ||
| 203 | my $sub = $node->{ $key }; | ||
| 204 | |||
| 205 | if( $key =~ m/([a-z_]+)([^a-z_]+)/ ) { | ||
| 206 | my $name = $1; | ||
| 207 | my $t = $2; | ||
| 208 | my $min; | ||
| 209 | my $max; | ||
| 210 | if( $t eq '+' ) { | ||
| 211 | $min = 1; | ||
| 212 | $max = 1000; | ||
| 213 | } | ||
| 214 | elsif( $t eq '*' ) { | ||
| 215 | $min = 0; | ||
| 216 | $max = 1000; | ||
| 217 | } | ||
| 218 | elsif( $t eq '?' ) { | ||
| 219 | $min = 0; | ||
| 220 | $max = 1; | ||
| 221 | } | ||
| 222 | elsif( $t eq '@' ) { | ||
| 223 | $name = 'multi_'.$name; | ||
| 224 | $min = 1; | ||
| 225 | $max = 1; | ||
| 226 | } | ||
| 227 | elsif( $t =~ m/\{([0-9]+),([0-9]+)\}/ ) { | ||
| 228 | $min = $1; | ||
| 229 | $max = $2; | ||
| 230 | $t = 'r'; # range | ||
| 231 | } | ||
| 232 | |||
| 233 | if( ref( $sub ) eq 'HASH' ) { | ||
| 234 | my $res = readxbs( $sub ); | ||
| 235 | $sub->{'_t'} = $t; | ||
| 236 | $sub->{'_min'} = $min; | ||
| 237 | $sub->{'_max'} = $max; | ||
| 238 | } | ||
| 239 | if( ref( $sub ) eq 'ARRAY' ) { | ||
| 240 | for my $item ( @$sub ) { | ||
| 241 | my $res = readxbs( $item ); | ||
| 242 | $item->{'_t'} = $t; | ||
| 243 | $item->{'_min'} = $min; | ||
| 244 | $item->{'_max'} = $max; | ||
| 245 | } | ||
| 246 | } | ||
| 247 | |||
| 248 | push( @demand, $name ) if( $min ); | ||
| 249 | $node->{$name} = $node->{$key}; | ||
| 250 | delete $node->{$key}; | ||
| 251 | } | ||
| 252 | else { | ||
| 253 | if( ref( $sub ) eq 'HASH' ) { | ||
| 254 | readxbs( $sub ); | ||
| 255 | $sub->{'_t'} = 'r'; | ||
| 256 | $sub->{'_min'} = 1; | ||
| 257 | $sub->{'_max'} = 1; | ||
| 258 | } | ||
| 259 | if( ref( $sub ) eq 'ARRAY' ) { | ||
| 260 | for my $item ( @$sub ) { | ||
| 261 | readxbs( $item ); | ||
| 262 | $item->{'_t'} = 'r'; | ||
| 263 | $item->{'_min'} = 1; | ||
| 264 | $item->{'_max'} = 1; | ||
| 265 | } | ||
| 266 | } | ||
| 267 | |||
| 268 | push( @demand, $key ); | ||
| 269 | } | ||
| 270 | } | ||
| 271 | if( @demand ) { $node->{'_demand'} = \@demand; } | ||
| 272 | } | ||
| 273 | |||
| 274 | sub free_tree { my $self = shift; XML::ED::Bare::free_tree_c( $self->{'structroot'} ); } | ||
| 275 | |||
| 276 | 1; | ||
| 277 | |||
| 278 | __END__ |