| 1 | package SVG::Parser::SAX; | ||||||||||||||||||||
| 2 | 6 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 3 | 6 | ▶ | use Carp; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | |||||||||||||||||||||
| 5 | 6 | require 5.004; | |||||||||||||||||||
| 6 | |||||||||||||||||||||
| 7 | 6 | ▶ | use XML::SAX; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | 6 | ▶ | use SVG::Parser::Base; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | 6 | ▶ | use SVG::Parser::SAX::Handler; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | |||||||||||||||||||||
| 11 | 6 | ▶ | use vars qw(@ISA $VERSION); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 12 | 6 | @ISA=qw(SVG::Parser::Base); # this changes once the parser type is known | |||||||||||||||||||
| 13 | |||||||||||||||||||||
| 14 | 6 | $VERSION="1.03"; | |||||||||||||||||||
| 15 | |||||||||||||||||||||
| 16 | #------------------------------------------------------------------------------- | ||||||||||||||||||||
| 17 | |||||||||||||||||||||
| 18 | =head1 NAME | ||||||||||||||||||||
| 19 | |||||||||||||||||||||
| 20 | SVG::Parser::SAX - XML SAX Parser for SVG documents | ||||||||||||||||||||
| 21 | |||||||||||||||||||||
| 22 | =head1 SYNOPSIS | ||||||||||||||||||||
| 23 | |||||||||||||||||||||
| 24 | #!/usr/bin/perl -w | ||||||||||||||||||||
| 25 | use strict; | ||||||||||||||||||||
| 26 | use SVG::Parser::SAX; | ||||||||||||||||||||
| 27 | |||||||||||||||||||||
| 28 | die "Usage: $0 <file>\n" unless @ARGV; | ||||||||||||||||||||
| 29 | |||||||||||||||||||||
| 30 | my $xml; | ||||||||||||||||||||
| 31 | { | ||||||||||||||||||||
| 32 | local $/=undef; | ||||||||||||||||||||
| 33 | $xml=<>; | ||||||||||||||||||||
| 34 | } | ||||||||||||||||||||
| 35 | |||||||||||||||||||||
| 36 | my $parser=new SVG::Parser::SAX(-debug => 1); | ||||||||||||||||||||
| 37 | |||||||||||||||||||||
| 38 | my $svg=$parser->parse($xml); | ||||||||||||||||||||
| 39 | |||||||||||||||||||||
| 40 | print $svg->xmlify; | ||||||||||||||||||||
| 41 | |||||||||||||||||||||
| 42 | =head1 DESCRIPTION | ||||||||||||||||||||
| 43 | |||||||||||||||||||||
| 44 | SVG::Parser::SAX is the SAX-based parser module used by SVG::Parser when an | ||||||||||||||||||||
| 45 | underlying XML::SAX-based parser is selected. It may also be used directly, as shown | ||||||||||||||||||||
| 46 | in the synopsis above. | ||||||||||||||||||||
| 47 | |||||||||||||||||||||
| 48 | Use SVG::Parser to retain maximum flexibility as to which underlying parser is chosen. | ||||||||||||||||||||
| 49 | Use SVG::Parser::SAX to supply SAX-specific parser options or where the presence | ||||||||||||||||||||
| 50 | of a functional XML::SAX parser is known and/or preferred. | ||||||||||||||||||||
| 51 | |||||||||||||||||||||
| 52 | =head2 EXPORTS | ||||||||||||||||||||
| 53 | |||||||||||||||||||||
| 54 | None. However, a preferred SAX parser implementations can be specified by | ||||||||||||||||||||
| 55 | passing the package name to SVG::Parser::SAX in the import list. For example: | ||||||||||||||||||||
| 56 | |||||||||||||||||||||
| 57 | use SVG::Parser::SAX qw(XML::LibXML::SAX::Parser); | ||||||||||||||||||||
| 58 | |||||||||||||||||||||
| 59 | A minimum version number may be additionally suppied as a second import item: | ||||||||||||||||||||
| 60 | |||||||||||||||||||||
| 61 | use SVG::Parser::SAX (XML::LibXML::SAX::Parser => 1.40); | ||||||||||||||||||||
| 62 | |||||||||||||||||||||
| 63 | This overrides the automatic selection of a suitable SAX parser. To try several | ||||||||||||||||||||
| 64 | different parsers in turn, use the SVG::Parser module instead and restrict it | ||||||||||||||||||||
| 65 | to only try SAX-based parsers. To make use of the automatic selection mechanism, | ||||||||||||||||||||
| 66 | omit the import list. | ||||||||||||||||||||
| 67 | |||||||||||||||||||||
| 68 | When loaded via SVG::Parser, this parent class may be specified by placing it | ||||||||||||||||||||
| 69 | after the '=' in a parser specification: | ||||||||||||||||||||
| 70 | |||||||||||||||||||||
| 71 | See L<SVG::Parser> for more details. | ||||||||||||||||||||
| 72 | |||||||||||||||||||||
| 73 | =head2 EXAMPLES | ||||||||||||||||||||
| 74 | |||||||||||||||||||||
| 75 | See C<svgsaxparse> in the examples directory of the distribution. | ||||||||||||||||||||
| 76 | |||||||||||||||||||||
| 77 | =head1 AUTHOR | ||||||||||||||||||||
| 78 | |||||||||||||||||||||
| 79 | Peter Wainwright, peter.wainwright@cybrid.net | ||||||||||||||||||||
| 80 | |||||||||||||||||||||
| 81 | =head1 SEE ALSO | ||||||||||||||||||||
| 82 | |||||||||||||||||||||
| 83 | L<SVG>, L<SVG::Parser>, L<SVG::Parser::Expat>, L<XML::SAX> | ||||||||||||||||||||
| 84 | |||||||||||||||||||||
| 85 | =cut | ||||||||||||||||||||
| 86 | |||||||||||||||||||||
| 87 | #------------------------------------------------------------------------------- | ||||||||||||||||||||
| 88 | |||||||||||||||||||||
| 89 | 4 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: called CC=3 SCAR=11.4
Pod
undocumented
| |||||||||||||||||||||
| 90 | 4 | my $proto=shift; | |||||||||||||||||||
| 91 | 4 | ▶ | my $class=ref($proto) || $proto; | ||||||||||||||||||
|
Condition33%1/3
ref $proto || $proto
MC/DC0%0/2
ref $proto || $proto
ref $proto
$proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
| |||||||||||||||||||||
| 92 | 4 | my %attrs=@_; | |||||||||||||||||||
| 93 | |||||||||||||||||||||
| 94 | 4 | my $handler=SVG::Parser::SAX::Handler->new(%attrs); | |||||||||||||||||||
| 95 | 4 | my $self=XML::SAX::ParserFactory->parser( | |||||||||||||||||||
| 96 | Handler => $handler, | ||||||||||||||||||||
| 97 | ); | ||||||||||||||||||||
| 98 | |||||||||||||||||||||
| 99 | # first time through set up the @ISA for the package | ||||||||||||||||||||
| 100 | 4 | ▶ | @ISA=(ref($self),"SVG::Parser::Base") if @ISA==1; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 101 | |||||||||||||||||||||
| 102 | 4 | return bless $self,$class; | |||||||||||||||||||
| 103 | } | ||||||||||||||||||||
| 104 | |||||||||||||||||||||
| 105 | #------------------------------------------------------------------------------- | ||||||||||||||||||||
| 106 | # Import method to change default inheritance, if required | ||||||||||||||||||||
| 107 | |||||||||||||||||||||
| 108 | 2 | ▶ | sub import { | ||||||||||||||||||
|
Subroutine
import: called CC=5 SCAR=28.4
| |||||||||||||||||||||
| 109 | 2 | my $package=shift; | |||||||||||||||||||
| 110 | |||||||||||||||||||||
| 111 | # permit an alternative XML::SAX parser to be our parser | ||||||||||||||||||||
| 112 | 2 | ▶ | if (@_) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 113 | 0 | my ($superclass,$version)=@_; | |||||||||||||||||||
| 114 | |||||||||||||||||||||
| 115 | # select specific XML::SAX parser: 'pkg' or 'pkg => version' | ||||||||||||||||||||
| 116 | 0 | ▶ | if ($version) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 117 | 0 | ▶ | $XML::SAX::ParserPackage="$superclass ($version)" if eval qq[ | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 118 | use $superclass $version; | ||||||||||||||||||||
| 119 | 1; | ||||||||||||||||||||
| 120 | ]; | ||||||||||||||||||||
| 121 | } else { | ||||||||||||||||||||
| 122 | 0 | ▶ | $XML::SAX::ParserPackage=$superclass if eval qq[ | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 123 | use $superclass; | ||||||||||||||||||||
| 124 | 1; | ||||||||||||||||||||
| 125 | ]; | ||||||||||||||||||||
| 126 | } | ||||||||||||||||||||
| 127 | } | ||||||||||||||||||||
| 128 | } | ||||||||||||||||||||
| 129 | |||||||||||||||||||||
| 130 | #------------------------------------------------------------------------------- | ||||||||||||||||||||
| 131 | # Expat -> SAX compatability | ||||||||||||||||||||
| 132 | |||||||||||||||||||||
| 133 | # parsefile take a filename or a file handle, parse_file takes a file handle | ||||||||||||||||||||
| 134 | 3 | ▶ | sub parsefile { | ||||||||||||||||||
|
Subroutine
parsefile: called CC=5 SCAR=16.7
Pod
undocumented
| |||||||||||||||||||||
| 135 | 3 | my ($self,$source,@args)=@_; | |||||||||||||||||||
| 136 | |||||||||||||||||||||
| 137 | 3 | my $type=$self->identify($source); | |||||||||||||||||||
| 138 | 3 | ▶ | return "" if $type eq $self->ARG_IS_INVALID; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 139 | |||||||||||||||||||||
| 140 | 3 | ▶ | if ($type eq $self->ARG_IS_STRING) { | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 141 | 2 | local(*FH); | |||||||||||||||||||
| 142 | 2 | ▶ | open FH,$source or croak "Couldn't open $: $!"; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 143 | 2 | return $self->SUPER::parse_file(*FH,@_); | |||||||||||||||||||
| 144 | } elsif ($type eq $self->ARG_IS_HASHRF) { | ||||||||||||||||||||
| 145 | 0 | return $self->SUPER::parse({ %$source,@args }); | |||||||||||||||||||
| 146 | } else { | ||||||||||||||||||||
| 147 | 1 | return $self->SUPER::parse_file($source,@_); | |||||||||||||||||||
| 148 | } | ||||||||||||||||||||
| 149 | } | ||||||||||||||||||||
| 150 | |||||||||||||||||||||
| 151 | 6 | *parse_file=\&parsefile; | |||||||||||||||||||
| 152 | |||||||||||||||||||||
| 153 | #------------------------------------------------------------------------------- | ||||||||||||||||||||
| 154 | |||||||||||||||||||||
| 155 | 6 | 1; | |||||||||||||||||||