From 8b03f4feca1add26e17e4c056ae68fbc01b29e22 Mon Sep 17 00:00:00 2001 From: Tommy Reynolds Date: Thu, 15 Dec 2005 05:42:05 +0000 Subject: Output elements, so they can now be compared. --- bin/xmlpp | 249 +++++++++++++++++++++++++++++++------------------------------- 1 file changed, 125 insertions(+), 124 deletions(-) diff --git a/bin/xmlpp b/bin/xmlpp index 7ab83ef..380a2a6 100755 --- a/bin/xmlpp +++ b/bin/xmlpp @@ -2,12 +2,12 @@ # # Copyright (c) 2002, DecisionSoft Limited All rights reserved. -# Please see: +# Please see: # http://software.decisionsoft.com/licence.html # for more information. -# +# -# $Revision: 1.2 $ +# $Revision: 1.3 $ # # xmlpp: XML pretty printing # @@ -37,7 +37,7 @@ if ($opt_s){ # expect to find attributeOrdering.txt file in same directory # as xmlpp is being run from - + my $scriptDir = $0; if ($scriptDir =~ m#/#){ $scriptDir =~ s#/[^/]+$##; @@ -45,22 +45,22 @@ if ($opt_s){ else{ $scriptDir ="."; } - + # get attribute ordering from external file if (open(SORTLIST, "<$scriptDir/attributeOrdering.txt")) { @sortlist = ; chomp @sortlist; close (SORTLIST); @specialSort = grep(/^\w+/, @sortlist); - } - else { + } + else { # print STDERR "Could not open $scriptDir/attributeOrdering.txt: $!\nWARNING attribute sorting will only be alphabetic\n\n"; } } # set line separator to ">" speeding up parsing of XML files -# with no line breaks +# with no line breaks $/ = ">"; @@ -78,7 +78,7 @@ if ($opt_z && (!$filename or $filename eq '-')) { } if (!$opt_z && scalar(@ARGV) > 1) { - print STDERR "Warning: Multiple files specified without -z option\n"; + print STDERR "Warning: Multiple files specified without -z option\n"; } my $fh; @@ -103,71 +103,72 @@ do { my $input; while ($input .= <$fh>) { - while ($input) { - if ($input =~ s/^<($re_name)((?:\s+$re_name\s*=\s*$re_attr)*\s*)(\/?)>(.*)$/$4/s ) { - my %attr; - my ($name,$attr,$selfclose) = ($1,$2,$3); - while ($attr =~ m/($re_name)\s*=\s*($re_attr)/gs) { - my ($name,$value) = ($1,$2); - $value =~ s/^["'](.*)["']$/$1/s; - $attr{$name} = $value; - } - if ($opt_e) { - parseStart($name, 0, %attr); - if ($selfclose) { parseEnd($name) } - } else { - parseStart($name, $selfclose, %attr); - } - } elsif ($input =~ s/^<\/($re_name)\s*>(.*)$/$2/s) { - parseEnd($1); - } elsif ($input =~ s/^(.*)$/$2/s) { - parseComment($1); - } elsif ($input =~ s/^([^<]+)(.*)$/$2/s) { - parseDefault($1); - } elsif ($input =~ s/^(<\?[^>]*\?>)(.*)$/$2/s) { - parsePI("$1\n"); - } elsif ($input =~ s/^(<\!DOCTYPE[^\[>]*(\[[^\]]*\])?[^>]*>)(.*)$/$3/s) { - parseDoctype("$1"); - } elsif ($input =~ s/^(<\!ENTITY.*>)//s) { - } else { - last; - } - } - if (eof($fh)) { - last; - } + while ($input) { + if ($input =~ s/^<($re_name)((?:\s+$re_name\s*=\s*$re_attr)*\s*)(\/?)>(.*)$/$4/s ) { + my %attr; + my ($name,$attr,$selfclose) = ($1,$2,$3); + while ($attr =~ m/($re_name)\s*=\s*($re_attr)/gs) { + my ($name,$value) = ($1,$2); + $value =~ s/^["'](.*)["']$/$1/s; + $attr{$name} = $value; + } + if ($opt_e) { + parseStart($name, 0, %attr); + if ($selfclose) { parseEnd($name) } + } else { + parseStart($name, $selfclose, %attr); + } + } elsif ($input =~ s/^<\/($re_name)\s*>(.*)$/$2/s) { + parseEnd($1); + } elsif ($input =~ s/^(.*)$/$2/s) { + parseComment($1); + } elsif ($input =~ s/^([^<]+)(.*)$/$2/s) { + parseDefault($1); + } elsif ($input =~ s/^(<\?[^>]*\?>)(.*)$/$2/s) { + parsePI("$1\n"); + } elsif ($input =~ s/^(<\!DOCTYPE[^\[>]*(\[[^\]]*\])?[^>]*>)(.*)$/$3/s) { + parseDoctype("$1"); + } elsif ($input =~ s/^(<\!ENTITY[^>]*>)(.*)$/$2/s) { + $output .= "$1"; + } else { + last; + } + } + if (eof($fh)) { + last; + } } if ($input) { - $input =~ m/([^\n]+)/gs; - print STDERR "WARNING: junk remaining on input: $1\n"; + $input =~ m/([^\n]+)/gs; + print STDERR "WARNING: junk remaining on input: $1\n"; } $fh->close(); if (!$opt_z) { - if(!$opt_H){ - print "$output\n" - } else { - print html_escape($output)."\n" - } + if(!$opt_H){ + print "$output\n" + } else { + print html_escape($output)."\n" + } } else { - if ($input) { - print STDERR "Not overwriting file\n"; - } else { - open FOUT,"> $filename" or die "Cannot overwrite file: $!"; - if(!$opt_H){ - print FOUT "$output\n" - } else { - print FOUT html_escape($output)."\n" - } - close FOUT - } + if ($input) { + print STDERR "Not overwriting file\n"; + } else { + open FOUT,"> $filename" or die "Cannot overwrite file: $!"; + if(!$opt_H){ + print FOUT "$output\n" + } else { + print FOUT html_escape($output)."\n" + } + close FOUT + } } } while ( !$stdin && $opt_z && ($fh = open_next_file(\$filename)) ); - + sub parseStart { @@ -175,21 +176,21 @@ sub parseStart { my $selfclose = shift; my %attr = @_; - $textContent =~ s/\s+$//; + $textContent =~ s/\s+$//; printContent($textContent); if($inAnnotation) { - return; + return; } if($schemaHackMode and $s =~ m/(^|:)annotation$/) { - $inAnnotation = 1; - $textContent = ''; - $lastTag = 1; - return; + $inAnnotation = 1; + $textContent = ''; + $lastTag = 1; + return; } if (length($output)) { - $output .= "\n"; + $output .= "\n"; } $output .= " " x $indent; @@ -211,59 +212,59 @@ sub parseStart { # alphabetic order). Else create a list that has attributes needing # special ordering. foreach $attribute (@alphaSorted){ - $isSpecial = 0; - foreach $sortAttrib (@specialSort){ - if ($attribute eq $sortAttrib){ - push @needSpecialSort, $attribute; - $isSpecial = 1; - } - } - if (!$isSpecial){ - push @final, $attribute; - } + $isSpecial = 0; + foreach $sortAttrib (@specialSort){ + if ($attribute eq $sortAttrib){ + push @needSpecialSort, $attribute; + $isSpecial = 1; + } + } + if (!$isSpecial){ + push @final, $attribute; + } } # now read through the specialSort list backwards looking for - # any match in the needSpecialSort list. Unshift this onto the + # any match in the needSpecialSort list. Unshift this onto the # front of the final array to maintain proper order. foreach my $attribute (reverse @specialSort){ - foreach (@needSpecialSort){ - if ($attribute eq $_){ - unshift @final, $attribute; - } - } + foreach (@needSpecialSort){ + if ($attribute eq $_){ + unshift @final, $attribute; + } + } } @k = @final; } foreach my $attr (@k) { - # - # Remove (min|max)Occurs = 1 if schemaHackMode - # - if ($schemaHackMode and $attr =~ m/^(minOccurs|maxOccurs)$/ and $attr{$attr} eq "1") { - next; - } - - if ($splitAttributes) { - $output .= "\n"." " x $indent." "; - } - if ($attr{$attr} =~ /'/) { - $output .= " $attr=\"$attr{$attr}\""; - } else { - $output .= " $attr='$attr{$attr}'"; - } + # + # Remove (min|max)Occurs = 1 if schemaHackMode + # + if ($schemaHackMode and $attr =~ m/^(minOccurs|maxOccurs)$/ and $attr{$attr} eq "1") { + next; + } + + if ($splitAttributes) { + $output .= "\n"." " x $indent." "; + } + if ($attr{$attr} =~ /'/) { + $output .= " $attr=\"$attr{$attr}\""; + } else { + $output .= " $attr='$attr{$attr}'"; + } } if ($splitAttributes and @k) { - $output .= "\n"." " x $indent; + $output .= "\n"." " x $indent; } if ($selfclose) { - $output .= " />"; - $lastTag = 0; + $output .= " />"; + $lastTag = 0; } else { - $output .= ">"; - $indent++; - $lastTag = 1; + $output .= ">"; + $indent++; + $lastTag = 1; } $textContent = ''; } @@ -272,21 +273,21 @@ sub parseEnd { my $s = shift; if($inAnnotation) { - if($s =~ m/(^|:)annotation$/) { - $inAnnotation = 0; - } - return; + if($s =~ m/(^|:)annotation$/) { + $inAnnotation = 0; + } + return; } if($normaliseWhiteSpace) { - $textContent =~ s/^\s*(.*?)\s*$/$1/; + $textContent =~ s/^\s*(.*?)\s*$/$1/; } $indent--; printContent($textContent); if ($lastTag == 0) { - $output .= "\n"; - $output .= " " x $indent; - } + $output .= "\n"; + $output .= " " x $indent; + } $output .= ""; $textContent = ''; $lastTag = 0; @@ -317,7 +318,7 @@ sub parseDoctype { } sub parseComment { - my $s = shift; + my $s = shift; if($inAnnotation) { return } printContent($textContent,1); if ($s =~ /([^\<]*)(<.*>)(.*)/ms) { @@ -332,9 +333,9 @@ sub parseComment { } $s =~ s/\n\s*$/\n /msg; if ($newLineComments) { - $output .= "\n\n"; + $output .= "\n\n"; } else { - $output .= ""; + $output .= ""; } $textContent=''; } @@ -345,16 +346,16 @@ sub printContent { my ($LF,$ret) = ("",""); if ($s =~ m/\n\s*$/) { - $LF = "\n"; + $LF = "\n"; } if ($s =~ m/^[\s\n]*$/) { - $ret = undef; + $ret = undef; } else { - $output .= "$s"; - $ret = 1; + $output .= "$s"; + $ret = 1; } if ($printLF) { - $output .= $LF; + $output .= $LF; } } @@ -371,11 +372,11 @@ sub open_next_file { my $filename = shift; $$filename = shift @ARGV; while ($$filename and ! -f $$filename) { - print STDERR "WARNING: Could not find file: $$filename\n"; - $$filename = shift @ARGV; + print STDERR "WARNING: Could not find file: $$filename\n"; + $$filename = shift @ARGV; } if(!$$filename) { - return undef; + return undef; } my $fh = new FileHandle; $fh->open("< $$filename") or die "Can't open $$filename: $!"; -- cgit