Changeset 253

Show
Ignore:
Timestamp:
04/24/07 16:18:21 (2 years ago)
Author:
kindlund
Message:

Initial testing of multi-value elements.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • honeyclient/branches/exp/kindlund-multi_config/lib/HoneyClient/Util/Config.pm

    r234 r253  
    357357 
    358358Even after performing a recursive search, if no variable name exists, 
    359 then the function will croak with errors. 
     359then the function will issue a warning and return undef. 
     360 
     361If the variable found is an element that contains child elements, then 
     362a corresponding hashtable will be returned.  For example, if we perform 
     363a getVar(name => "foo") on the following XML: 
     364 
     365<HoneyClient> 
     366    <foo> 
     367        <bar>123</bar> 
     368        <bar>456</bar> 
     369        <yok>789</yok> 
     370        <yok>xxx</yok> 
     371    </foo> 
     372</HoneyClient> 
     373 
     374Then the following $hashref will be returned: 
     375 
     376$hashref = { 
     377    'bar' => [ '123', '456' ], 
     378    'yok' => [ '789', 'xxx' ], 
     379
    360380 
    361381I<Inputs>: 
     
    367387should return the attribute associated with the variable's element. 
    368388 
    369 I<Output>: The variable's element/attribute value, if found; warns and 
    370 returns undef otherwise. 
     389I<Output>: The variable's element/attribute value or hashtable (for  
     390multi-value elements), if found; warns and returns undef otherwise. 
     391 
     392B<Note>: If the target variable to return is an element that contains 
     393B<combinations> of text and sub-elements, then only the text within 
     394the sub-elements will be returned in the previously mentioned 
     395$hashref format. 
     396 
     397For example, if we perform a getVar(name => "foo") on the following XML: 
     398 
     399<HoneyClient> 
     400    <foo> 
     401        THIS_TEXT_WILL_BE_LOST 
     402        <bar>123</bar> 
     403        <bar>456</bar> 
     404        <yok>789</yok> 
     405        <yok>xxx</yok> 
     406        <yok><CHILD>zzz</CHILD></yok> 
     407    </foo> 
     408</HoneyClient> 
     409 
     410Then the following $hashref will be returned: 
     411 
     412$hashref = { 
     413    'bar' => [ '123', '456' ], 
     414    'yok' => [ '789', 'xxx', 'zzz' ], 
     415
     416 
     417Notice how the B<THIS_TEXT_WILL_BE_LOST> string got dropped and that 
     418the B<E<lt>CHILDE<gt>> tags were silently stripped from the B<zzz> 
     419string.  In other words, in each target element, B<don't mix text 
     420with sub-elements> and B<don't nest sub-elements> if you want the  
     421nested structure preserved when a getVar() is called on the 
     422B<grandparent element>. 
    371423 
    372424=back 
     
    428480    # The first string is the path that matches the node we want and all ancestors 
    429481    # The second string tells us whether to get the text() or an attribute 
    430     my $exp = $namespace . "/ancestor-or-self::*/$args{name}/" . 
    431         (defined $args{attribute} ? "attribute::" . $args{attribute}:"text()"); 
     482    my $exp = $namespace . "/ancestor-or-self::*/$args{name}" . 
     483        (defined $args{attribute} ? "/attribute::" . $args{attribute} : ""); 
    432484    my $nodeset = $xp->findnodes($exp); 
    433485 
     
    440492        return; 
    441493    } 
    442     my $val = $nodeset->pop->string_value; 
    443  
    444     # Trail leading and trailing whitespace  
    445     $val =~ s/^\s+|\s+$//g; 
    446  
    447     # For some reason attributes return attribute_name="value" 
    448     $val =~ s/.*"(.*)".*/$1/; 
     494     
     495    # Figure out if the (most specific) node has any children. 
     496    my $parent = $nodeset->pop(); 
     497    $nodeset = $xp->findnodes("*", $parent); 
     498    my $val = undef; 
     499    if ($nodeset->size() <= 0) { 
     500        # There are no child elements, thus stingify 
     501        # all textual components. 
     502 
     503        $val = $parent->string_value(); 
     504 
     505        # Trail leading and trailing whitespace  
     506        $val =~ s/^\s+|\s+$//g; 
     507    } else {  
     508 
     509        # There are child elements; return a 
     510        # hashtable accordingly. 
     511        my @children = $nodeset->get_nodelist(); 
     512 
     513        # Now, build the hashtable of array references. 
     514        $val = {}; 
     515        foreach my $child (@children) { 
     516            push  (@{$val->{$child->getName()}}, $child->string_value()); 
     517        } 
     518    } 
    449519 
    450520    return $val; 
     
    452522 
    453523=pod 
    454  
    455 =head1 EXPORTS 
    456524 
    457525=head2 setVar(name => $varName, namespace => $caller, attribute => $attribute, value => $value)