| 1 | package SVG::Parser::Expat; | ||
| 2 | use strict; | ||
| 3 | |||
| 4 | require 5.004; | ||
| 5 | |||
| 6 | use base qw(XML::Parser SVG::Parser::Base); | ||
| 7 | use SVG 2.0; | ||
| 8 | |||
| 9 | use vars qw($VERSION @ISA); | ||
| 10 | $VERSION="1.03"; | ||
| 11 | |||
| 12 | #--------------------------------------------------------------------------------------- | ||
| 13 | |||
| 14 | =head1 NAME | ||
| 15 | |||
| 16 | SVG::Parser::Expat - XML Expat Parser for SVG documents | ||
| 17 | |||
| 18 | =head1 SYNOPSIS | ||
| 19 | |||
| 20 | #!/usr/bin/perl -w | ||
| 21 | use strict; | ||
| 22 | use SVG::Parser::Expat; | ||
| 23 | |||
| 24 | die "Usage: $0 <file>\n" unless @ARGV; | ||
| 25 | |||
| 26 | my $xml; | ||
| 27 | { | ||
| 28 | local $/=undef; | ||
| 29 | $xml=<>; | ||
| 30 | } | ||
| 31 | |||
| 32 | my $parser=new SVG::Parser::Expat; | ||
| 33 | |||
| 34 | my $svg=$parser->parse($xml); | ||
| 35 | |||
| 36 | print $svg->xmlify; | ||
| 37 | |||
| 38 | =head1 DESCRIPTION | ||
| 39 | |||
| 40 | SVG::Parser::Expat is the Expat-specific parser module used by SVG::Parser when an | ||
| 41 | underlying XML::Parser-based parser is selected. It may also be used directly, as shown | ||
| 42 | in the synopsis above. | ||
| 43 | |||
| 44 | Use SVG::Parser to retain maximum flexibility as to which underlying parser is chosen. | ||
| 45 | Use SVG::Parser::Expat to supply Expat-specific parser options or where the presence | ||
| 46 | of XML::Parser is known and/or preferred. | ||
| 47 | |||
| 48 | =head2 EXPORTS | ||
| 49 | |||
| 50 | None. However, an alternative parent class (other than XML::Parser) can be specified by | ||
| 51 | passing the package name to SVG::Parser::Expat in the import list. For example: | ||
| 52 | |||
| 53 | use SVG::Parser::Expat qw(My::XML::Parser::Subclass); | ||
| 54 | |||
| 55 | Where My::XML::Parser::Subclass is a subclass like: | ||
| 56 | |||
| 57 | package My::XML::Parser::Subclass; | ||
| 58 | use strict; | ||
| 59 | use vars qw(@ISA); | ||
| 60 | use XML::Parser; | ||
| 61 | @ISA=qw(XML::Parser); | ||
| 62 | |||
| 63 | ...custom methods... | ||
| 64 | |||
| 65 | 1; | ||
| 66 | |||
| 67 | When loaded via SVG::Parser, this parent class may be specified by placing it after | ||
| 68 | the '=' in a parser specification: | ||
| 69 | |||
| 70 | use SVG::Parser qw(Expat=My::XML::Parser::Subclass); | ||
| 71 | |||
| 72 | See L<SVG::Parser> for more details. | ||
| 73 | |||
| 74 | =head2 EXAMPLES | ||
| 75 | |||
| 76 | See C<svgexpatparse> in the examples directory of the distribution. | ||
| 77 | |||
| 78 | =head1 AUTHOR | ||
| 79 | |||
| 80 | Peter Wainwright, peter.wainwright@cybrid.net | ||
| 81 | |||
| 82 | =head1 SEE ALSO | ||
| 83 | |||
| 84 | L<SVG>, L<SVG::Parser>, L<SVG::Parser::SAX>, L<XML::Parser> | ||
| 85 | |||
| 86 | =cut | ||
| 87 | |||
| 88 | #--------------------------------------------------------------------------------------- | ||
| 89 | # SVG::Parser::Expat constructor. Attributes with no minus prefix are passed to | ||
| 90 | # the parent parser class. Attributes with a minus are set locally. | ||
| 91 | |||
| 92 | sub new { | ||
| 93 | my $proto=shift; | ||
| 94 | my $class=ref($proto) || $proto; | ||
| 95 | my %attrs=@_; | ||
| 96 | |||
| 97 | # pass on non-minus-prefixed attributes to XML::Parser | ||
| 98 | my %parser_attrs; | ||
| 99 | foreach (keys %attrs) { | ||
| 100 | $parser_attrs{$_}=delete $attrs{$_} unless /^-/; | ||
| 101 | } | ||
| 102 | |||
| 103 | my $parser=$class->SUPER::new(%parser_attrs); | ||
| 104 | $parser->setHandlers( | ||
| 105 | XMLDecl => sub { XMLDecl($parser, @_) }, | ||
| 106 | Doctype => sub { Doctype($parser, @_) }, | ||
| 107 | Init => sub { StartDocument($parser, @_) }, | ||
| 108 | Final => sub { FinishDocument($parser, @_) }, | ||
| 109 | |||
| 110 | Start => sub { StartTag($parser, @_) }, | ||
| 111 | End => sub { EndTag($parser, @_) }, | ||
| 112 | Char => sub { Text($parser, @_) }, | ||
| 113 | CdataStart => sub { $parser->SVG::Parser::Base::CdataStart(@_) }, | ||
| 114 | CdataEnd => sub { $parser->SVG::Parser::Base::CdataEnd(@_) }, | ||
| 115 | Proc => sub { PI($parser, @_) }, | ||
| 116 | Comment => sub { Comment($parser, @_) }, | ||
| 117 | |||
| 118 | Unparsed => sub { Unparsed($parser, @_) }, | ||
| 119 | Notation => sub { Notation($parser,@_) }, | ||
| 120 | Entity => sub { Entity($parser, @_) }, | ||
| 121 | Element => sub { Element($parser,@_) }, | ||
| 122 | Attlist => sub { Attlist($parser,@_) }, | ||
| 123 | ); | ||
| 124 | |||
| 125 | # minus-prefixed attributes stay here, double-minus to SVG object | ||
| 126 | foreach (keys %attrs) { | ||
| 127 | if (/^-(-.+)$/) { | ||
| 128 | $parser->{__svg_attr}{$1}=$attrs{$_}; | ||
| 129 | } else { | ||
| 130 | $parser->{$_}=$attrs{$_}; | ||
| 131 | } | ||
| 132 | } | ||
| 133 | |||
| 134 | return $parser; | ||
| 135 | } | ||
| 136 | |||
| 137 | #--------------------------------------------------------------------------------------- | ||
| 138 | # Import method to change default inheritance, if required | ||
| 139 | |||
| 140 | sub import { | ||
| 141 | my $package=shift; | ||
| 142 | |||
| 143 | # permit an alternative XML::Parser subclass to be our parent class | ||
| 144 | if (@_) { | ||
| 145 | my $superclass=shift; | ||
| 146 | |||
| 147 | $ISA[0]=$superclass,return if eval qq[ | ||
| 148 | use $superclass qw(@_); | ||
| 149 | 1; | ||
| 150 | ]; | ||
| 151 | |||
| 152 | die "Parent parser class $superclass not found: $@\n"; | ||
| 153 | } | ||
| 154 | } | ||
| 155 | |||
| 156 | #--------------------------------------------------------------------------------------- | ||
| 157 | # Handlers | ||
| 158 | |||
| 159 | # create and set SVG document object as root element | ||
| 160 | sub StartDocument { | ||
| 161 | my ($parser,$expat)=@_; | ||
| 162 | return $parser->SVG::Parser::Base::StartDocument(); | ||
| 163 | } | ||
| 164 | |||
| 165 | # handle start of element - extend chain by one | ||
| 166 | sub StartTag { | ||
| 167 | my ($parser,$expat,$type,%attrs)=@_; | ||
| 168 | return $parser->SVG::Parser::Base::StartTag($type,%attrs); | ||
| 169 | } | ||
| 170 | |||
| 171 | # handle end of element - shorten chain by one | ||
| 172 | sub EndTag { | ||
| 173 | my ($parser,$expat,$type)=@_; | ||
| 174 | return $parser->SVG::Parser::Base::EndTag($type); | ||
| 175 | } | ||
| 176 | |||
| 177 | # handle cannonical data (text) | ||
| 178 | sub Text { | ||
| 179 | my ($parser,$expat,$text)=@_; | ||
| 180 | return $parser->SVG::Parser::Base::Text($text); | ||
| 181 | } | ||
| 182 | |||
| 183 | # handle processing instructions | ||
| 184 | sub PI { | ||
| 185 | my ($parser,$expat,$target,$data)=@_; | ||
| 186 | return $parser->SVG::Parser::Base::PI($target,$data); | ||
| 187 | } | ||
| 188 | |||
| 189 | # handle XML Comments | ||
| 190 | sub Comment { | ||
| 191 | my ($parser,$expat,$data)=@_; | ||
| 192 | return $parser->SVG::Parser::Base::Comment($data); | ||
| 193 | } | ||
| 194 | |||
| 195 | # return root SVG document object as result of parse() | ||
| 196 | sub FinishDocument { | ||
| 197 | my ($parser,$expat)=@_; | ||
| 198 | return $parser->SVG::Parser::Base::FinishDocument(); | ||
| 199 | } | ||
| 200 | |||
| 201 | #--------------------------------------------------------------------------------------- | ||
| 202 | |||
| 203 | # handle XML declaration, if present | ||
| 204 | sub XMLDecl { | ||
| 205 | my ($parser,$expat,$version,$encoding,$standalone)=@_; | ||
| 206 | return $parser->SVG::Parser::Base::XMLDecl($version,$encoding,$standalone); | ||
| 207 | } | ||
| 208 | |||
| 209 | # handle Doctype declaration, if present | ||
| 210 | sub Doctype { | ||
| 211 | my ($parser,$expat,$name,$sysid,$pubid,$internal)=@_; | ||
| 212 | return $parser->SVG::Parser::Base::Doctype($name,$sysid,$pubid,$internal); | ||
| 213 | } | ||
| 214 | |||
| 215 | #--------------------------------------------------------------------------------------- | ||
| 216 | |||
| 217 | # Unparsed (Expat, Entity, Base, Sysid, Pubid, Notation) | ||
| 218 | sub Unparsed { | ||
| 219 | my ($parser,$expat,$name,$base,$sysid,$pubid,$notation)=@_; | ||
| 220 | return $parser->SVG::Parser::Base::Unparsed($name,$base,$sysid,$pubid,$notation); | ||
| 221 | } | ||
| 222 | |||
| 223 | # Notation (Expat, Notation, Base, Sysid, Pubid) | ||
| 224 | sub Notation { | ||
| 225 | my ($parser,$expat,$name,$base,$sysid,$pubid)=@_; | ||
| 226 | return $parser->SVG::Parser::Base::Notation($name,$base,$sysid,$pubid); | ||
| 227 | } | ||
| 228 | |||
| 229 | # Entity (Expat, Name, Val, Sysid, Pubid, Ndata, IsParam) | ||
| 230 | sub Entity { | ||
| 231 | my ($parser,$expat,$name,$val,$sysid,$pubid,$data,$isp)=@_; | ||
| 232 | return $parser->SVG::Parser::Base::Entity($name,$val,$sysid,$pubid,$data,$isp); | ||
| 233 | } | ||
| 234 | |||
| 235 | # Element (Expat, Name, Model) | ||
| 236 | sub Element { | ||
| 237 | my ($parser,$expat,$name,$model)=@_; | ||
| 238 | return $parser->SVG::Parser::Base::Element($name,$model); | ||
| 239 | } | ||
| 240 | |||
| 241 | # Attlist (Expat, Elname, Attname, Type, Default, Fixed) | ||
| 242 | sub Attlist { | ||
| 243 | my ($parser,$expat,$name,$attr,$type,$default,$fixed)=@_; | ||
| 244 | return $parser->SVG::Parser::Base::Attlist($name,$attr,$type,$default,$fixed); | ||
| 245 | } | ||
| 246 | |||
| 247 | #--------------------------------------------------------------------------------------- | ||
| 248 | # SAX -> Expat compatability | ||
| 249 | |||
| 250 | sub parse_file { | ||
| 251 | shift->parsefile(@_); | ||
| 252 | } | ||
| 253 | |||
| 254 | #--------------------------------------------------------------------------------------- | ||
| 255 | |||
| 256 | 1; |