GeniusTrader/MetaInfo.pm untested

filter: Statementstmt 0.0% 0 / 77 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 127 CC 13 SCAR 30.1
CC
13
Cov
0%
get
42.0
set
30.0
list
6.0
CRAP 20.4
1 package Finance::GeniusTrader::MetaInfo;
2  
3 # Copyright 2000-2002 Raphaƫl Hertzog, Fabien Fulhaber
4 # This file is distributed under the terms of the General Public License
5 # version 2 or (at your option) any later version.
6  
7 use strict;
8  
9 use XML::LibXML;
10  
11 =head1 NAME
12  
13 Finance::GeniusTrader::MetaInfo - keep various meta informations
14  
15 =head1 DESCRIPTION
16  
17 =head2 Goal
18  
19 This object is used to gather meta informations of different kinds
20 applying to various objects (state of a trading system, history of order,
21 support & resistance of prices, top & bottom prices on a period,
22 lines drawn on a graph, ...). It stores various informations in an
23 internal XML structure that can be stored in a file and reloaded later.
24  
25 Informations are stored on a key/value basis. Key is a path in an
26 XML DOM tree (/ is the separator like for xpath expressions). Several
27 values can be stored in a single key if you use attributes
28 to distinguish them.
29  
30 =head2 API
31  
32 =over
33  
34 =item C<< my $info = Finance::GeniusTrader::MetaInfo->new; >>
35  
36 Create a new empty Finance::GeniusTrader::MetaInfo object.
37  
38 =cut
39 sub new {
40 my $type = shift;
41 my $class = ref($type) || $type;
42  
43 my $self = {};
44  
45 $self->{'_parser'} = XML::LibXML->new();
46  
47 return bless $self, $class;
48 }
49  
50 =item C<< $info->set($key, $value, { "attrname" => "attrvalue" }); >>
51  
52 Stores $value corresponding to $key with an attribute "attrname" (whose
53 value is "attrvalue"). The third parameter is optional if you don't need
54 attributes for this key.
55  
56 If the key already existed, then the value is replaced.
57  
58 =cut
59 sub set {
60 my ($self, $key, $value, $attr) = @_;
61  
62 # If the document hasn't been created (aka loaded), create it
63 if (!defined($self->{'doc'}))
64 {
65 $self->{'doc'} = XML::LibXML::Document->new("1.0", "ISO-8859-1");
66 $self->{'doc'}->setDocumentElement(
67 $self->{'doc'}->createElement('metainfo'));
68 }
69  
70 # Split the key in successive element names to lookup
71 # The last one is treated apart
72 my @elts = split("/", $key);
73 my $last = $elts[scalar(@elts) - 1];
74 delete $elts[scalar(@elts) - 1];
75  
76 my $elt = $self->{'doc'}->getDocumentElement;
77 my @list;
78 # Browse the tree and build nodes when needed
79 foreach (@elts) {
80 if (scalar(@list = $elt->getElementsByTagName($_))) {
81 $elt = $list[0];
82 } else {
83 $elt->appendChild($self->{'doc'}->createElement($_));
84 $elt = $elt->lastChild;
85 }
86 }
87 # Create the xpath expression to find a node corresponding to the one
88 # that we'll add, so that we can replace it
89 my $xpath = _build_xpath_expr($last, $attr);
90 # Create the new node
91 my $node = $self->{'doc'}->createElement($last);
92 if (defined($attr)) {
93 foreach (keys %{$attr}) {
94 $node->setAttribute($_, $attr->{$_});
95 }
96 }
97 $node->appendText($value);
98 # Add the new node (eventually remove the old one)
99 if (scalar(@list = $elt->findnodes($xpath))) {
100 $elt->insertBefore($node, $list[0]);
101 $list[0]->unbindNode();
102 } else {
103 $elt->appendChild($node);
104 $elt->appendTextNode("\n\n");
105 }
106 }
107  
108 =item C<< $info->get($key, { "attrname" => "attrvalue" }); >>
109  
110 Get the value corresponding the $key and the attributes indicated in the
111 second optional argument.
112  
113 =cut
114 sub get {
115 my ($self, $key, $attr) = @_;
116  
117 # Stop if we have no document
118 return undef if (! defined($self->{'doc'}));
119
120 # Build the xpath expression
121 $key = _build_xpath_expr($key, $attr);
122
123 # Find the corresponding node
124 my @elt = $self->{'doc'}->getDocumentElement()->findnodes($key);
125 # Stop if no node has been found
126 return undef if (! scalar(@elt));
127 # Conver the node list in a list of values
128 my @data;
129 foreach (@elt)
130 {
131 push @data, $_->firstChild->nodeValue;
132 }
133 # Return either the first value or all depending on the context
134 if (scalar(@data) == 0) {
135 return undef;
136 } elsif (scalar(@data) >= 1) {
137 return wantarray ? @data : $data[0];
138 }
139 }
140  
141 =item C<< my @list = $info->list($key, { "attrname" => "attrvalue" }); >>
142  
143 List all the set of attributes available for elements corresponding
144 to the $key. You can eventually restrict the list to a subset og them
145 by specifying one or more attributes.
146  
147 Each element of @list is a hash describing the attributes. If you want
148 the value of that node you have to use $info->get(...).
149  
150 =cut
151 sub list {
152 my ($self, $key, $attr) = @_;
153
154 # Stop if we have no document
155 return if (! defined($self->{'doc'}));
156
157 # Build the xpath expression to find the nodes
158 $key = _build_xpath_expr($key, $attr);
159  
160 # Find the corresponding nodes and return their attributes
161 my @list;
162 foreach my $node ($self->{'doc'}->getDocumentElement()->findnodes($key))
163 {
164 my $attributes = {};
165 foreach ($node->attributes)
166 {
167 $attributes->{$_->nodeName} = $_->nodeValue;
168 }
169 push @list, $attributes;
170 }
171 return @list;
172 }
173
174 =item C<< $info->load("/path/to/file.xml") >>
175  
176 Load the XML file as MetaInfo object.
177  
178 =cut
179 sub load {
180 my ($self, $file) = @_;
181
182 $self->{'filename'} = $file;
183 $self->{'doc'} = $self->{'_parser'}->parse_file($file);
184 }
185  
186 =item C<< $info->save("/path/to/file.xml") >>
187  
188 Save the current Finance::GeniusTrader::MetaInfo object in the given XML file. All values
189 previously set can be reloaded later with $info->load(...).
190  
191 =cut
192 sub save {
193 my ($self, $file) = @_;
194  
195 if (defined($file)) {
196 $self->{'filename'} = $file;
197 }
198 open (XMLFILE, "> $self->{'filename'}")
199 || die "Can't open $self->{'filename'} for writing: $!\n";
200 print XMLFILE $self->{'doc'}->toString;
201 close XMLFILE;
202 }
203  
204 =item C<< $info->dump >>
205  
206 Output the current internal XML file to the standard output. Useful
207 for debug purposes.
208  
209 =cut
210 sub dump {
211 my ($self) = @_;
212 print $self->{'doc'}->toString;
213 }
214  
215 # Private helper functions
216 sub _build_xpath_expr {
217 my ($key, $attr) = @_;
218
219 # Transform the key in a xpath expression to find the good node
220 if (defined($attr))
221 {
222 $key .= "[";
223 $key .= join " and ", map { "\@$_='$attr->{$_}'" } keys %{$attr};
224 $key .= "]";
225 }
226 return $key;
227 }
228  
229 =pod
230  
231 =back
232  
233 =cut
234 1;