]> err.no Git - sope/blob - sope-appserver/NGObjWeb/Templates/WOxTemplateBuilder.m
improved SOPE security exceptions
[sope] / sope-appserver / NGObjWeb / Templates / WOxTemplateBuilder.m
1 /*
2   Copyright (C) 2000-2004 SKYRIX Software AG
3
4   This file is part of OpenGroupware.org.
5
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
9   later version.
10
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.
15
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
19   02111-1307, USA.
20 */
21
22 #include "WOxTemplateBuilder.h"
23 #include <NGObjWeb/WOxElemBuilder.h>
24 #include <NGObjWeb/WOApplication.h>
25 #include <NGObjWeb/WOElement.h>
26 #include <DOM/DOM.h>
27 #include <DOM/DOMBuilderFactory.h>
28 #include "common.h"
29
30 @implementation WOxTemplateBuilder
31
32 static BOOL  profLoading = NO;
33 static Class DateClass = Nil;
34
35 + (int)version {
36   return [super version] + 0 /* v2 */;
37 }
38
39 + (void)initialize {
40   NSAssert2([super version] == 2,
41             @"invalid superclass (%@) version %i !",
42             NSStringFromClass([self superclass]), [super version]);
43   
44   if (DateClass == Nil)
45     DateClass = [NSDate class];
46 }
47
48 - (WOxElemBuilder *)builderForDocument:(id<DOMDocument>)_document {
49   return [[WOApplication application] builderForDocument:_document];
50 }
51 - (Class)templateClass {
52   return [WOTemplate class];
53 }
54
55 - (WOTemplate *)buildTemplateFromDocument:(id<NSObject,DOMDocument>)_doc
56   url:(NSURL *)_url
57 {
58   WOTemplate        *template;
59   NSTimeInterval    st = 0.0;
60   WOxElemBuilder    *builder;
61   WOElement         *root;
62   WOComponentScript *script;
63   
64   if (_doc == nil)
65     return nil;
66   
67   if (profLoading)
68     st = [[DateClass date] timeIntervalSince1970];
69   
70   builder = [self builderForDocument:_doc];
71   
72   root = [[builder buildTemplateFromDocument:_doc] retain];
73   
74   template = [[self templateClass] alloc];
75   template = [template initWithURL:_url rootElement:root];
76   template = [template autorelease];
77   
78   /* transform builder info's into element defs ... */
79   
80   if (template) {
81     NSEnumerator *scinfos;
82     WOxElemBuilderComponentInfo *scinfo;
83     
84     scinfos = [[builder subcomponentInfos] objectEnumerator];
85     
86     while ((scinfo = [scinfos nextObject])) {
87       [template addSubcomponentWithKey:[scinfo componentId]
88                 name:[scinfo pageName]
89                 bindings:[scinfo bindings]];
90     }
91     
92     if ((script = [builder componentScript]))
93       [template setComponentScript:script];
94   }
95   
96   /* reset building state */
97   [builder reset];
98   
99   if (profLoading) {
100     NSTimeInterval diff;
101     diff = [[DateClass date] timeIntervalSince1970] - st;
102     printf("  building from XML: %0.3fs\n", diff);
103   }
104   return template;
105 }
106
107 - (id<DOMBuilder>)xmlParserForURL:(NSURL *)_url {
108   static DOMBuilderFactory *factory = nil;
109   
110   if (factory == nil)
111     factory = [[DOMBuilderFactory standardDOMBuilderFactory] retain];
112   
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"];
118   
119   // TODO: do we want to cache the builder?
120   return [factory createDOMBuilderForMimeType:@"text/xml"];
121 }
122
123 - (WOTemplate *)buildTemplateAtURL:(NSURL *)_url {
124   id<NSObject,DOMDocument> domDocument;
125   NSAutoreleasePool *pool;
126   id<DOMBuilder>    builder;
127   WOTemplate        *template;
128   
129 #if 0  
130   [self logWithFormat:@"loading XML template %@ ...", self->path];
131 #endif
132   
133   pool = [[NSAutoreleasePool alloc] init];
134   
135   builder = [self xmlParserForURL:_url];
136   NSAssert(builder != nil, @"missing XML parser ..");
137   
138   domDocument = [builder buildFromSource:_url];
139 #if 0
140   [@"file://" stringByAppendingString:self->path]];
141 #endif
142
143   /* construct template for DOM document */
144   
145   if (domDocument) {
146     template = [self buildTemplateFromDocument:domDocument url:_url];
147     
148     /* should scan document for class/script information */
149   }
150   else
151     template = nil;
152
153   template = [template retain];
154   [pool release];
155   
156   return [template autorelease];
157 }
158
159 @end /* WOxTemplateBuilder */