| 1 | package SVG::Parser; | ||||||||||||||
| 2 | 4 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 3 | |||||||||||||||
| 4 | 4 | require 5.004; | |||||||||||||
| 5 | |||||||||||||||
| 6 | 4 | ▶ | use vars qw(@ISA $VERSION); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 7 | |||||||||||||||
| 8 | 4 | $VERSION="1.03"; | |||||||||||||
| 9 | |||||||||||||||
| 10 | #------------------------------------------------------------------------------- | ||||||||||||||
| 11 | # Pick a parser, any parser... | ||||||||||||||
| 12 | |||||||||||||||
| 13 | # Override this list in import to force a different choosing order | ||||||||||||||
| 14 | 4 | ▶ | @ISA=qw(SVG::Parser::SAX SVG::Parser::Expat) unless @ISA; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 15 | #@ISA=qw(SVG::Parser::Expat SVG::Parser::SAX) unless @ISA; | ||||||||||||||
| 16 | |||||||||||||||
| 17 | #------------------------------------------------------------------------------- | ||||||||||||||
| 18 | # Import method to change default inheritance, if required | ||||||||||||||
| 19 | |||||||||||||||
| 20 | 4 | ▶ | sub import { | ||||||||||||
|
Subroutine
import: called CC=7 SCAR=20.7
| |||||||||||||||
| 21 | 4 | my $package=shift; | |||||||||||||
| 22 | |||||||||||||||
| 23 | 4 | ▶ | @ISA=@_ if @_; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 24 | 4 | my @classes; | |||||||||||||
| 25 | |||||||||||||||
| 26 | 4 | foreach my $superclassspec (@ISA) { | |||||||||||||
| 27 | # extract parameters to pass to superclass import method if present | ||||||||||||||
| 28 | 4 | my ($superclass,$importlist)=split /=/,$superclassspec,2; | |||||||||||||
| 29 | 4 | ▶ | my @importlist=$importlist ? (split '=',$importlist) : (); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 30 | |||||||||||||||
| 31 | # shorthand shortcuts | ||||||||||||||
| 32 | 4 | ▶ | $superclass="SVG::Parser::SAX" if $superclass eq 'SAX'; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 33 | 4 | ▶ | $superclass="SVG::Parser::Expat" if $superclass eq 'Expat'; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 34 | |||||||||||||||
| 35 | # test each superclass specifier in turn | ||||||||||||||
| 36 | 4 | ▶ | if (eval qq[use $superclass qw(@importlist); 1;]) { | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Branch: 1 branch
| |||||||||||||||
| 37 | 4 | @ISA = ($superclass); | |||||||||||||
| 38 | 4 | return; | |||||||||||||
| 39 | } | ||||||||||||||
| 40 | |||||||||||||||
| 41 | 0 | push @classes,"$superclass qw(",(join " ",@importlist),")"; | |||||||||||||
| 42 | } | ||||||||||||||
| 43 | |||||||||||||||
| 44 | 0 | die "No XML parser found (searched for ",(join ",",@classes),")\n"; | |||||||||||||
| 45 | } | ||||||||||||||
| 46 | |||||||||||||||
| 47 | #------------------------------------------------------------------------------- | ||||||||||||||
| 48 | # Allow basic calls to 'parse' to handle strings and handles equally well for | ||||||||||||||
| 49 | # either parser. More complex API usage will not work. | ||||||||||||||
| 50 | |||||||||||||||
| 51 | 3 | ▶ | sub parse { | ||||||||||||
|
Subroutine
parse: called CC=6 SCAR=19.7
Pod
documented
| |||||||||||||||
| 52 | 3 | my ($self,$source,%args)=@_; | |||||||||||||
| 53 | |||||||||||||||
| 54 | 3 | ▶ | my $type=$self->identify($source) or return ""; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 55 | |||||||||||||||
| 56 | 3 | ▶ | if ($type eq $self->ARG_IS_HANDLE) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 57 | # both parse()rs will accept a handle | ||||||||||||||
| 58 | 0 | return $self->SUPER::parse($source,%args); | |||||||||||||
| 59 | } elsif ($type eq $self->ARG_IS_STRING) { | ||||||||||||||
| 60 | # the API for strings, however, differs | ||||||||||||||
| 61 | 1 | ▶ | if ($self->isa("SVG::Parser::SAX")) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 62 | 1 | my %parser_options=( Source => {String => $source} ); | |||||||||||||
| 63 | 1 | return $self->SUPER::parse(%parser_options,%args); | |||||||||||||
| 64 | } else { | ||||||||||||||
| 65 | 0 | return $self->SUPER::parse($source,%args); | |||||||||||||
| 66 | } | ||||||||||||||
| 67 | } else { | ||||||||||||||
| 68 | # a hash reference only makes sense to the SAX parser | ||||||||||||||
| 69 | |||||||||||||||
| 70 | 2 | ▶ | if ($self->isa("SVG::Parser::SAX")) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 71 | # combine extra hash values if present | ||||||||||||||
| 72 | 2 | $source={ %$source, %args }; | |||||||||||||
| 73 | 2 | return $self->SUPER::parse($source); | |||||||||||||
| 74 | } else { | ||||||||||||||
| 75 | # the source is unknown | ||||||||||||||
| 76 | 0 | die "Invalid argument $source to SVG::Parser in Expat mode"; | |||||||||||||
| 77 | } | ||||||||||||||
| 78 | } | ||||||||||||||
| 79 | } | ||||||||||||||
| 80 | |||||||||||||||
| 81 | #------------------------------------------------------------------------------- | ||||||||||||||
| 82 | |||||||||||||||
| 83 | =head1 NAME | ||||||||||||||
| 84 | |||||||||||||||
| 85 | SVG::Parser - XML Parser for SVG documents | ||||||||||||||
| 86 | |||||||||||||||
| 87 | =head1 SYNOPSIS | ||||||||||||||
| 88 | |||||||||||||||
| 89 | #!/usr/bin/perl -w | ||||||||||||||
| 90 | use strict; | ||||||||||||||
| 91 | use SVG::Parser; | ||||||||||||||
| 92 | |||||||||||||||
| 93 | die "Usage: $0 <file>\n" unless @ARGV; | ||||||||||||||
| 94 | |||||||||||||||
| 95 | my $xml; | ||||||||||||||
| 96 | { | ||||||||||||||
| 97 | local $/=undef; | ||||||||||||||
| 98 | $xml=<>; | ||||||||||||||
| 99 | } | ||||||||||||||
| 100 | |||||||||||||||
| 101 | my $parser=new SVG::Parser(-debug => 1); | ||||||||||||||
| 102 | my $svg=$parser->parse($xml); | ||||||||||||||
| 103 | print $svg->xmlify; | ||||||||||||||
| 104 | |||||||||||||||
| 105 | and: | ||||||||||||||
| 106 | |||||||||||||||
| 107 | #!/usr/bin/perl -w | ||||||||||||||
| 108 | use strict; | ||||||||||||||
| 109 | use SVG::Parser qw(SAX=XML::LibXML::Parser::SAX Expat SAX); | ||||||||||||||
| 110 | |||||||||||||||
| 111 | die "Usage: $0 <file>\n" unless @ARGV; | ||||||||||||||
| 112 | my $svg=SVG::Parser->new()->parsefile($ARGV[0]); | ||||||||||||||
| 113 | print $svg->xmlify; | ||||||||||||||
| 114 | |||||||||||||||
| 115 | |||||||||||||||
| 116 | |||||||||||||||
| 117 | =head1 DESCRIPTION | ||||||||||||||
| 118 | |||||||||||||||
| 119 | SVG::Parser is an XML parser for SVG Documents. It takes XML as input and | ||||||||||||||
| 120 | produces an SVG object as its output. | ||||||||||||||
| 121 | |||||||||||||||
| 122 | SVG::Parser supports both XML::SAX and XML::Parser (Expat) parsers, with SAX | ||||||||||||||
| 123 | preferred by default. Only one of these needs to be installed for SVG::Parser | ||||||||||||||
| 124 | to function. | ||||||||||||||
| 125 | |||||||||||||||
| 126 | A list of preferred parsers may be specified in the import list - SVG::Parser | ||||||||||||||
| 127 | will use the first parser that successfully loads. Some basic measures are taken | ||||||||||||||
| 128 | to provide cross-compatability. Applications requiring more advanced parser | ||||||||||||||
| 129 | features should use the relevant parser module directly; see | ||||||||||||||
| 130 | L<SVG::Parser::Expat> and L<SVG::Parser::SAX>. | ||||||||||||||
| 131 | |||||||||||||||
| 132 | =head2 METHODS | ||||||||||||||
| 133 | |||||||||||||||
| 134 | SVG::Parser provides all methods supported by its parent parser class. In | ||||||||||||||
| 135 | addition it provides the following: | ||||||||||||||
| 136 | |||||||||||||||
| 137 | =over 4 | ||||||||||||||
| 138 | |||||||||||||||
| 139 | =item * new([%attrs]) | ||||||||||||||
| 140 | |||||||||||||||
| 141 | Create a new SVG::Parser object. Optional attributes may be passed as arguments; | ||||||||||||||
| 142 | all attributes without a leading '-' prefix are passed to the parent | ||||||||||||||
| 143 | constructor. For example: | ||||||||||||||
| 144 | |||||||||||||||
| 145 | my $parser=new SVG::Parser(%parser_options); | ||||||||||||||
| 146 | |||||||||||||||
| 147 | Note that parser options are dependant on which parser type is selected. | ||||||||||||||
| 148 | |||||||||||||||
| 149 | Attributes with a leading '-' are processed by SVG::Parser. Currently the only | ||||||||||||||
| 150 | recognised attribute is '-debug', which generates a simple but possibly useful | ||||||||||||||
| 151 | debug trace of the parsing process to standard error. For example: | ||||||||||||||
| 152 | |||||||||||||||
| 153 | my $parser=new SVG::Parser(-debug => 1); | ||||||||||||||
| 154 | |||||||||||||||
| 155 | or: | ||||||||||||||
| 156 | |||||||||||||||
| 157 | my $parser=SVG::Parser->new(-debug => 1); | ||||||||||||||
| 158 | |||||||||||||||
| 159 | Attributes with a leading '--' are passed to the SVG constructor when creating | ||||||||||||||
| 160 | the SVG object returned as the result of the parse: | ||||||||||||||
| 161 | |||||||||||||||
| 162 | my $parser=new SVG::Parser( | ||||||||||||||
| 163 | -debug => 1, | ||||||||||||||
| 164 | "--indent" => "\t", | ||||||||||||||
| 165 | "--raiseerror" => 1 | ||||||||||||||
| 166 | ); | ||||||||||||||
| 167 | |||||||||||||||
| 168 | The leading '-' is stripped from attribute names passed this way, so this sets | ||||||||||||||
| 169 | the '-indent' and '-raiseerror' attributes in the SVG module. See L<SVG> for a | ||||||||||||||
| 170 | list of valid SVG options. | ||||||||||||||
| 171 | |||||||||||||||
| 172 | (The C<new> constructor is provided by XML::SAX::Expat or SVG::Parser::SAX, | ||||||||||||||
| 173 | but operates identically in either case.) | ||||||||||||||
| 174 | |||||||||||||||
| 175 | =item * parse($xml) | ||||||||||||||
| 176 | |||||||||||||||
| 177 | Parse an XML document and return an SVG object which may then be used to | ||||||||||||||
| 178 | manipulate the SVG content before regenerating the output XML. For example: | ||||||||||||||
| 179 | |||||||||||||||
| 180 | my $svg=$parser->parse($svgxml); | ||||||||||||||
| 181 | |||||||||||||||
| 182 | Because the parse() method differs in use beteen XML::Parser and XML::SAX, | ||||||||||||||
| 183 | SVG::Parser provides its own parse() method. This calls the parent parser with | ||||||||||||||
| 184 | the correct first argument when given either a filehandle or a string as input. | ||||||||||||||
| 185 | |||||||||||||||
| 186 | Additional arguments are passed to the parent parser class, but since | ||||||||||||||
| 187 | XML::Parser and XML::SAX parsers take options in different formats this is | ||||||||||||||
| 188 | of limited use. SVG::Parser does not currently provide any translation of | ||||||||||||||
| 189 | parser options. | ||||||||||||||
| 190 | |||||||||||||||
| 191 | See L<XML::Parser>, L<XML::SAX>, and L<XML::Parser::PerlSAX> for other ways to | ||||||||||||||
| 192 | parse input XML. | ||||||||||||||
| 193 | |||||||||||||||
| 194 | =item * parse_file($filehandle|$filename) | ||||||||||||||
| 195 | |||||||||||||||
| 196 | =item * parsefile($filehandle|$filename) | ||||||||||||||
| 197 | |||||||||||||||
| 198 | Since the parse_file() method (XML::SAX) and parsefile() method (XML::Parser) | ||||||||||||||
| 199 | differ in both name and usage, SVG::Parser provides its own version of both | ||||||||||||||
| 200 | methods that determines whether the passed argument is a filehandle or | ||||||||||||||
| 201 | a file name and directs the call to the appropriate parent parser method. | ||||||||||||||
| 202 | |||||||||||||||
| 203 | Both methods will work equally well whichever parent parser class is in use: | ||||||||||||||
| 204 | |||||||||||||||
| 205 | my $svg=$parser->parse_file($svgxml); | ||||||||||||||
| 206 | my $svg=$parser->parsefile($svgxml); | ||||||||||||||
| 207 | my $svg=$parser->parse_file(*SVGIN); | ||||||||||||||
| 208 | my $svg=$parser->parsefile(*SVGIN); | ||||||||||||||
| 209 | ...etc... | ||||||||||||||
| 210 | |||||||||||||||
| 211 | (These methods will also work when using SVG::Parser::Expat or SVG::Parser::SAX | ||||||||||||||
| 212 | directly.) | ||||||||||||||
| 213 | |||||||||||||||
| 214 | =back | ||||||||||||||
| 215 | |||||||||||||||
| 216 | =head2 EXPORTS | ||||||||||||||
| 217 | |||||||||||||||
| 218 | None. However, a list of preferred parsers can be specified by passing the | ||||||||||||||
| 219 | package name to SVG::Parser in the import list. This allows an SVG parser | ||||||||||||||
| 220 | application to use the best parser available without knowing what XML parsers | ||||||||||||||
| 221 | might be available on the target platform. SAX is generally preferred to Expat, | ||||||||||||||
| 222 | but an Expat-based parser may be preferable to the slow Perl-based SAX | ||||||||||||||
| 223 | parser XML::SAX::PurePerl. (See L<XML::SAX::PurePerl>.) | ||||||||||||||
| 224 | |||||||||||||||
| 225 | Each parser specification consists of one of the two supported SVG parsers, | ||||||||||||||
| 226 | SVG::Parser::Expat or SVG::Parser::SAX, optionally followed by an '=' and an | ||||||||||||||
| 227 | explicit parser package. For example: | ||||||||||||||
| 228 | |||||||||||||||
| 229 | use SVG::Parser qw(SVG::Parser::SAX SVG::Parser::Expat); | ||||||||||||||
| 230 | |||||||||||||||
| 231 | Instead of specifying the full SVG parser name, 'Expat' and 'SAX' may be used as | ||||||||||||||
| 232 | shorthand. For example: | ||||||||||||||
| 233 | |||||||||||||||
| 234 | use SVG::Parser qw(SAX Expat); | ||||||||||||||
| 235 | |||||||||||||||
| 236 | Both the above examples produce the default behaviour. To prefer Expat over SAX | ||||||||||||||
| 237 | use either of: | ||||||||||||||
| 238 | |||||||||||||||
| 239 | use SVG::Parser qw(SVG::Parser::Expat SVG::Parser::SAX); | ||||||||||||||
| 240 | use SVG::Parser qw(Expat SAX); | ||||||||||||||
| 241 | |||||||||||||||
| 242 | To use Expat with a specific XML::Parser subclass: | ||||||||||||||
| 243 | |||||||||||||||
| 244 | use SVG::Parser qw(SVG::Parser::Expat=My::XML::Parser::Subclass); | ||||||||||||||
| 245 | |||||||||||||||
| 246 | To use SAX with the XML::LibXML SAX parser: | ||||||||||||||
| 247 | |||||||||||||||
| 248 | use SVG::Parser qw(SVG::Parser::SAX=XML::LibXML::SAX::Parser); | ||||||||||||||
| 249 | |||||||||||||||
| 250 | A number of specifications can be listed to have SVG::Parse try a number of | ||||||||||||||
| 251 | possible parser alternatives in decreasing order of preference: | ||||||||||||||
| 252 | |||||||||||||||
| 253 | use SVG::Parser qw( | ||||||||||||||
| 254 | SAX=My::SAXParser | ||||||||||||||
| 255 | Expat=My::Best::ExpatParser | ||||||||||||||
| 256 | SAX=XML::LibXML::SAX::Parser | ||||||||||||||
| 257 | Expat=My::Other::ExpatParser | ||||||||||||||
| 258 | Expat | ||||||||||||||
| 259 | SAX | ||||||||||||||
| 260 | ) | ||||||||||||||
| 261 | |||||||||||||||
| 262 | You can test different scenarios from the command line. For example: | ||||||||||||||
| 263 | |||||||||||||||
| 264 | perl -MSVG::Parser=SAX mysvgapp.pl | ||||||||||||||
| 265 | perl -MSVG::Parser=Expat,SAX mysvgapp.pl | ||||||||||||||
| 266 | perl -MSVG::Parser=SAX=XML::LibXML::SAX::Parser,Expat mysvgapp.pl | ||||||||||||||
| 267 | |||||||||||||||
| 268 | To pass additional items in the import list to the parent Expat or SAX parser | ||||||||||||||
| 269 | class, use additional '=' separators in the parser specification. In the case | ||||||||||||||
| 270 | of XML::SAX a minimum version number may be required this way: | ||||||||||||||
| 271 | |||||||||||||||
| 272 | # require version 1.40+ of the LibXML SAX parser, otherwise use Perl | ||||||||||||||
| 273 | use SVG::Parser qw( | ||||||||||||||
| 274 | SAX=XML::LibXML::SAX::Parser=1.40 | ||||||||||||||
| 275 | SAX=XML::SAX::PurePerl | ||||||||||||||
| 276 | ); | ||||||||||||||
| 277 | |||||||||||||||
| 278 | Similarly, from the command line: | ||||||||||||||
| 279 | |||||||||||||||
| 280 | perl -MSVG::Parser=SAX=XML::LibXML::SAX::Parser=1.40,SAX=XML::SAX::PurePerl mysvgapp.pl | ||||||||||||||
| 281 | |||||||||||||||
| 282 | =head2 EXAMPLES | ||||||||||||||
| 283 | |||||||||||||||
| 284 | See C<svgparse>, C<svgparse2>, and C<svgparse3> in the examples directory of the | ||||||||||||||
| 285 | distribution, along with C<svgexpatparse> and C<svgsaxparse> for examples of using | ||||||||||||||
| 286 | the SVG::Parser::Expat and SVG::Parser::SAX modules directly. | ||||||||||||||
| 287 | |||||||||||||||
| 288 | =head1 AUTHOR | ||||||||||||||
| 289 | |||||||||||||||
| 290 | Peter Wainwright, peter.wainwright@cybrid.net | ||||||||||||||
| 291 | |||||||||||||||
| 292 | =head1 SEE ALSO | ||||||||||||||
| 293 | |||||||||||||||
| 294 | L<SVG>, L<SVG::Parser::Expat>, L<SVG::Parser::SAX>, L<XML::Parser>, L<XML::SAX> | ||||||||||||||
| 295 | |||||||||||||||
| 296 | =cut | ||||||||||||||
| 297 | |||||||||||||||
| 298 | 4 | 1; | |||||||||||||