#!/usr/bin/perl
package XmlManipulator;

use strict;
use XML::LibXML;
use XML::LibXML::XPathContext;

use mUtil::XmlExtractor;
use mUtil::XmlFormatter;

use base "XmlExtractor";


#sub new {
#	my $self = XmlExtractor->new(@_);
#	bless $self;
#	return $self;
#}

sub setCurrentNode {
	my $self = shift;
	$self->{currentNode} = shift;
}
sub setNodeData {
	my $self = shift;
	my $value = shift;	# optional
	my %attrs = @_;		# optional
	
	if (defined($value)) {
		# delete existing value
		for my $childNode ($self->{currentNode}->childNodes()) {
			if ($childNode->nodeType == XML::LibXML::XML_CDATA_SECTION_NODE) {
				$self->{currentNode}->removeChild($childNode);
			} elsif ($childNode->nodeType == XML::LibXML::XML_TEXT_NODE) {
				$self->{currentNode}->removeChild($childNode);
			}
		}
		# add new value
		my $textNode = $self->{'doc'}->createTextNode($value);
		$self->{currentNode}->addChild($textNode);		
	}
	
	if (%attrs) {
		# delete old attributes
		for (my $att_idx = 0; $att_idx < $self->{currentNode}->attributes->length; $att_idx++) {
			my $att_node = $self->{currentNode}->attributes->item(0);	
		}

		# add new attributes
		while (my ($attr_key, $attr_val) = each (%attrs)) {
			my $attr = $self->{'doc'}->createAttribute($attr_key, $attr_val);
			$self->{currentNode}->addChild($attr);
		}
	}
}
sub	printIt {
	my $self = shift; 
	return $self->{'doc'}->toString();
}


1;

