]> err.no Git - sope/blob - sope-appserver/WEExtensions/WEStringTable.m
added strict OSX bundle dependencies
[sope] / sope-appserver / WEExtensions / WEStringTable.m
1 /*
2   Copyright (C) 2004-2005 SKYRIX Software AG
3
4   This file is part of OpenGroupware.org.
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 "WEStringTable.h"
23 #include "common.h"
24
25 @implementation WEStringTable
26
27 static BOOL debugOn          = NO;
28 static BOOL useLatin1Strings = NO;
29
30 + (void)initialize {
31   NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
32   
33   debugOn          = [ud boolForKey:@"WEStringTableDebugEnabled"];
34   useLatin1Strings = [ud boolForKey:@"WEStringTableUseLatin1"];
35 }
36
37 + (id)stringTableWithPath:(NSString *)_path {
38   return [[(WEStringTable *)[self alloc] initWithPath:_path] autorelease];
39 }
40
41 - (id)initWithPath:(NSString *)_path {
42   self->path = [_path copyWithZone:[self zone]];
43   return self;
44 }
45
46 - (void)dealloc {
47   [self->path     release];
48   [self->lastRead release];
49   [self->data     release];
50   [super dealloc];
51 }
52
53 /* loading */
54
55 - (NSException *)reportParsingError:(NSException *)_error {
56   [self logWithFormat:@"%s: could not load strings file '%@': %@", 
57           __PRETTY_FUNCTION__, self->path, _error];
58   return nil;
59 }
60
61 - (NSStringEncoding)stringsFileEncoding {
62   return useLatin1Strings ? NSISOLatin1StringEncoding : NSUTF8StringEncoding;
63 }
64
65 - (void)checkState {
66   NSString     *plistString;
67   NSDictionary *plist;
68   NSData       *rawData;
69   
70   if (self->data != nil)
71     return;
72   
73   rawData = [[NSData alloc] initWithContentsOfMappedFile:self->path];
74   if (rawData == nil) {
75     [self logWithFormat:@"ERROR: could not load strings file: %@", self->path];
76     return;
77   }
78   
79   if (debugOn) {
80     [self debugWithFormat:@"read strings file %@, len: %d", 
81           self->path, [rawData length]];
82   }
83   
84   plistString = [[NSString alloc] initWithData:rawData 
85                                   encoding:[self stringsFileEncoding]];
86   [rawData release]; rawData = nil;
87   if (plistString == nil) {
88     [self logWithFormat:@"ERROR: could not decode strings file charset: %@", 
89             self->path];
90     return;
91   }
92   
93   if (debugOn) {
94     [self debugWithFormat:@"  string len %d, class %@", 
95           [plistString length], NSStringFromClass([plistString class])];
96   }
97   
98   NS_DURING {
99     if ((plist = [plistString propertyListFromStringsFileFormat]) == nil) {
100       NSLog(@"%s: could not load strings file '%@'",
101             __PRETTY_FUNCTION__,
102             self->path);
103     }
104     self->data     = [plist copy];
105     self->lastRead = [[NSDate date] retain];
106   }
107   NS_HANDLER
108     [[self reportParsingError:localException] raise];
109   NS_ENDHANDLER;
110
111   if (debugOn)
112     [self debugWithFormat:@"  parsed entries: %d", [self->data count]];
113   
114   [plistString release];
115 }
116
117 /* access */
118
119 - (NSString *)stringForKey:(NSString *)_key withDefaultValue:(NSString *)_def {
120   NSString *value;
121   
122   [self checkState];
123   value = [self->data objectForKey:_key];
124   return value ? value : _def;
125 }
126
127 /* debugging */
128
129 - (BOOL)isDebuggingEnabled {
130   return debugOn;
131 }
132
133 @end /* WEStringTable */