/** GSWRepetition.m -
GSWeb: Class GSWRepetition
Copyright (C) 1999-2002 Free Software Foundation, Inc.
Written by: Manuel Guesdon
Date: Jan 1999
$Revision$
$Date$
This file is part of the GNUstep Web Library.
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.
**/
static char rcsId[] = "$Id$";
#include
//====================================================================
@implementation GSWRepetition
//--------------------------------------------------------------------
-(id)initWithName:(NSString*)name
associations:(NSDictionary*)associations
contentElements:(NSArray*)elements
{
//OK
LOGObjectFnStart();
if ((self=[super initWithName:name
associations:nil
template:nil]))
{
_list=[[associations objectForKey:list__Key
withDefaultObject:[_list autorelease]] retain];
NSDebugMLLog(@"gswdync",@"list=%@",_list);
_item=[[associations objectForKey:item__Key
withDefaultObject:[_item autorelease]] retain];
NSDebugMLLog(@"gswdync",@"item=%@",_item);
if (_item && ![_item isValueSettable])
{
ExceptionRaise0(@"GSWRepetition",@"'item' parameter must be settable");
};
_identifier=[[associations objectForKey:identifier__Key
withDefaultObject:[_identifier autorelease]] retain];
NSDebugMLLog(@"gswdync",@"identifier=%@",_identifier);
_count=[[associations objectForKey:count__Key
withDefaultObject:[_count autorelease]] retain];
NSDebugMLLog(@"gswdync",@"count=%@",_count);
_index=[[associations objectForKey:index__Key
withDefaultObject:[_index autorelease]] retain];
NSDebugMLLog(@"gswdync",@"index=%@",_index);
if (_index && ![_index isValueSettable])
{
ExceptionRaise0(@"GSWRepetition",@"'index' parameter must be settable");
};
if (elements)
{
_childrenGroup=[[GSWHTMLStaticGroup alloc]initWithContentElements:elements];
};
};
LOGObjectFnStop();
return self;
};
//--------------------------------------------------------------------
-(id)initWithName:(NSString*)name
associations:(NSDictionary*)associations
template:(GSWElement*)templateElement
{
LOGObjectFnStart();
self=[self initWithName:name
associations:associations
contentElements:templateElement ? [NSArray arrayWithObject:templateElement] : nil];
LOGObjectFnStop();
return self;
};
//--------------------------------------------------------------------
-(void)dealloc
{
DESTROY(_list);
DESTROY(_item);
DESTROY(_identifier);
DESTROY(_count);
DESTROY(_index);
DESTROY(_childrenGroup);
[super dealloc];
}
//--------------------------------------------------------------------
-(void)setDefinitionName:(NSString*)definitionName
{
[super setDefinitionName:definitionName];
if (definitionName && _childrenGroup)
[_childrenGroup setDefinitionName:[NSString stringWithFormat:@"%@-StaticGroup",definitionName]];
};
//--------------------------------------------------------------------
-(NSString*)description
{
return [NSString stringWithFormat:@"<%s %p>",
object_get_class_name(self),
(void*)self];
};
//====================================================================
@implementation GSWRepetition (GSWRepetitionA)
//--------------------------------------------------------------------
-(void)appendToResponse:(GSWResponse*)response
inContext:(GSWContext*)context
{
//OK
GSWComponent* component=nil;
NSArray* listValue=nil;
int i=0;
int countValue=0;
#ifndef NDEBBUG
int elementsNb=[(GSWElementIDString*)[context elementID]elementsNb];
#endif
LOGObjectFnStart();
GSWStartElement(context);
GSWSaveAppendToResponseElementID(context);
component=[context component];
NSDebugMLLog(@"gswdync",@"_list=%@",_list);
if (_list)
{
listValue=[_list valueInComponent:component];
NSAssert2(!listValue || [listValue respondsToSelector:@selector(count)],
@"The list (%@) (of class:%@) doesn't respond to 'count'",
_list,
[listValue class]);
countValue=[listValue count];
};
NSDebugMLLog(@"gswdync",@"_count=%@",_count);
if (_count)
{
id tmpCountValue=[_count valueInComponent:component];
int tmpCount=0;
NSAssert3(!tmpCountValue || [tmpCountValue respondsToSelector:@selector(intValue)],
@"The 'count' (%@) value %@ (of class:%@) doesn't respond to 'intValue'",
_count,
tmpCountValue,
[tmpCountValue class]);
tmpCount=[tmpCountValue intValue];
NSDebugMLLog(@"gswdync",@"tmpCount=%d",tmpCount);
if (_list)
countValue=min(tmpCount,countValue);
else
countValue=tmpCount;
};
NSDebugMLLog(@"gswdync",@"countValue=%d",countValue);
[context incrementLoopLevel];
for(i=0;icurrentIndex) {
NSDebugMLLog(@"gswdync",@"[list objectAtIndex:currentIndex]=%@",[list objectAtIndex:currentIndex]);
[_item setValue:[list objectAtIndex:currentIndex]
inComponent:component];
} else {
//NSLog(@"startOneIterationWithIndex SKIPPING setValue:inComponent index=%d list.count=%d",currentIndex, [list count]);
}
}
NSDebugMLLog(@"gswdync",@"currentIndex=%d",currentIndex);
NSDebugMLLog(@"gswdync",@"_index=%@",_index);
if (_index)
[_index setValue:[NSNumber numberWithShort:currentIndex]
inComponent:component];
if (currentIndex==0)
[context appendZeroElementIDComponent];
else
[context incrementLastElementIDComponent];
}
NS_HANDLER
{
localException=ExceptionByAddingUserInfoObjectFrameInfo0(localException,@"In startOneIterationWithIndex");
[localException raise];
}
NS_ENDHANDLER;
LOGObjectFnStop();
};
@end