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

  1. #
  2. # Copyright (C) 1998, 1999 Ken MacLeod
  3. # XML::Grove::AsString is free software; you can redistribute it and/or
  4. # modify it under the same terms as Perl itself.
  5. #
  6. # $Id: AsString.pm,v 1.6 1999/08/25 17:08:09 kmacleod Exp $
  7. #
  8.  
  9. use strict;
  10.  
  11. package XML::Grove::AsString;
  12. use Data::Grove::Visitor;
  13.  
  14. sub new {
  15.     my $class = shift;
  16.     my $self = ($#_ == 0) ? { %{ (shift) } } : { @_ };
  17.  
  18.     return bless $self, $class;
  19. }
  20.  
  21. sub as_string {
  22.     my $self = shift; my $object = shift; my $fh = shift;
  23.  
  24.     if (defined $fh) {
  25.     return ();
  26.     } else {
  27.     return join('', $object->accept($self, $fh));
  28.     }
  29. }
  30.  
  31. sub visit_document {
  32.     my $self = shift; my $document = shift;
  33.  
  34.     return $document->children_accept($self, @_);
  35. }
  36.  
  37. sub visit_element {
  38.     my $self = shift; my $element = shift;
  39.  
  40.     return $element->children_accept($self, @_);
  41. }
  42.  
  43. sub visit_entity {
  44.     my $self = shift; my $entity = shift; my $fh = shift;
  45.  
  46.     my $mapper = $self->{EntityMap};
  47.     return '' if (!defined $mapper);
  48.  
  49.     my $mapping;
  50.     if (ref($mapper) eq 'CODE') {
  51.     $mapping = &$mapper($entity->{Data},
  52.                 $self->{EntityMapOptions});
  53.     } else {
  54.     $mapping = $mapper->lookup($entity->{Data},
  55.                    $self->{EntityMapOptions});
  56.     }
  57.  
  58.     if ($self->{EntityMapFilter}) {
  59.     my $filter = $self->{Filter};
  60.     if (defined $filter) {
  61.         $mapping = &$filter($mapping);
  62.     }
  63.     }
  64.  
  65.     return $self->_print($fh, $mapping);
  66. }
  67.  
  68. sub visit_pi {
  69.     return ();
  70. }
  71.  
  72. sub visit_comment {
  73.     return ();
  74. }
  75.  
  76. sub visit_characters {
  77.     my $self = shift; my $characters = shift; my $fh = shift;
  78.  
  79.     my $data = $characters->{Data};
  80.     if (defined ($self->{Filter})) {
  81.     $data = &{$self->{Filter}}($data);
  82.     }
  83.  
  84.     return $self->_print($fh, $data);
  85. }
  86.  
  87. sub _print {
  88.     my $self = shift; my $fh = shift; my $string = shift;
  89.  
  90.     if (defined $fh) {
  91.     $fh->print($string);
  92.     return ();
  93.     } else {
  94.     return ($string);
  95.     }
  96. }
  97.  
  98. package XML::Grove;
  99.  
  100. sub as_string {
  101.     my $xml_object = shift;
  102.  
  103.     return XML::Grove::AsString->new(@_)->as_string($xml_object);
  104. }
  105.  
  106. package XML::Grove::Element;
  107.  
  108. sub attr_as_string {
  109.     my $element = shift; my $attr = shift;
  110.  
  111.     my $writer = new XML::Grove::AsString (@_);
  112.     return $element->attr_accept ($attr, $writer);
  113. }
  114.  
  115. 1;
  116.  
  117. __END__
  118.  
  119. =head1 NAME
  120.  
  121. XML::Grove::AsString - output content of XML objects as a string
  122.  
  123. =head1 SYNOPSIS
  124.  
  125.  use XML::Grove::AsString;
  126.  
  127.  # Using as_string method on XML::Grove::Document or XML::Grove::Element:
  128.  $string = $xml_object->as_string OPTIONS;
  129.  $string = $element->attr_as_string $attr, OPTIONS;
  130.  
  131.  # Using an XML::Grove::AsString instance:
  132.  $writer = new XML::Grove::AsString OPTIONS;
  133.  
  134.  $string = $writer->as_string($xml_object);
  135.  $writer->as_string($xml_object, $file_handle);
  136.  
  137. =head1 DESCRIPTION
  138.  
  139. Calling `C<as_string>' on an XML object returns the character data
  140. contents of that object as a string, including all elements below that
  141. object.  Calling `C<attr_as_string>' on an element returns the
  142. contents of the named attribute as a string.  Comments, processing
  143. instructions, and, by default, entities all return an empty string.
  144.  
  145. I<OPTIONS> may either be a key-value list or a hash containing the
  146. options described below.  I<OPTIONS> may be modified directly in the
  147. object.  The default options are no filtering and entities are mapped
  148. to empty strings.
  149.  
  150. =head1 OPTIONS
  151.  
  152. =over 4
  153.  
  154. =item Filter
  155.  
  156. `C<Filter>' is an anonymous sub that gets called to process character
  157. data before it is appended to the string to be returned.  This can be
  158. used, for example, to escape characters that are special in output
  159. formats.  The `C<Filter>' sub is called like this:
  160.  
  161.     $string = &$filter ($character_data);
  162.  
  163. =item EntityMap
  164.  
  165. `C<EntityMap>' is an object that accepts `C<lookup>' methods or an
  166. anonymous sub that gets called with the entity replacement text (data)
  167. and mapper options as arguments and returns the corresponding
  168. character replacements.  It is called like this if it is an object:
  169.  
  170.     $replacement_text = $entity_map->lookup ($entity_data,
  171.                          $entity_map_options);
  172.  
  173. or this if it is a sub:
  174.  
  175.     $replacement_text = &$entity_map ($entity_data,
  176.                       $entity_map_options);
  177.  
  178. =item EntityMapOptions
  179.  
  180. `C<EntityMapOptions>' is a hash passed through to the `C<lookup>'
  181. method or anonymous sub, the type of value is defined by the entity
  182. mapping package or the anonymous sub.
  183.  
  184. =item EntityMapFilter
  185.  
  186. `C<EntityMapFilter>' is a flag to indicate if mapped entities should
  187. be filtered after mapping.
  188.  
  189. =back
  190.  
  191. =head1 EXAMPLES
  192.  
  193. Here is an example of entity mapping using the Text::EntityMap module:
  194.  
  195.     use Text::EntityMap;
  196.     use XML::Grove::AsString;
  197.  
  198.     $html_iso_dia = Text::EntityMap->load ('ISOdia.2html');
  199.     $html_iso_pub = Text::EntityMap->load ('ISOpub.2html');
  200.     $html_map = Text::EntityMap->group ($html_iso_dia,
  201.                     $html_iso_pub);
  202.  
  203.     $element->as_string (EntityMap => $html_map);
  204.  
  205. =head1 AUTHOR
  206.  
  207. Ken MacLeod, ken@bitsko.slc.ut.us
  208.  
  209. =head1 SEE ALSO
  210.  
  211. perl(1), XML::Grove(3)
  212.  
  213. Extensible Markup Language (XML) <http://www.w3c.org/XML>
  214.  
  215. =cut
  216.