home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2004 December / PCpro_2004_12.ISO / files / webserver / xampp / xampp-perl-addon-1.4.9-installer.exe / DocumentLocator.pm < prev    next >
Encoding:
Perl POD Document  |  2002-01-22  |  2.6 KB  |  122 lines

  1. # $Id: DocumentLocator.pm,v 1.2 2002/01/22 21:38:06 matt Exp $
  2.  
  3. package XML::SAX::DocumentLocator;
  4. use strict;
  5.  
  6. sub new {
  7.     my $class = shift;
  8.     my %object;
  9.     tie %object, $class, @_;
  10.  
  11.     return bless \%object, $class;
  12. }
  13.  
  14. sub TIEHASH {
  15.     my $class = shift;
  16.     my ($pubmeth, $sysmeth, $linemeth, $colmeth) = @_;
  17.     return bless { 
  18.         pubmeth => $pubmeth,
  19.         sysmeth => $sysmeth,
  20.         linemeth => $linemeth,
  21.         colmeth => $colmeth,
  22.     }, $class;
  23. }
  24.  
  25. sub FETCH {
  26.     my ($self, $key) = @_;
  27.     my $method;
  28.     if ($key eq 'PublicId') {
  29.         $method = $self->{pubmeth};
  30.     }
  31.     elsif ($key eq 'SystemId') {
  32.         $method = $self->{sysmeth};
  33.     }
  34.     elsif ($key eq 'LineNumber') {
  35.         $method = $self->{linemeth};
  36.     }
  37.     elsif ($key eq 'ColumnNumber') {
  38.         $method = $self->{colmeth};
  39.     }
  40.     if ($method) {
  41.         my $value = $method->($key);
  42.         return $value;
  43.     }
  44.     return undef;
  45. }
  46.  
  47. sub EXISTS {
  48.     my ($self, $key) = @_;
  49.     if ($key =~ /^(PublicId|SystemId|LineNumber|ColumnNumber)$/) {
  50.         return 1;
  51.     }
  52.     return 0;
  53. }
  54.  
  55. sub STORE {
  56.     my ($self, $key, $value) = @_;
  57. }
  58.  
  59. sub DELETE {
  60.     my ($self, $key) = @_;
  61. }
  62.  
  63. sub CLEAR {
  64.     my ($self) = @_;
  65. }
  66.  
  67. sub FIRSTKEY {
  68.     my ($self) = @_;
  69.     # assignment resets.
  70.     $self->{keys} = {
  71.         PublicId => 1,
  72.         SystemId => 1,
  73.         LineNumber => 1,
  74.         ColumnNumber => 1,
  75.     };
  76.     return each %{$self->{keys}};
  77. }
  78.  
  79. sub NEXTKEY {
  80.     my ($self, $lastkey) = @_;
  81.     return each %{$self->{keys}};
  82. }
  83.  
  84. 1;
  85. __END__
  86.  
  87. =head1 NAME
  88.  
  89. XML::SAX::DocumentLocator - Helper class for document locators
  90.  
  91. =head1 SYNOPSIS
  92.  
  93.   my $locator = XML::SAX::DocumentLocator->new(
  94.       sub { $object->get_public_id },
  95.       sub { $object->get_system_id },
  96.       sub { $reader->current_line },
  97.       sub { $reader->current_column },
  98.   );
  99.  
  100. =head1 DESCRIPTION
  101.  
  102. This module gives you a tied hash reference that calls the
  103. specified closures when asked for PublicId, SystemId,
  104. LineNumber and ColumnNumber.
  105.  
  106. It is useful for writing SAX Parsers so that you don't have
  107. to constantly update the line numbers in a hash reference on
  108. the object you pass to set_document_locator(). See the source
  109. code for XML::SAX::PurePerl for a usage example.
  110.  
  111. =head1 API
  112.  
  113. There is only 1 method: C<new>. Simply pass it a list of
  114. closures that when called will return the PublicId, the
  115. SystemId, the LineNumber and the ColumnNumber, respectively.
  116.  
  117. The closures are passed a single parameter, the key being
  118. requested. But you're free to ignore that.
  119.  
  120. =cut
  121.  
  122.