Skip navigation.
 
mlRe: CoreData bug, XML-related
FROM : Ondra Cada
DATE : Wed Jul 26 17:55:27 2006

On 26.7.2006, at 16:30, Ondra Cada wrote:

> I've bumped into a CoreData bug, which self-evidently is related to 
> its XML generator and/or parser. Here's a simple repeat procedure:
>
> (i) take the standard Apple example /Developer/Examples/CoreData/
> EventManager
> (ii) build the thing and launch
> (iii) create a new event, name it
>
> "#990000"><
>
> (incl. quote marks), set some dates, save using XML format, close
> (iv) reopen the file. The name is now '"#990000"><&#990000">&lt;'.
>
> Is this a known bug? More important, is there a known work-around?


Well this *seems to* work, but (a) is ugly, (b) is bound to be veeery 
slow, (c) does not keep the API contract ("do not override 
primitiveValueForKey")... anybody knows of a better solution? (Mean 
better *in principle*, I know I could optimize)...

@implementation MyRootObject:NSManagedObject // all managed objects 
have to inherit from this ugly thing
static NSString *amp,*lt,*gt;
static inline id encode(id value,id self,NSString *key) {
    NSEntityDescription *ed=[self entity];
    NSAttributeDescription *ad=[[ed attributesByName] 
objectForKey:key];
    if ([ad attributeType]!=NSStringAttributeType || ![value 
isKindOfClass:[NSString class]])
        return value; // not a string
    if (![value containsString:@"&"] && ![value containsString:@"<"] 
&& ![value containsString:@">"])
        return value; // no need to encode, prevent recursion 
(hopefully :))
    //NSLog(@"encoding %@:%x %@ (%@)",[self class],self,key,value);
    value=[[value componentsSeparatedByString:@"&"] 
componentsJoinedByString:amp];
    value=[[value componentsSeparatedByString:@"<"] 
componentsJoinedByString:lt];
    value=[[value componentsSeparatedByString:@">"] 
componentsJoinedByString:gt];
    //NSLog(@"          -----> (%@)",value);
    return value;
}
static inline id decode(id value,id self,NSString *key) {
    NSEntityDescription *ed=[self entity];
    NSAttributeDescription *ad=[[ed attributesByName] 
objectForKey:key];
    if ([ad attributeType]!=NSStringAttributeType || ![value 
isKindOfClass:[NSString class]])
        return value; // not a string
    if (![value containsString:amp] && ![value containsString:lt] 
&& ![value containsString:gt])
        return value; // no need to decode, prevent recursion 
(hopefully :))
    //NSLog(@"decoding %@:%x %@ (%@)",[self class],self,key,value);
    value=[[value componentsSeparatedByString:amp] 
componentsJoinedByString:@"&"];
    value=[[value componentsSeparatedByString:lt] 
componentsJoinedByString:@"<"];
    value=[[value componentsSeparatedByString:gt] 
componentsJoinedByString:@">"];
    //NSLog(@"          -----> (%@)",value);
    return value;
}

-primitiveValueForKey:(NSString*)key {
    return decode([super primitiveValueForKey:key],self,key);
}
-(void)setPrimitiveValue:value forKey:(NSString*)key {
    [super setPrimitiveValue:encode(value,self,key) forKey:key];
}
-valueForKey:(NSString*)key {
    return decode([super valueForKey:key],self,key);
}
-(void)setValue:value forKey:(NSString*)key {
    [super setValue:encode(value,self,key) forKey:key];
}
+(void)initialize {
    amp=[[NSString stringWithFormat:@"%C",0x20b0] retain];
    lt=[[NSString stringWithFormat:@"%C",0x276e] retain];
    gt=[[NSString stringWithFormat:@"%C",0x276f] retain];
}
@end
---
Ondra Čada
OCSoftware:    <email_removed>              http://www.ocs.cz
private        <email_removed>            http://www.ocs.cz/oc

Related mailsAuthorDate
mlCoreData bug, XML-related Ondra Cada Jul 26, 16:30
mlRe: CoreData bug, XML-related Ondra Cada Jul 26, 17:55
mlRe: CoreData bug, XML-related Gerriet M. Denkman… Jul 26, 22:23