GRNOC-Config

 view release on metacpan or  search on metacpan

lib/GRNOC/Config.pm  view on Meta::CPAN


    ###ok ready to parse
    my $root=$self->{'doc'}->getDocumentElement();
    
    my @path_args = split('\/',$path);
    #looking for attributes?
    if($path_args[$#path_args] =~ /@/){
	my @nodes = $root->findnodes($path);

	my @tmp;
	if($#nodes >= 1 || $self->{'force_array'}){
	    #print STDERR "returning an array of values\n";
	    foreach my $node (@nodes){
		push(@tmp,$node->getValue());
	    }
	    return \@tmp;
	}else{
	    #print STDERR "returning a single value\n";
	    if(!defined($nodes[0])){
		return undef;
	    }
	    return $nodes[0]->getValue();
	}

	$self->{'error'}{'msg'} = "$path does not exist in the config";
	return undef;
    }else{
	#looking for nodes
	my @nodes = $root->findnodes( $path );
	my @tmp;
       
	if($#nodes >= 0){
	    if($#nodes >= 1 || $self->{'force_array'}){
		#print STDERR "Returning an array of nodes\n";
		foreach my $node (@nodes){
		    push(@tmp,XML::Simple::XMLin($node->toString(), ForceArray => $self->{'force_array'}));
		}
		#print STDERR Data::Dumper::Dumper(@tmp);
		if(defined($nodes[0])){
		    return \@tmp;
		}

	    }else{
		#print STDERR "Returning a single node\n";
		return XML::Simple::XMLin($nodes[0]->toString(),ForceArray => $self->{'force_array'});
		
	    }
	}
	$self->{'error'}{'msg'} = "$path does not exist in the config";
	return undef;
    }

    return undef ;
}


=head2 update_node

=cut

sub update_node{
    my $self = shift;
    my $path = shift;
    my $new_value = shift;


    if($path eq ''){
	$self->{'error'}{'msg'} = "Path not specified.  Please specify a path\n";
        return undef;
    }

    if(!defined($self->{'doc'})){
        $self->{'error'}{'msg'} = "Config not loaded!!\n";
        if($self->{'debug'}){
            print STDERR "cannot find doc!\n";
        }
        return undef;
    }

    $path .= "/text()";

    my $root = $self->{'doc'}->getDocumentElement();
    my $nodes = $root->find($path);
    my $node;

    if($nodes->size() == 1){
	$node = $nodes->pop();
    }else{
	$self->{'error'}{'msg'} = "Multiple Nodes at that Path please specify\n";
	return undef;
    }

    if(defined($new_value)){
	$node->setData($new_value);
    }

    my $res = _save_xml($self);
    return $res;
}

=head2 add_node


=cut

sub add_node{
    my $self = shift;
    my $path = shift;
    my $node_name = shift;
    my $node_value = shift;

    if($path eq ''){
        $self->{'error'}{'msg'} = "Path not specified.  Please specify a path\n";
        return undef;
    }

    if(!defined($self->{'doc'})){
        $self->{'error'}{'msg'} = "Config not loaded!!\n";
        if($self->{'debug'}){
	    print STDERR "cannot find doc!\n";
        }

lib/GRNOC/Config.pm  view on Meta::CPAN


    my $root = $self->{'doc'}->getDocumentElement();
    my $nodes = $root->find($path);
    my $node;
    
    while(my $node = $nodes->pop()){
	my $parent = $node->parentNode;
	$parent->removeChild($node);
    }

    return _save_xml($self);
}




=head2 delete_node

=cut

sub delete_node{
    my $self = shift;
    my $path = shift;

    if($path eq ''){
	$self->{'error'}{'msg'} = "Path not specified.  Please specify a path\n";
        return undef;
    }

    if(!defined($self->{'doc'})){
        $self->{'error'}{'msg'} = "Config not loaded!!\n";
        if($self->{'debug'}){
            print STDERR "cannot find doc!\n";
        }
        return undef;
    }

    my $root = $self->{'doc'}->getDocumentElement();
    my $nodes = $root->find($path);
    my $node;

    if($nodes->size() == 1){
        $node = $nodes->pop();
    }else{
        print STDERR "Multiple Nodes at that path\n";
        $self->{'error'}{'msg'} = "Multiple Nodes at that Path please specify\n";
        return undef;
    }

    my $parent = $node->parentNode;
    $parent->removeChild($node);

    return _save_xml($self);

}

=head2 update_attribute

=cut

sub update_attribute{
    my $self = shift;
    my $path = shift;
    my $attribute_name = shift;
    my $attribute_value = shift;

    if($path eq ''){
        $self->{'error'}{'msg'} = "Path not specified.  Please specify a path\n";
        return undef;
    }

    if($attribute_name eq ''){
	$self->{'error'}{'msg'} = "Attribute not specified.  Please specify an attribute\n";
	return undef;
    }

    if(!defined($self->{'doc'})){
        $self->{'error'}{'msg'} = "Config not loaded!!\n";
        if($self->{'debug'}){
            print STDERR "cannot find doc!\n";
        }
        return undef;
    }

    my $root = $self->{'doc'}->getDocumentElement();
    my $nodes = $root->find($path);
    my $node;

    if($nodes->size() == 1){
        $node = $nodes->pop();
    }else{
        $self->{'error'}{'msg'} = "Multiple Nodes at that Path please specify\n";
        return undef;
    }


    if(defined($node->getAttribute($attribute_name))){
	$node->setAttribute($attribute_name,$attribute_value);
    }else{
	$self->{'error'}{'msg'} = "That attribute is not defined, add the attribute before trying to update\n";
	return undef;
    }

    _save_xml($self);
}

=head2 add_attribute

=cut

sub add_attribute{
    my $self = shift;
    my $path = shift;
    my $attribute_name = shift;
    my $attribute_value = shift;
    
    if($path eq ''){
        $self->{'error'}{'msg'} = "Path not specified.  Please specify a path\n";
        return undef;
    }



( run in 3.695 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )