changeset 106:83e356614c1e

DOM Builder now is a navigator like SimpleBuilder PostToDOM transformation
author wizard
date Wed, 12 May 2010 17:52:12 +0400
parents a6e9759ff88a
children 0e72ad99eef7
files Lib/IMPL/DOM/Navigator/Builder.pm Lib/IMPL/DOM/Transform/PostToDOM.pm
diffstat 2 files changed, 43 insertions(+), 15 deletions(-) [+]
line wrap: on
line diff
--- a/Lib/IMPL/DOM/Navigator/Builder.pm	Tue May 11 02:59:49 2010 +0400
+++ b/Lib/IMPL/DOM/Navigator/Builder.pm	Wed May 12 17:52:12 2010 +0400
@@ -2,21 +2,24 @@
 use strict;
 use warnings;
 
-use base qw(IMPL::Object);
+use base qw(IMPL::DOM::Navigator);
 use IMPL::Class::Property;
 use IMPL::Class::Property::Direct;
 require IMPL::DOM::Navigator::SchemaNavigator;
 require IMPL::DOM::Schema::ValidationError;
+use IMPL::DOM::Document;
 
 BEGIN {
     private _direct property _schemaNavi => prop_all;
-    private _direct property _nodesPath => prop_all;
-    private _direct property _nodeCurrent => prop_all;
     private _direct property _docClass => prop_all;
     public _direct property BuildErrors => prop_get | prop_list;
     public _direct property Document => prop_get | owner_set;
 }
 
+our %CTOR = (
+    'IMPL::DOM::Navigator' => sub { IMPL::DOM::Document::Empty; }
+);
+
 sub CTOR {
     my ($this,$docClass,$schema) = @_;
     
@@ -35,15 +38,14 @@
         my $node;
         if (! $this->{$Document}) {
             $node = $this->{$Document} = $this->{$_docClass}->new(nodeName => $nodeName,%props);
+            $this->_initNavigator($node);
         } else {
-            die new IMPL::InvalidOperationException('Can\t create a second top level element') unless $this->{$_nodeCurrent};
+            die new IMPL::InvalidOperationException('Can\t create a second top level element') unless $this->Current;
             $node = $this->{$Document}->Create($nodeName,$class,\%props);
-            push @{$this->{$_nodesPath}}, $this->{$_nodeCurrent};
-            $this->{$_nodeCurrent}->appendChild($node);
+            $this->Current->appendChild($node);
+            $this->internalNavigateNodeSet($node);
         }
         
-        $this->{$_nodeCurrent} = $node;
-        
         if (@errors) {
         	$this->BuildErrors->Append(
         		map {
@@ -103,7 +105,7 @@
     my ($this) = @_;
     
     $this->{$_schemaNavi}->SchemaBack();
-    $this->{$_nodeCurrent} = pop @{$this->{$_nodesPath}};
+    $this->SUPER::Back();
 }
 
 1;
--- a/Lib/IMPL/DOM/Transform/PostToDOM.pm	Tue May 11 02:59:49 2010 +0400
+++ b/Lib/IMPL/DOM/Transform/PostToDOM.pm	Wed May 12 17:52:12 2010 +0400
@@ -1,4 +1,4 @@
-package IMPL::DOM::Post2DOM;
+package IMPL::DOM::PostToDOM;
 use strict;
 use warnings;
 
@@ -8,17 +8,22 @@
 use base qw(IMPL::Transform);
 
 BEGIN {
-    public property Navigator => prop_get | owner_set;
+    public property documentClass => prop_get | owner_set;
+    public property documentSchema => prop_get | owner_set;
+    private property _navi => prop_all;
 }
 
 our %CTOR = (
     'IMPL::Transform' => sub {
-        return (
-            HASH => \&TransfromPostData
-        );
+        HASH => \&TransfromPostData
     }
 );
 
+sub CTOR {
+	my ($this,$docClass,$docSchema) = @_;
+	$docClass ||= 'IMPL::DOM::Document'
+}
+
 sub TransformPostData {
     my ($this,$data) = @_;
     
@@ -26,12 +31,33 @@
     
     while (my ($key,$value) = each %$data) {
     	# TODO: review
-        my $node = $navi->Navigate($key);
+        my $node = $navi->Navigate(split /\//, $key);
         $node->nodeValue($value);
     }
     
     return $navi->Document;
 }
 
+package IMPL::DOM::PostToDOM::Navigator;
+use base qw(IMPL::DOM::Navigator::Builder);
+
+__PACKAGE__->PassThroughArgs;
+
+sub Navigate {
+	my ($this,@path) = @_;
+	
+	if (@path > 1) {
+		my $node;
+		foreach my $query (@path) {
+			unless($this->dosafe(sub {
+				$node = $this->SUPER::Navigate($query);
+			})) {
+				$node = $this->NavigateCreate($query);				
+			}
+		}
+	} else {
+		die new IMPL::InvalidArgumentException("A path is a required parameter");
+	}
+}
 
 1;