annotate Lib/IMPL/DOM/Transform/ObjectToDOM.pm @ 247:2270de2469ff

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