开发者

Parsing a Java Properties file in Objective-C for iPhone

I'm looking for a way in the iPhone SDK to read in a Properties file (not the XML flavor) for example this one:

# a comment
! a comment

a = a string
b = a string with escape sequences \t \n \r \\ \" \' 开发者_高级运维\ (space) \u0123
c = a string with a continuation line \
    continuation line
d.e.f = another string

would result in four key/value pairs.

I can't change this format as it is sent to me by a web service. Can you please direct me?

Thanks, Emmanuel


I would take a look at ParseKit http://parsekit.com/. Otherwise you could use RegexKitLite and create some regular expressions.


I've ended with this solution if anybody is interested :

@interface NSDictionary (PropertiesFile)

+ (NSDictionary *)dictionaryWithPropertiesFile:(NSString *)file;

@end

@implementation NSDictionary (PropertiesFile)

+ (NSDictionary *)dictionaryWithPropertiesFile:(NSString *)file {
    NSError *error = nil;
    NSString *propertyFileContent = [[NSString alloc] initWithContentsOfFile:file encoding:NSUTF8StringEncoding error:&error];
    if (error) return nil;

    NSArray *properties = [propertyFileContent componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]];
    if (properties.count == 0) return nil;

    NSMutableDictionary *result = [[NSMutableDictionary alloc] initWithCapacity:properties.count];
    for (NSString *propertySting in properties) {
        NSArray *property = [propertySting componentsSeparatedByString:@"="];

        if (property.count != 2) continue;

        [result setObject:property[1] forKey:property[0]];
    }
    return result.allKeys.count > 0 ? result : nil;
}

@end


It's a perfectly simple parsing problem. Read a line. Ignore if comment. Check for continuation, and read/append continuation lines as needed. Look for the "=". Make the left side of the "=" (after trimming white space) the key. Either parse the right side yourself or put it into an NSString and use stringWithFormat on it to "reduce" any escapes to pure character form. Return key and reduced right side.

(But refreshing my memory on the properties file format reminds me that:

The key contains all of the characters in the line starting with the first non-white space character and up to, but not including, the first unescaped '=', ':', or white space character other than a line terminator. All of these key termination characters may be included in the key by escaping them with a preceding backslash character;

So a little scanning of the line is required to separate the key from the rest. Nothing particularly difficult, though.)


Have you considered using lex/yacc or flex/bison to generate your own compiler code from a description of the grammar for properties files? I'm not sure if there are any existing grammars defined for a Java properties file, but it seems like it would be a pretty simple grammar to write.

Here's another SO post that mentions this approach for general purpose parsing


Take a look at this PropertyParser

NSString *text = @"sample key = sample value";

PropertyParser *propertyParser = [[PropertyParser alloc] init];

NSMutableDictionary *keyValueMap = [propertyParser parse:text];


You can now use NSRegularExpression class to do this.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜