/** GSWConditional.m - <title>GSWeb: Class GSWConditional</title>

   Copyright (C) 1999-2004 Free Software Foundation, Inc.
   
   Written by:	Manuel Guesdon <mguesdon@orange-concept.com>
   Date:        Jan 1999
   
   $Revision: 37929 $
   $Date: 2014-05-30 20:28:04 +0200 (Fri, 30 May 2014) $
   $Id: GSWConditional.m 37929 2014-05-30 18:28:04Z buzzdee $
   
   <abstract></abstract>

   This file is part of the GNUstep Web Library.

   <license>
   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.
   
   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.
   
   You should have received a copy of the GNU Library General Public
   License along with this library; if not, write to the Free
   Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
   </license>
**/

#include "config.h"

#include "GSWeb.h"

/**
Bindings

	condition	if evaluated to YES (or NO if negate evaluted to YES), the enclosed code is emitted/used

  negate		If evaluated to yes, negate the condition (defaut=NO)
**/

static GSWIMP_BOOL standardEvaluateConditionInContextIMP = NULL;

static Class standardClass = Nil;

//====================================================================
@implementation GSWConditional

//--------------------------------------------------------------------
+ (void) initialize
{
  if (self == [GSWConditional class])
    {
      standardClass=[GSWConditional class];

      standardEvaluateConditionInContextIMP = 
        (GSWIMP_BOOL)[self instanceMethodForSelector:evaluateConditionInContextSEL];
    };
};

//--------------------------------------------------------------------
-(id)initWithName:(NSString*)aName
     associations:(NSDictionary*)someAssociations
         template:(GSWElement*)templateElement
{
  if ((self = [super initWithName:nil
		     associations:nil
		     template: templateElement]))
    {
      // here, we do not need to remove associations
      ASSIGN(_condition, [someAssociations objectForKey: condition__Key]);
      ASSIGN(_negate, [someAssociations objectForKey: negate__Key]);
      
      if (_condition == nil)
	{
	  [NSException raise:NSInvalidArgumentException
		       format:@"%s: Missing 'condition' attribute.",
		       __PRETTY_FUNCTION__];      
	}
    }
  return self;
};


//--------------------------------------------------------------------
-(void)dealloc
{
  DESTROY(_condition);
  DESTROY(_negate);

  [super dealloc];
}

//--------------------------------------------------------------------
-(NSString*)description
{
  return [NSString stringWithFormat:@"<%s %p condition: %@ negate: %@>",
				   object_getClassName(self),
				   (void*)self, _condition, _negate];
};


//--------------------------------------------------------------------
-(void)takeValuesFromRequest:(GSWRequest*)request
                   inContext:(GSWContext*)context
{
  GSWComponent * component = GSWContext_component(context);
  BOOL conVal = [_condition boolValueInComponent:component];
  BOOL doNegate = NO;
  if (_negate != nil)
    doNegate = [_negate boolValueInComponent:component];

  if ((conVal && !doNegate) 
      || (!conVal && doNegate))
    {
      [super takeValuesFromRequest:request
	     inContext:context];
    }
}

//--------------------------------------------------------------------
-(id <GSWActionResults>)invokeActionForRequest:(GSWRequest*) request
                                    inContext:(GSWContext*) context
{
  id <GSWActionResults> result=nil;
  GSWComponent * component = GSWContext_component(context);
  BOOL conVal = [_condition boolValueInComponent:component];
  BOOL doNegate = NO;
  if (_negate != nil)
    doNegate = [_negate boolValueInComponent:component];

  if ((conVal && !doNegate)
      || (!conVal && doNegate))
    {
      result=[super invokeActionForRequest:request
		    inContext:context];
    }
  return result;
}

//--------------------------------------------------------------------
-(void)appendToResponse:(GSWResponse*) response
              inContext:(GSWContext*) context
{
  GSWComponent * component = GSWContext_component(context);
  BOOL conVal = [_condition boolValueInComponent:component];
  BOOL doNegate = NO;
  if (_negate != nil)
    doNegate = [_negate boolValueInComponent:component];
  
  if ((conVal && !doNegate)
      || (!conVal && doNegate))
    {
      [super appendChildrenToResponse:response
	     inContext:context];
    }
}

@end

