| 1 | package XML::SemanticCompare::SAX; | ||
| 2 | use base qw( XML::SAX::Base ); | ||
| 3 | use XML::SAX::ParserFactory; | ||
| 4 | |||
| 5 | use Data::Dumper; | ||
| 6 | |||
| 7 | # add versioning to this module | ||
| 8 | use vars qw /$VERSION/; | ||
| 9 | $VERSION = 0.95; | ||
| 10 | |||
| 11 | #----------------------------------------------------------------- | ||
| 12 | # new | ||
| 13 | #----------------------------------------------------------------- | ||
| 14 | sub new { | ||
| 15 | my ( $class, %options ) = @_; | ||
| 16 | |||
| 17 | # create an object | ||
| 18 | my $self = {}; | ||
| 19 | |||
| 20 | # stack of elements | ||
| 21 | $self->{elements} = (); | ||
| 22 | $self->{items} = (); | ||
| 23 | bless $self, $class; | ||
| 24 | $self->select_parser($options{parser}) if defined $options{parser}; | ||
| 25 | return $self; | ||
| 26 | } | ||
| 27 | |||
| 28 | sub select_parser { | ||
| 29 | my $self = shift; | ||
| 30 | my $parser = shift; | ||
| 31 | $parser = 'XML::LibXML::SAX::Parser' unless defined $parser; | ||
| 32 | $XML::SAX::ParserPackage = $parser; | ||
| 33 | } | ||
| 34 | |||
| 35 | #----------------------------------------------------------------- | ||
| 36 | # parse | ||
| 37 | # args: method => 'string', data => direct XML | ||
| 38 | # OR | ||
| 39 | # method => 'file', data => filename | ||
| 40 | #----------------------------------------------------------------- | ||
| 41 | sub parse { | ||
| 42 | my ( $self, %args ) = @_; | ||
| 43 | die("parse() needs arguments 'method' and 'data'.") | ||
| 44 | unless $args{method} and $args{data}; | ||
| 45 | my $parser = XML::SAX::ParserFactory->parser( Handler => $self ); | ||
| 46 | if ( $args{method} eq 'string' ) { | ||
| 47 | $parser->parse_string( $args{data} ); | ||
| 48 | } elsif ( $args{method} eq 'file' ) { | ||
| 49 | $parser->parse_file( $args{data} ); | ||
| 50 | } else { | ||
| 51 | die("in parse(): 'method' is neither 'string' nor 'file'."); | ||
| 52 | } | ||
| 53 | |||
| 54 | return \@{$self->{items}}; | ||
| 55 | } | ||
| 56 | |||
| 57 | sub start_element { | ||
| 58 | my ( $self, $element ) = @_; | ||
| 59 | # [namespace-uri()='http://foo.example.com'] | ||
| 60 | my $uri = $element->{NamespaceURI}; | ||
| 61 | my $e = $element->{LocalName} . "[namespace-uri() = '$uri']" if defined $uri and ($uri ne '' or $uri eq '#'); | ||
| 62 | $e = $element->{LocalName} unless defined $e; | ||
| 63 | $e = $self->peek() . "$e/"; | ||
| 64 | push @{ $self->{elements} }, $e; | ||
| 65 | push @{ $self->{items} }, $e; | ||
| 66 | |||
| 67 | foreach my $key( keys %{ $element->{Attributes} } ) { | ||
| 68 | # ignore xmlns declarations ... | ||
| 69 | next if $element->{Attributes}->{$key}->{Prefix} =~ /^xmlns$/; | ||
| 70 | next if $element->{Attributes}->{$key}->{LocalName} =~ /^xmlns$/; | ||
| 71 | my $uri_ns = $element->{Attributes}->{$key}->{NamespaceURI}; | ||
| 72 | |||
| 73 | my $attr = $element->{Attributes}->{$key}->{LocalName}; | ||
| 74 | my $value = $element->{Attributes}->{$key}->{Value}; | ||
| 75 | my $string = "@" . "[$attr='$value' and namespace-uri() = '" . "$uri_ns']" if defined $uri_ns and $uri_ns ne ''; | ||
| 76 | $string = "[\@$attr=$value]" unless defined $string; | ||
| 77 | # @foo[namespace-uri()='http://foo.example.com'] | ||
| 78 | push @{ $self->{items} }, $e . $string; | ||
| 79 | } | ||
| 80 | } | ||
| 81 | |||
| 82 | sub end_element { | ||
| 83 | my ( $self, $element ) = @_; | ||
| 84 | pop @{$self->{elements}}; | ||
| 85 | } | ||
| 86 | |||
| 87 | sub characters { | ||
| 88 | my ( $self, $characters ) = @_; | ||
| 89 | return if $characters->{Data} =~ m/^\s*$/gi; | ||
| 90 | my $text = $characters->{Data}; | ||
| 91 | # remove leading/trailing | ||
| 92 | $text = $self->trim($text); | ||
| 93 | push @{ $self->{items} }, $self->peek() . "text()=$text"; | ||
| 94 | } | ||
| 95 | |||
| 96 | sub start_document { | ||
| 97 | my ( $self, $document ) = @_; | ||
| 98 | |||
| 99 | # initialize everything | ||
| 100 | $self->{elements} = (); | ||
| 101 | $self->{items} = (); | ||
| 102 | push @{$self->{elements}}, "/"; | ||
| 103 | } | ||
| 104 | |||
| 105 | sub end_document { | ||
| 106 | my ( $self, $document ) = @_; | ||
| 107 | } | ||
| 108 | |||
| 109 | sub ignorable_whitespace { | ||
| 110 | my ( $self, $characters ) = @_; | ||
| 111 | } | ||
| 112 | |||
| 113 | #********************************************************************* | ||
| 114 | # | ||
| 115 | # XML-SAX 2.0 error events | ||
| 116 | # | ||
| 117 | #******************************************************************** | ||
| 118 | sub fatal_error { | ||
| 119 | my ($self) = shift; | ||
| 120 | my $msg = $self->_format_msg(@_); | ||
| 121 | die("Parsing XML fatally failed: $msg"); | ||
| 122 | } | ||
| 123 | |||
| 124 | sub error { | ||
| 125 | my ($self) = shift; | ||
| 126 | my $msg = $self->_format_msg(@_); | ||
| 127 | die("Parsing XML failed: $msg"); | ||
| 128 | } | ||
| 129 | |||
| 130 | sub warning { | ||
| 131 | my ($self) = shift; | ||
| 132 | my $msg = $self->_format_msg(@_); | ||
| 133 | die($msg); | ||
| 134 | } | ||
| 135 | |||
| 136 | sub _format_msg { | ||
| 137 | my ( $self, $message ) = @_; | ||
| 138 | return $message unless ref($message) eq 'XML::SAX::Exception::Parse'; | ||
| 139 | my $pubId = $message->{PublicId} || ''; | ||
| 140 | my $sysId = $message->{SystemId} || ''; | ||
| 141 | my $linNo = $message->{LineNumber} || '?'; | ||
| 142 | my $colNo = $message->{ColumnNumber} || '?'; | ||
| 143 | my $msg = $message->{Message} || ''; | ||
| 144 | return "$msg [line $linNo, column $colNo] $sysId $pubId"; | ||
| 145 | } | ||
| 146 | |||
| 147 | sub peek { | ||
| 148 | my ($self) = @_; | ||
| 149 | # my version of peek | ||
| 150 | my $obj = pop @{$self->{elements}}; | ||
| 151 | push @{$self->{elements}}, $obj; | ||
| 152 | return $obj; | ||
| 153 | } | ||
| 154 | |||
| 155 | sub trim { | ||
| 156 | my ( $self, $text ) = @_; | ||
| 157 | return $text unless defined $text; | ||
| 158 | $text =~ s/^\s+//; | ||
| 159 | $text =~ s/\s+$//; | ||
| 160 | return $text; | ||
| 161 | } | ||
| 162 | |||
| 163 | |||
| 164 | 1; | ||
| 165 | __END__ |