summaryrefslogtreecommitdiffabout
path: root/lib/Config/HAProxy/Node.pm
blob: 82100676c4f57de061e13fcafe41764616334934 (plain)
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
package Config::HAProxy::Node;
use strict;
use warnings;
use Carp;
use Config::HAProxy::Iterator;

sub new {
    my $class = shift;
    local %_ = @_;
    bless {
	_kw => $_{kw},
	_argv => $_{argv} // [],
	_orig => $_{orig},
	_locus => $_{locus},
	_parent => $_{parent},
	_index => -1
    }, $class;
}

my @ATTRIBUTES = qw(kw orig locus parent index);

{
    no strict 'refs';
    foreach my $attribute (@ATTRIBUTES) {
	*{ __PACKAGE__ . '::' . $attribute } = sub {
	    my $self = shift;
	    if (defined(my $val = shift)) {
		croak "too many arguments" if @_;
		$self->{'_'.$attribute} = $val;
	    }
	    return $self->{'_'.$attribute};
	}
    }
}

sub argv {
    my $self = shift;
    if (my $val = shift) {
	croak "too many arguments" if @_;
	$self->{_argv} = $val;
    }
    return @{$self->{_argv}};
}

sub arg {
    my $self = shift;
    my $n = shift;
    if (my $val = shift) {
	croak "too many arguments" if @_;
	$self->{_argv}[$n] = $val;
    }
    return $self->{_argv}[$n];
}

sub drop {
    my $self = shift;
    $self->parent->delete_node($self->index);
}

sub iterator {
    return new Config::HAProxy::Iterator(@_);
}

sub depth {
    my $self = shift;
    my $n = 0;
    while ($self = $self->parent) {
	$n++;
    }
    return $n - 1;
}

sub root {
    my $self = shift;
    while ($self->parent()) {
	$self = $self->parent();
    }
    return $self;
}

sub as_string {
    my $self = shift;
    if (defined(my $v = $self->orig)) {
	return $v;
    }
    return '' unless $self->kw;
    return $self->orig(join(' ', ($self->kw, $self->argv())));
}

# use overload
#     '""' => sub { shift->as_string };

sub is_root { 0 }
sub is_section { 0 }
sub is_statement { 0 }
sub is_empty { 0 }
sub is_comment { 0 }

1;
__END__

=head1 NAME

Config::HAProxy::Node - Abstract HAProxy configuration node

=head1 DESCRIPTION

The class B<Config::HAProxy::Node> represents an abstract node in the
HAProxy configuration parse tree. It serves as a base class for classes
representing configuration tree, section, simple statement, comment and
empty line.

=head1 CONSTRUCTOR

    $obj = new Config::HAProxy::Node(%args);

Returns new object. B<%args> can contain the following keys:

=over 4

=item B<kw>

Configuration keyword (string),

=item B<argv>

Reference to the list of arguments.
    
=item B<orig>

Original text as read from the configuration file.

=item B<locus>

Locus (a B<Text::Locus> object) where this statement occurred.

=item B<parent>

Parent node.

=back

=head1 METHODS

=head2 B<kw>, B<argv>, B<orig>, B<locus>, B<parent>

These methods return the corresponding field of the node. When called
with an argument, they set the field prior to returning it. The B<argv>
method returns array of strings and takes as its argument a reference to
the array of strings:

    @a = $node->argv;

    $node->argv([@a]);

=head2 index  

Index (0-based) of this node in the parent node.
    
=head2 arg

    $a = $node->arg($n)

Returns the B<$n>th argument (0-based) from the argument list.

=head2 drop

    $node->drop;

Removes this node and destroys it.

=head2 iterator

    $itr = $node->iterator(@args);

Returns the iterator for this node. See B<Config::HAProxy::Iterator> for
a detailed discussion.

=head2 depth

    $n = $node->depth;

Returns the depth of this node in the configuration tree. Depth is the
number of parent nodes between the root of tree and this node. Top-level
nodes have depth 0.

=head2 root

    $root_node = $node->root;

Returns the root node of the parse tree this node belongs to.

=head2 as_string

    $s = $node->as_string;

Returns canonical string representation of this node. The canonical
representation consists of the keyword followed by arguments delimited
with horizontal space characters.

=head1 ABSTRACT METHODS

Derived classes must overload at least one of the following methods:
    
=head2 is_root

True if the node is a root node, false otherwise.
    
=head2 is_section

True if the node represents a section (i.e. contains subnodes).
    
=head2 is_statement

True if the node is a simple statement.
    
=head2 is_empty

True if the node represents an empty line.
    
=head2 is_comment

True if the node represents a comment.

=head1 SEE ALSO

B<Config::HAProxy::Node::Comment>,    
B<Config::HAProxy::Node::Empty>,    
B<Config::HAProxy::Node::Root>,    
B<Config::HAProxy::Node::Section>,    
B<Config::HAProxy::Node::Statement>,    
B<Config::HAProxy::Iterator>,
B<Config::HAProxy>,
B<Text::Locus>.
    
=cut

Return to:

Send suggestions and report system problems to the System administrator.