From 9743f2e272d26b49239e6e313e2f58795cdb8fc1 Mon Sep 17 00:00:00 2001 From: helge Date: Wed, 7 Sep 2005 10:48:34 +0000 Subject: [PATCH] added support methods for builder subclasses git-svn-id: http://svn.opengroupware.org/SOPE/trunk@1102 e4a50df8-12e2-0310-a44c-efbce7f8a7e3 --- sope-appserver/NGObjWeb/ChangeLog | 5 + .../NGObjWeb/NGObjWeb/WOxElemBuilder.h | 23 ++- .../NGObjWeb/Templates/WOxElemBuilder.m | 158 +++++++++++++++++- sope-appserver/NGObjWeb/Version | 2 +- 4 files changed, 176 insertions(+), 12 deletions(-) diff --git a/sope-appserver/NGObjWeb/ChangeLog b/sope-appserver/NGObjWeb/ChangeLog index 94a2d871..6a39de19 100644 --- a/sope-appserver/NGObjWeb/ChangeLog +++ b/sope-appserver/NGObjWeb/ChangeLog @@ -1,3 +1,8 @@ +2005-09-07 Helge Hess + + * Templates/WOxElemBuilder.m: added several support methods to assist + builder subclasses to build WOElements (moved in from OGo) (v4.5.198) + 2005-09-06 Helge Hess * v4.5.197 diff --git a/sope-appserver/NGObjWeb/NGObjWeb/WOxElemBuilder.h b/sope-appserver/NGObjWeb/NGObjWeb/WOxElemBuilder.h index 9c61f907..a25b1368 100644 --- a/sope-appserver/NGObjWeb/NGObjWeb/WOxElemBuilder.h +++ b/sope-appserver/NGObjWeb/NGObjWeb/WOxElemBuilder.h @@ -101,10 +101,10 @@ /* logging */ -/* +#if 0 - (void)logWithFormat:(NSString *)_format, ...; - (void)debugWithFormat:(NSString *)_format, ...; -*/ +#endif /* managing builder queues */ @@ -127,8 +127,27 @@ - (void)reset; +/* support methods for subclasses */ + +- (id)lookupUniqueTag:(NSString *)_name + inElement:(id)_elem; + +- (WOElement *)elementForRawString:(NSString *)_rawstr; +- (WOElement *)elementForElementsAndStrings:(NSArray *)_elements; + +- (WOElement *)wrapElement:(WOElement *)_element + inCondition:(WOAssociation *)_condition + negate:(BOOL)_flag; + +- (WOElement *)wrapElements:(NSArray *)_sub inElementOfClass:(Class)_class; + +- (WOElement *)wrapChildrenOfElement:(id)_tag + inElementOfClass:(Class)_class + templateBuilder:(id)_b; + @end + @interface WOxElemBuilderComponentInfo : NSObject { NSString *cid; diff --git a/sope-appserver/NGObjWeb/Templates/WOxElemBuilder.m b/sope-appserver/NGObjWeb/Templates/WOxElemBuilder.m index b1a729a6..84559a9d 100644 --- a/sope-appserver/NGObjWeb/Templates/WOxElemBuilder.m +++ b/sope-appserver/NGObjWeb/Templates/WOxElemBuilder.m @@ -26,11 +26,14 @@ #include #include #include +#include #include "WOComponentFault.h" #include "common.h" @interface WOElement(UsedPrivates) - (id)initWithValue:(id)_value escapeHTML:(BOOL)_flag; ++ (id)allocForCount:(int)_count zone:(NSZone *)_zone; +- (id)initWithContentElements:(NSArray *)_elements; @end @interface WOAssociation(misc) @@ -94,14 +97,17 @@ @implementation WOxElemBuilder -static Class StrClass = Nil; -static Class AStrClass = Nil; -static NSDictionary *defaultAssocMap = nil; -static Class ValAssoc = Nil; -static BOOL logAssocMap = NO; -static BOOL logAssocCreation = NO; -static BOOL debugOn = NO; -static NGLogger *logger = nil; +static Class StrClass = Nil; +static Class AStrClass = Nil; +static NSDictionary *defaultAssocMap = nil; +static Class ValAssoc = Nil; +static BOOL logAssocMap = NO; +static BOOL logAssocCreation = NO; +static BOOL debugOn = NO; +static NGLogger *logger = nil; +static Class CompoundElemClass = Nil; +static NSNumber *yesNum = nil; +static WOAssociation *yesAssoc = nil; + (int)version { return 1; @@ -144,6 +150,13 @@ static NGLogger *logger = nil; if (ValAssoc == Nil) ValAssoc = NSClassFromString(@"WOValueAssociation"); + + CompoundElemClass = NSClassFromString(@"WOCompoundElement"); + + if (yesNum == nil) + yesNum = [[NSNumber numberWithBool:YES] retain]; + if (yesAssoc == nil) + yesAssoc = [[WOAssociation associationWithValue:yesNum] retain]; } + (WOxElemBuilder *)createBuilderQueue:(NSArray *)_classNames { @@ -634,7 +647,134 @@ static NGLogger *logger = nil; - (void)reset { [self->subcomponentInfos removeAllObjects]; - ASSIGN(self->script, (id)nil); + [self->script release]; self->script = nil; +} + +/* support methods for subclasses */ + +- (id)lookupUniqueTag:(NSString *)_name + inElement:(id)_elem +{ + id list; + + if ((list = [_elem getElementsByTagName:_name]) == nil) + return nil; + if ([list length] == 0) + return nil; + if ([list length] > 1) { + [self warnWithFormat: + @"more than once occurence of tag %@ in element: %@", + _name, _elem]; + } + return [list objectAtIndex:0]; +} + +- (WOElement *)elementForRawString:(NSString *)_rawstr { + /* Note: returns a retained element! */ + WOAssociation *a; + + if (_rawstr == nil) return nil; + a = [WOAssociation associationWithValue:_rawstr]; + return [[StrClass alloc] initWithValue:a escapeHTML:NO]; +} + +- (WOElement *)elementForElementsAndStrings:(NSArray *)_elements { + /* Note: returns a retained element! */ + NSMutableArray *ma; + WOElement *element; + unsigned i, count; + + if ((count = [_elements count]) == 0) + return nil; + + ma = [[NSMutableArray alloc] initWithCapacity:count]; + for (i = 0; i < count; i++) { + id elem; + + elem = [_elements objectAtIndex:i]; + if ([elem isKindOfClass:[WOElement class]]) { + [ma addObject:elem]; + continue; + } + + if ((elem = [self elementForRawString:[elem stringValue]])) + [ma addObject:elem]; + } + if ((count = [ma count]) == 0) + element = nil; + else if (count == 1) { + element = [[ma objectAtIndex:0] retain]; + } + else { + element = [[CompoundElemClass allocForCount:count zone:NULL] + initWithContentElements:ma]; + } + [ma release]; + return element; +} + +- (WOElement *)wrapElement:(WOElement *)_element + inCondition:(WOAssociation *)_condition + negate:(BOOL)_flag +{ + // NOTE: *releases* _element parameter! + // returns retained conditional + static Class WOConditionalClass = Nil; + static NSString *key = @"condition"; + NSMutableDictionary *assocs; + WOElement *element; + NSArray *children; + + if (WOConditionalClass == Nil) + WOConditionalClass = NSClassFromString(@"WOConditional"); + + if (_element == nil) + return nil; + if (_condition == nil) + return _element; + + if (_flag) { + assocs = [[NSMutableDictionary alloc] + initWithObjectsAndKeys:_condition, key, + yesAssoc, @"negate", nil]; + } + else { + assocs = [[NSMutableDictionary alloc] initWithObjects:&_condition + forKeys:&key count:1]; + } + children = [[NSArray alloc] initWithObjects:&_element count:1]; + element = [[WOConditionalClass alloc] initWithName:nil + associations:assocs + contentElements:children]; + [children release]; + [_element release]; + [assocs release]; + return element; +} + +- (WOElement *)wrapElements:(NSArray *)_sub inElementOfClass:(Class)_class { + WOElement *element; + + if (_sub == nil) + return nil; + + element = [[_class alloc] initWithName:nil + associations:nil + contentElements:_sub]; + return element; +} + +- (WOElement *)wrapChildrenOfElement:(id)_tag + inElementOfClass:(Class)_class + templateBuilder:(id)_b +{ + NSArray *children; + + children = [_tag hasChildNodes] + ? [_b buildNodes:[_tag childNodes] templateBuilder:_b] + : nil; + + return [self wrapElements:children inElementOfClass:_class]; } @end /* WOxElemBuilder */ diff --git a/sope-appserver/NGObjWeb/Version b/sope-appserver/NGObjWeb/Version index 0af1b101..e3f32677 100644 --- a/sope-appserver/NGObjWeb/Version +++ b/sope-appserver/NGObjWeb/Version @@ -1,6 +1,6 @@ # version file -SUBMINOR_VERSION:=197 +SUBMINOR_VERSION:=198 # v4.5.122 requires libNGExtensions v4.5.153 # v4.5.91 requires libNGExtensions v4.5.134 -- 2.39.5