From 81a4534b9dfdd924e683c76478b8dec53c131c84c170c54f2c1517800fadc5ae Mon Sep 17 00:00:00 2001 From: Jan Engelhardt Date: Wed, 5 Aug 2015 09:45:20 +0000 Subject: [PATCH] - Recognize XML tags with arbitrary namespace prefixes OBS-URL: https://build.opensuse.org/package/show/mozilla:addons/mozaddon-devel?expand=0&rev=4 --- mozaddon-devel.changes | 6 +++ mozaddon-devel.spec | 2 +- mozaddondev-getappid | 101 ++++++++++++++++++++++++++--------------- 3 files changed, 72 insertions(+), 37 deletions(-) diff --git a/mozaddon-devel.changes b/mozaddon-devel.changes index 941c880..f78900e 100644 --- a/mozaddon-devel.changes +++ b/mozaddon-devel.changes @@ -1,3 +1,9 @@ +------------------------------------------------------------------- +Wed Aug 5 09:41:47 UTC 2015 - jengelh@inai.de + +- Recognize XML tags with arbitrary namespace + prefixes + ------------------------------------------------------------------- Mon Apr 1 20:36:52 UTC 2013 - jengelh@inai.de diff --git a/mozaddon-devel.spec b/mozaddon-devel.spec index caf01a0..f16b2e9 100644 --- a/mozaddon-devel.spec +++ b/mozaddon-devel.spec @@ -1,7 +1,7 @@ # # spec file for package mozaddon-devel # -# Copyright (c) 2013 SUSE LINUX Products GmbH, Nuernberg, Germany. +# Copyright (c) 2015 SUSE LINUX GmbH, Nuernberg, Germany. # # All modifications and additions to the file contributed by third parties # remain the property of their copyright owners, unless otherwise agreed diff --git a/mozaddondev-getappid b/mozaddondev-getappid index 052bb07..e803c67 100644 --- a/mozaddondev-getappid +++ b/mozaddondev-getappid @@ -3,53 +3,82 @@ # authored by Jan Engelhardt, 2011-03-24 # released into the Public Domain # +use Archive::Zip qw(:ERROR_CODES :CONSTANTS); use XML::Simple; +&main(); -my $file = shift || die "Usage: $0 [install.rdf|somefile.xpi]\n"; -my $xml; +sub get_rdf +{ + my $file = shift @_; -if ($file =~ /\.xpi$/) { - use Archive::Zip qw/:ERROR_CODES :CONSTANTS/; - my $zip = Archive::Zip->new(); - if ( $zip->read($file) != AZ_OK ) { - die "zip file read error\n"; + if ($file eq "install.rdf") { + return XMLin($file); + } + if (substr($file, -4, 4) eq ".xpi") { + my $zip = Archive::Zip->new(); + if ($zip->read($file) != AZ_OK) { + die "zip file read error\n"; + } + my $data = $zip->contents("install.rdf"); + die "missing install.rdf in $file\n" unless $data; + return XMLin($data); } - my $data = $zip->contents("install.rdf"); - die "missing install.rdf in $file\n" unless $data; - $xml = XMLin($data) || die "$!\n"; -} elsif ($file =~ /install.rdf/) { - $xml = XMLin($file) || die "$!\n"; -} else { die "unsupported file format\n"; } -my $desc; -for my $tag (qw/RDF:Description Description/) { - if (exists $xml->{$tag}) { - if (ref $xml->{$tag} eq 'ARRAY') { - $desc = $xml->{$tag}; - } else { - $desc = [ $xml->{$tag} ]; +sub get_desc +{ + my $xml = shift @_; + my $desc; + foreach my $tag (keys %$xml) { + if ($tag !~ m{^(\w+:)?Description$}) { + next; + } + if (ref($xml->{$tag}) eq "ARRAY") { + return $xml->{$tag}; + } + return [$xml->{$tag}]; + } +} + +sub get_id +{ + my $desc = shift @_; + my $id; + + foreach (qw(id em:id)) { + if (exists($desc->{$_})) { + $id = $desc->{$_}; + last; } } + return $id; } -my $uuid; -my $id; -for my $x (@$desc) { - if ($x->{"em:id"} =~ /{[[:xdigit:]]+-/) { - print STDERR "Warning: multiple uuids!\n" if defined $uuid; - $uuid = $x->{"em:id"}; - } elsif ($x->{"em:id"} =~ /@/) { - print STDERR "Warning: multiple ids!\n" if defined $id; - $id = $x->{"em:id"}; +sub main +{ + my $file = shift @ARGV; + if (!defined($file)) { + print "Usage: $0 {install.rdf|something.xpi}\n"; + exit 1; + } + my $xml = get_rdf($file); + if (!defined($xml)) { + die "xml: $!\n"; + } + my $desc_list = &get_desc($xml); + my $id; + foreach my $one_desc (@$desc_list) { + my $value = &get_id($one_desc); + if ($value =~ /\@|{[[:xdigit:]]+-/) { + if (defined($id)) { + print STDERR "Warning: multiple IDs/UUIDs!\n"; + } + $id = $value; + } + } + if (!defined($id)) { + exit 1; } -} - -if (defined $id) { print "$id\n"; -} elsif (defined $uuid) { - print "$uuid\n"; -} else { - exit 1; }