2 Copyright (C) 2000-2004 SKYRIX Software AG
4 This file is part of OpenGroupware.org.
6 OGo is free software; you can redistribute it and/or modify it under
7 the terms of the GNU Lesser General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
11 OGo is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
14 License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with OGo; see the file COPYING. If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
22 #include "WOxTemplateBuilder.h"
23 #include <NGObjWeb/WOxElemBuilder.h>
24 #include <NGObjWeb/WOApplication.h>
25 #include <NGObjWeb/WOElement.h>
27 #include <DOM/DOMBuilderFactory.h>
30 @implementation WOxTemplateBuilder
32 static BOOL profLoading = NO;
33 static Class DateClass = Nil;
36 return [super version] + 0 /* v2 */;
40 NSAssert2([super version] == 2,
41 @"invalid superclass (%@) version %i !",
42 NSStringFromClass([self superclass]), [super version]);
45 DateClass = [NSDate class];
48 - (WOxElemBuilder *)builderForDocument:(id<DOMDocument>)_document {
49 return [[WOApplication application] builderForDocument:_document];
51 - (Class)templateClass {
52 return [WOTemplate class];
55 - (WOTemplate *)buildTemplateFromDocument:(id<NSObject,DOMDocument>)_doc
59 NSTimeInterval st = 0.0;
60 WOxElemBuilder *builder;
62 WOComponentScript *script;
68 st = [[DateClass date] timeIntervalSince1970];
70 builder = [self builderForDocument:_doc];
72 root = [[builder buildTemplateFromDocument:_doc] retain];
74 template = [[self templateClass] alloc];
75 template = [template initWithURL:_url rootElement:root];
76 template = [template autorelease];
78 /* transform builder info's into element defs ... */
81 NSEnumerator *scinfos;
82 WOxElemBuilderComponentInfo *scinfo;
84 scinfos = [[builder subcomponentInfos] objectEnumerator];
86 while ((scinfo = [scinfos nextObject])) {
87 [template addSubcomponentWithKey:[scinfo componentId]
88 name:[scinfo pageName]
89 bindings:[scinfo bindings]];
92 if ((script = [builder componentScript]))
93 [template setComponentScript:script];
96 /* reset building state */
101 diff = [[DateClass date] timeIntervalSince1970] - st;
102 printf(" building from XML: %0.3fs\n", diff);
107 - (id<DOMBuilder>)xmlParserForURL:(NSURL *)_url {
108 static DOMBuilderFactory *factory = nil;
111 factory = [[DOMBuilderFactory standardDOMBuilderFactory] retain];
113 // TODO: somewhat hackish
114 if ([[_url path] hasSuffix:@".html"])
115 return [factory createDOMBuilderForMimeType:@"text/html"];
116 if ([[_url path] hasSuffix:@".stx"])
117 return [factory createDOMBuilderForMimeType:@"text/structured"];
119 // TODO: do we want to cache the builder?
120 return [factory createDOMBuilderForMimeType:@"text/xml"];
123 - (WOTemplate *)buildTemplateAtURL:(NSURL *)_url {
124 id<NSObject,DOMDocument> domDocument;
125 NSAutoreleasePool *pool;
126 id<DOMBuilder> builder;
127 WOTemplate *template;
130 [self logWithFormat:@"loading XML template %@ ...", self->path];
133 pool = [[NSAutoreleasePool alloc] init];
135 builder = [self xmlParserForURL:_url];
136 NSAssert(builder != nil, @"missing XML parser ..");
138 domDocument = [builder buildFromSource:_url];
140 [@"file://" stringByAppendingString:self->path]];
143 /* construct template for DOM document */
146 template = [self buildTemplateFromDocument:domDocument url:_url];
148 /* should scan document for class/script information */
153 template = [template retain];
156 return [template autorelease];
159 @end /* WOxTemplateBuilder */