@@ -15,29 +15,36 @@ #include "config.h" #include -#import "OFXMLProcessingInstructions.h" -#import "OFXMLNode+Private.h" +#import "OFXMLProcessingInstruction.h" #import "OFString.h" +#import "OFXMLAttribute.h" #import "OFXMLElement.h" +#import "OFXMLNode+Private.h" #import "OFInvalidArgumentException.h" -@implementation OFXMLProcessingInstructions -+ (instancetype)processingInstructionsWithString: (OFString *)string +@implementation OFXMLProcessingInstruction +@synthesize target = _target, data = _data; + ++ (instancetype)processingInstructionWithTarget: (OFString *)target + data: (OFString *)data { - return [[[self alloc] initWithString: string] autorelease]; + return [[[self alloc] initWithTarget: target + data: data] autorelease]; } -- (instancetype)initWithString: (OFString *)string +- (instancetype)initWithTarget: (OFString *)target + data: (OFString *)data { self = [super of_init]; @try { - _processingInstructions = [string copy]; + _target = [target copy]; + _data = [data copy]; } @catch (id e) { [self release]; @throw e; } @@ -44,20 +51,25 @@ return self; } - (instancetype)initWithSerialization: (OFXMLElement *)element { - self = [super of_init]; - @try { void *pool = objc_autoreleasePoolPush(); + OFXMLAttribute *targetAttr; if (![element.name isEqual: self.className] || - ![element.namespace isEqual: OF_SERIALIZATION_NS]) + ![element.namespace isEqual: OFSerializationNS]) + @throw [OFInvalidArgumentException exception]; + + targetAttr = [element attributeForName: @"target" + namespace: OFSerializationNS]; + if (targetAttr.stringValue.length == 0) @throw [OFInvalidArgumentException exception]; - _processingInstructions = [element.stringValue copy]; + self = [self initWithTarget: targetAttr.stringValue + data: element.stringValue]; objc_autoreleasePoolPop(pool); } @catch (id e) { [self release]; @throw e; @@ -66,81 +78,110 @@ return self; } - (void)dealloc { - [_processingInstructions release]; + [_target release]; + [_data release]; [super dealloc]; } - (bool)isEqual: (id)object { - OFXMLProcessingInstructions *processingInstructions; + OFXMLProcessingInstruction *processingInstruction; if (object == self) return true; - if (![object isKindOfClass: [OFXMLProcessingInstructions class]]) + if (![object isKindOfClass: [OFXMLProcessingInstruction class]]) + return false; + + processingInstruction = object; + + if (![processingInstruction->_target isEqual: _target]) + return false; + + if (processingInstruction->_data != _data && + ![processingInstruction->_data isEqual: _data]) return false; - processingInstructions = object; - - return [processingInstructions->_processingInstructions - isEqual: _processingInstructions]; + return true; } - (unsigned long)hash { - return _processingInstructions.hash; + unsigned long hash; + + OFHashInit(&hash); + OFHashAddHash(&hash, _target.hash); + OFHashAddHash(&hash, _data.hash); + OFHashFinalize(&hash); + + return hash; } - (OFString *)stringValue { return @""; } - (OFString *)XMLString { - return [OFString stringWithFormat: @"", _processingInstructions]; + if (_data.length > 0) + return [OFString stringWithFormat: @"", + _target, _data]; + else + return [OFString stringWithFormat: @"", _target]; } - (OFString *)XMLStringWithIndentation: (unsigned int)indentation { - return [OFString stringWithFormat: @"", _processingInstructions]; + return self.XMLString; } - (OFString *)XMLStringWithIndentation: (unsigned int)indentation level: (unsigned int)level { - OFString *ret; - if (indentation > 0 && level > 0) { - char *whitespaces = of_alloc((level * indentation) + 1, 1); + OFString *ret; + char *whitespaces = OFAllocMemory((level * indentation) + 1, 1); memset(whitespaces, ' ', level * indentation); whitespaces[level * indentation] = 0; @try { - ret = [OFString stringWithFormat: - @"%s", whitespaces, _processingInstructions]; - } @finally { - free(whitespaces); - } - } else - ret = [OFString stringWithFormat: @"", - _processingInstructions]; - - return ret; + if (_data.length > 0) + ret = [OFString stringWithFormat: + @"%s", whitespaces, + _target, _data]; + else + ret = [OFString stringWithFormat: + @"%s", whitespaces, _target]; + } @finally { + OFFreeMemory(whitespaces); + } + + return ret; + } else + return self.XMLString; } - (OFString *)description { - return [OFString stringWithFormat: @"", _processingInstructions]; + return self.XMLString; } - (OFXMLElement *)XMLElementBySerializing { - return [OFXMLElement elementWithName: self.className - namespace: OF_SERIALIZATION_NS - stringValue: _processingInstructions]; + OFXMLElement *ret = [OFXMLElement elementWithName: self.className + namespace: OFSerializationNS + stringValue: _data]; + void *pool = objc_autoreleasePoolPush(); + + [ret addAttribute: [OFXMLAttribute attributeWithName: @"target" + stringValue: _target]]; + + objc_autoreleasePoolPop(pool); + + return ret; } @end