root/Xml/Working/analyzeElementsAndAttributes.pl
| Revision 42, 2.2 kB (checked in by andrew, 19 months ago) | |
|---|---|
|
|
| Line | |
|---|---|
| 1 | #!/usr/bin/perl |
| 2 | |
| 3 | use strict; |
| 4 | |
| 5 | my @files = @ARGV; |
| 6 | |
| 7 | unless( @files ) { |
| 8 | print "usage: \n./analyzeElementsAndAttributes.pl *xsd\n"; |
| 9 | exit; |
| 10 | } |
| 11 | |
| 12 | print "Analyzing elements & attributes of files:\n\t".join( ', ', @files )."\n\n"; |
| 13 | my @elements; |
| 14 | my @attributes; |
| 15 | #my %elements; |
| 16 | #my %attributes; |
| 17 | my %elements_files; |
| 18 | my %attributes_files; |
| 19 | my %repeatedElements; |
| 20 | |
| 21 | foreach my $file (@files ) { |
| 22 | open( FILE, "< $file" ) |
| 23 | or die "Couldn't open $file"; |
| 24 | while( my $line = <FILE> ) { |
| 25 | chomp $line; |
| 26 | if( $line =~ m/<(xs:)?element[^>*?]name\s*=\s*"(\w+)"/o ) { |
| 27 | push( @elements, $2 ); |
| 28 | push( @{ $elements_files{ $2 } }, $file ); |
| 29 | $repeatedElements{ $2 } = undef |
| 30 | if( scalar( @{ $elements_files{ $2 } } ) > 1 ); |
| 31 | } elsif( $line =~ m/<(xs:)?attribute[^>*?]name\s*=\s*"(\w+)"/o ) { |
| 32 | push( @attributes, $2 ); |
| 33 | push( @{ $attributes_files{ $2 } }, $file ); |
| 34 | } |
| 35 | } |
| 36 | close( FILE ); |
| 37 | } |
| 38 | |
| 39 | print "Elements and attributes with the same names:\n"; |
| 40 | my @el_and_attr = intersect( \@elements, \@attributes ); |
| 41 | foreach my $name ( @el_and_attr ) { |
| 42 | print "\t$name: (element in: ". |
| 43 | join( ', ', @{ $elements_files{ $name } } ). |
| 44 | " attribute in: ". |
| 45 | join( ', ', @{ $attributes_files{ $name } } ). |
| 46 | ")\n"; |
| 47 | } |
| 48 | |
| 49 | if( keys %repeatedElements ) { |
| 50 | print "Elements repeated more than once:\n"; |
| 51 | foreach my $name ( keys %repeatedElements ) { |
| 52 | print "\t$name: (repeated in: ". |
| 53 | join( ', ', @{ $elements_files{ $name } } ). |
| 54 | ")\n"; |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | sub unique { |
| 59 | my %a; |
| 60 | if( ( scalar( @_ ) eq 1 ) && ( ref( $_[0] ) eq 'ARRAY' ) ) { |
| 61 | %a = map{ $_ => undef} @{ $_[0] }; |
| 62 | } else { |
| 63 | %a = map{ $_ => undef} @_; |
| 64 | } |
| 65 | return sort( keys %a ); |
| 66 | } |
| 67 | |
| 68 | sub setdiff { |
| 69 | my ($a_in, $b_in) = @_; |
| 70 | my %a = map{ $_ => undef} @$a_in; |
| 71 | my %b = map{ $_ => undef} @$b_in; |
| 72 | my @diff; |
| 73 | foreach ( keys %a ){ |
| 74 | push( @diff, $_ ) |
| 75 | if( not exists( $b{ $_ } ) ); |
| 76 | } |
| 77 | return sort( @diff ); |
| 78 | } |
| 79 | |
| 80 | sub listdiff { |
| 81 | my ($a_in, $b_in) = @_; |
| 82 | my %b = map{ $_ => undef} @$b_in; |
| 83 | my @diff; |
| 84 | foreach ( @$a_in ){ |
| 85 | push( @diff, $_ ) |
| 86 | if( not exists( $b{ $_ } ) ); |
| 87 | } |
| 88 | return sort( @diff ); |
| 89 | } |
| 90 | |
| 91 | sub intersect { |
| 92 | my ($a_in, $b_in) = @_; |
| 93 | my %a = map{ $_ => undef} @$a_in; |
| 94 | my %b = map{ $_ => undef} @$b_in; |
| 95 | my @intersect; |
| 96 | foreach ( keys %a ){ |
| 97 | push( @intersect, $_ ) |
| 98 | if( exists( $b{ $_ } ) ); |
| 99 | } |
| 100 | return sort( @intersect ); |
| 101 | } |
Note: See TracBrowser
for help on using the browser.
