Index: src/OFXMLElement.h ================================================================== --- src/OFXMLElement.h +++ src/OFXMLElement.h @@ -348,16 +348,60 @@ * @param child An OFXMLNode which is added as a child */ - (void)addChild: (OFXMLNode*)child; /*! - * @brief Removes the first child that is equal to the specified OFXMLElement. + * @brief Inserts a child at the specified index. + * + * @param child An OFXMLNode which is added as a child + * @param index The index where the child is added + */ +- (void)insertChild: (OFXMLNode*)child + atIndex: (size_t)index; + +/*! + * @brief Inserts the specified children at the specified index. + * + * @param child An OFXMLNode which is added as a child + * @param index The index where the child is added + */ +- (void)insertChildren: (OFArray*)children + atIndex: (size_t)index; + +/*! + * @brief Removes the first child that is equal to the specified OFXMLNode. * * @param child The child to remove from the OFXMLElement */ - (void)removeChild: (OFXMLNode*)child; +/*! + * @brief Removes the child at the specified index. + * + * @param index The index of the child to remove + */ + +- (void)removeChildAtIndex: (size_t)index; +/*! + * @brief Replaces the first child that is equal to the specified OFXMLNode + * with the specified node. + * + * @param child The child to replace + * @param node The node to replace the child with + */ +- (void)replaceChild: (OFXMLNode*)child + withNode: (OFXMLNode*)node; + +/*! + * @brief Replaces the child at the specified index with the specified node. + * + * @param index The index of the child to replace + * @param node The node to replace the child with + */ +- (void)replaceChildAtIndex: (size_t)index + withNode: (OFXMLNode*)node; + /*! * @brief Returns all children that are elements. * * @return All children that are elements */ Index: src/OFXMLElement.m ================================================================== --- src/OFXMLElement.m +++ src/OFXMLElement.m @@ -940,10 +940,44 @@ if (children == nil) children = [[OFMutableArray alloc] init]; [children addObject: child]; } + +- (void)insertChild: (OFXMLNode*)child + atIndex: (size_t)index +{ + if ([child isKindOfClass: [OFXMLAttribute class]]) + @throw [OFInvalidArgumentException + exceptionWithClass: [self class] + selector: _cmd]; + + if (children == nil) + children = [[OFMutableArray alloc] init]; + + [children insertObject: child + atIndex: index]; +} + +- (void)insertChildren: (OFArray*)children_ + atIndex: (size_t)index +{ + void *pool = objc_autoreleasePoolPush(); + OFEnumerator *enumerator = [children_ objectEnumerator]; + OFXMLNode *node; + + while ((node = [enumerator nextObject]) != nil) + if ([node isKindOfClass: [OFXMLAttribute class]]) + @throw [OFInvalidArgumentException + exceptionWithClass: [self class] + selector: _cmd]; + + [children insertObjectsFromArray: children_ + atIndex: index]; + + objc_autoreleasePoolPop(pool); +} - (void)removeChild: (OFXMLNode*)child { if ([child isKindOfClass: [OFXMLAttribute class]]) @throw [OFInvalidArgumentException @@ -950,10 +984,40 @@ exceptionWithClass: [self class] selector: _cmd]; [children removeObject: child]; } + +- (void)removeChildAtIndex: (size_t)index +{ + [children removeObjectAtIndex: index]; +} + +- (void)replaceChild: (OFXMLNode*)child + withNode: (OFXMLNode*)node +{ + if ([node isKindOfClass: [OFXMLAttribute class]] || + [child isKindOfClass: [OFXMLAttribute class]]) + @throw [OFInvalidArgumentException + exceptionWithClass: [self class] + selector: _cmd]; + + [children replaceObject: child + withObject: node]; +} + +- (void)replaceChildAtIndex: (size_t)index + withNode: (OFXMLNode*)node +{ + if ([node isKindOfClass: [OFXMLAttribute class]]) + @throw [OFInvalidArgumentException + exceptionWithClass: [self class] + selector: _cmd]; + + [children replaceObjectAtIndex: index + withObject: node]; +} - (OFXMLElement*)elementForName: (OFString*)elementName { return [[self elementsForName: elementName] firstObject]; }