home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2006 November (DVD) / PCWELT_11_2006.ISO / casper / filesystem.squashfs / usr / share / perl5 / XML / Grove / IDs.pm < prev    next >
Encoding:
Text File  |  1999-10-23  |  2.3 KB  |  104 lines

  1. #
  2. # Copyright (C) 1998, 1999 Ken MacLeod
  3. # XML::Grove::IDs is free software; you can redistribute it
  4. # and/or modify it under the same terms as Perl itself.
  5. #
  6. # $Id: IDs.pm,v 1.2 1999/08/17 15:01:28 kmacleod Exp $
  7. #
  8.  
  9. use strict;
  10.  
  11. package XML::Grove::IDs;
  12.  
  13. use Data::Grove::Visitor;
  14.  
  15. sub new {
  16.     my ($type, $name, $elements) = @_;
  17.     $name = 'id' if(!defined $name);
  18.     return (bless {Name => $name, Elements => $elements}, $type);
  19. }
  20.  
  21. sub visit_document {
  22.     my $self = shift; my $grove = shift; my $hash = shift;
  23.     $grove->children_accept ($self, $hash);
  24. }
  25.  
  26. sub visit_element {
  27.     my $self = shift; my $element = shift; my $hash = shift;
  28.  
  29.     if(!$self->{Elements} or $self->{Elements}{$element->{Name}}) {
  30.            my $id = $element->{Attributes}{$self->{Name}};
  31.            $hash->{$id} = $element
  32.                if (defined $id);
  33.     }
  34.  
  35.     $element->children_accept ($self, $hash);
  36. }
  37.  
  38. ###
  39. ### Extend the XML::Grove::Document and XML::Grove::Element packages with our
  40. ### new function.
  41. ###
  42.  
  43. package XML::Grove::Document;
  44.  
  45. sub get_ids {
  46.     my $self = shift;
  47.  
  48.     my $hash = {};
  49.     $self->accept(XML::Grove::IDs->new(@_), $hash);
  50.     return $hash;
  51. }
  52.  
  53. package XML::Grove::Element;
  54.  
  55. sub get_ids {
  56.     my $self = shift;
  57.  
  58.     my $hash = {};
  59.     $self->accept(XML::Grove::IDs->new(@_), $hash);
  60.     return $hash;
  61. }
  62.  
  63. 1;
  64.  
  65. __END__
  66.  
  67. =head1 NAME
  68.  
  69. XML::Grove::IDs - return an index of `id' attributes in a grove
  70.  
  71. =head1 SYNOPSIS
  72.  
  73.  use XML::Grove::IDs;
  74.  
  75.  # Using get_ids method on XML::Grove::Document or XML::Grove::Element:
  76.  $hash = $grove_object->get_ids($attr_name, $elements);
  77.  
  78.  # Using an XML::Grove::IDs instance:
  79.  $indexer = XML::Grove::IDs->new($attr_name, $elements);
  80.  my $hash = {};
  81.  $grove_object->accept($indexer, $hash);
  82.  
  83. =head1 DESCRIPTION
  84.  
  85. C<XML::Grove::IDs> returns a hash index of all nodes in a grove with
  86. an `id' attribute.  The keys of the hash are the ID attribute value
  87. and the value at that key is the element.  `C<$attr_name>' and
  88. `C<$elements>' are optional.  The attribute name defaults to `C<id>'
  89. if `C<$attr_name>' is not supplied.  Indexing can be restricted to
  90. only certain elements, by name, by providing a hash containing NAME=>1
  91. values.
  92.  
  93. =head1 AUTHOR
  94.  
  95. Ken MacLeod, ken@bitsko.slc.ut.us
  96.  
  97. =head1 SEE ALSO
  98.  
  99. perl(1), XML::Grove(3), Data::Grove::Visitor(3)
  100.  
  101. Extensible Markup Language (XML) <http://www.w3c.org/XML>
  102.  
  103. =cut
  104.