]> err.no Git - sope/blob - Recycler/NGObjDOM/ODResourceManager.m
fixed a warning in resource manager
[sope] / Recycler / NGObjDOM / ODResourceManager.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 "ODResourceManager.h"
24 #include "common.h"
25
26 @interface WOResourceManager(Privates)
27
28 - (id)definitionForComponent:(NSString *)_name
29   languages:(NSArray *)_languages;
30 - (NSString *)pathToComponentNamed:(NSString *)_name
31   inFramework:(NSString *)_fw;
32
33 @end
34
35 @implementation ODResourceManager
36
37 + (int)version {
38   return [super version] + 0 /* v3 */;
39 }
40 + (void)initialize {
41   static BOOL isInitialized = NO;
42   NSDictionary *defs;
43   
44   NSAssert2([super version] == 3,
45             @"invalid superclass (%@) version %i !",
46             NSStringFromClass([self superclass]), [super version]);
47   
48   if (isInitialized) return;
49   isInitialized = YES;
50
51   defs = [NSDictionary dictionaryWithObjectsAndKeys:
52                            [NSArray arrayWithObjects:
53                                       @"xml", @"wml", @"xhtml", nil],
54                            @"ODXMLComponentExtensions",
55                            nil];
56   // .svg, ... can be added using the default
57     
58   [[NSUserDefaults standardUserDefaults] registerDefaults:defs];
59 }
60
61 + (NSArray *)xmlComponentExtensions {
62   static NSArray *exts = nil;
63
64   if (exts == nil) {
65     exts = [[[NSUserDefaults standardUserDefaults]
66                              arrayForKey:@"ODXMLComponentExtensions"]
67                              copy];
68   }
69   
70   return exts;
71 }
72
73 - (void)dealloc {
74   RELEASE(self->nameToCDef);
75   [super dealloc];
76 }
77
78 /* lookup */
79
80 - (NSString *)pathToComponentNamed:(NSString *)_name
81   inFramework:(NSString *)_framework
82   languages:(NSArray *)_langs
83 {
84   /* search for component template .. */
85   
86   if (_name == nil) {
87 #ifdef DEBUG
88     NSLog(@"WARNING(%s): tried to get path to component with <nil> name !",
89           __PRETTY_FUNCTION__);
90 #endif
91     return nil;
92   }
93   
94   /* scan for name.$ext resource ... */
95   {
96     NSEnumerator *e;
97     NSString *ext;
98     
99     e = [[[self class] xmlComponentExtensions] objectEnumerator];
100     
101     while ((ext = [e nextObject])) {
102       NSString *templateName;
103       NSString *path;
104       
105       templateName = [_name stringByAppendingPathExtension:ext];
106       
107       path = [self pathForResourceNamed:templateName
108                    inFramework:_framework
109                    languages:_langs];
110       if (path) return path;
111     }
112   }
113   
114   /* this resource manager does not search in WOProjectSearchPath ... */
115   
116   return [super pathToComponentNamed:_name inFramework:_framework];
117 }
118
119 - (id)definitionForComponent:(NSString *)_name
120   languages:(NSArray *)_languages
121 {
122   id cdef;
123   id cacheKey = nil;
124   
125   if (_name == nil)
126     return nil;
127   
128   if (_languages == nil)
129     _languages = [NSArray array];
130   
131   /* look into cache */
132   
133   if ([[WOApplication application] isCachingEnabled]) {
134     cacheKey = [NSArray arrayWithObjects:_name, _languages, nil];
135     
136     if ((cdef = [self->nameToCDef objectForKey:cacheKey]))
137       return cdef;
138   }
139   else {
140     cdef     = nil;
141     cacheKey = nil;
142   }
143   
144   /* look for .wo component if no XML component could be found ... */
145   
146   if (cdef == nil)
147     cdef = [super definitionForComponent:_name languages:_languages];
148   
149   /* return result */
150   
151   return cdef;
152 }
153
154 @end /* ODResourceManager */