236
|
1 package IMPL::DOM::Transform::ObjectToDOM;
|
|
2 use strict;
|
|
3
|
|
4 use IMPL::Const qw(:prop :access);
|
|
5 use IMPL::declare {
|
|
6 require => {
|
|
7 PropertyInfo => 'IMPL::Class::PropertyInfo',
|
|
8 Builder => 'IMPL::DOM::Navigator::Builder',
|
|
9 Exception => 'IMPL::Exception',
|
|
10 ArgumentException => '-IMPL::InvalidArgumentException',
|
|
11 OperationException => '-IMPL::InvalidOperationException'
|
|
12 },
|
|
13 base => [
|
|
14 'IMPL::Transform' => sub {
|
|
15 -plain => \&TransformPlain,
|
|
16 HASH => \&TransformHash,
|
|
17 -default => \&TransformDefault
|
|
18 }
|
|
19 ],
|
|
20 props => [
|
|
21 documentSchema => PROP_RO,
|
|
22 _schema => PROP_RW,
|
|
23 _navi => PROP_RW
|
|
24 ]
|
|
25 };
|
|
26
|
246
|
27 use constant {
|
|
28 SchemaNode => 'IMPL::DOM::Schema::Node'
|
|
29 };
|
|
30
|
236
|
31 sub CTOR {
|
237
|
32 my ($this,$docName,$docSchema,$transforms) = @_;
|
236
|
33
|
246
|
34 my $docNodeSchema = $docSchema->selectSingleNode(sub { $_->isa(SchemaNode) and $_->name eq $docName } )
|
236
|
35 or die OperationException->new("Can't find a node schema for the document '$docName'");
|
|
36
|
|
37 my $docClass = ($docNodeSchema->can('nativeType') ? $docNodeSchema->nativeType : undef) || 'IMPL::DOM::Document';
|
|
38
|
|
39 $this->documentSchema($docNodeSchema);
|
|
40
|
|
41 $this->_navi(
|
|
42 Builder->new(
|
|
43 $docClass,
|
|
44 $docSchema,
|
|
45 ignoreUndefined => 1
|
|
46 )
|
|
47 );
|
|
48 $this->_schema($docSchema);
|
|
49
|
|
50 $this->_navi->NavigateCreate($docName);
|
|
51 }
|
|
52
|
|
53 sub TransformPlain {
|
|
54 my ($this,$data) = @_;
|
|
55
|
|
56 $this->_navi->Current->nodeValue( $this->_navi->inflateValue($data) );
|
|
57 return $this->_navi->Current;
|
|
58 }
|
|
59
|
|
60 sub TransformHash {
|
|
61 my ($this,$data) = @_;
|
|
62
|
|
63 die ArgumentException->new(data => 'A HASH reference is required')
|
|
64 unless ref $data eq 'HASH';
|
|
65
|
|
66 KEYLOOP: foreach my $key (keys %$data) {
|
|
67 my $value = $data->{$key};
|
|
68
|
|
69 if (ref $value eq 'ARRAY') {
|
|
70 foreach my $subval (@$value) {
|
|
71
|
|
72 my $node = $this->_navi->NavigateCreate($key);
|
|
73
|
|
74 unless(defined $node) {
|
|
75 $this->_navi->Back();
|
|
76 next KEYLOOP;
|
|
77 }
|
|
78
|
|
79 $this->Transform($subval);
|
|
80
|
|
81 $this->_navi->Back();
|
|
82 }
|
|
83 } else {
|
|
84 my $node = $this->_navi->NavigateCreate($key);
|
|
85
|
|
86 unless(defined $node) {
|
|
87 $this->_navi->Back();
|
|
88 next KEYLOOP;
|
|
89 }
|
|
90
|
|
91 $this->Transform($value);
|
|
92
|
|
93 $this->_navi->Back();
|
|
94 }
|
|
95 }
|
|
96 return $this->_navi->Current;
|
|
97 }
|
|
98
|
|
99 sub TransformDefault {
|
|
100 my ($this,$data) = @_;
|
|
101
|
|
102 if ( ref $data and eval { $data->can('GetMeta') } ) {
|
|
103 my %props = map {
|
|
104 $_->name, 1
|
|
105 } $data->GetMeta(PropertyInfo, sub { $_->access == ACCESS_PUBLIC }, 1 );
|
|
106
|
|
107 my %values = map {
|
|
108 $_,
|
|
109 $data->$_();
|
|
110 } keys %props;
|
|
111
|
|
112 return $this->Transform(\%values);
|
|
113 } else {
|
|
114 die OperationException->new("Don't know how to transform $data");
|
|
115 }
|
|
116
|
|
117 return $this->_navi->Current;
|
|
118 }
|
|
119
|
|
120 sub buildErrors {
|
|
121 my ($this) = @_;
|
|
122
|
|
123 return $this->_navi->buildErrors;
|
|
124 }
|
|
125
|
|
126 1;
|
|
127
|
|
128 __END__
|
|
129
|
|
130 =pod
|
|
131
|
|
132 =head1 NAME
|
|
133
|
|
134 C<IMPL::DOM::Transform::ObjectToDOM> -преобразование объекта
|
|
135
|
|
136 =head1 SYNOPSIS
|
|
137
|
|
138 =cut |