Revision af205393
Von Bernd Blessmann vor fast 11 Jahren hinzugefügt
SL/Helper/Csv/Dispatcher.pm | ||
---|---|---|
24 | 24 |
sub dispatch { |
25 | 25 |
my ($self, $line) = @_; |
26 | 26 |
|
27 |
eval "require " . $self->_csv->profile->{class};
|
|
28 |
my $obj = $self->_csv->profile->{class}->new;
|
|
27 |
my $class = $self->_class_by_line($line);
|
|
28 |
croak 'no class given' unless $class;
|
|
29 | 29 |
|
30 |
for my $spec (@{ $self->_specs }) { |
|
30 |
eval "require " . $class; |
|
31 |
my $obj = $class->new; |
|
32 |
|
|
33 |
my $specs = $self->_specs_by_line($line); |
|
34 |
for my $spec (@{ $specs }) { |
|
31 | 35 |
$self->apply($obj, $spec, $line->{$spec->{key}}); |
32 | 36 |
} |
33 | 37 |
|
34 | 38 |
return $obj; |
35 | 39 |
} |
36 | 40 |
|
41 |
# return class for given line |
|
42 |
# if only one profile is given, return this profiles class |
|
43 |
# if more than one profile is given, identify class by first |
|
44 |
# column??? |
|
45 |
sub _class_by_line { |
|
46 |
my ($self, $line) = @_; |
|
47 |
|
|
48 |
my $class; |
|
49 |
if ($self->_csv->is_multiplexed) { |
|
50 |
foreach my $p (@{ $self->_csv->profile }) { |
|
51 |
my $row_ident = $p->{row_ident}; |
|
52 |
if ($line->{datatype} eq $row_ident) { |
|
53 |
$class = $p->{class}; |
|
54 |
last; |
|
55 |
} |
|
56 |
} |
|
57 |
} else { |
|
58 |
$class = @{ $self->_csv->profile }[0]->{class}; |
|
59 |
} |
|
60 |
|
|
61 |
return $class; |
|
62 |
} |
|
63 |
|
|
64 |
sub _specs_by_line { |
|
65 |
my ($self, $line) = @_; |
|
66 |
|
|
67 |
my $spec; |
|
68 |
my $i = 0; |
|
69 |
if ($self->_csv->is_multiplexed) { |
|
70 |
foreach my $p (@{ $self->_csv->profile }) { |
|
71 |
my $row_ident = $p->{row_ident}; |
|
72 |
if ($line->{datatype} eq $row_ident) { |
|
73 |
$spec = @{ $self->_specs }[$i]; |
|
74 |
last; |
|
75 |
} |
|
76 |
$i++; |
|
77 |
} |
|
78 |
} else { |
|
79 |
$spec = @{ $self->_specs }[0]; |
|
80 |
} |
|
81 |
|
|
82 |
return $spec; |
|
83 |
} |
|
84 |
|
|
85 |
|
|
37 | 86 |
sub apply { |
38 | 87 |
my ($self, $obj, $spec, $value) = @_; |
39 | 88 |
return unless $value; |
... | ... | |
70 | 119 |
sub parse_profile { |
71 | 120 |
my ($self, %params) = @_; |
72 | 121 |
|
73 |
my $header = $self->_csv->header; |
|
74 |
my $profile = $self->_csv->profile->{profile}; |
|
122 |
my $profile; |
|
123 |
my $class; |
|
124 |
my $header; |
|
125 |
my @specs; |
|
126 |
|
|
127 |
my $i = 0; |
|
128 |
foreach my $h (@{ $self->_csv->header }) { |
|
129 |
$header = $h; |
|
130 |
if ($self->_csv->profile) { |
|
131 |
$profile = @{ $self->_csv->profile }[$i]->{profile}; |
|
132 |
$class = @{ $self->_csv->profile }[$i]->{class}; |
|
133 |
} |
|
134 |
|
|
135 |
my $spec = $self->_parse_profile(profile => $profile, |
|
136 |
class => $class, |
|
137 |
header => $header); |
|
138 |
push @specs, $spec; |
|
139 |
$i++; |
|
140 |
} |
|
141 |
|
|
142 |
$self->_specs(\@specs); |
|
143 |
|
|
144 |
return ! $self->errors; |
|
145 |
} |
|
146 |
|
|
147 |
sub _parse_profile { |
|
148 |
my ($self, %params) = @_; |
|
149 |
|
|
150 |
my $profile = $params{profile}; |
|
151 |
my $class = $params{class}; |
|
152 |
my $header = $params{header}; |
|
153 |
|
|
75 | 154 |
my @specs; |
76 | 155 |
|
77 | 156 |
for my $col (@$header) { |
78 | 157 |
next unless $col; |
79 | 158 |
if ($self->_csv->strict_profile) { |
80 | 159 |
if (exists $profile->{$col}) { |
81 |
push @specs, $self->make_spec($col, $profile->{$col}); |
|
160 |
push @specs, $self->make_spec($col, $profile->{$col}, $class);
|
|
82 | 161 |
} else { |
83 | 162 |
$self->unknown_column($col, undef); |
84 | 163 |
} |
85 | 164 |
} else { |
86 | 165 |
if (exists $profile->{$col}) { |
87 |
push @specs, $self->make_spec($col, $profile->{$col}); |
|
166 |
push @specs, $self->make_spec($col, $profile->{$col}, $class);
|
|
88 | 167 |
} else { |
89 |
push @specs, $self->make_spec($col, $col); |
|
168 |
push @specs, $self->make_spec($col, $col, $class);
|
|
90 | 169 |
} |
91 | 170 |
} |
92 | 171 |
} |
93 | 172 |
|
94 |
$self->_specs(\@specs); |
|
95 | 173 |
$self->_csv->_push_error($self->errors); |
96 |
return ! $self->errors; |
|
174 |
|
|
175 |
return \@specs; |
|
97 | 176 |
} |
98 | 177 |
|
99 | 178 |
sub make_spec { |
100 |
my ($self, $col, $path) = @_; |
|
179 |
my ($self, $col, $path, $cur_class) = @_;
|
|
101 | 180 |
|
102 | 181 |
my $spec = { key => $col, steps => [] }; |
103 | 182 |
|
104 | 183 |
return unless $path; |
105 | 184 |
|
106 |
my $cur_class = $self->_csv->profile->{class}; |
|
107 |
|
|
108 | 185 |
return unless $cur_class; |
109 | 186 |
|
110 | 187 |
for my $step_index ( split /\.(?!\d)/, $path ) { |
Auch abrufbar als: Unified diff
SL::Helper::Csv kann mit Multiplex-Daten (mehreren Profilen) umgehen.