--- deliantra/Deliantra/res2pm 2007/07/03 00:16:50 1.10 +++ deliantra/Deliantra/res2pm 2007/08/10 12:13:16 1.11 @@ -29,7 +29,9 @@ EOF use Data::Dumper; -use XML::Parser::Grove; +use XML::Grove::Builder; +use XML::Parser::PerlSAX; + sub dump_hash { my ($names, $refs) = @_; @@ -64,7 +66,9 @@ } } -my $type = XML::Parser->new (Style => 'grove')->parsefile ("res/types.xml"); +my $grove_builder = XML::Grove::Builder->new; +my $parser = XML::Parser::PerlSAX->new ( Handler => $grove_builder ); +my $type = $parser->parse ( Source => { SystemId => "res/types.xml" } ); my %bitmask; my %list; @@ -77,7 +81,7 @@ my %spell; sub string($) { - local $_ = join "", @{shift->contents}; + local $_ = join "", map $_->{Data}, @{shift->{Contents}}; $_ =~ s/^\s+//; $_ =~ s/\s+$//; $_ =~ s/\s+/ /g; @@ -88,10 +92,10 @@ my ($e, $sect) = @_; my $arch = { - type => $e->attr ("type"), - name => $e->attr ("editor"), + type => $e->{Attributes}->{type}, + name => $e->{Attributes}->{editor}, desc => string $e, - $e->attr("arch_begin") ? (end => $e->attr("arch_end")) : (), + $e->{Attributes}->{arch_begin} ? (end => $e->{Attributes}->{arch_end}) : (), }; delete $arch->{name} unless defined $arch->{name}; @@ -102,12 +106,12 @@ } elsif ($arch->{type} =~ s/^(list)_(.*)/$1/) { $arch->{value} = $list{$2} ||= {}; } elsif ($arch->{type} eq "fixed") { - $arch->{value} = $e->attr ("value"); + $arch->{value} = $e->{Attributes}->{value}; } elsif ($arch->{type} =~ s/^bool_special$/bool/) { - $arch->{value} = [$e->attr ("false"), $e->attr ("true")]; + $arch->{value} = [$e->{Attributes}->{false}, $e->{Attributes}->{true}]; } - push @$sect, [$e->attr ("arch") || $e->attr("arch_begin"), $arch]; + push @$sect, [$e->{Attributes}->{arch} || $e->{Attributes}->{arch_begin}, $arch]; } sub parse_type { @@ -115,92 +119,92 @@ my %main; - for my $e (grep ref, @{$e->contents}) { - if ($e->name eq "required") { + for my $e (grep { $_->isa ('XML::Grove::Element') } @{$e->{Contents}}) { + if ($e->{Name} eq "required") { # not used - #for my $i (grep ref, @{$e->contents}) { - # $type->{required}{$i->attr ("arch")} = $i->attr ("value"); + #for my $i (grep $_->isa ('XML::Grove::Element'), @{$e->{Contents}}) { + # $type->{required}{$i->{Attributes}->{arch}} = $i->{Attributes}->{value}; #} - } elsif ($e->name eq "attribute") { + } elsif ($e->{Name} eq "attribute") { parse_attr $e, $type->{attr} ||= []; - } elsif ($e->name eq "ignore") { - for my $i (grep ref, @{$e->contents}) { - if ($i->name eq "ignore_list") { - push @{$type->{ignore}}, $ignore_list{$i->attr ("name")} ||= []; - } elsif ($i->name eq "attribute") { - push @{$type->{ignore}}, $i->attr ("arch"); + } elsif ($e->{Name} eq "ignore") { + for my $i (grep $_->isa ('XML::Grove::Element'), @{$e->{Contents}}) { + if ($i->{Name} eq "ignore_list") { + push @{$type->{ignore}}, $ignore_list{$i->{Attributes}->{name}} ||= []; + } elsif ($i->{Name} eq "attribute") { + push @{$type->{ignore}}, $i->{Attributes}->{arch}; } } - } elsif ($e->name eq "import_type") { - #push @{$type->{import}}, $type{$e->attr ("name")} ||= {}; - push @{$type->{import}}, $e->attr ("name"); - } elsif ($e->name eq "use") { + } elsif ($e->{Name} eq "import_type") { + #push @{$type->{import}}, $type{$e->{Attributes}->{name}} ||= {}; + push @{$type->{import}}, $e->{Attributes}->{name}; + } elsif ($e->{Name} eq "use") { $type->{use} = string $e; - } elsif ($e->name eq "description") { + } elsif ($e->{Name} eq "description") { $type->{desc} = string $e; - } elsif ($e->name eq "section") { + } elsif ($e->{Name} eq "section") { my @attr; - for my $i (grep ref, @{$e->contents}) { + for my $i (grep $_->isa ('XML::Grove::Element'), @{$e->{Contents}}) { parse_attr $i, \@attr; } - push @{ $type->{section} }, [$e->attr ("name") => \@attr]; + push @{ $type->{section} }, [$e->{Attributes}->{name} => \@attr]; } else { - warn "unknown types subelement ", $e->name; + warn "unknown types subelement ", $e->{Name}; } } $type } -for my $e (grep ref, @{$type->root->contents}) { - if ($e->name eq "bitmask") { - my $bm = $bitmask{$e->attr ("name")} ||= {}; - for my $b (grep ref, @{$e->contents}) { - $bm->{$b->attr ("bit")} = $b->attr ("name"); +for my $e (grep $_->isa ('XML::Grove::Element'), @{$type->root->{Contents}}) { + if ($e->{Name} eq "bitmask") { + my $bm = $bitmask{$e->{Attributes}->{name}} ||= {}; + for my $b (grep $_->isa ('XML::Grove::Element'), @{$e->{Contents}}) { + $bm->{$b->{Attributes}->{bit}} = $b->{Attributes}->{name}; } - } elsif ($e->name eq "list") { - my $list = $list{$e->attr ("name")} ||= {}; - for my $b (grep ref, @{$e->contents}) { - $list->{$b->attr ("value")} = $b->attr ("name"); + } elsif ($e->{Name} eq "list") { + my $list = $list{$e->{Attributes}->{name}} ||= {}; + for my $b (grep $_->isa ('XML::Grove::Element'), @{$e->{Contents}}) { + $list->{$b->{Attributes}->{value}} = $b->{Attributes}->{name}; } - } elsif ($e->name eq "ignore_list") { - my $list = $ignore_list{$e->attr ("name")} ||= []; - for my $b (grep ref, @{$e->contents}) { - push @$list, $b->attr ("arch"); + } elsif ($e->{Name} eq "ignore_list") { + my $list = $ignore_list{$e->{Attributes}->{name}} ||= []; + for my $b (grep $_->isa ('XML::Grove::Element'), @{$e->{Contents}}) { + push @$list, $b->{Attributes}->{arch}; } - } elsif ($e->name eq "default_type") { + } elsif ($e->{Name} eq "default_type") { parse_type $e, \%default_attr; - } elsif ($e->name eq "type") { - my $type = $type{$e->attr ("name")} ||= {}; + } elsif ($e->{Name} eq "type") { + my $type = $type{$e->{Attributes}->{name}} ||= {}; - $type->{name} = $e->attr ("name"); + $type->{name} = $e->{Attributes}->{name}; parse_type $e, $type; - if ($e->attr ("number") > 0) { - $attr{$e->attr ("number")} = $type; - } elsif ($e->attr ("name") eq "Misc") { + if ($e->{Attributes}->{number} > 0) { + $attr{$e->{Attributes}->{number}} = $type; + } elsif ($e->{Attributes}->{name} eq "Misc") { delete $type->{required}; } else { push @attr0, $type; } } else { - warn "unknown types element ", $e->name; + warn "unknown types element ", $e->{Name}; } } -my $type = XML::Parser->new (Style => 'grove')->parsefile ("res/typenumbers.xml"); +my $type = $parser->parse ( Source => { SystemId => "res/typenumbers.xml" } ); -for (grep ref, @{$type->root->contents}) { - $typename{$_->attr ("number")} = $_->attr ("name"); +for (grep $_->isa ('XML::Grove::Element'), @{$type->root->{Contents}}) { + $typename{$_->{Attributes}->{number}} = $_->{Attributes}->{name}; } -my $spell = XML::Parser->new (Style => 'grove')->parsefile ("res/spells.xml") +my $spell = $parser->parse ( Source => { SystemId => "res/spells.xml" } ) or die; -for (grep ref, @{$spell->root->contents}) { - $spell{$_->attr ("id")} = $_->attr ("name"); +for (grep $_->isa ('XML::Grove::Element'), @{$spell->root->{Contents}}) { + $spell{$_->{Attributes}->{id}} = $_->{Attributes}->{name}; } dump_hash ["BITMASK", "LIST", "IGNORE_LIST", "DEFAULT_ATTR", "TYPE", "ATTR", "TYPENAME", "SPELL"],