]> err.no Git - sope/blob - sope-appserver/NGObjWeb/_WOStringTable.m
started some makefiles
[sope] / sope-appserver / NGObjWeb / _WOStringTable.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 "_WOStringTable.h"
24 #include "common.h"
25
26 @implementation _WOStringTable
27
28 static NSStringEncoding stringFilesEncoding = NSISOLatin1StringEncoding;
29
30 - (id)initWithPath:(NSString *)_path {
31   if ((self = [super init])) {
32     self->path = [_path copyWithZone:[self zone]];
33   }
34   return self;
35 }
36 - (id)init {
37   return [self initWithPath:nil];
38 }
39
40 - (void)dealloc {
41   [self->path     release];
42   [self->lastRead release];
43   [self->data     release];
44   [super dealloc];
45 }
46
47 /* loading */
48
49 - (NSException *)_handlePropertyListParseException:(NSException *)_exception {
50   [self logWithFormat:@"could not load strings file file '%@': %@",
51           self->path, _exception];
52   self->data = nil;
53   return nil;
54 }
55
56 - (void)checkState {
57   NSString     *tmp;
58   NSDictionary *plist;
59   NSData       *sdata;
60   
61   if (self->data != nil)
62     return;
63
64 #if !LIB_FOUNDATION_LIBRARY /* potentially effects OGo ;-) */
65   /*
66     For WO4.5 compatibility, we need first to try to open .strings file as a 
67     dictionary
68     Dictionary must be either in UTF-8 or UTF-16 encoding.
69     If we don't do that, then a dict in UTF-8 encoding will be opened as a 
70     dict using defaultCString encoding
71   */
72   plist = [NSDictionary dictionaryWithContentsOfFile:self->path];
73   if (plist != nil) {
74     self->data = [plist copy];
75     return;
76   }
77 #endif
78   
79   /* If file was not a dictionary, then it's a standard strings file */
80   
81   if ((sdata = [[NSData alloc] initWithContentsOfFile:self->path]) == nil) {
82     [self debugWithFormat:@"ERROR: could not read strings file: %@", 
83           self->path];
84     self->data = nil;
85     return;
86   }
87   
88   tmp = [[NSString alloc] initWithData:sdata encoding:stringFilesEncoding];
89   [sdata release]; sdata = nil;
90   if (tmp == nil) {
91     [self logWithFormat:@"ERROR: file is not in required encoding (%d): %@",
92           stringFilesEncoding, self->path];
93     self->data = nil;
94     return;
95   }
96   
97   NS_DURING {
98     if ((plist = [tmp propertyListFromStringsFileFormat]) == nil) {
99       NSLog(@"%s: could not load strings file '%@'",
100             __PRETTY_FUNCTION__,
101             self->path);
102     }
103     [tmp release]; tmp = nil;
104     self->data = [plist copy];
105   }
106   NS_HANDLER {
107     [tmp release]; tmp = nil;
108     [[self _handlePropertyListParseException:localException] raise];
109   }
110   NS_ENDHANDLER;
111 }
112
113 /* access */
114
115 - (NSString *)stringForKey:(NSString *)_key withDefaultValue:(NSString *)_def {
116   NSString *value;
117   
118   [self checkState];
119   value = [self->data objectForKey:_key];
120   return value != nil ? value : _def;
121 }
122
123 /* description */
124
125 - (NSString *)description {
126   NSMutableString *ms;
127   
128   ms = [NSMutableString stringWithCapacity:128];
129   [ms appendFormat:@"<0x%08X[%@]: ", self, NSStringFromClass([self class])];
130   
131   if (self->path)     [ms appendFormat:@" path='%@'",   self->path];
132   if (self->data)     [ms appendFormat:@" strings=#%d", [self->data count]];
133   if (self->lastRead) [ms appendFormat:@" loaddate=%@", self->lastRead];
134   
135   [ms appendString:@">"];
136   return ms;
137 }
138   
139 @end /* _WOStringTable */