XML-DTD

 view release on metacpan or  search on metacpan

lib/XML/DTD/Element.pm  view on Meta::CPAN

package XML::DTD::Element;

use XML::DTD::Component;
use XML::DTD::ContentModel;
use XML::DTD::Error;

use 5.008;
use strict;
use warnings;

our @ISA = qw(XML::DTD::Component);

our $VERSION = '0.09';


# Constructor
sub new {
  my $arg = shift;
  my $man = shift;
  my $elt = shift;

  my $cls = ref($arg) || $arg;
  my $obj = ref($arg) && $arg;

  my $self;
  if ($obj) {
    # Called as a copy constructor
    $self = { %$obj };
    bless $self, $cls;
  } else {
    # Called as the main constructor
    throw XML::DTD::Error("Constructor for XML::DTD::Element called ".
			  "with undefined element string")
      if (! defined($elt));
    $self = { };
    bless $self, $cls;
    $self->define('element', $elt, '<!ELEMENT', '>');
    $self->_parse($man, $elt);
  }
  return $self;
}


# Write an XML representation
sub writexml {
  my $self = shift;
  my $xmlw = shift;

  $xmlw->open('element', {'name' => $self->{'NAME'},
			  'ltws' => $self->{'WS0'}});
  my $ws2 = (defined($self->{'WS2'}) and $self->{'WS2'} ne '')?
    $self->{'WS2'}:undef;
  my $ws = {'ltws' => $self->{'WS1'}, 'rtws' => $ws2};
  $xmlw->open('contentspec', $ws);
  $xmlw->pcdata($self->{'CONTENTSPECTEXT'});
  $xmlw->close;
  $self->{'CONTENTSPEC'}->writexmlelts($xmlw);
  $xmlw->close;
}


# Return the element name
sub name {
  my $self = shift;

  return $self->{'NAME'};
}


# Return the content specification text
sub contentspec {
  my $self = shift;

  return $self->{'CONTENTSPECTEXT'};
}


# Return the parsed content specification as a content model object reference
sub contentmodel {
  my $self = shift;

  return $self->{'CONTENTSPEC'};
}


# Parse the element declaration
sub _parse {
  my $self = shift;
  my $entman = shift;
  my $eltdcl = shift;

  if ($eltdcl=~/<\!ELEMENT(\s+)([\w\.:\-_]+|%[\w\.:\-_]+;)(\s+)(.+)(\s*)>/s) {
    $self->{'WS0'} = $1;
    my $name = $2;
    $self->{'WS1'} = $3;
    my $cntspc = $4;
    $self->{'WS2'} = $5;

    $name = $entman->peexpand($name)
      if ($name =~ /^%([\w\.:\-_]+);$/);

    $self->{'NAME'} = $name;

    $self->{'CONTENTSPECTEXT'} = $cntspc;
    $self->{'CONTENTSPEC'} = XML::DTD::ContentModel->new($cntspc, $entman);
  } else {
    throw XML::DTD::Error("Error parsing element name and contentspec string ".
			  $eltdcl, $self);
  }
}


1;
__END__



( run in 0.487 second using v1.01-cache-2.11-cpan-39bf76dae61 )