File: Error.pm

package info (click to toggle)
psp 0.5.5-3
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k, sarge
  • size: 4,820 kB
  • ctags: 2,333
  • sloc: perl: 21,074; ansic: 4,553; sh: 2,407; makefile: 461; php: 11; pascal: 6
file content (268 lines) | stat: -rw-r--r-- 5,961 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
package PSP::Error;

# Copyright (c) 2000, FundsXpress Financial Network, Inc.
# This library is free software released under the GNU Lesser General
# Public License, Version 2.1.  Please read the important licensing and
# disclaimer information included below.

# $Id: Error.pm,v 1.1.1.2 2003/12/06 19:47:26 hartmans Exp $

use strict;

=head1 NAME

 PSP::Error - manages a heirarchy of errors.

=head1 SYNOPSIS

 #more to come.

=head1 DESCRIPTION

more to come.

=cut

use PSP::parent;
@PSP::Error::ISA = qw(PSP::parent);

=head1 METHODS

=head2 new

 class
 (PSP::Error $this)
   new (PSP::FieldSpace $fs, hash \%field_set, 
	str $name, string[] \@children_order, string [] \@field_order)

DESCRIPTION:

This is the constructor for this package\'s object. Basically, this
simply takes all the input data and makes it member data of the
object. Note that C<$children_order> and C<$field_order> must be
complete orderings and it is the callers responsibility to ensure that
they all. Generally the children of this class should provide a
default ordering which can be overidden; thus allowing users to supply
null or partial lists, or possibly not worry about it.

=cut

sub new {
  my ($proto, $name, $fs, $args) = @_;
  $args ||= [];

  my $this = $proto->SUPER::new();

  $this->name($name);
  $this->fieldspace($fs);
  $this->{_label} = undef;
  $this->{_args} = $args;
  ($this->{_type} = ref($proto)||$proto) =~ s/PSP::Error::(.*?)$/\L$1/ or
    $this->{_type} = 'base';

  return $this;
}

=head2 matches

 instance
 (boolean does_match) matches (string pattern)

DESCRIPTION:

=cut 

sub matches {
  my ($this,$pattern) = @_;
  return $this->{_name} eq $pattern;
}

=head2 name and fieldspace

 instance
 (str $name) name ([str $name])

 instance
 (fieldspace $fs) fieldspace ([fieldspace $fs])

 instance
 (str $label) label ([str $label])

DESCRIPTION:

Returns and possibly sets the name and/or fieldspace for this error object.

=cut

sub name {
  my ($this,$name) = @_;
  defined $name and $this->{_name} = $name;
  return $this->{_name};
}
sub fieldspace {
  my ($this,$fieldspace) = @_;
  defined $fieldspace and $this->{_fieldspace} = $fieldspace;
  return $this->{_fieldspace};
}
sub label {
  my ($this,$label) = @_;
  defined $label and $this->{_label} = $label;
  return "" if ! defined $this->{_label};
  return $this->{_label};
}

=head2 add_error and remove_error

 instance
 (boolean $success) add_error (str $message[, string labels[] ])
 instance
 (boolean $success) remove_error ([ string labels[] ])

DESCRIPTION:

Adds an error possibly identified by labels[] to this.  Returns true on 
success.

=cut

sub add_error {
  my ($this, $arg, @labels) = @_;

  # find the node in the error tree where the message array will be.
  my $node = $this;
  for my $name (@labels) {
    my $child_node = $node->find_child($name);
    $node = $child_node || $node->add_child($node->new($name));
  }
  # add input error arg.
  push @{$node->{_args}}, $arg;
}
sub remove_error {
  my ($this, @labels) = @_;

  # find the node in the error tree where the message array will be.
  my $node = $this;
  my $prev_node;
  for my $name (@labels) {
    my $child_node = $node->find_child($name);
    $prev_node = $node;
    $node = $child_node or return;
  }
  $prev_node and $prev_node->del_child($node);
}

=head2 as_string

Should print the error messages associated with the fields associated
with the error object.

=cut

sub as_string {
  my ($this,$node,$indent) = @_;
  $node ||= $this;
  $indent ||= "";

  my $str = "";
  if (defined @{$node->{_args}}) {
    $str .= $node->label().
      join("",map { $indent.$_."\n" } @{$node->{_args}});
  }

  $indent .= "  ";
  for my $child ($node->children()) {
    $str .= $child->as_string($child,$indent);
  }

  return $str;
}

sub as_html {
  my ($this, $node, $indent) = @_;
  $node ||= $this;
  $indent ||= "";

  my $str = "";
  if (@{$node->{_args}}) {
    $str .= $node->label().$indent.
      join("<br>\n$indent", @{$node->{_args}})."<br>\n";
  }

  $indent .= "&nbsp; ";
  for my $child ($node->children) {
    $str .= $child->as_html($child,$indent);
  }

  return $str;
}

sub as_bullets {
  my ($this, $node, $indent) = @_;
  $node ||= $this;
  $indent ||= "";

  my $str = "";
  if (@{$node->{_args}}) {
    $str .= $node->label().$indent.
      join("<br>\n$indent", @{$node->{_args}})."\n";
  }

  $indent .= "  ";
  my $children_str = "";
  for my $child ($node->children) {
    my $new_str = $child->as_bullets($child,$indent) or next;
    $children_str .= "<li>\n".$new_str;
  }
  $children_str and $str .= "<ul>\n$children_str</ul>\n";

  return $str;
}

sub isa {
  my ($this, $type) = @_;
  return ($type eq $this->{_type});
}

sub free_internals {
  my ($this) = @_;

  $this->SUPER::free();

  delete $this->{fieldspace};
}

1;
__END__

=head1 BUGS

No known bugs, but this does not mean no bugs exist.

=head1 SEE ALSO

L<PSP::parent>

=head1 COPYRIGHT

 PSP - Perl Server Pages
 Copyright (c) 2000, FundsXpress Financial Network, Inc.

 This library is free software; you can redistribute it and/or
 modify it under the terms of the GNU Lesser General Public
 License as published by the Free Software Foundation; either
 version 2 of the License, or (at your option) any later version.

 BECAUSE THIS LIBRARY IS LICENSED FREE OF CHARGE, THIS LIBRARY IS
 BEING PROVIDED "AS IS WITH ALL FAULTS," WITHOUT ANY WARRANTIES
 OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING, WITHOUT
 LIMITATION, ANY IMPLIED WARRANTIES OF TITLE, NONINFRINGEMENT,
 MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, AND THE
 ENTIRE RISK AS TO SATISFACTORY QUALITY, PERFORMANCE, ACCURACY,
 AND EFFORT IS WITH THE YOU.  See the GNU Lesser General Public
 License for more details.

 You should have received a copy of the GNU Lesser General Public
 License along with this library; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA

=cut