# ---*--- This is a Perl File ---*---

package xmlHandler;
{
  sub new{
    my ($type,$file) = (shift,shift);
    my $self = bless {}, $type;
    $self->{file}=$file;
    return $self;
  }
  # Return all items for group that have itemtype(example "patch id"
  sub GetItemList{
    my $self = shift;
    my ($group,$itemtype) = (shift,shift);
    my $session=0;
    my @list;
    open(FILE,"$self->{file}") || print STDOUT "Cannot read configuration file $self->{file}: $! \n";
    if (! -r FILE){return 1;}
    @lines = <FILE>;
    close(FILE);
    while($line = shift @lines){
      chomp($line);
      $line =~ s/^\s+//;
      next if ($line =~ /^\#/);
      if ($line =~ /^<$group/){$session = 1;next;}
      if ($line =~ /^<\/$group/){$session = 0;last;}
      if ($session){
	if ($line =~ /^<$itemtype/){
	  $line =~ /^<$itemtype=\"(.*)\".*/;
	  push(@list,$1);
	}
      }
    }
    #print "List is @list\n";
    return @list;
  }
  #return item for "item" for group
  sub GetItem{
    my $self  =shift;
    my ($group,$item)=(shift,shift);
    my $session = 0;
    open(FILE,"$self->{file}") || print STDOUT "Cannot read configuration file $self->{file}: $! \n";
    if (! -r FILE){return 1;}
    @lines = <FILE>;
    close(FILE);
    while($line = shift @lines){
      chomp($line);
      $line =~ s/^\s+//;
      next if ($line =~ /^\#/);
      if ($line =~ /^<$group/){$session = 1;next;}
      if ($line =~ /^<\/$group/){$session = 0;last;}
      if ($session){
	if ($line =~ /^<$itemtype/){
	  $line =~ /^<$itemtype=\"(.*)\".*/;
	  return $1;
	}
      }
    }
    return "";#item was not found
  }
  
  #return array of all items for session
  sub GetSession{
    my $self = shift;
    my $session = shift;
    my $count = 0;#count sessions
    my $start=0; # 0 - out of session ,1 - in the session
    open(FILE,"$self->{file}") || print STDOUT "Cannot read config file $self->{file}: $! \n";
    if (! -r FILE){return 1;}
    @lines = <FILE>;
    close(FILE);
    while($line = shift @lines){
      chomp($line);
      # $line = ~ s/^\s+//;
      next if ($line =~ /^\#/);
      if ($line =~ /^<$session/){$start=1;next;}
      if ($line =~ /^\/>/){# finish session
	$session = 0;
	$count++;
      }
      if ($start){
	$line=~ /(.*)=\"(.*)\"/;
	if ($1){
	  $self->{params}{$count}{$1}=$2;
	}
      }
    }
    return %param;
  }#end sub
}

1;
