| SGML-Grove documentation | Contained in the SGML-Grove distribution. |
SGML::Element - an element of an SGML, XML, or HTML document
$element->gi; $element->name; $element->attr ($attr[, $value]); $element->attr_as_string ($attr[, $context, ...]); $element->attributes [($attributes)]; $element->contents [($contents)]; $element->as_string([$context, ...]); $element->iter; $element->accept($visitor, ...); $element->accept_gi($visitor, ...); $element->children_accept($visitor, ...); $element->children_accept_gi($visitor, ...);
An SGML::Element represents an element in an SGML or XML document.
An Element contains a generic identifier, or name, for the element,
the elements attributes and the ordered contents of the element.
$element->gi and $element->name are synonyms, they
return the generic identifier of the element.
$element->attr returns the value of an attribute, if a second
argument is given then that value is assigned to the attribute and
returned. The value of an attribute may be an array of scalar or
SGML::SData objects, an SGML::Notation, or an array of
SGML::Entity or SGML::ExtEntity objects. attr returns
undef for implied attributes.
$element->attr_as_string returns the value of an attribute as a
string, possibly modified by $context. (XXX undefined results if
the attribute is not cdata/sdata.)
$element->attributes returns a reference to a hash containing
the attributes of the element, or undef if there are no attributes
defined for for this element. The keys of the hash are the attribute
names and the values are as defined above.
$element->attributes($attributes) assigns the attributes from
the hash $attributes. No hash entries are made for implied
attributes.
$element->contents returns a reference to an array containing
the children of the element. The contents of the element may contain
other elements, scalars, SGML::SData, SGML::PI, SGML::Entity,
SGML::ExtEntity, or SGML::SubDocEntity objects.
$element->contents($contents) assigns the contents from the
array $contents.
$element->as_string returns the entire hierarchy of this
element as a string, possibly modified by $context. See
SGML::SData and SGML::PI for more detail. (XXX does not expand
entities.)
$element->iter returns an iterator for the element, see
Class::Visitor for details.
$element->accept($visitor[, ...]) issues a call back to
$visitor->visit_SGML_Element($element[, ...]). See examples
visitor.pl and simple-dump.pl for more information.
$element->accept_gi($visitor[, ...]) issues a call back to
$visitor->visit_gi_GI($element[, ...]) where GI is the
generic identifier of this element. accept_gi maps strange
characters in the GI to underscore (`_') [XXX more specific].
children_accept and children_accept_gi call accept and
accept_gi, respectively, on each object in the element's content.
Element handles scalars internally for as_string,
children_accept, and children_accept_gi. For children_accept
and children_accept_gi (both), Element calls back with
$visitor->visit_scalar($scalar[, ...]).
For as_string, Element will use the string unless
$context->{cdata_mapper} is defined, in which case it returns the
result of calling the cdata_mapper subroutine with the scalar and
the remaining arguments. The actual implementation is:
&{$context->{cdata_mapper}} ($scalar, @_);
Ken MacLeod, ken@bitsko.slc.ut.us
perl(1), SGML::Grove(3), Text::EntityMap(3), SGML::SData(3), SGML::PI(3), Class::Visitor(3).
| SGML-Grove documentation | Contained in the SGML-Grove distribution. |
# # Copyright (C) 1997 Ken MacLeod # See the file COPYING for distribution terms. # # $Id: Element.pm,v 1.2 1998/01/18 00:21:13 ken Exp $ # package SGML::Element; use strict; use Class::Visitor; visitor_class 'SGML::Element', 'Class::Visitor::Base', [ 'contents' => '@', # [0] 'gi' => '$', # [1] 'attributes' => '@', # [2] ];
sub name { gi(@_); } sub attr { my $self = shift; my $attr = shift; if (@_) { my $value = shift; if (ref ($value) eq 'ARRAY') { return $self->[2]->{$attr} = $value; } else { return $self->[2]->{$attr} = [$value]; } } else { if (!defined $self->[2]) { return undef; } else { return $self->[2]->{$attr}; } } } # $element->attr_as_string($attr[, $context]); sub attr_as_string { my $self = shift; my $attr = shift; my $attributes = $self->[2]; return "" if (!defined $attributes); my $value = $attributes->{$attr}; return "" if (!defined($value)); return $value if (!ref ($value)); # return tokens my ($ii, @string); for ($ii = 0; $ii <= $#{$value}; $ii ++) { my $child = $value->[$ii]; if (!ref ($child)) { my $context = shift; if (defined ($context->{'cdata_mapper'})) { push (@string, &{$context->{'cdata_mapper'}}($child, @_)); } else { push (@string, $child); } } else { push (@string, $child->as_string(@_)); } } return (join ("", @string)); } # $element->as_string($context); sub as_string { my $self = shift; my $context = shift; my @string; my $ii; for ($ii = 0; $ii <= $#{$self->[0]}; $ii ++) { my $child = $self->[0][$ii]; if (!ref ($child)) { if (defined ($context->{'cdata_mapper'})) { push (@string, &{$context->{'cdata_mapper'}}($child, @_)); } else { push (@string, $child); } } else { push (@string, $child->as_string($context, @_)); } } return (join ("", @string)); } sub accept_gi { my $self = shift; my $visitor = shift; my $gi = $self->gi; # convert all non-word characters to `_' (matched in # SpecBuilder.pm) $gi =~ s/\W/_/g; my $alias = "visit_gi_" . $gi; $visitor->$alias ($self, @_); } sub children_accept_gi { my $self = shift; my $visitor = shift; my $ii; for ($ii = 0; $ii <= $#{$self->[0]}; $ii ++) { my $child = $self->[0][$ii]; if (!ref ($child)) { $visitor->visit_scalar ($child, @_); } else { $child->accept_gi ($visitor, @_); } } } 1;