File Coverage

blib/lib/SVG/Parser.pm
Criterion Covered Total %
statement 29 34 85.2
branch 11 20 55.0
condition n/a
subroutine 5 5 100.0
pod 1 1 100.0
total 46 60 76.6


line stmt bran cond sub pod time code
1             package SVG::Parser;
2 4     4   27692 use strict;
  4         48  
  4         182  
3              
4             require 5.004;
5              
6 4     4   21 use vars qw(@ISA $VERSION);
  4         7  
  4         2160  
7              
8             $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             @ISA=qw(SVG::Parser::SAX SVG::Parser::Expat) unless @ISA;
15             #@ISA=qw(SVG::Parser::Expat SVG::Parser::SAX) unless @ISA;
16              
17             #-------------------------------------------------------------------------------
18             # Import method to change default inheritance, if required
19              
20             sub import {
21 4     4   27 my $package=shift;
22              
23 4 50       19 @ISA=@_ if @_;
24 4         21 my @classes;
25              
26 4         9 foreach my $superclassspec (@ISA) {
27             # extract parameters to pass to superclass import method if present
28 4         18 my ($superclass,$importlist)=split /=/,$superclassspec,2;
29 4 50       17 my @importlist=$importlist ? (split '=',$importlist) : ();
30              
31             # shorthand shortcuts
32 4 50       22 $superclass="SVG::Parser::SAX" if $superclass eq 'SAX';
33 4 50       12 $superclass="SVG::Parser::Expat" if $superclass eq 'Expat';
34              
35             # test each superclass specifier in turn
36 4 50   4   2381 if (eval qq[use $superclass qw(@importlist); 1;]) {
  4         13  
  4         43  
  4         239  
37 4         75 @ISA = ($superclass);
38 4         8036 return;
39             }
40              
41 0         0 push @classes,"$superclass qw(",(join " ",@importlist),")";
42             }
43              
44 0         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             sub parse {
52 3     3 1 482 my ($self,$source,%args)=@_;
53              
54 3 50       23 my $type=$self->identify($source) or return "";
55              
56 3 50       58 if ($type eq $self->ARG_IS_HANDLE) {
    100          
57             # both parse()rs will accept a handle
58 0         0 return $self->SUPER::parse($source,%args);
59             } elsif ($type eq $self->ARG_IS_STRING) {
60             # the API for strings, however, differs
61 1 50       14 if ($self->isa("SVG::Parser::SAX")) {
62 1         5 my %parser_options=( Source => {String => $source} );
63 1         10 return $self->SUPER::parse(%parser_options,%args);
64             } else {
65 0         0 return $self->SUPER::parse($source,%args);
66             }
67             } else {
68             # a hash reference only makes sense to the SAX parser
69              
70 2 50       28 if ($self->isa("SVG::Parser::SAX")) {
71             # combine extra hash values if present
72 2         15 $source={ %$source, %args };
73 2         23 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 \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 \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 and L.
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 for a
170             list of valid SVG options.
171              
172             (The C 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, L, and L 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.)
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, C, and C in the examples directory of the
285             distribution, along with C and C 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, L, L, L, L
295              
296             =cut
297              
298             1;