# HG changeset patch # User Sergey # Date 1252067895 -14400 # Node ID 65a7bb156fb753ddd404bc17fdffe045b8227916 # Parent bb8d67f811ea2e29e846f557f6d9063744050586 Дом модель и схема diff -r bb8d67f811ea -r 65a7bb156fb7 Lib/IMPL/DOM/Navigator/Builder.pm --- a/Lib/IMPL/DOM/Navigator/Builder.pm Wed Sep 02 23:11:14 2009 +0400 +++ b/Lib/IMPL/DOM/Navigator/Builder.pm Fri Sep 04 16:38:15 2009 +0400 @@ -23,8 +23,13 @@ sub NavigateCreate { my ($this,$nodeName,%props) = @_; - if ( my $nodeSchema = $this->{$_navigatorSchema}->Navigate($nodeName) ) { + if ( my $nodeSchema = $this->{$_navigatorSchema}->Navigate(sub { $_[0]->nodeName eq $nodeName or $_[0]->isa('IMPL::DOM::Schema::AnyNode') }) ) { + my $class = $nodeSchema->type; + my $node = $this->{$Document}->Create($nodeName, $class, \%props); + + $this->Current()->appendNode($node); + $this->Current($node); } else { die new IMPL::InvalidOperationException("Requested elemnt not found in the schema"); diff -r bb8d67f811ea -r 65a7bb156fb7 Lib/IMPL/DOM/Node.pm --- a/Lib/IMPL/DOM/Node.pm Wed Sep 02 23:11:14 2009 +0400 +++ b/Lib/IMPL/DOM/Node.pm Fri Sep 04 16:38:15 2009 +0400 @@ -40,6 +40,20 @@ return $node; } +sub appendNode { + my ($this,$node,$pos) = @_; + + die new IMPL::InvalidOperationException("You can't insert the node to itselft") if $this == $node; + + $node->{$parentNode}->removeNode($node) if ($node->{$parentNode}); + + $this->childNodes->Append($node); + + $node->_setParent( $this ); + + return $node; +} + sub _getChildNodes { my ($this) = @_; @@ -94,9 +108,14 @@ } sub selectNodes { - my ($this,$name) = @_; + my ($this,$query) = @_; + my @result; - my @result = grep $_->nodeName eq $name, @{$this->childNodes}; + if (ref $query eq 'CODE') { + @result = grep &$query($_), @{$this->childNodes}; + } else { + @result = grep $_->nodeName eq $query, @{$this->childNodes}; + } return wantarray ? @result : \@result; } @@ -113,6 +132,7 @@ my ($this,$node) = @_; $this->{$parentNode} = $node; + # prevent from creating cyclicreferences weaken($this->{$parentNode}); } diff -r bb8d67f811ea -r 65a7bb156fb7 Lib/IMPL/DOM/XMLReader.pm --- a/Lib/IMPL/DOM/XMLReader.pm Wed Sep 02 23:11:14 2009 +0400 +++ b/Lib/IMPL/DOM/XMLReader.pm Fri Sep 04 16:38:15 2009 +0400 @@ -7,6 +7,8 @@ use IMPL::Class::Property::Direct; use XML::Parser; +__PACKAGE__->PassThroughArgs; + BEGIN { public _direct property Navigator => prop_get | owner_set; private _direct property _current => prop_all; @@ -31,9 +33,9 @@ my $parser = new XML::Parser( Handlers => { - Start => sub {shift; goto &OnStart($this,@_)}, - End => sub {shift; goto &OnEnd($this,@_)}, - Char => sub {shift; goto &OnChar($this,@_)} + Start => sub {shift; goto &_OnStart($this,@_)}, + End => sub {shift; goto &_OnEnd($this,@_)}, + Char => sub {shift; goto &_OnChar($this,@_)} } ); @@ -41,19 +43,19 @@ } -sub OnBegin { +sub _OnBegin { my ($this,$element,%attrs) = @_; $this->{$_current} = $this->Navigator->NavigateCreate($element,%attrs); } -sub OnEnd { +sub _OnEnd { my ($this,$element) = @_; $this->{$_current} = $this->Back; } -sub OnChar { +sub _OnChar { my ($this,$val) = @_; $this->{$_current}->nodeValue($this->{$_current}->nodeValue . $val); @@ -70,11 +72,29 @@ my $reader = new IMPL::DOM::XMLReader(Navigator => $DomBuilder); my $obj = $reader->parsefile("data.xml"); -=head2 DESCRIPTION +=head1 DESCRIPTION , . . C C +=head1 METHODS + +=over + +=item C $builder)> + + , + +=item C<$obj->Parse($in)> + + . xml , HANDLE. + +=item C<$obj->ParseFile($fileName)> + + C<$fileName>. + +=back + =cut diff -r bb8d67f811ea -r 65a7bb156fb7 Lib/IMPL/Test/Unit.pm --- a/Lib/IMPL/Test/Unit.pm Wed Sep 02 23:11:14 2009 +0400 +++ b/Lib/IMPL/Test/Unit.pm Fri Sep 04 16:38:15 2009 +0400 @@ -36,6 +36,10 @@ } sub Cleanup { + my ($this,$session) = @_; + + $session->{$_} = $this->$_() foreach map $_->DataList, $this->get_meta('IMPL::Test::Unit::SharedData'); + 1; } @@ -81,7 +85,7 @@ $elapsed = tv_interval ( $t0 ); } finally { # we need to call Cleanup anyway - $this->Cleanup; + $this->Cleanup($session); }; return new IMPL::Test::Result( diff -r bb8d67f811ea -r 65a7bb156fb7 _test/DOM.t --- a/_test/DOM.t Wed Sep 02 23:11:14 2009 +0400 +++ b/_test/DOM.t Fri Sep 04 16:38:15 2009 +0400 @@ -8,6 +8,7 @@ my $plan = new IMPL::Test::Plan qw( Test::DOM::Navigator + Test::DOM::Node ); $plan->AddListener(new IMPL::Test::TAPListener); diff -r bb8d67f811ea -r 65a7bb156fb7 _test/Test/DOM/Node.pm --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/_test/Test/DOM/Node.pm Fri Sep 04 16:38:15 2009 +0400 @@ -0,0 +1,33 @@ +package Test::DOM::Node; +use strict; +use warnings; + +use base qw(IMPL::Test::Unit); +use IMPL::Test qw(test shared failed); +use IMPL::Class::Property; + +require IMPL::DOM::Node; + +__PACKAGE__->PassThroughArgs; + +BEGIN { + shared public property Root => prop_all; +} + +test Create => sub { + my ($this) = @_; + + $this->Root(new IMPL::DOM::Node(nodeName => 'Root')) or failed "Failed to create a node"; +}; + +test AppendNode => sub { + my ($this) = @_; + + my $child = $this->Root->appendNode(new IMPL::DOM::Node(nodeName => 'Child')) or failed "Failed to append a child node"; + + my $firstChild = $this->Root->firstChild; + + failed "firstChild returned incorrect results" unless $firstChild == $child; +}; + +1; diff -r bb8d67f811ea -r 65a7bb156fb7 impl.kpf --- a/impl.kpf Wed Sep 02 23:11:14 2009 +0400 +++ b/impl.kpf Fri Sep 04 16:38:15 2009 +0400 @@ -248,6 +248,32 @@ default + + + + + + 9011 + + + _test/Test/DOM/Node.pm + + Perl + + + + application/x-www-form-urlencoded + GET + 1 + 0 + 0 + + + enabled + + + default +