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