]> err.no Git - sope/blob - sope-appserver/NGObjWeb/Templates/WOApplication+Builders.m
fixed some NGMail framework build issue
[sope] / sope-appserver / NGObjWeb / Templates / WOApplication+Builders.m
1 /*
2   Copyright (C) 2000-2005 SKYRIX Software AG
3
4   This file is part of SOPE.
5
6   SOPE 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   SOPE 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 SOPE; 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 <NGObjWeb/WOApplication.h>
23 #include "WOxTemplateBuilder.h"
24 #include <NGObjWeb/WOxElemBuilder.h>
25 #include "common.h"
26
27 @implementation WOApplication(BuilderStack)
28
29 - (void)scanForBuilderBundlesInDirectory:(NSString *)_path {
30   NGBundleManager *bm;
31   NSFileManager *fm;
32   NSEnumerator  *pathes;
33   NSString      *lPath;
34
35   bm = [NGBundleManager defaultBundleManager];
36   
37   fm = [NSFileManager defaultManager];
38   pathes = [[fm directoryContentsAtPath:_path] objectEnumerator];
39   while ((lPath = [pathes nextObject])) {
40     NSBundle *bundle;
41     BOOL isDir;
42     
43     lPath = [_path stringByAppendingPathComponent:lPath];
44     
45     if (![fm fileExistsAtPath:lPath isDirectory:&isDir])
46       continue;
47     if (!isDir)
48       continue;
49     
50     if ((bundle = [bm bundleWithPath:lPath]) == nil) {
51       [self warnWithFormat:@"could not get bundle for path: '%@'",
52               lPath];
53       continue;
54     }
55     
56     if (![bundle load]) {
57       [self warnWithFormat:@"could not load bundle: '%@'", lPath];
58       continue;
59     }
60     
61     [self debugWithFormat:@"loaded elem builder bundle: %@",
62             [lPath lastPathComponent]];
63   }
64 }
65
66 - (void)loadBuilderBundles {
67   // TODO: DUP to SoProductRegistry.m
68   NSFileManager *fm;
69   NSProcessInfo *pi;
70   NSArray       *pathes;
71   NSString      *relPath;
72   unsigned      i;
73   
74   /* scan library pathes */
75   
76   fm = [NSFileManager defaultManager];
77   pi = [NSProcessInfo processInfo];
78   
79 #if COCOA_Foundation_LIBRARY
80   /* 
81      TODO: (like COMPILE_FOR_GNUSTEP)
82      This should actually check whether we are compiling in the
83      GNUstep environment since this modifies the location of bundles.
84   */
85   pathes = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory,
86                                                NSAllDomainsMask,
87                                                YES);
88 #else
89   pathes = [[pi environment] objectForKey:@"GNUSTEP_PATHPREFIX_LIST"];
90   if (pathes == nil)
91     pathes = [[pi environment] objectForKey:@"GNUSTEP_PATHLIST"];
92   
93   pathes = [[pathes stringValue] componentsSeparatedByString:@":"];
94 #endif
95   
96   if ([pathes count] == 0) {
97     [self debugWithFormat:@"found no builder bundle pathes."];
98     return;
99   }
100   
101   [self debugWithFormat:@"scanning for builder bundles ..."];
102   
103 #if COCOA_Foundation_LIBRARY || NeXT_Foundation_LIBRARY
104   relPath = @"";
105 #else
106   relPath = @"Library/";
107 #endif
108   relPath = [NSString stringWithFormat:@"%@WOxElemBuilders-%i.%i/", relPath,
109                         SOPE_MAJOR_VERSION, SOPE_MINOR_VERSION];
110   for (i = 0; i < [pathes count]; i++) {
111     NSString *lPath;
112     BOOL     isDir;
113     
114     lPath = [[pathes objectAtIndex:i] stringByAppendingPathComponent:relPath];
115     if (![fm fileExistsAtPath:lPath isDirectory:&isDir])
116       continue;
117     if (!isDir)
118       continue;
119     
120     [self debugWithFormat:@"  directory %@", lPath];
121     [self scanForBuilderBundlesInDirectory:lPath];
122   }
123   
124   /* look into FHS pathes */
125   
126   relPath = [NSString stringWithFormat:@"lib/sope-%i.%i/wox-builders/",
127                         SOPE_MAJOR_VERSION, SOPE_MINOR_VERSION];
128   pathes = [NSArray arrayWithObjects:
129                       [@"/usr/local/" stringByAppendingString:relPath],
130                       [@"/usr/"       stringByAppendingString:relPath],
131                     nil];
132   for (i = 0; i < [pathes count]; i++) {
133     NSString *lPath;
134     BOOL     isDir;
135     
136     lPath = [pathes objectAtIndex:i];
137     if (![fm fileExistsAtPath:lPath isDirectory:&isDir])
138       continue;
139     if (!isDir)
140       continue;
141     
142     [self debugWithFormat:@"  directory %@", lPath];
143     [self scanForBuilderBundlesInDirectory:lPath];
144   }
145   
146   /* report result */
147   
148   [self debugWithFormat:@"finished scan for builders."];
149 }
150
151 - (WOxElemBuilder *)builderForDocument:(id<DOMDocument>)_document {
152   static WOxElemBuilder *builder    = nil;
153   static NSArray        *defClasses = nil;
154   NSUserDefaults *ud;
155   NSArray *classes = nil;
156   NSArray *infos;
157   
158   if (builder != nil)
159     return builder;
160     
161   ud = [NSUserDefaults standardUserDefaults];
162   if (defClasses == nil)
163     defClasses = [[ud arrayForKey:@"WOxBuilderClasses"] copy];
164   
165   /* ensure that bundles are loaded */
166   [self loadBuilderBundles];
167   
168   infos = [[NGBundleManager defaultBundleManager]
169                             providedResourcesOfType:@"WOxElemBuilder"];
170   if ([infos count] > 0) {
171     classes = [NSMutableArray arrayWithCapacity:24];
172     [(id)classes addObjectsFromArray:[infos valueForKey:@"name"]];
173     [(id)classes addObjectsFromArray:defClasses];
174   }
175   else
176     classes = defClasses;
177   
178   if ([ud boolForKey:@"WOxLogBuilderQueue"]) {
179     NSEnumerator *e;
180     NSString *b;
181       
182     if ([classes count] > 0) {
183       [self debugWithFormat:@"builder stack:"];
184       e = [classes objectEnumerator];
185       while ((b = [e nextObject]))
186         [self logWithFormat:@"  %@", b];
187     }
188     else {
189       [self debugWithFormat:@"empty wox-element builder stack !"];
190     }
191   }
192   
193   builder = [[WOxElemBuilder createBuilderQueue:classes] retain];
194   return builder;
195 }
196
197 @end /* WOApplication(BuilderStack) */