CAM-XML
view release on metacpan or search on metacpan
lib/CAM/XML.pm view on Meta::CPAN
}
@{ $self->{children} } = @good;
return $self;
}
=item $self->add(CAM::XML instance)
=item $self->add(-text => $text)
=item $self->add(-cdata => $text)
=item $self->add(-xml => $rawxml)
=item $self->add(<multiple elements of the above types>)
Add content within the current tag. Order of addition may be
significant. This content can be any one of 1) subsidiary XML tags
(CAM::XML), 2) literal text content (C<-text> or C<-cdata>), or 3)
pre-formatted XML content (C<-xml>).
In C<-text> and C<-cdata> content, any reserved characters will be
automatically escaped. Those two modes differ only in their XML
representation: C<-cdata> is more human-readable if there are a lot of
"&", "<" and ">" characters in your text, where C<-text> is usually more
compact for short strings. These strings are not escaped until
output.
Content in C<-xml> mode is parsed in as CAM::XML objects. If it is not
valid XML, a warning will be emitted and the add will fail.
=cut
sub add
lib/CAM/XML.pm view on Meta::CPAN
while (@_ > 0)
{
my $add = shift;
# Test different kinds of input
!$add ? croak 'Undefined object'
: ref $add && $add->isa(__PACKAGE__) ? push @{ $self->{children} }, $add
: ref $add && $add->isa('CAM::XML::Text') ? push @{ $self->{children} }, $add
: ref $add ? croak 'Invalid object type to add to a CAM::XML node'
: $add =~ m/\A-(text|cdata)\z/xms ? $self->_add_text($1, shift)
: $add eq '-xml' ? $self->_add_xml(shift)
: croak "Unknown flag '$add'. Expected '-text' or '-cdata' or '-xml'";
}
return $self;
}
sub _add_text
{
my $self = shift;
my $type = shift;
my $text = shift;
lib/CAM/XML/Text.pm view on Meta::CPAN
}
=item $self->toString()
=cut
sub toString
{
my $self = shift;
return $self->{type} eq 'text' ? CAM::XML->_XML_escape($self->{text}) ##no critic for use of private sub
: $self->{type} eq 'cdata' ? CAM::XML->_CDATA_escape($self->{text}) ##no critic for use of private sub
: $self->{text};
}
=item $self->getInnerText()
=cut
sub getInnerText
{
my $self = shift;
lib/CAM/XML/XMLTree.pm view on Meta::CPAN
=item Char($text)
=cut
sub Char
{
my $expat = shift;
my $text = shift;
$expat->{Stack}->[-1]
->add(($expat->{Cdata} ? '-cdata' : '-text') => $text);
return;
}
=item CdataStart()
=cut
sub CdataStart
{
my $expat = shift;
for (my $i=1; $i<=3; $i++)
{
my $sub = $pkg->new('sub');
$sub->setAttributes(id => $i);
$root->add($sub);
$sub->add(-text => "sub $i");
$root->add(-text => "\n");
}
$root->add(-xml => '<test> just a test </test>');
$root->add(-text => "\n");
$root->add(-cdata => 'This is a simple CDATA test.');
$root->add(-text => "\n");
$root->add(-cdata => 'This is a complex <![CDATA[]]> test.');
$root->add(-text => "\n");
$root->add(-text => '& ampersand');
$root->add(-text => "\n");
is_deeply([sort $root->getAttributeNames()], ['test'], 'getAttributeNames');
is($root->getAttribute('test'), '1', 'getAttribute');
is(scalar $root->getNodes(-tag => 'root'), 1, 'getNodes by tag');
is(scalar $root->getNodes(-tag => 'sub'), 3, 'getNodes by tag');
is(scalar $root->getNodes(-tag => 'foo'), 0, 'getNodes by tag');
#& ampersand
#</root>
ok($root->getChildNode(0), 'getChildNode');
is($root->getChildNode(4), undef, 'getChildNode');
is(scalar $root->getChildNodes(), 4, 'getChildNodes');
# Get the XML output
$str = $root->header() . $root->toString() . "\n";
is($str, $comparestr1, 'Plain XML with cdata');
is($root->getInnerText(), $comparetext1, 'getInnerText');
$parsed = $pkg->parse($comparestr1);
SKIP: {
# Hack to make our test data structure LOOK like XML::Parser output
splice(@{$root->{children}}, 11, 1,
CAM::XML::Text->new('cdata', 'This is a complex <![CDATA['),
CAM::XML::Text->new('text', ']]>'),
CAM::XML::Text->new('cdata', ' test.'),
);
is_deeply($parsed, $root, 'Parse test');
}
$str = $parsed->header() . $parsed->toString() . "\n";
is($str, $comparestr1, 'Deparse parsed XML');
$root = $pkg->new('level1')
->add($pkg->new('level2')
->add($pkg->new('level3'))
ok($root2->setChildren($root->getChildren()), 'setChildren');
eval { $root2->setChildren(1,2,3); }; ok($@, 'setChildren (bad)');
eval { $root2->setChildren(undef); }; ok($@, 'setChildren (bad)');
eval { $root2->setChildren(bless {}, 'Foo'); }; ok($@, 'setChildren (bad)');
ok(scalar $root2->getChildren() > 0, 'getChildren');
ok($root2->setChildren(CAM::XML->new("foo")), 'setChildren');
is(scalar $root2->getChildren(), 1, 'getChildren');
ok($root2->setChildren(CAM::XML::Text->new(text => "")), 'setChildren');
is(scalar $root2->getChildren(), 1, 'getChildren');
ok($root2->setChildren(CAM::XML::Text->new(cdata => "")), 'setChildren');
is(scalar $root2->getChildren(), 1, 'getChildren');
$parsed = $pkg->parse($comparestr3);
$str = $parsed->header() . $parsed->toString(-formatted => 1);
is($str, $comparefmt3, 'Fomatted XML - text spacing');
$str = $parsed->header() . $parsed->toString(-formatted => 1, -textformat => 0);
is($str, $comparefmttxt3, 'Fomatted XML - text spacing');
my $esctest;
$esctest = CAM::XML->new('esc')->add(-text => 'one & two');
( run in 0.543 second using v1.01-cache-2.11-cpan-454fe037f31 )