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