home *** CD-ROM | disk | FTP | other *** search
/ Australian Personal Computer 2004 July / APC0407D2.iso / workshop / apache / files / ActivePerl-5.6.1.638-MSWin32-x86.msi / _16bb162a5044e2c29a1b493bcf4397df < prev    next >
Encoding:
Text File  |  2004-04-13  |  7.2 KB  |  239 lines

  1. package PPM::XML::Element;
  2. use vars qw( $VERSION );
  3.  
  4. #
  5. # PPM::XML::Element
  6. #
  7. # Base class for XML Elements.  Provides the ability to output the XML document
  8. # once it's been parsed using the XML::Parser module.
  9. #
  10. ###############################################################################
  11. $VERSION = do { my @r = q$Revision: 1.7 $ =~ /\d+/g; sprintf '%d.'.'%02d'x$#r, @r };
  12.  
  13. ###############################################################################
  14. # Required inclusions.
  15. ###############################################################################
  16. #use HTML::Entities;                     # Needed for escaping char entities
  17.  
  18. ###############################################################################
  19. # Allow for creation via 'new'.
  20. ###############################################################################
  21. sub new
  22. {
  23.     my ($class, %args) = @_;
  24.     bless \%args, $class;
  25. }
  26.  
  27. ###############################################################################
  28. # Subroutine:   output
  29. ###############################################################################
  30. # Outputs the entire XML document on the currently selected filehandle.
  31. ###############################################################################
  32. sub output
  33. {
  34.     my $self = shift;
  35.     print $self->as_text();
  36. }
  37.  
  38. ###############################################################################
  39. # Subroutine:   content
  40. ###############################################################################
  41. # Returns a string containing all of the content of this element.
  42. ###############################################################################
  43. sub content
  44. {
  45.     my $self = shift;
  46.     my @kids = @{ $self->{'Kids'} };
  47.     my $text;
  48.  
  49.     if (@kids > 0)
  50.     {
  51.         foreach (@kids)
  52.         {
  53.             # Allow for outputting of char data
  54.             if ((ref $_) =~ /::Characters$/o) {
  55.                 my $t = $_->{Text};
  56.                 $t =~ s[&][&]g;
  57.                 $t =~ s[<][<]g;
  58.                 $t =~ s[>][>]g;
  59.                 $text .= $t;
  60.             }
  61.             else
  62.                 { $text .= $_->as_text(); }
  63.         }
  64.     }
  65.  
  66.     return $text;
  67. }
  68.  
  69. ###############################################################################
  70. # Subroutine:   add_child ($elemref)
  71. ###############################################################################
  72. # Adds a new child element to ourselves.
  73. ###############################################################################
  74. sub add_child (\$)
  75. {
  76.     my $self    = shift;
  77.     my $elemref = shift;
  78.     push( @{$self->{'Kids'}}, $elemref );
  79. }
  80.  
  81. ###############################################################################
  82. # Subroutine:   remove_child ($elemref)
  83. ###############################################################################
  84. # Removes a child element from ourselves.  Returns non-zero if it was able to
  85. # remove the child element, and zero if it was unable to do so.
  86. ###############################################################################
  87. sub remove_child
  88. {
  89.     my $self    = shift;
  90.     my $elemref = shift;
  91.  
  92.     foreach my $idx (0 .. @{$self->{'Kids'}})
  93.     {
  94.         if ($self->{'Kids'}[$idx] == $elemref)
  95.         {
  96.             splice( @{$self->{'Kids'}}, $idx, 1 );
  97.             return 1;
  98.         }
  99.     }
  100.  
  101.     return 0;
  102. }
  103.  
  104. ###############################################################################
  105. # Subroutine:   add_text ($text)
  106. ###############################################################################
  107. # Adds character data to the given element.  Returns undef if unable to add the
  108. # text to this element, and returns a reference to the character data element
  109. # if successful.
  110. ###############################################################################
  111. sub add_text
  112. {
  113.     my $self = shift;
  114.     my $text = shift;
  115.  
  116.     return if (!defined $text);
  117.  
  118.     my $type = ref $self;                   # Do package name magic
  119.     $type =~ s/::[^:]+?$/::Characters/o;
  120.  
  121.     my $elem = new $type;
  122.     $elem->{'Text'} = $text;
  123.     $self->add_child( $elem );
  124.     return $elem;
  125. }
  126.  
  127. ###############################################################################
  128. # Subroutine:   as_text
  129. ###############################################################################
  130. # Returns a string containing the entire XML document.
  131. ###############################################################################
  132. sub as_text
  133. {
  134.     my $self = shift;
  135.     my $text;
  136.  
  137.     my $type = ref $self;
  138.     $type =~ s/.*:://;
  139.  
  140.     $text = '<' . $type;
  141.     foreach (sort keys %{$self})
  142.     {
  143.         if ($_ !~ /Text|Kids/)
  144.             { $text .= " $_=\"" . $self->{$_} . '"'; }
  145.     }
  146.  
  147.     my $cont = $self->content();
  148.     if (defined $cont)
  149.         { $text .= '>' . $cont . '</' . $type . '>'; }
  150.     else
  151.         { $text .= ' />'; }
  152.  
  153.     return $text;
  154. }
  155.  
  156. __END__
  157.  
  158. ###############################################################################
  159. # PPD Documentation
  160. ###############################################################################
  161.  
  162. =head1 NAME
  163.  
  164. PPM::XML::Element - Base element class for XML elements
  165.  
  166. =head1 SYNOPSIS
  167.  
  168.  use PPM::XML::Element;
  169.  @ISA = qw( PPM::XML::Element );
  170.  
  171. =head1 DESCRIPTION
  172.  
  173. Base element class for XML elements.  To be derived from to create your own
  174. elements for use with the XML::Parser module.  Supports output of empty
  175. elements using <.... />.
  176.  
  177. It is recommended that you use a version of the XML::Parser module which 
  178. includes support for Styles; by deriving your own elements from 
  179. PPM::XML::Element and using the 'Objects' style it becomes B<much> easier 
  180. to create your own parser.
  181.  
  182. =head1 METHODS
  183.  
  184. =over 4
  185.  
  186. =item add_text ($text)
  187.  
  188. Adds character data to the end of the element.  The element created is placed
  189. within the same package space as the element it was created under (e.g. adding
  190. text to a XML::Foobar::Stuff element would put the character data into an
  191. XML::Foobar::Characters element).  If successful, this method returns a
  192. reference to the newly created element.
  193.  
  194. =item as_text
  195.  
  196. Returns a string value containing the entire XML document from this element on
  197. down.
  198.  
  199. =item content
  200.  
  201. Returns a string value containing the entire content of this XML element.  Note
  202. that this is quite similar to the C<as_text()> method except that it does not
  203. include any information about this element in particular.
  204.  
  205. =item output
  206.  
  207. Recursively outputs the structure of the XML document from this element on
  208. down.
  209.  
  210. =item add_child ($elemref)
  211.  
  212. Adds the child element to the list of children for this element.  Note that
  213. the element given must be a reference to an object derived from 
  214. C<PPM::XML::Element>.
  215.  
  216. =item remove_child ($elemref)
  217.  
  218. Removes the given child element from the list of children for this element.
  219. This method returns non-zero if it is able to locate and remove the child
  220. element, returning zero if it is unable to do so.
  221.  
  222. =back
  223.  
  224. =head1 LIMITATIONS
  225.  
  226. The C<PPM::XML::Element> module has no provisions for outputting processor
  227. directives or external entities.  It only outputs child elements and any
  228. character data which the elements may contain.
  229.  
  230. =head1 AUTHORS
  231.  
  232. Graham TerMarsch <gtermars@activestate.com>
  233.  
  234. =head1 SEE ALSO
  235.  
  236. L<XML::Parser>
  237.  
  238. =cut
  239.