view Lib/IMPL/DOM/Transform/PostToDOM.pm @ 144:b56ebc31bf18

Empty nodes no more created while transforming a post request to the DOM document minor speed improvements to the object CTOR caching Added support for a secure processing (and 'laundering' ) a CGI parameters Many minor fixes
author wizard
date Tue, 13 Jul 2010 02:05:38 +0400
parents 06a34c197b05
children c2aa10fbb396
line wrap: on
line source

package IMPL::DOM::Transform::PostToDOM;
use strict;
use warnings;

use IMPL::DOM::Navigator::Builder;
use IMPL::Class::Property;

use base qw(IMPL::Transform);

BEGIN {
    public property documentClass => prop_get | owner_set;
    public property documentSchema => prop_get | owner_set;
    public property prefix => prop_get | owner_set;
    private property _navi => prop_all;
    public property Errors => prop_all | prop_list;
    private property _schema => prop_all;
}

our %CTOR = (
    'IMPL::Transform' => sub {
    	-plain => \&TransformPlain,
        HASH => \&TransformContainer,
        CGI => \&TransformCGI,
        CGIWrapper => \&TransformCGI
    }
);

sub CTOR {
	my ($this,$docClass,$docSchema,$prefix) = @_;
	$docClass ||= 'IMPL::DOM::Document';
	
	$this->_navi(
		IMPL::DOM::Navigator::Builder->new(
			$docClass,
			$docSchema
		)
	);
	$this->_schema($docSchema);
	$this->prefix($prefix) if $prefix;
}

sub TransformContainer {
    my ($this,$data) = @_;
    
    my $navi = $this->_navi;
        
    while (my ($key,$value) = each %$data) {
    	
    	$navi->NavigateCreate($key);
    	
    	$this->Transform($value);
    	
    	$navi->Back();
    }
    
    return $navi->Current;
}

sub TransformPlain {
	my ($this,$data) = @_;
	
	$this->_navi->Current->nodeValue( $this->_navi->inflateValue($data) );
}

sub TransformCGI {
	my ($this,$query) = @_;

	my $data={};
	
	my $prefix = $this->prefix;
	$prefix = qr/$prefix/;
	
	foreach my $param (grep $_=~/$prefix/, $query->param()) {
		my $value = $query->param($param) or next;
		
		my @parts = split /\//,$param;
		
		my $node = $data;
		while ( my $part = shift @parts ) {
			if (@parts) {
				$node = ($node->{$part} ||= {});
			} else {			
				$node->{$part} = $value;
			}
		}  
	}
	
	my $doc = $this->Transform($data);
	$this->Errors->Append( $this->_navi->BuildErrors);
	$this->Errors->Append( $this->_schema->Validate($doc));
	return $doc;
}

1;