* GSWeb/GSWCheckBoxList.m

fix appendToResponse:inContext:
  respect GNUstep coding standard (curly brackets placement, etc.)
  use GSWAssignAndRemoveAssociation() in initWithName:associations:template:
* GSWeb/GSWElement.[hm]
  add GSWAssignAndRemoveAssociation() to make controls code simplier



git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/gsweb/trunk@37850 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
Manuel Guesdon 2014-05-06 16:28:44 +00:00
parent 6194720fed
commit af78e8fe37
4 changed files with 237 additions and 216 deletions

View file

@ -1,4 +1,11 @@
2014-04-29 Manuel Guesdon <mguesdon@orange-concept.com> 2014-05-06 Manuel Guesdon <mguesdon@orange-concept.com>
* GSWeb/GSWCheckBoxList.m
fix appendToResponse:inContext:
respect GNUstep coding standard (curly brackets placement, etc.)
use GSWAssignAndRemoveAssociation() in initWithName:associations:template:
* GSWeb/GSWElement.[hm]
add GSWAssignAndRemoveAssociation() to make controls code simplier
w2014-04-29 Manuel Guesdon <mguesdon@orange-concept.com>
* GSWeb/GSWApplication.m: * GSWeb/GSWApplication.m:
fix -baseURL fix -baseURL
* GSWeb/GSWResourceManager.m: * GSWeb/GSWResourceManager.m:

View file

@ -53,9 +53,6 @@ Bindings
selections Array of selected objects (used to pre-check checkboxes and modified to reflect user choices) selections Array of selected objects (used to pre-check checkboxes and modified to reflect user choices)
It contains objects from list, not value binding evaluated ones ! It contains objects from list, not value binding evaluated ones !
selectionValues Array of selected values (used to pre-check checkboxes and modified to reflect user choices)
It contains evaluated values binding !
name Name of the element in the form (should be unique). If not specified, GSWeb assign one. name Name of the element in the form (should be unique). If not specified, GSWeb assign one.
disabled If evaluated to yes, the check box appear inactivated. disabled If evaluated to yes, the check box appear inactivated.
@ -88,69 +85,41 @@ static Class standardClass = Nil;
associations:(NSDictionary*)associations associations:(NSDictionary*)associations
template:(GSWElement*)template template:(GSWElement*)template
{ {
self = [super initWithName:aName associations:associations template: template]; if ((self = [super initWithName:aName associations:associations template: template]))
if (!self) { {
return nil; _loggedSlow = NO;
}
_loggedSlow = NO; GSWAssignAndRemoveAssociation(&_list,_associations,list__Key);
GSWAssignAndRemoveAssociation(&_item,_associations,item__Key);
GSWAssignAndRemoveAssociation(&_index,_associations,index__Key);
GSWAssignAndRemoveAssociation(&_selections,_associations,selections__Key);
GSWAssignAndRemoveAssociation(&_prefix,_associations,prefix__Key);
GSWAssignAndRemoveAssociation(&_prefix,_associations,prefix__Key);
GSWAssignAndRemoveAssociation(&_suffix,_associations,suffix__Key);
GSWAssignAndRemoveAssociation(&_escapeHTML,_associations,escapeHTML__Key);
ASSIGN(_list, [_associations objectForKey: list__Key]); if (GSWAssignAndRemoveAssociation(&_displayString,_associations,displayString__Key)==nil)
if (_list != nil) { {
[_associations removeObjectForKey: list__Key]; GSWAssignAndRemoveAssociation(&_displayString,_associations,value__Key);
} _defaultEscapeHTML = NO;
ASSIGN(_item, [_associations objectForKey: item__Key]); }
if (_item != nil) { else
[_associations removeObjectForKey: item__Key]; _defaultEscapeHTML = YES;
}
ASSIGN(_index, [_associations objectForKey: index__Key]);
if (_index != nil) {
[_associations removeObjectForKey: index__Key];
}
ASSIGN(_selections, [_associations objectForKey: selections__Key]);
if (_selections != nil) {
[_associations removeObjectForKey: selections__Key];
}
ASSIGN(_prefix, [_associations objectForKey: prefix__Key]);
if (_prefix != nil) {
[_associations removeObjectForKey: prefix__Key];
}
ASSIGN(_suffix, [_associations objectForKey: suffix__Key]);
if (_suffix != nil) {
[_associations removeObjectForKey: suffix__Key];
}
ASSIGN(_displayString, [_associations objectForKey: displayString__Key]);
if (_displayString != nil) {
[_associations removeObjectForKey: displayString__Key];
}
ASSIGN(_escapeHTML, [_associations objectForKey: escapeHTML__Key]);
if (_escapeHTML != nil) {
[_associations removeObjectForKey: escapeHTML__Key];
}
if (_displayString == nil) {
ASSIGN(_displayString, [_associations objectForKey: value__Key]); if (_list == nil
if (_displayString != nil) { || ((_value != nil || _displayString != nil)
[_associations removeObjectForKey: value__Key]; && (_item == nil || ![_item isValueSettable]))
|| (_selections != nil && ![_selections isValueSettable]))
{
[NSException raise:NSInvalidArgumentException
format:@"%s: 'list' must be present. 'item' must not be a constant if 'displayString' or 'value' is present. 'selection' must not be a constant if present.",
__PRETTY_FUNCTION__];
}
} }
_defaultEscapeHTML = NO;
} else {
_defaultEscapeHTML = YES;
}
if ((_list == nil) || ((_value != nil || _displayString != nil) && ((_item == nil) || (![_item isValueSettable]))) ||
((_selections != nil) && (![_selections isValueSettable]))) {
[NSException raise:NSInvalidArgumentException
format:@"%s: 'list' must be present. 'item' must not be a constant if 'displayString' or 'value' is present. 'selection' must not be a constant if present.",
__PRETTY_FUNCTION__];
}
return self; return self;
} }
-(void)dealloc -(void)dealloc
{ {
DESTROY(_list); DESTROY(_list);
@ -183,52 +152,60 @@ static Class standardClass = Nil;
inContext:(GSWContext*)context inContext:(GSWContext*)context
{ {
GSWComponent * component = GSWContext_component(context); GSWComponent * component = GSWContext_component(context);
if ((_selections != nil) && (![self disabledInComponent:component]) && ([context _wasFormSubmitted])) { if (_selections != nil
NSString * ctxName = [self nameInContext:context]; && ![self disabledInComponent:component]
NSArray * formValues = [request formValuesForKey: ctxName]; && [context _wasFormSubmitted])
NSMutableArray * mutArray = nil; {
int count = 0; NSArray* selections = nil;
int count2 = 0; NSString * ctxName = [self nameInContext:context];
int i = 0; NSArray * formValues = [request formValuesForKey: ctxName];
int formValuesCount = [formValues count];
if ((formValues != nil) && (count = [formValues count])) { if (formValuesCount==0)
mutArray = [NSMutableArray arrayWithCapacity:count]; selections = [NSArray array];
} else { else
mutArray = [NSMutableArray arrayWithCapacity:5]; {
NSArray* listValue = [_list valueInComponent:component];
int listCount = 0;
int i=0;
if ([listValue isKindOfClass:[NSArray class]] == NO)
{
[NSException raise:NSInvalidArgumentException
format:@"%s: Evaluating 'list' binding returned a '%@' class and not a NSArray.",
__PRETTY_FUNCTION__, [listValue class]];
}
listCount = [listValue count];
selections = [NSMutableArray arrayWithCapacity:formValuesCount];
for (i = 0; i < listCount; i++)
{
id item = [listValue objectAtIndex:i];
id value = nil;
[_item setValue: item
inComponent: component];
value = [_value valueInComponent:component];
if (value != nil)
{
if ([formValues containsObject:NSStringWithObject(value)])
[(NSMutableArray*)selections addObject:item];
}
else
{
NSLog(@"%s 'value' evaluated to nil in component %@.\nUnable to select item %@",
__PRETTY_FUNCTION__, self, value);
}
}
}
[_selections setValue: selections
inComponent: component];
} }
if ((formValues != nil) && (count > 0)) {
id listValue = [_list valueInComponent:component];
if ([listValue isKindOfClass:[NSArray class]] == NO) {
[NSException raise:NSInvalidArgumentException
format:@"%s: Evaluating 'list' binding returned a '%@' class and not a NSArray.",
__PRETTY_FUNCTION__, [listValue class]];
}
count2 = [listValue count];
for (i = 0; i < count2; i++) {
id obj1 = [(NSArray*) listValue objectAtIndex:i];
id obj2 = nil;
[_item setValue:obj1 inComponent: component];
obj2= [_value valueInComponent:component];
if (obj2 != nil) {
if ([formValues containsObject:obj2]) {
[mutArray addObject:obj1];
}
} else {
NSLog(@"%s 'value' evaluated to nil in component %@.\nUnable to select item %@",
__PRETTY_FUNCTION__, self, obj1);
}
}
}
[_selections setValue:mutArray inComponent: component];
}
} }
-(void)_fastTakeValuesFromRequest:(GSWRequest*)request -(void)_fastTakeValuesFromRequest:(GSWRequest*)request
@ -236,136 +213,156 @@ static Class standardClass = Nil;
{ {
GSWComponent * component = GSWContext_component(context); GSWComponent * component = GSWContext_component(context);
if ((_selections != nil) && (![self disabledInComponent:component]) && ([context _wasFormSubmitted])) { if (_selections != nil
&& ![self disabledInComponent:component]
&& [context _wasFormSubmitted])
{
NSArray* selections = nil;
NSString * ctxName = [self nameInContext:context];
NSArray * formValues = [request formValuesForKey: ctxName];
int formValuesCount = [formValues count];
if (formValuesCount==0)
{
selections = [NSArray array];
}
else
{
NSArray* listValue = [_list valueInComponent:component];
int listCount = 0;
int i = 0;
NSString * ctxName = [self nameInContext:context]; if ([listValue isKindOfClass:[NSArray class]] == NO)
NSArray * formValues = [request formValuesForKey: ctxName]; {
int count = 0; [NSException raise:NSInvalidArgumentException
int i = 0; format:@"%s: Evaluating 'list' binding returned a '%@' class and not a NSArray.",
NSMutableArray * mutablearray; __PRETTY_FUNCTION__, [listValue class]];
}
count = (formValues == nil) ? 0 : [formValues count]; listCount = [listValue count];
mutablearray = [NSMutableArray arrayWithCapacity:count];
for (i = 0; i < listCount; i++)
if (count > 0) { {
id listValue = [_list valueInComponent:component]; int itemIndex = [NSStringWithObject([formValues objectAtIndex:i]) intValue];
[(NSMutableArray*)selections addObject:[listValue objectAtIndex:itemIndex]];
if ([listValue isKindOfClass:[NSArray class]] == NO) { }
[NSException raise:NSInvalidArgumentException }
format:@"%s: Evaluating 'list' binding returned a '%@' class and not a NSArray.", [_selections setValue: selections
__PRETTY_FUNCTION__, [listValue class]]; inComponent: component];
}
for (i = 0; i < count; i++) {
int j = [NSStringWithObject([formValues objectAtIndex:i]) intValue];
[mutablearray addObject:[listValue objectAtIndex:j]];
}
} }
[_selections setValue:mutablearray inComponent: component];
}
} }
-(void)appendToResponse:(GSWResponse*)response -(void)appendToResponse:(GSWResponse*)response
inContext:(GSWContext*)context inContext:(GSWContext*)context
{ {
int count = 0;
int j = 0;
GSWComponent * component = GSWContext_component(context); GSWComponent * component = GSWContext_component(context);
BOOL doEscape;
NSString * ctxName = [self nameInContext:context];
id listValue = [_list valueInComponent:component]; id listValue = [_list valueInComponent:component];
id selectionsValue = nil; int listCount = 0;
doEscape = (_escapeHTML == nil) ? _defaultEscapeHTML : [_escapeHTML boolValueInComponent:component]; if ([listValue isKindOfClass:[NSArray class]] == NO)
if ([listValue isKindOfClass:[NSArray class]] == NO) {
[NSException raise:NSInvalidArgumentException
format:@"%s: Evaluating 'list' binding returned a '%@' class and not a NSArray.",
__PRETTY_FUNCTION__, [listValue class]];
}
selectionsValue = _selections == nil ? nil : [_selections valueInComponent:component];
if (selectionsValue != nil) {
count = [selectionsValue count];
}
for (j = 0; j < count; j++) {
NSString * prefixStr = nil;
NSString * suffixStr = nil;
NSString * dispStr = nil;
id obj2 = nil;
id displayValue = nil;
id valueValue = nil;
if ((_prefix != nil)) {
prefixStr = NSStringWithObject([_prefix valueInComponent:component]);
}
if ((_suffix != nil)) {
suffixStr = NSStringWithObject([_suffix valueInComponent:component]);
}
if (_index != nil) {
[_index setValue:GSWIntToNSString(j)
inComponent:component];
}
obj2 = [listValue objectAtIndex:j];
if ((_item != nil) && (_displayString != nil)) {
[_item setValue:obj2
inComponent:component];
displayValue = [_displayString valueInComponent:component];
if (displayValue == nil) {
dispStr = NSStringWithObject(obj2);
NSLog(@"%s: 'displayString' evaluated to nil in component %@. Using %@",
__PRETTY_FUNCTION__, component, dispStr);
} else {
dispStr = NSStringWithObject(displayValue);
}
} else {
dispStr = NSStringWithObject(obj2);
}
GSWResponse_appendContentAsciiString(response, @"<input name=\"");
GSWResponse_appendContentString(response,ctxName);
GSWResponse_appendContentAsciiString(response,@"\" type=checkbox value=\"");
if (_value != nil)
{ {
valueValue = [_value valueInComponent:component]; [NSException raise:NSInvalidArgumentException
if (valueValue != nil) { format:@"%s: Evaluating 'list' binding returned a '%@' class and not a NSArray.",
GSWResponse_appendContentHTMLConvertString(response, NSStringWithObject(valueValue)); __PRETTY_FUNCTION__, [listValue class]];
} else {
NSLog(@"%s: 'value' evaluated to nil in component %@. Using to index.",
__PRETTY_FUNCTION__, self);
}
} }
if (valueValue == nil) {
GSWResponse_appendContentAsciiString(response,GSWIntToNSString(j));
}
if ([selectionsValue containsObject:obj2]) {
GSWResponse_appendContentAsciiString(response,@"\" checked>");
} else {
GSWResponse_appendContentAsciiString(response,@"\">");
}
if (prefixStr != nil) {
GSWResponse_appendContentString(response,prefixStr);
}
if (doEscape)
{
GSWResponse_appendContentHTMLConvertString(response,dispStr);
} else {
GSWResponse_appendContentString(response,dispStr);
}
if (suffixStr != nil) {
GSWResponse_appendContentString(response,suffixStr);
}
} // for
listCount=[listValue count];
if (listCount>0)
{
NSString* ctxName = [self nameInContext:context];
int i = 0;
BOOL doEscape = NO;
id selections = nil;
if (_escapeHTML==nil)
doEscape=_defaultEscapeHTML;
else
doEscape=[_escapeHTML boolValueInComponent:component];
if (_selections!=nil)
selections = [_selections valueInComponent:component];
for (i = 0; i < listCount; i++)
{
NSString * prefixStr = nil;
NSString * suffixStr = nil;
NSString * displayString = nil;
id item = nil;
id value = nil;
if (_prefix != nil)
prefixStr = NSStringWithObject([_prefix valueInComponent:component]);
if (_suffix != nil)
suffixStr = NSStringWithObject([_suffix valueInComponent:component]);
if (_index != nil)
{
[_index setValue:GSWIntToNSString(i)
inComponent:component];
}
item = [listValue objectAtIndex:i];
if (_item != nil
&& _displayString != nil)
{
id displayValue=nil;
[_item setValue:item
inComponent:component];
displayValue= [_displayString valueInComponent:component];
if (displayValue == nil)
{
displayString = NSStringWithObject(displayValue);
NSLog(@"%s: 'displayString' evaluated to nil in component %@. Using %@",
__PRETTY_FUNCTION__, component, displayString);
}
else
{
displayString = NSStringWithObject(displayValue);
}
}
else
displayString = NSStringWithObject(item);
GSWResponse_appendContentAsciiString(response, @"<input name=\"");
GSWResponse_appendContentString(response,ctxName);
GSWResponse_appendContentAsciiString(response,@"\" type=checkbox value=\"");
if (_value != nil)
{
value = [_value valueInComponent:component];
if (value != nil)
{
GSWResponse_appendContentHTMLConvertString(response, NSStringWithObject(value));
}
else
{
NSLog(@"%s: 'value' evaluated to nil in component %@. Using to index.",
__PRETTY_FUNCTION__, self);
}
}
if (value == nil)
GSWResponse_appendContentAsciiString(response,GSWIntToNSString(i));
if ([selections containsObject:item])
GSWResponse_appendContentAsciiString(response,@"\" checked>");
else
GSWResponse_appendContentAsciiString(response,@"\">");
if (prefixStr != nil)
GSWResponse_appendContentString(response,prefixStr);
if (doEscape)
GSWResponse_appendContentHTMLConvertString(response,displayString);
else
GSWResponse_appendContentString(response,displayString);
if (suffixStr != nil)
GSWResponse_appendContentString(response,suffixStr);
} // for
}
} }
-(BOOL)appendStringAtRight:(id)_unkwnon -(BOOL)appendStringAtRight:(id)_unkwnon
@ -388,16 +385,18 @@ static Class standardClass = Nil;
-(void)takeValuesFromRequest:(GSWRequest*)request -(void)takeValuesFromRequest:(GSWRequest*)request
inContext:(GSWContext*)context inContext:(GSWContext*)context
{ {
if (_value != nil) { if (_value != nil)
if (!_loggedSlow) { {
NSLog(@"%s Warning: Avoid using the 'value' binding as it is much slower than omitting it, and it is just cosmetic.", if (!_loggedSlow)
__PRETTY_FUNCTION__); {
_loggedSlow = YES; NSLog(@"%s Warning: Avoid using the 'value' binding as it is much slower than omitting it, and it is just cosmetic.",
__PRETTY_FUNCTION__);
_loggedSlow = YES;
}
[self _slowTakeValuesFromRequest:request inContext:context];
} }
[self _slowTakeValuesFromRequest:request inContext:context]; else
} else {
[self _fastTakeValuesFromRequest:request inContext:context]; [self _fastTakeValuesFromRequest:request inContext:context];
}
} }
@end @end

View file

@ -44,6 +44,11 @@ GSWEB_EXPORT BYTE ElementsMap_attributeElement;
@class GSWContext; @class GSWContext;
@class GSWRequest; @class GSWRequest;
@class GSWAssociation;
GSWEB_EXPORT GSWAssociation* GSWAssignAndRemoveAssociation(GSWAssociation** associationPtr,
NSMutableDictionary* associations,
NSString* associationName);
#ifndef NDEBBUG #ifndef NDEBBUG
#define GSWELEMENT_HAS_DECLARATION_NAME #define GSWELEMENT_HAS_DECLARATION_NAME

View file

@ -45,6 +45,16 @@ BYTE ElementsMap_gswebElement = (BYTE)0x57; // 'W'
BYTE ElementsMap_dynamicElement = (BYTE)0x43; // 'C' BYTE ElementsMap_dynamicElement = (BYTE)0x43; // 'C'
BYTE ElementsMap_attributeElement = (BYTE)0x41; // 'A' BYTE ElementsMap_attributeElement = (BYTE)0x41; // 'A'
GSWAssociation* GSWAssignAndRemoveAssociation(GSWAssociation** associationPtr,
NSMutableDictionary* associations,
NSString* associationName)
{
ASSIGN(*associationPtr,([associations objectForKey:associationName]));
if (*associationPtr)
[associations removeObjectForKey:associationName];
return *associationPtr;
}
//==================================================================== //====================================================================
@implementation GSWElement @implementation GSWElement