Permalink
Browse files

Cleanup

- Renamed parameter to defaultTableName
- Renamed method to stringRepresentationWithEncoding
- Fixed some indents and braces
  • Loading branch information...
1 parent a6ae102 commit 6ea48fbe7338dfa43240f07e09f279b7fa166f68 @odrobnik odrobnik committed Aug 3, 2012
@@ -15,7 +15,7 @@
@property (nonatomic, assign) NSStringEncoding inputEncoding;
@property (nonatomic, retain) NSSet *tablesToSkip;
@property (nonatomic, retain) NSString *customMacroPrefix;
-@property (nonatomic, retain) NSString *customTableName;
+@property (nonatomic, retain) NSString *defaultTableName;
- (void)beginProcessingFile:(NSURL *)fileURL;
@@ -21,12 +21,12 @@ - (void)addEntryToTables:(DTLocalizableStringEntry *)entry;
@implementation DTLocalizableStringAggregator
{
- NSDictionary *_validMacros;
- NSMutableDictionary *_stringTables;
-
- NSOperationQueue *_processingQueue;
- dispatch_queue_t _tableQueue;
- dispatch_group_t _tableGroup;
+ NSDictionary *_validMacros;
+ NSMutableDictionary *_stringTables;
+
+ NSOperationQueue *_processingQueue;
+ dispatch_queue_t _tableQueue;
+ dispatch_group_t _tableGroup;
DTLocalizableStringEntryWriteCallback _entryWriteCallback;
}
@@ -37,38 +37,38 @@ @implementation DTLocalizableStringAggregator
@synthesize inputEncoding = _inputEncoding;
@synthesize tablesToSkip = _tablesToSkip;
@synthesize customMacroPrefix = _customMacroPrefix;
-@synthesize customTableName = _customTableName;
+@synthesize defaultTableName = _defaultTableName;
- (id)init
{
- self = [super init];
- if (self)
- {
- _tableQueue = dispatch_queue_create("DTLocalizableStringAggregator", 0);
- _tableGroup = dispatch_group_create();
-
- _processingQueue = [[NSOperationQueue alloc] init];
- [_processingQueue setMaxConcurrentOperationCount:10];
-
- _wantsPositionalParameters = YES; // default
- _inputEncoding = NSUTF8StringEncoding; // default
- }
- return self;
+ self = [super init];
+ if (self)
+ {
+ _tableQueue = dispatch_queue_create("DTLocalizableStringAggregator", 0);
+ _tableGroup = dispatch_group_create();
+
+ _processingQueue = [[NSOperationQueue alloc] init];
+ [_processingQueue setMaxConcurrentOperationCount:10];
+
+ _wantsPositionalParameters = YES; // default
+ _inputEncoding = NSUTF8StringEncoding; // default
+ }
+ return self;
}
-- (void)dealloc
+- (void)dealloc
{
dispatch_release(_tableQueue);
- dispatch_release(_tableGroup);
+ dispatch_release(_tableGroup);
}
-- (void)setCustomMacroPrefix:(NSString *)customMacroPrefix
+- (void)setCustomMacroPrefix:(NSString *)customMacroPrefix
{
- if (customMacroPrefix != _customMacroPrefix)
- {
- _customMacroPrefix = customMacroPrefix;
- _validMacros = nil;
- }
+ if (customMacroPrefix != _customMacroPrefix)
+ {
+ _customMacroPrefix = customMacroPrefix;
+ _validMacros = nil;
+ }
}
#define KEY @"rawKey"
@@ -77,144 +77,147 @@ - (void)setCustomMacroPrefix:(NSString *)customMacroPrefix
#define BUNDLE @"bundle"
#define TABLE @"tableName"
-- (NSDictionary *)validMacros
+- (NSDictionary *)validMacros
{
- if (!_validMacros)
- {
- // we know the allowed formats for NSLocalizedString() macros, so we can hard-code them
- // there's no need to parse this stuff when we know what format things must be
- NSArray *prefixes = [NSArray arrayWithObjects:@"NSLocalizedString", @"CFCopyLocalizedString", _customMacroPrefix, nil];
- NSDictionary *suffixes = [NSDictionary dictionaryWithObjectsAndKeys:
- [NSArray arrayWithObjects:KEY, COMMENT, nil], @"",
- [NSArray arrayWithObjects:KEY, TABLE, COMMENT, nil], @"FromTable",
- [NSArray arrayWithObjects:KEY, TABLE, BUNDLE, COMMENT, nil], @"FromTableInBundle",
- [NSArray arrayWithObjects:KEY, TABLE, BUNDLE, VALUE, COMMENT, nil], @"WithDefaultValue",
- nil];
-
- NSMutableDictionary *validMacros = [NSMutableDictionary dictionary];
- for (NSString *prefix in prefixes)
- {
- for (NSString *suffix in suffixes)
- {
- NSString *macroName = [prefix stringByAppendingString:suffix];
- NSArray *parameters = [suffixes objectForKey:suffix];
-
- [validMacros setObject:parameters forKey:macroName];
- }
- }
-
- _validMacros = validMacros;
- }
-
- return _validMacros;
+ if (!_validMacros)
+ {
+ // we know the allowed formats for NSLocalizedString() macros, so we can hard-code them
+ // there's no need to parse this stuff when we know what format things must be
+ NSArray *prefixes = [NSArray arrayWithObjects:@"NSLocalizedString", @"CFCopyLocalizedString", _customMacroPrefix, nil];
+ NSDictionary *suffixes = [NSDictionary dictionaryWithObjectsAndKeys:
+ [NSArray arrayWithObjects:KEY, COMMENT, nil], @"",
+ [NSArray arrayWithObjects:KEY, TABLE, COMMENT, nil], @"FromTable",
+ [NSArray arrayWithObjects:KEY, TABLE, BUNDLE, COMMENT, nil], @"FromTableInBundle",
+ [NSArray arrayWithObjects:KEY, TABLE, BUNDLE, VALUE, COMMENT, nil], @"WithDefaultValue",
+ nil];
+
+ NSMutableDictionary *validMacros = [NSMutableDictionary dictionary];
+ for (NSString *prefix in prefixes)
+ {
+ for (NSString *suffix in suffixes)
+ {
+ NSString *macroName = [prefix stringByAppendingString:suffix];
+ NSArray *parameters = [suffixes objectForKey:suffix];
+
+ [validMacros setObject:parameters forKey:macroName];
+ }
+ }
+
+ _validMacros = validMacros;
+ }
+
+ return _validMacros;
}
#define QUOTE @"\""
- (void)beginProcessingFile:(NSURL *)fileURL
{
- NSDictionary *validMacros = [self validMacros];
-
- DTLocalizableStringScanner *scanner = [[DTLocalizableStringScanner alloc] initWithContentsOfURL:fileURL encoding:_inputEncoding validMacros:validMacros];
-
- [scanner setEntryFoundCallback:^(DTLocalizableStringEntry *entry)
+ NSDictionary *validMacros = [self validMacros];
+
+ DTLocalizableStringScanner *scanner = [[DTLocalizableStringScanner alloc] initWithContentsOfURL:fileURL encoding:_inputEncoding validMacros:validMacros];
+
+ [scanner setEntryFoundCallback:^(DTLocalizableStringEntry *entry)
{
- NSString *key = [entry rawKey];
- NSString *value = [entry rawValue];
- BOOL shouldBeAdded = ([key hasPrefix:QUOTE] && [key hasSuffix:QUOTE]);
-
- if (value)
- {
- shouldBeAdded &= ([value hasPrefix:QUOTE] && [value hasSuffix:QUOTE]);
- }
-
- if (shouldBeAdded)
- {
- dispatch_group_async(_tableGroup, _tableQueue, ^{
- [self addEntryToTables:entry];
- });
- }
- else
- {
- NSLog(@"skipping: %@", entry);
- }
+ NSString *key = [entry rawKey];
+ NSString *value = [entry rawValue];
+ BOOL shouldBeAdded = ([key hasPrefix:QUOTE] && [key hasSuffix:QUOTE]);
+
+ if (value)
+ {
+ shouldBeAdded &= ([value hasPrefix:QUOTE] && [value hasSuffix:QUOTE]);
+ }
+
+ if (shouldBeAdded)
+ {
+ dispatch_group_async(_tableGroup, _tableQueue, ^{
+ [self addEntryToTables:entry];
+ });
+ }
+ else
+ {
+ NSLog(@"skipping: %@", entry);
+ }
}];
-
- [_processingQueue addOperation:scanner];
+
+ [_processingQueue addOperation:scanner];
}
- (void)addEntryToTables:(DTLocalizableStringEntry *)entry
{
- NSAssert(dispatch_get_current_queue() == _tableQueue, @"method called from invalid queue");
- if (!_stringTables)
- {
- _stringTables = [NSMutableDictionary dictionary];
- }
-
- if ([entry.tableName length] == 0)
- entry.tableName = _customTableName ?: @"Localizable";
-
- NSString *tableName = [entry tableName];
+ NSAssert(dispatch_get_current_queue() == _tableQueue, @"method called from invalid queue");
+ if (!_stringTables)
+ {
+ _stringTables = [NSMutableDictionary dictionary];
+ }
- BOOL shouldSkip = [_tablesToSkip containsObject:tableName];
-
- if (!shouldSkip)
- {
- // find the string table for this token, or create it
- DTLocalizableStringTable *table = [_stringTables objectForKey:tableName];
- if (!table)
- {
- // need to create it
+ // use default table name is no name set
+ if (![entry.tableName length])
+ {
+ entry.tableName = _defaultTableName ? _defaultTableName : @"Localizable";
+ }
+
+ NSString *tableName = [entry tableName];
+
+ BOOL shouldSkip = [_tablesToSkip containsObject:tableName];
+
+ if (!shouldSkip)
+ {
+ // find the string table for this token, or create it
+ DTLocalizableStringTable *table = [_stringTables objectForKey:tableName];
+ if (!table)
+ {
+ // need to create it
table = [[DTLocalizableStringTable alloc] initWithName:tableName];
- [_stringTables setObject:table forKey:tableName];
- }
-
- if (entry.rawValue)
- {
+ [_stringTables setObject:table forKey:tableName];
+ }
+
+ if (entry.rawValue)
+ {
// ...WithDefaultValue
- if (_wantsPositionalParameters)
- {
+ if (_wantsPositionalParameters)
+ {
entry.rawValue = [entry.rawValue stringByNumberingFormatPlaceholders];
}
[table addEntry:entry];
}
- else
- {
+ else
+ {
// all other options use the key and variations thereof
// support for predicate token splitting
NSArray *keyVariants = [entry.rawKey variantsFromPredicateVariations];
// add all variants
- for (NSString *oneVariant in keyVariants)
- {
+ for (NSString *oneVariant in keyVariants)
+ {
DTLocalizableStringEntry *splitEntry = [entry copy];
NSString *value = oneVariant;
- if (_wantsPositionalParameters)
- {
+ if (_wantsPositionalParameters)
+ {
value = [oneVariant stringByNumberingFormatPlaceholders];
}
-
+
// adjust key and value of the new entry
splitEntry.rawKey = oneVariant;
splitEntry.rawValue = value;
-
+
// add token to this table
[table addEntry:splitEntry];
}
}
- }
+ }
}
-- (NSArray *)aggregatedStringTables
+- (NSArray *)aggregatedStringTables
{
- // wait for both of these things to finish
- [_processingQueue waitUntilAllOperationsAreFinished];
- dispatch_group_wait(_tableGroup, DISPATCH_TIME_FOREVER);
-
- return [_stringTables allValues];
+ // wait for both of these things to finish
+ [_processingQueue waitUntilAllOperationsAreFinished];
+ dispatch_group_wait(_tableGroup, DISPATCH_TIME_FOREVER);
+
+ return [_stringTables allValues];
}
@end
@@ -21,7 +21,7 @@ typedef void(^DTLocalizableStringEntryWriteCallback)(DTLocalizableStringEntry *)
- (void)addEntry:(DTLocalizableStringEntry *)entry;
-- (NSString*)writeAsStringEncoding:(NSStringEncoding)encoding error:(NSError **)error entryWriteCallback:(DTLocalizableStringEntryWriteCallback)entryWriteCallback;
+- (NSString *)stringRepresentationWithEncoding:(NSStringEncoding)encoding error:(NSError **)error entryWriteCallback:(DTLocalizableStringEntryWriteCallback)entryWriteCallback;
- (BOOL)writeToFolderAtURL:(NSURL *)url encoding:(NSStringEncoding)encoding error:(NSError **)error entryWriteCallback:(DTLocalizableStringEntryWriteCallback)entryWriteCallback;
@end
@@ -75,7 +75,7 @@ - (void)addEntry:(DTLocalizableStringEntry *)entry
[_entryIndexByKey setObject:entry forKey:entry.rawKey];
}
-- (NSString*)writeAsStringEncoding:(NSStringEncoding)encoding error:(NSError **)error entryWriteCallback:(DTLocalizableStringEntryWriteCallback)entryWriteCallback
+- (NSString*)stringRepresentationWithEncoding:(NSStringEncoding)encoding error:(NSError **)error entryWriteCallback:(DTLocalizableStringEntryWriteCallback)entryWriteCallback
{
NSArray *sortedEntries = [_entries sortedArrayUsingSelector:@selector(compare:)];
@@ -143,7 +143,7 @@ - (BOOL)writeToFolderAtURL:(NSURL *)url encoding:(NSStringEncoding)encoding erro
return NO;
}
- NSString *tmpString = [self writeAsStringEncoding:encoding error:error entryWriteCallback:entryWriteCallback];
+ NSString *tmpString = [self stringRepresentationWithEncoding:encoding error:error entryWriteCallback:entryWriteCallback];
return [tmpString writeToURL:tableURL
atomically:YES
@@ -32,7 +32,7 @@ - (NSString *)stringByNumberingFormatPlaceholders
[tmpString appendString:@"1$"];
}
[tmpString appendString:[self substringWithRange:NSMakeRange(lastLocation + 1, currentLocation - lastLocation)]];
- [tmpString appendFormat:@"%d$", placeholderCount];
+ [tmpString appendFormat:@"%ld$", placeholderCount];
}
lastLocation = currentLocation;
}];
View
@@ -29,7 +29,7 @@ int main (int argc, const char *argv[])
BOOL wantsDecodedUnicodeSequences = NO;
NSMutableSet *tablesToSkip = [NSMutableSet set];
NSString *customMacroPrefix = nil;
- NSString *customTableName = nil;
+ NSString *defaultTableName = nil;
// analyze options
BOOL optionsInvalid = NO;
@@ -150,7 +150,7 @@ int main (int argc, const char *argv[])
break;
}
- customTableName = [NSString stringWithUTF8String:argv[i]];
+ defaultTableName = [NSString stringWithUTF8String:argv[i]];
}
i++;
@@ -171,7 +171,7 @@ int main (int argc, const char *argv[])
aggregator.inputEncoding = inputStringEncoding;
aggregator.customMacroPrefix = customMacroPrefix;
aggregator.tablesToSkip = tablesToSkip;
- aggregator.customTableName = customTableName;
+ aggregator.defaultTableName = defaultTableName;
// go, go, go!
for (NSURL *file in files) {
Oops, something went wrong.

0 comments on commit 6ea48fb

Please sign in to comment.