/* Copyright (C) 2000-2004 SKYRIX Software AG This file is part of OpenGroupware.org. OGo is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. OGo 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 Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with OGo; see the file COPYING. If not, write to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ // $Id$ #include "WOxTemplateBuilder.h" #include #include #include #include #include #include "common.h" @implementation WOxTemplateBuilder static BOOL profLoading = NO; static Class DateClass = Nil; + (int)version { return [super version] + 0 /* v1 */; } + (void)initialize { NSAssert2([super version] == 1, @"invalid superclass (%@) version %i !", NSStringFromClass([self superclass]), [super version]); if (DateClass == Nil) DateClass = [NSDate class]; } - (WOxElemBuilder *)builderForDocument:(id)_document { return [[WOApplication application] builderForDocument:_document]; } - (Class)templateClass { return [WOTemplate class]; } - (WOTemplate *)buildTemplateFromDocument:(id)_doc url:(NSURL *)_url { WOTemplate *template; NSTimeInterval st = 0.0; WOxElemBuilder *builder; WOElement *root; WOComponentScript *script; if (_doc == nil) return nil; if (profLoading) st = [[DateClass date] timeIntervalSince1970]; builder = [self builderForDocument:_doc]; root = [[builder buildTemplateFromDocument:_doc] retain]; template = [[self templateClass] alloc]; template = [template initWithURL:_url rootElement:root]; template = [template autorelease]; /* transform builder info's into element defs ... */ if (template) { NSEnumerator *scinfos; WOxElemBuilderComponentInfo *scinfo; scinfos = [[builder subcomponentInfos] objectEnumerator]; while ((scinfo = [scinfos nextObject])) { [template addSubcomponentWithKey:[scinfo componentId] name:[scinfo pageName] bindings:[scinfo bindings]]; } if ((script = [builder componentScript])) [template setComponentScript:script]; } /* reset building state */ [builder reset]; if (profLoading) { NSTimeInterval diff; diff = [[DateClass date] timeIntervalSince1970] - st; printf(" building from XML: %0.3fs\n", diff); } return template; } - (id)xmlParser { //static id builder = nil; return [[DOMBuilderFactory standardDOMBuilderFactory] createDOMBuilderForMimeType:@"text/xml"]; } - (WOTemplate *)buildTemplateAtURL:(NSURL *)_url { id domDocument; NSAutoreleasePool *pool; id builder; WOTemplate *template; //NSLog(@"loading XML template %@ ...", self->path); pool = [[NSAutoreleasePool alloc] init]; builder = [self xmlParser]; NSAssert(builder != nil, @"missing XML parser .."); domDocument = [builder buildFromSource:_url]; //[@"file://" stringByAppendingString:self->path]]; /* construct template for DOM document */ if (domDocument) { template = [self buildTemplateFromDocument:domDocument url:_url]; /* should scan document for class/script information */ } else template = nil; template = [template retain]; [pool release]; return [template autorelease]; } @end /* WOxTemplateBuilder */ @implementation WOApplication(BuilderStack) - (WOxElemBuilder *)builderForDocument:(id)_document { static WOxElemBuilder *builder = nil; static NSArray *defClasses = nil; NSUserDefaults *ud; NSArray *classes = nil; NSArray *infos; if (builder != nil) return builder; ud = [NSUserDefaults standardUserDefaults]; if (defClasses == nil) defClasses = [[ud arrayForKey:@"WOxBuilderClasses"] copy]; infos = [[NGBundleManager defaultBundleManager] providedResourcesOfType:@"WOxElemBuilder"]; if ([infos count] > 0) { classes = [NSMutableArray arrayWithCapacity:24]; [(id)classes addObjectsFromArray:[infos valueForKey:@"name"]]; [(id)classes addObjectsFromArray:defClasses]; } else classes = defClasses; if ([ud boolForKey:@"WOxLogBuilderQueue"]) { NSEnumerator *e; NSString *b; if ([classes count] > 0) { [self debugWithFormat:@"builder stack:"]; e = [classes objectEnumerator]; while ((b = [e nextObject])) [self logWithFormat:@" %@", b]; } else { [self debugWithFormat:@"empty wox-element builder stack !"]; } } builder = [[WOxElemBuilder createBuilderQueue:classes] retain]; return builder; } @end /* WOApplication(BuilderStack) */