@@ -435,19 +435,22 @@ ret->isa = [OFString class]; return ret; } - (OFString*)_XMLStringWithParent: (OFXMLElement*)parent + indentation: (unsigned int)indentation + level: (size_t)level { OFAutoreleasePool *pool, *pool2; char *cString; size_t length, i, j, attributesCount; OFString *prefix, *parentPrefix; OFXMLAttribute **attributesCArray; OFString *ret, *tmp; OFMutableDictionary *allNamespaces; OFString *defaultNS; + BOOL indentAfter = YES; if (characters != nil) return [characters stringByXMLEscaping]; if (CDATA != nil) @@ -483,12 +486,15 @@ defaultNS = parent->defaultNamespace; else defaultNS = defaultNamespace; i = 0; - length = [name cStringLength] + 3; + length = [name cStringLength] + 3 + (level * indentation); cString = [self allocMemoryWithSize: length]; + + for (j = 0; j < level * indentation; j++) + cString[i++] = ' '; /* Start of tag */ cString[i++] = '<'; if (prefix != nil && ![ns isEqual: defaultNS]) { @@ -585,27 +591,47 @@ SEL appendSel = @selector(appendCStringWithoutUTF8Checking:); tmp = [OFMutableString string]; append = [tmp methodForSelector: appendSel]; - for (j = 0; j < childrenCount; j++) + for (j = 0; j < childrenCount; j++) { + if (indentation > 0 && childrenCArray[j]->name != nil) + append(tmp, appendSel, "\n"); + append(tmp, appendSel, - [[childrenCArray[j] _XMLStringWithParent: self] + [[childrenCArray[j] + _XMLStringWithParent: self + indentation: indentation + level: level + 1] cString]); + } - length += [tmp cStringLength] + [name cStringLength] + 2; + if (indentation > 0 && childrenCount > 0 && + childrenCArray[j - 1]->name != nil) + append(tmp, appendSel, "\n"); + else + indentAfter = NO; + + length += [tmp cStringLength] + [name cStringLength] + 2 + + (indentAfter ? level * indentation : 0); @try { cString = [self resizeMemory: cString toSize: length]; } @catch (id e) { [self freeMemory: cString]; @throw e; } cString[i++] = '>'; + memcpy(cString + i, [tmp cString], [tmp cStringLength]); i += [tmp cStringLength]; + + if (indentAfter) + for (j = 0; j < level * indentation; j++) + cString[i++] = ' '; + cString[i++] = '<'; cString[i++] = '/'; if (prefix != nil) { length += [prefix cStringLength] + 1; @try { @@ -640,16 +666,25 @@ return ret; } - (OFString*)XMLString { - return [self _XMLStringWithParent: nil]; + return [self _XMLStringWithParent: nil + indentation: 0 + level: 0]; +} + +- (OFString*)XMLStringWithIndentation: (unsigned int)indentation +{ + return [self _XMLStringWithParent: nil + indentation: indentation + level: 0]; } - (OFString*)description { - return [self XMLString]; + return [self XMLStringWithIndentation: 2]; } - (OFXMLElement*)XMLElementBySerializing { OFAutoreleasePool *pool;