Re: CoreData bug, XML-related
Re: CoreData bug, XML-related
- Subject: Re: CoreData bug, XML-related
- From: Ondra Cada <email@hidden>
- Date: Wed, 26 Jul 2006 17:55:27 +0200
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"><󱬰"><'.
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@hidden http://www.ocs.cz
private email@hidden http://www.ocs.cz/oc
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden