1 # $Id: SAX.pm,v 1.31 2008-08-05 12:36:24 grant Exp $
6 use vars qw($VERSION @ISA @EXPORT_OK);
13 @EXPORT_OK = qw(Namespaces Validation);
15 use File::Basename qw(dirname);
17 use Symbol qw(gensym);
18 use XML::SAX::ParserFactory (); # loaded for simplicity
20 use constant PARSER_DETAILS => "ParserDetails.ini";
22 use constant Namespaces => "http://xml.org/sax/features/namespaces";
23 use constant Validation => "http://xml.org/sax/features/validation";
25 my $known_parsers = undef;
27 # load_parsers takes the ParserDetails.ini file out of the same directory
28 # that XML::SAX is in, and looks at it. Format in POD below
33 http://xml.org/sax/features/namespaces = 1
34 http://xml.org/sax/features/validation = 0
39 [XML::SAX::AnotherParser]
40 http://xml.org/sax/features/namespaces = 0
41 http://xml.org/sax/features/validation = 1
54 # get directory from wherever XML::SAX is installed
56 $dir = $INC{'XML/SAX.pm'};
61 if (!open($fh, File::Spec->catfile($dir, "SAX", PARSER_DETAILS))) {
62 XML::SAX->do_warn("could not find " . PARSER_DETAILS . " in $dir/SAX\n");
66 $known_parsers = $class->_parse_ini_file($fh);
78 while (defined(my $line = <$fh>)) {
85 $line =~ s/[#;].*$//m;
87 next if $line =~ /^$/m;
90 if ($line =~ /^\[\s*(.*)\s*\]$/m) {
91 push @config, { Name => $1 };
96 elsif ($line =~ /^(.*?)\s*?=\s*(.*)$/) {
98 push @config, { Name => '' };
100 $config[-1]{Features}{$1} = $2;
103 # not whitespace, comment, or instruction
105 die "Invalid line in ini: $lineno\n>>> $original\n";
114 if (!$known_parsers) {
115 $class->load_parsers();
117 return $known_parsers;
122 my ($parser_module) = @_;
124 if (!$known_parsers) {
125 $class->load_parsers();
128 @$known_parsers = grep { $_->{Name} ne $parser_module } @$known_parsers;
135 my ($parser_module) = @_;
137 if (!$known_parsers) {
138 $class->load_parsers();
141 # first load module, then query features, then push onto known_parsers,
143 my $parser_file = $parser_module;
144 $parser_file =~ s/::/\//g;
145 $parser_file .= ".pm";
147 require $parser_file;
149 my @features = $parser_module->supported_features();
151 my $new = { Name => $parser_module };
152 foreach my $feature (@features) {
153 $new->{Features}{$feature} = 1;
156 # If exists in list already, move to end.
159 for (my $i = 0; $i < @$known_parsers; $i++) {
160 my $p = $known_parsers->[$i];
161 if ($p->{Name} eq $parser_module) {
166 splice(@$known_parsers, $pos, 1);
167 push @$known_parsers, $new;
171 # Otherwise (not in list), add at end of list.
173 push @$known_parsers, $new;
182 # get directory from wherever XML::SAX is installed
183 my $dir = $INC{'XML/SAX.pm'};
184 $dir = dirname($dir);
186 my $file = File::Spec->catfile($dir, "SAX", PARSER_DETAILS);
191 open($fh, ">$file") ||
192 die "Cannot write to $file: $!";
194 foreach my $p (@$known_parsers) {
195 print $fh "[$p->{Name}]\n";
196 foreach my $key (keys %{$p->{Features}}) {
197 print $fh "$key = $p->{Features}{$key}\n";
211 # Don't output warnings if running under Test::Harness
212 warn(@_) unless $ENV{HARNESS_ACTIVE};
220 XML::SAX - Simple API for XML
226 # get a list of known parsers
227 my $parsers = XML::SAX->parsers();
229 # add/update a parser
230 XML::SAX->add_parser(q(XML::SAX::PurePerl));
233 XML::SAX->remove_parser(q(XML::SAX::Foodelberry));
236 XML::SAX->save_parsers();
240 XML::SAX is a SAX parser access API for Perl. It includes classes
241 and APIs required for implementing SAX drivers, along with a factory
242 class for returning any SAX parser installed on the user's system.
244 =head1 USING A SAX2 PARSER
246 The factory class is XML::SAX::ParserFactory. Please see the
247 documentation of that module for how to instantiate a SAX parser:
248 L<XML::SAX::ParserFactory>. However if you don't want to load up
249 another manual page, here's a short synopsis:
251 use XML::SAX::ParserFactory;
252 use XML::SAX::XYZHandler;
253 my $handler = XML::SAX::XYZHandler->new();
254 my $p = XML::SAX::ParserFactory->parser(Handler => $handler);
255 $p->parse_uri("foo.xml");
256 # or $p->parse_string("<foo/>") or $p->parse_file($fh);
258 This will automatically load a SAX2 parser (defaulting to
259 XML::SAX::PurePerl if no others are found) and return it to you.
261 In order to learn how to use SAX to parse XML, you will need to read
262 L<XML::SAX::Intro> and for reference, L<XML::SAX::Specification>.
264 =head1 WRITING A SAX2 PARSER
266 The first thing to remember in writing a SAX2 parser is to subclass
267 XML::SAX::Base. This will make your life infinitely easier, by providing
268 a number of methods automagically for you. See L<XML::SAX::Base> for more
271 When writing a SAX2 parser that is compatible with XML::SAX, you need
272 to inform XML::SAX of the presence of that driver when you install it.
273 In order to do that, XML::SAX contains methods for saving the fact that
274 the parser exists on your system to a "INI" file, which is then loaded
275 to determine which parsers are installed.
277 The best way to do this is to follow these rules:
281 =item * Add XML::SAX as a prerequisite in Makefile.PL:
285 PREREQ_PM => { 'XML::SAX' => 0 },
289 Alternatively you may wish to check for it in other ways that will
290 cause more than just a warning.
292 =item * Add the following code snippet to your Makefile.PL:
296 my $script = shift->SUPER::install(@_);
297 if (ExtUtils::MakeMaker::prompt(
298 "Do you want to modify ParserDetails.ini?", 'Y')
300 $script =~ s/install :: (.*)$/install :: $1 install_sax_driver/m;
301 $script .= <<"INSTALL";
304 \t\@\$(PERL) -MXML::SAX -e "XML::SAX->add_parser(q(\$(NAME)))->save_parsers()"
311 Note that you should check the output of this - \$(NAME) will use the name of
312 your distribution, which may not be exactly what you want. For example XML::LibXML
313 has a driver called XML::LibXML::SAX::Generator, which is used in place of
314 \$(NAME) in the above.
316 =item * Add an XML::SAX test:
318 A test file should be added to your t/ directory containing something like the
322 BEGIN { plan tests => 3 }
324 use XML::SAX::PurePerl::DebugHandler;
325 XML::SAX->add_parser(q(XML::SAX::MyDriver));
326 local $XML::SAX::ParserPackage = 'XML::SAX::MyDriver';
328 my $handler = XML::SAX::PurePerl::DebugHandler->new();
330 my $parser = XML::SAX::ParserFactory->parser(Handler => $handler);
332 ok($parser->isa('XML::SAX::MyDriver');
333 $parser->parse_string("<tag/>");
334 ok($handler->{seen}{start_element});
341 By default, XML::SAX exports nothing into the caller's namespace. However you
342 can request the symbols C<Namespaces> and C<Validation> which are the
343 URIs for those features, allowing an easier way to request those features
346 use XML::SAX qw(Namespaces Validation);
347 my $factory = XML::SAX::ParserFactory->new();
348 $factory->require_feature(Namespaces);
349 $factory->require_feature(Validation);
350 my $parser = $factory->parser();
354 Current maintainer: Grant McLean, grantm@cpan.org
356 Originally written by:
358 Matt Sergeant, matt@sergeant.org
360 Kip Hampton, khampton@totalcinema.com
362 Robin Berjon, robin@knowscape.com
366 This is free software, you may use it and distribute it under
367 the same terms as Perl itself.
371 L<XML::SAX::Base> for writing SAX Filters and Parsers
373 L<XML::SAX::PurePerl> for an XML parser written in 100%
376 L<XML::SAX::Exception> for details on exception handling