]> err.no Git - sope/blob - Recycler/NGObjDOM/ODRWebObject.m
minor improvement to quoting
[sope] / Recycler / NGObjDOM / ODRWebObject.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 // $Id$
22
23 #include <NGObjDOM/ODRWebObject.h>
24 #include <NGObjDOM/ODNamespaces.h>
25 #include <NGObjWeb/WODynamicElement.h>
26 #include "common.h"
27
28 @interface NSObject(WOPrivates)
29 + (BOOL)isDynamicElement;
30 - (BOOL)isDynamicElement;
31 @end
32
33 @interface _WODRTemplateElement : WODynamicElement
34 {
35 @public
36   /* both non-retained */
37   ODNodeRenderer *renderer;
38   id domNode;
39 }
40 @end
41
42 @implementation ODRWebObject
43
44 - (NSMutableDictionary *)associationsForNodeAttributes:(id)_domNode {
45   NSMutableDictionary *md;
46   NSEnumerator *attrs;
47   id attrNode;
48   
49   md = [NSMutableDictionary dictionaryWithCapacity:16];
50   
51   attrs = [(id)[_domNode attributes] objectEnumerator];
52   while ((attrNode = [attrs nextObject])) {
53     NSString *nsuri;
54     
55     nsuri = [attrNode namespaceURI];
56     
57     if ([nsuri isEqualToString:XMLNS_OD_BIND]) {
58       [md setObject:[WOAssociation associationWithKeyPath:[attrNode textValue]]
59           forKey:[(id<DOMAttr>)attrNode name]];
60     }
61     else {
62       [md setObject:[WOAssociation associationWithValue:[attrNode textValue]]
63           forKey:[(id<DOMAttr>)attrNode name]];
64     }
65   }
66   
67   return md;
68 }
69
70 - (WOElement *)constructElementForElementNode:(id)_domNode {
71   Class clazz;
72   
73   clazz = NSClassFromString([_domNode tagName]);
74
75   if (clazz == Nil) {
76     NSLog(@"%s: Can't find class for node %@",
77           __PRETTY_FUNCTION__, _domNode);
78     return nil;
79   }
80   else if ([clazz isDynamicElement]) {
81     WODynamicElement     *elem;
82     NSString             *elemName;
83     NSMutableDictionary  *assocs;
84     _WODRTemplateElement *template;
85
86     elemName = [[[_domNode attributes] namedItem:@"id"] textValue];
87     assocs   = [self associationsForNodeAttributes:_domNode];
88     template = nil;
89
90     if ([_domNode hasChildNodes]) {
91       template = [[_WODRTemplateElement alloc] init];
92       template->renderer = self;
93       template->domNode  = _domNode;
94       AUTORELEASE(template);
95     }
96     
97     elem  = [clazz alloc];
98     elem  = [elem initWithName:elemName
99                   associations:assocs
100                   template:template];
101     
102     if ([assocs count] > 0)
103       [elem setExtraAttributes:assocs];
104     
105     [assocs removeAllObjects];
106     
107     return AUTORELEASE(elem);
108   }
109   else {
110     NSLog(@"%s: Can't handle element class %@ for node %@",
111           __PRETTY_FUNCTION__,
112           clazz, _domNode);
113     return nil;
114   }
115 }
116
117 - (void)takeValuesForNode:(id)_domNode
118   fromRequest:(WORequest *)_request
119   inContext:(WOContext *)_context
120 {
121   WOElement *e;
122   
123   e = [self constructElementForElementNode:_domNode];
124   [e takeValuesFromRequest:_request inContext:_context];
125 }
126
127 - (id)invokeActionForNode:(id)_domNode
128   fromRequest:(WORequest *)_request
129   inContext:(WOContext *)_context
130 {
131   WOElement *e;
132   id result;
133   
134   e = [self constructElementForElementNode:_domNode];
135   result = [e invokeActionForRequest:_request inContext:_context];
136   
137   return result;
138 }
139
140 - (void)appendNode:(id)_domNode
141   toResponse:(WOResponse *)_response
142   inContext:(WOContext *)_context
143 {
144   WOElement *e;
145   
146   if ([_domNode nodeType] != DOM_ELEMENT_NODE) {
147     [super appendNode:_domNode toResponse:_response inContext:_context];
148     return;
149   }
150   
151   e = [self constructElementForElementNode:_domNode];
152   [e appendToResponse:_response inContext:_context];
153 }
154
155 @end /* ODRWebObject */
156
157 @implementation _WODRTemplateElement
158
159 - (id)_childRendererForNode:(id)_node {
160   static id childRenderer = nil;
161   if (childRenderer == nil)
162     childRenderer = [[NSClassFromString(@"WODRChildNodes") alloc] init];
163   return childRenderer;
164 }
165
166 - (void)takeValuesFromRequest:(WORequest *)_request inContext:(WOContext *)_ctx {
167   if (![self->domNode hasChildNodes])
168     return;
169   
170   [self->renderer takeValuesForChildNodes:[self->domNode childNodes]
171        fromRequest:_request
172        inContext:_ctx];
173 }
174
175 - (id)invokeActionForRequest:(WORequest *)_request inContext:(WOContext *)_ctx {
176   if (![self->domNode hasChildNodes])
177     return nil;
178   
179   return [self->renderer invokeActionForChildNodes:[self->domNode childNodes]
180               fromRequest:_request
181               inContext:_ctx];
182 }
183
184 - (void)appendToResponse:(WOResponse *)_response inContext:(WOContext *)_ctx {
185   if ([self->domNode hasChildNodes]) {
186     //NSLog(@"%s: %@ ..", __PRETTY_FUNCTION__, self->domNode);
187   
188     [self->renderer appendChildNodes:[self->domNode childNodes]
189          toResponse:_response
190          inContext:_ctx];
191   }
192 }
193
194 @end /* _WODRTemplateElement */
195