package Modules::Apache::Config::Parser::Parser;

########## Confixx(R) 3.0 Professional ############
####### Copyright SWsoft, Inc. 2004-2005 ##########
#### http://www.sw-soft.com - info@sw-soft.com ####





use strict;





use Modules::Apache::Config::Parser::Lines;
use Modules::Apache::Config::Parser::Section;





# -------------------- new -------------------- #
sub new{
    my ($proto) = @_;
    my ( $class, $self );
    $class = ref($proto) || $proto;
    $self = {};
    $self->{PARTS} = [];
    bless ($self, $class);
    return $self;
}
# -------------------- end new -------------------- #





# -------------------- add -------------------- #
sub add{
    my ( $self, $object ) = @_;
    push ( @{$self->{PARTS}}, $object );
}
# -------------------- end add -------------------- #





# -------------------- read -------------------- #
sub read{
	my ( $self, $file ) = @_;
	my ( $line, $last, @sections, $confixxState );
	
	while ( defined ( $line = $file->getline() ) ) {

		if ( !$confixxState && ( $line =~ /^\s*#+\s*CONFIXX/ ) ) {
			$confixxState = 1;
		} elsif ( ( $confixxState == 1 ) && ( $line =~ /^\s*#+\s*\/CONFIXX/ ) ) {
			$confixxState = 2;
		} elsif ( $confixxState == 2 ) {
			$confixxState = 0;
		}

	# --------------- end of section --------------- #
	if ( @sections > 0 && $sections[$#sections]->endMatch( $line ) ) {
	    $sections[$#sections]->setEnd( $line );
	    $sections[$#sections]->add( $last ) if ( $last );
	    $last = pop( @sections );
	    $self->_appendLast ( \@sections, \$last );
	    next;
	}
	# -------------------- end end of section --------------- #






	# -------------------- new section -------------------- #
	if ( Modules::Apache::Config::Parser::Section::startingTag( $line ) )	{
	    $self->_appendLast (\@sections,\$last);
	    push ( @sections, new Modules::Apache::Config::Parser::Section($line, $confixxState ) );
	    next;
	}
	# -------------------- end new section -------------------- #





	# -------------------- lines -------------------- #
	$last = new Modules::Apache::Config::Parser::Lines() unless ( $last );
	$last->add( $line, $confixxState );
	# -------------------- end lines -------------------- #

    }

    $self->_appendLast ( \@sections, \$last );
}
# -------------------- end read -------------------- #





# -------------------- _appendLast -------------------- #
sub _appendLast{
    my ( $self, $sections, $last ) = @_;
    if ($$last)
    {
	if ( @$sections > 0 )
	{
	    $sections->[$#$sections]->add ($$last);
	}
	else
	{
	    $self->add ($$last);
	}
	undef ($$last);
    }
}
# -------------------- end _appendLast -------------------- #





# -------------------- output -------------------- #
sub output{
    my ( $self, $file, $ip, $port ) = @_;
    map { $_->output($file) unless($_->exclude($ip,$port)) } @{$self->{PARTS}};
		unless ( $::mhost_conf ) {
			( $::mhost_conf = $::vhost_conf ) =~ s/confixx_vhost\.conf$/confixx_mhost.conf/;
		}
    $file->print ( "\nInclude $::mhost_conf\n" );
}
# -------------------- end output -------------------- #





# -------------------- getConfixxParameters -------------------- #
sub getConfixxParameters{
    my ( $self ) = @_;
    my ( @parameters, @first );
    foreach my $object ( @{$self->{PARTS}} )
    {
	if ( $object->isa("Modules::Apache::Config::Parser::Section") && $object->isConfixxHost() )
	{
	    @parameters = $object->getConfixxParameters();
	    return @parameters if ( $parameters[4] == "80" );
	    @first = @parameters unless (@first);
	}
    }
    return @first;
}
# -------------------- end getConfixxParameters -------------------- #





1;
