view Lib/IMPL/DOM/Navigator/Builder.pm @ 104:196bf443b5e1

DOM::Schema RC0 inflators support, validation and some other things, Minor and major fixes almost for everything. A 'Source' property of the ValidationErrors generated from a NodeSet or a NodeList is subject to change in the future.
author wizard
date Tue, 11 May 2010 02:42:59 +0400
parents c289ed9662ca
children 83e356614c1e
line wrap: on
line source

package IMPL::DOM::Navigator::Builder;
use strict;
use warnings;

use base qw(IMPL::Object);
use IMPL::Class::Property;
use IMPL::Class::Property::Direct;
require IMPL::DOM::Navigator::SchemaNavigator;
require IMPL::DOM::Schema::ValidationError;

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;
}

sub CTOR {
    my ($this,$docClass,$schema) = @_;
    
    $this->{$_docClass} = $docClass;
    $this->{$_schemaNavi} = $schema ? IMPL::DOM::Navigator::SchemaNavigator->new($schema) : undef;
}

sub NavigateCreate {
    my ($this,$nodeName,%props) = @_;
    
    if (my $schemaNode = $this->{$_schemaNavi}->NavigateName($nodeName)) {
        my $class = $schemaNode->can('nativeType') ? $schemaNode->nativeType || 'IMPL::DOM::Node' : 'IMPL::DOM::Node';
        
        my @errors = $this->inflateProperties($schemaNode,\%props);
        
        my $node;
        if (! $this->{$Document}) {
            $node = $this->{$Document} = $this->{$_docClass}->new(nodeName => $nodeName,%props);
        } else {
            die new IMPL::InvalidOperationException('Can\t create a second top level element') unless $this->{$_nodeCurrent};
            $node = $this->{$Document}->Create($nodeName,$class,\%props);
            push @{$this->{$_nodesPath}}, $this->{$_nodeCurrent};
            $this->{$_nodeCurrent}->appendChild($node);
        }
        
        $this->{$_nodeCurrent} = $node;
        
        if (@errors) {
        	$this->BuildErrors->Append(
        		map {
					IMPL::DOM::Schema::ValidationError->new(
						Node => $node,
						Source => $this->{$_schemaNavi}->SourceSchemaNode,
						Schema => $schemaNode,
						Message => $schemaNode->messageInflateError,
						Error => $_
					)        			
        		} @errors
        	);
        }
        
        return $node;
    } else {
        die new IMPL::InvalidOperationException("The specified node is undefined", $nodeName);
    }
}

sub inflateProperties {
	my ($this,$schemaNode,$refProps) = @_;
	my @errors;
	foreach my $schemaProp ( $schemaNode->selectNodes('Property') ) {
		next if not exists $refProps->{$schemaProp->name};
		my $result = eval {$schemaProp->inflateValue($refProps->{$schemaProp->name}) };
		if (my $e = $@) {
			push @errors, $e;
		} else {
			$refProps->{$schemaProp->name} = $result;
		}		
	}
	return @errors;
}

sub inflateValue {
	my ($this,$value,$node) = @_;
	
	my $nodeSchema = $this->{$_schemaNavi}->Current;
	
	my $result = eval { $nodeSchema->inflateValue($value) };
	if (my $e=$@) {
		$this->BuildErrors->Append(new IMPL::DOM::Schema::ValidationError(
			Schema =>  $nodeSchema,
			Node => $node,
			Error => $e,
			Message => $nodeSchema->messageInflateError,
			Source => $this->{$_schemaNavi}->SourceSchemaNode
		));
		return $value;
	} else {
		return $result;
	}
}

sub Back {
    my ($this) = @_;
    
    $this->{$_schemaNavi}->SchemaBack();
    $this->{$_nodeCurrent} = pop @{$this->{$_nodesPath}};
}

1;

__END__

=pod

=head1 NAME

C< IMPL::DOM::Navigator::Builder > - Навигатор, строящий документ по указанной схеме.

=head1 SYNOPSIS

=begin code

my $builder = new IMPL::DOM::Navigator::Builder(new MyApp::Document,$schema);
my $reader = new IMPL::DOM::XMLReader(Navigator => $builder);

$reader->ParseFile("document.xml");

my @errors = $schema->Validate($builder->Document);

=end code

=head1 DESCRIPTION

Построитель DOM документов по указанной схеме. Обычно используется в связке
с объектами для чтения такими как C<IMPL::DOM::XMLReader>.

=head1 METHODS

=over

=item C< CTOR($classDocument,$schema) >

Создает новый объект, принимает на вход класс документа (или фабрику, например
L<IMPL::Object::Factory>) и схему. В процессе процедуры построения документа
будет создан объект документа.

=item C< NavigateCreate($nodeName,\%props) >

Создает новый узел с указанным именем и переходит в него. В случае если в схеме
подходящий узел не найден, то вызывается исключение.

При этом по имени узла ищется его схема, после чего определяется класс для
создания экземпляра и созданный узел доавляется в документ. При создании
нового узла используется метод документа C<< IMPL::DOM::Document->Create >>

Свойства узла передаются при создании через параметр C<props>, но имя создаваемого
узла НЕ может быть переопределено свойством C<nodeName>, оно будет проигнорировано.

=item C< Document >

Свойство, которое содержит документ по окончании процедурв построения.

=back

=cut