line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
#!/usr/local/bin/perl
|
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
package Text::PORE::Object;
|
4
|
|
|
|
|
|
|
|
5
|
1
|
|
|
1
|
|
6
|
use Exporter;
|
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
1521
|
|
6
|
|
|
|
|
|
|
@Text::PORE::Object::ISA = qw(Exporter);
|
7
|
|
|
|
|
|
|
$Text::PORE::Object::VERSION = "0.05";
|
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
sub new
|
10
|
|
|
|
|
|
|
{
|
11
|
6
|
|
|
6
|
1
|
667
|
my ($type) = shift;
|
12
|
6
|
|
|
|
|
23
|
my (%att_list) = @_;
|
13
|
6
|
|
|
|
|
13
|
my ($self) = {};
|
14
|
|
|
|
|
|
|
|
15
|
6
|
|
|
|
|
16
|
foreach $key (keys %att_list) {
|
16
|
17
|
|
|
|
|
44
|
$self->{"\L$key\E"} = $att_list{$key};
|
17
|
|
|
|
|
|
|
}
|
18
|
|
|
|
|
|
|
|
19
|
6
|
|
|
|
|
57
|
bless $self;
|
20
|
6
|
|
|
|
|
35
|
$self;
|
21
|
|
|
|
|
|
|
}
|
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
#######################################
|
25
|
|
|
|
|
|
|
# getAttribute($name)
|
26
|
|
|
|
|
|
|
#######################################
|
27
|
|
|
|
|
|
|
sub getAttribute() {
|
28
|
0
|
|
|
0
|
1
|
0
|
return GetAttribute(@_);
|
29
|
|
|
|
|
|
|
};
|
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
#######################################
|
32
|
|
|
|
|
|
|
# setAttribute($name=>$value)
|
33
|
|
|
|
|
|
|
#######################################
|
34
|
|
|
|
|
|
|
sub setAttribute() {
|
35
|
0
|
|
|
0
|
1
|
0
|
LoadAttributes(@_);
|
36
|
|
|
|
|
|
|
}
|
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
#######################################
|
39
|
|
|
|
|
|
|
# setAttributes($name1=>$value1, $name2=>$value2, ..., $nameN=>$valueN)
|
40
|
|
|
|
|
|
|
#######################################
|
41
|
|
|
|
|
|
|
sub setAttributes() {
|
42
|
0
|
|
|
0
|
1
|
0
|
LoadAttributes(@_);
|
43
|
|
|
|
|
|
|
}
|
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
sub GetClassType
|
47
|
|
|
|
|
|
|
{
|
48
|
0
|
|
|
0
|
0
|
0
|
my($type) = shift;
|
49
|
0
|
|
|
|
|
0
|
my($dummy);
|
50
|
0
|
|
|
|
|
0
|
($type,$dummy) = split(/\=/,$type);
|
51
|
0
|
|
|
|
|
0
|
return ($type);
|
52
|
|
|
|
|
|
|
}
|
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
#######################################
|
56
|
|
|
|
|
|
|
# Given the attribute name,
|
57
|
|
|
|
|
|
|
# return the value of itself, closest ancestor or default
|
58
|
|
|
|
|
|
|
#######################################
|
59
|
|
|
|
|
|
|
sub GetAttribute
|
60
|
|
|
|
|
|
|
{
|
61
|
124
|
|
|
124
|
0
|
165
|
my ($self) = shift;
|
62
|
124
|
|
|
|
|
144
|
my ($att) = @_;
|
63
|
124
|
|
|
|
|
200
|
$att = "\L$att\E";
|
64
|
124
|
50
|
|
|
|
737
|
return ($self->{$att}) if (defined $self->{$att});
|
65
|
|
|
|
|
|
|
|
66
|
0
|
|
|
|
|
0
|
my ($obj_id);
|
67
|
0
|
0
|
|
|
|
0
|
if ($obj_id = $self->{"ID\_$att"}) {
|
|
|
0
|
|
|
|
|
|
68
|
|
|
|
|
|
|
#####################################
|
69
|
|
|
|
|
|
|
# this attribute exists, but the object is now allocated yet
|
70
|
|
|
|
|
|
|
# allocate the object now
|
71
|
|
|
|
|
|
|
#####################################
|
72
|
0
|
|
|
|
|
0
|
my($class) = $::obj_type_2_class{$self->{"TYPE_$att"}};
|
73
|
0
|
0
|
|
|
|
0
|
if ($class) {
|
74
|
0
|
|
|
|
|
0
|
require "$class.pm"; import $class;
|
|
0
|
|
|
|
|
0
|
|
75
|
0
|
0
|
|
|
|
0
|
if ($::_PRINT_NEW_) {
|
76
|
0
|
|
|
|
|
0
|
print "new $class(id=>$obj_id)\n";
|
77
|
|
|
|
|
|
|
}
|
78
|
0
|
|
|
|
|
0
|
$self->{$att} = $class->new (id=>$obj_id);
|
79
|
|
|
|
|
|
|
}
|
80
|
0
|
|
|
|
|
0
|
return ($self->{$att});
|
81
|
|
|
|
|
|
|
}
|
82
|
|
|
|
|
|
|
elsif ($self->{'parent'}) {
|
83
|
|
|
|
|
|
|
#####################################
|
84
|
|
|
|
|
|
|
# this attribute dosn't exists,
|
85
|
|
|
|
|
|
|
# look one level up
|
86
|
|
|
|
|
|
|
#####################################
|
87
|
0
|
|
|
|
|
0
|
return ($self->{'parent'}->GetAttribute($att));
|
88
|
|
|
|
|
|
|
}
|
89
|
|
|
|
|
|
|
else {
|
90
|
|
|
|
|
|
|
#########################################
|
91
|
|
|
|
|
|
|
# this attribute is not found anywhere within myself and
|
92
|
|
|
|
|
|
|
# my ancestors, return the default one
|
93
|
|
|
|
|
|
|
#########################################
|
94
|
|
|
|
|
|
|
#print "default:[$att]=[$::default_attribs{$att}]";
|
95
|
0
|
|
|
|
|
0
|
return ($::default_attribs{$att});
|
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
}
|
98
|
|
|
|
|
|
|
}
|
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
######################################
|
101
|
|
|
|
|
|
|
# returns a reference to a list of all attribute names
|
102
|
|
|
|
|
|
|
######################################
|
103
|
|
|
|
|
|
|
sub GetAllAttributeNames
|
104
|
|
|
|
|
|
|
{
|
105
|
0
|
|
|
0
|
0
|
0
|
my($self) = shift;
|
106
|
0
|
|
|
|
|
0
|
my ($att,@att_list);
|
107
|
|
|
|
|
|
|
|
108
|
0
|
|
|
|
|
0
|
$self->FinalizeAllAttributes;
|
109
|
0
|
|
|
|
|
0
|
foreach $att (sort keys %{$self}) {
|
|
0
|
|
|
|
|
0
|
|
110
|
0
|
0
|
|
|
|
0
|
if ($att =~ /^ID_|^TYPE_/) { next; }
|
|
0
|
|
|
|
|
0
|
|
111
|
0
|
|
|
|
|
0
|
push (@att_list, $att);
|
112
|
|
|
|
|
|
|
}
|
113
|
0
|
|
|
|
|
0
|
return (\@att_list);
|
114
|
|
|
|
|
|
|
}
|
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
######################################
|
117
|
|
|
|
|
|
|
# Finalize All the Attributes
|
118
|
|
|
|
|
|
|
# Internal Function, outsiders should not care about it.
|
119
|
|
|
|
|
|
|
######################################
|
120
|
|
|
|
|
|
|
sub FinalizeAllAttributes
|
121
|
|
|
|
|
|
|
{
|
122
|
0
|
|
|
0
|
0
|
0
|
my($self) = shift;
|
123
|
0
|
|
|
|
|
0
|
my ($att,$class,$obj_id);
|
124
|
0
|
|
|
|
|
0
|
foreach $att(keys %{$self}) {
|
|
0
|
|
|
|
|
0
|
|
125
|
0
|
0
|
|
|
|
0
|
if ($att =~ /^ID_(.+)$/) {
|
126
|
0
|
|
|
|
|
0
|
$obj_id = $self->{$att};
|
127
|
0
|
|
|
|
|
0
|
$att = $1;
|
128
|
0
|
|
|
|
|
0
|
$class = $::obj_type_2_class{$self->{"TYPE_$att"}};
|
129
|
0
|
0
|
|
|
|
0
|
if ($class) {
|
130
|
0
|
|
|
|
|
0
|
require "$class.pm"; import $class;
|
|
0
|
|
|
|
|
0
|
|
131
|
0
|
0
|
|
|
|
0
|
if ($::_PRINT_NEW_) {
|
132
|
0
|
|
|
|
|
0
|
print "new $class(id=>$obj_id)\n";
|
133
|
|
|
|
|
|
|
}
|
134
|
0
|
|
|
|
|
0
|
$self->{$att} = $class->new (id=>$obj_id);
|
135
|
|
|
|
|
|
|
}
|
136
|
0
|
|
|
|
|
0
|
$self->{"ID_$att"} = $self->{"TYPE_$att"} = undef;
|
137
|
|
|
|
|
|
|
}
|
138
|
|
|
|
|
|
|
}
|
139
|
|
|
|
|
|
|
}
|
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
######################################
|
142
|
|
|
|
|
|
|
# returns a reference to a hash
|
143
|
|
|
|
|
|
|
# the hash keys are the attribute names
|
144
|
|
|
|
|
|
|
# the hash values are the attribute values
|
145
|
|
|
|
|
|
|
# an example:
|
146
|
|
|
|
|
|
|
# $page = new Page(id=>$id);
|
147
|
|
|
|
|
|
|
# $hash_ref = $page->GetAllAttributes;
|
148
|
|
|
|
|
|
|
# foreach $attr (keys %$hash_ref) {
|
149
|
|
|
|
|
|
|
# print "name: $attr, value: $hash_ref->{$attr}";
|
150
|
|
|
|
|
|
|
# ....
|
151
|
|
|
|
|
|
|
######################################
|
152
|
|
|
|
|
|
|
sub GetAllAttributes
|
153
|
|
|
|
|
|
|
{
|
154
|
0
|
|
|
0
|
0
|
0
|
my($self) = shift;
|
155
|
0
|
|
|
|
|
0
|
my ($att,%obj);
|
156
|
0
|
|
|
|
|
0
|
$self->FinalizeAllAttributes;
|
157
|
0
|
|
|
|
|
0
|
foreach $att (sort keys %{$self}) {
|
|
0
|
|
|
|
|
0
|
|
158
|
0
|
0
|
|
|
|
0
|
if ($att =~ /^ID_|^TYPE_/) { next; }
|
|
0
|
|
|
|
|
0
|
|
159
|
|
|
|
|
|
|
else {
|
160
|
0
|
|
|
|
|
0
|
$obj{$att} = $self->{$att};
|
161
|
|
|
|
|
|
|
}
|
162
|
|
|
|
|
|
|
}
|
163
|
0
|
|
|
|
|
0
|
return (\%obj);
|
164
|
|
|
|
|
|
|
}
|
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
######################################
|
169
|
|
|
|
|
|
|
# Print All Attributes and Values for debugging purpose
|
170
|
|
|
|
|
|
|
######################################
|
171
|
|
|
|
|
|
|
sub PrintAllAttributes
|
172
|
|
|
|
|
|
|
{
|
173
|
0
|
|
|
0
|
0
|
0
|
my($self) = shift;
|
174
|
0
|
|
|
|
|
0
|
my $att_ref = $self->GetAllAttributes;
|
175
|
0
|
|
|
|
|
0
|
my ($attr,$val);
|
176
|
0
|
|
|
|
|
0
|
while (($attr,$val) = (each %{$att_ref})) {
|
|
0
|
|
|
|
|
0
|
|
177
|
0
|
|
|
|
|
0
|
print "'$attr'=[";
|
178
|
0
|
0
|
|
|
|
0
|
if (ref $val eq 'ARRAY') {
|
179
|
|
|
|
|
|
|
### multi-value attribute
|
180
|
0
|
|
|
|
|
0
|
print "multi: @$val";
|
181
|
|
|
|
|
|
|
}
|
182
|
0
|
|
|
|
|
0
|
else { print $val; }
|
183
|
0
|
|
|
|
|
0
|
print "] \n";
|
184
|
|
|
|
|
|
|
}
|
185
|
|
|
|
|
|
|
}
|
186
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
######################################
|
190
|
|
|
|
|
|
|
# Return 1 if these multi-value attribute
|
191
|
|
|
|
|
|
|
# $attr has value $value
|
192
|
|
|
|
|
|
|
# Return 0 otherwise
|
193
|
|
|
|
|
|
|
######################################
|
194
|
|
|
|
|
|
|
sub MultiValAttrHas
|
195
|
|
|
|
|
|
|
{
|
196
|
0
|
|
|
0
|
0
|
0
|
my($self) = shift;
|
197
|
0
|
|
|
|
|
0
|
my($attr,$val) = @_;
|
198
|
|
|
|
|
|
|
|
199
|
0
|
0
|
|
|
|
0
|
if (ref $self->{$attr} ne 'ARRAY') { return 0; }
|
|
0
|
|
|
|
|
0
|
|
200
|
0
|
|
|
|
|
0
|
foreach (@{$self->{$attr}}) {
|
|
0
|
|
|
|
|
0
|
|
201
|
0
|
0
|
|
|
|
0
|
if ($_ eq $val) { return 1; }
|
|
0
|
|
|
|
|
0
|
|
202
|
|
|
|
|
|
|
}
|
203
|
0
|
|
|
|
|
0
|
return 0;
|
204
|
|
|
|
|
|
|
}
|
205
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
|
207
|
|
|
|
|
|
|
#################################################
|
208
|
|
|
|
|
|
|
# Load Attributes
|
209
|
|
|
|
|
|
|
# In: pair(s) of attribute_name and attribute_value
|
210
|
|
|
|
|
|
|
# Example:
|
211
|
|
|
|
|
|
|
# $object->LoadAttributes($name1=>$value1,$name2=>$value2,...);
|
212
|
|
|
|
|
|
|
#################################################
|
213
|
|
|
|
|
|
|
sub LoadAttributes
|
214
|
|
|
|
|
|
|
{
|
215
|
27
|
|
|
27
|
0
|
40
|
my($self) = shift;
|
216
|
27
|
|
|
|
|
177
|
my(%att_list) = @_;
|
217
|
|
|
|
|
|
|
|
218
|
27
|
|
|
|
|
83
|
foreach $key (keys %att_list) {
|
219
|
26
|
|
|
|
|
105
|
$self->{"\L$key\E"} = $att_list{$key};
|
220
|
|
|
|
|
|
|
}
|
221
|
|
|
|
|
|
|
|
222
|
27
|
|
|
|
|
80
|
return $self;
|
223
|
|
|
|
|
|
|
}
|
224
|
|
|
|
|
|
|
|
225
|
|
|
|
|
|
|
###############################################
|
226
|
|
|
|
|
|
|
# given an attribute name, an id and a type,
|
227
|
|
|
|
|
|
|
# create a object, which is my child
|
228
|
|
|
|
|
|
|
# if id is 0, then the object is a scalar, use
|
229
|
|
|
|
|
|
|
# type as its value
|
230
|
|
|
|
|
|
|
###############################################
|
231
|
|
|
|
|
|
|
sub MakeChild
|
232
|
|
|
|
|
|
|
{
|
233
|
0
|
|
|
0
|
0
|
|
my $self = shift;
|
234
|
0
|
|
|
|
|
|
my($child_name, $id, $type) = @_;
|
235
|
|
|
|
|
|
|
|
236
|
|
|
|
|
|
|
###############################################
|
237
|
|
|
|
|
|
|
# it's not a object but a value, return value($type)
|
238
|
|
|
|
|
|
|
###############################################
|
239
|
0
|
0
|
|
|
|
|
if (!$id) {
|
240
|
0
|
|
|
|
|
|
$self->{$child_name} = $type;
|
241
|
|
|
|
|
|
|
}
|
242
|
|
|
|
|
|
|
|
243
|
|
|
|
|
|
|
###############################################
|
244
|
|
|
|
|
|
|
# it's a object
|
245
|
|
|
|
|
|
|
###############################################
|
246
|
0
|
|
|
|
|
|
my $class = $::obj_type_2_class{$type};
|
247
|
0
|
0
|
|
|
|
|
if ($class) {
|
248
|
0
|
|
|
|
|
|
require "$class.pm"; import $class;
|
|
0
|
|
|
|
|
|
|
249
|
0
|
0
|
|
|
|
|
if ($::_PRINT_NEW_) {
|
250
|
0
|
|
|
|
|
|
print "new $class(id=>$obj_id)\n";
|
251
|
|
|
|
|
|
|
}
|
252
|
0
|
|
|
|
|
|
my $obj = $class->new(id=>$id,parent=>$self);
|
253
|
0
|
|
|
|
|
|
$self->{$child_name} = $obj;
|
254
|
|
|
|
|
|
|
}
|
255
|
0
|
|
|
|
|
|
else { return undef; }
|
256
|
|
|
|
|
|
|
}
|
257
|
|
|
|
|
|
|
|
258
|
|
|
|
|
|
|
|
259
|
|
|
|
|
|
|
###############################################
|
260
|
|
|
|
|
|
|
# Atts2QueryString
|
261
|
|
|
|
|
|
|
# convert attributes to QueryString
|
262
|
|
|
|
|
|
|
###############################################
|
263
|
|
|
|
|
|
|
sub Atts2QueryString
|
264
|
|
|
|
|
|
|
{
|
265
|
0
|
|
|
0
|
0
|
|
my $self = shift;
|
266
|
0
|
|
|
|
|
|
my %atts = @_;
|
267
|
0
|
|
|
|
|
|
$self->LoadAttributes(%atts);
|
268
|
|
|
|
|
|
|
|
269
|
0
|
|
|
|
|
|
my $string = undef;
|
270
|
0
|
|
|
|
|
|
my $key;
|
271
|
|
|
|
|
|
|
my $value;
|
272
|
0
|
|
|
|
|
|
foreach $key (keys %{$self}) {
|
|
0
|
|
|
|
|
|
|
273
|
0
|
0
|
|
|
|
|
if ($key eq 'parent') { next; }
|
|
0
|
|
|
|
|
|
|
274
|
0
|
|
|
|
|
|
$value = urlencode ($self->{$key});
|
275
|
0
|
|
|
|
|
|
$key = urlencode_word ($key);
|
276
|
0
|
|
|
|
|
|
$string .= "$key=$value\&";
|
277
|
|
|
|
|
|
|
}
|
278
|
|
|
|
|
|
|
|
279
|
0
|
|
|
|
|
|
return $string;
|
280
|
|
|
|
|
|
|
}
|
281
|
|
|
|
|
|
|
|
282
|
|
|
|
|
|
|
1;
|
283
|
|
|
|
|
|
|
__END__
|