Skip to content
Browse files

(split)Merged with guillemot_dev

  • Loading branch information...
2 parents cbfc693 + 721178b commit ccf6525603a429a8ddd7b15472cf31b20ee794e5 @schwa schwa committed Apr 23, 2010
View
2 Common/Source/CXMLDocument.m
@@ -198,7 +198,7 @@ - (CXMLElement *)rootElement
{
xmlNodePtr theLibXMLNode = xmlDocGetRootElement((xmlDocPtr)_node);
-return([CXMLNode nodeWithLibXMLNode:theLibXMLNode]);
+return([CXMLNode nodeWithLibXMLNode:theLibXMLNode freeOnDealloc:NO]);
}
- (NSData *)XMLData
View
2 Common/Source/CXMLElement.h
@@ -46,5 +46,5 @@
//- (CXMLNode *)resolveNamespaceForName:(NSString *)name;
//- (NSString *)resolvePrefixForNamespaceURI:(NSString *)namespaceURI;
-- (NSString*)_XMLStringWithOptions:(NSUInteger)options appendingToString:(NSMutableString*)str;
+//- (NSString*)_XMLStringWithOptions:(NSUInteger)options appendingToString:(NSMutableString*)str;
@end
View
84 Common/Source/CXMLElement.m
@@ -45,7 +45,7 @@ - (NSArray *)elementsForName:(NSString *)name
{
if (theCurrentNode->type == XML_ELEMENT_NODE && xmlStrcmp(theName, theCurrentNode->name) == 0)
{
- CXMLNode *theNode = [CXMLNode nodeWithLibXMLNode:(xmlNodePtr)theCurrentNode];
+ CXMLNode *theNode = [CXMLNode nodeWithLibXMLNode:(xmlNodePtr)theCurrentNode freeOnDealloc:NO];
[theElements addObject:theNode];
}
theCurrentNode = theCurrentNode->next;
@@ -61,7 +61,7 @@ - (NSArray *)attributes
xmlAttrPtr theCurrentNode = _node->properties;
while (theCurrentNode != NULL)
{
- CXMLNode *theAttribute = [CXMLNode nodeWithLibXMLNode:(xmlNodePtr)theCurrentNode];
+ CXMLNode *theAttribute = [CXMLNode nodeWithLibXMLNode:(xmlNodePtr)theCurrentNode freeOnDealloc:NO];
[theAttributes addObject:theAttribute];
theCurrentNode = theCurrentNode->next;
}
@@ -78,7 +78,7 @@ - (CXMLNode *)attributeForName:(NSString *)name
{
if (xmlStrcmp(theName, theCurrentNode->name) == 0)
{
- CXMLNode *theAttribute = [CXMLNode nodeWithLibXMLNode:(xmlNodePtr)theCurrentNode];
+ CXMLNode *theAttribute = [CXMLNode nodeWithLibXMLNode:(xmlNodePtr)theCurrentNode freeOnDealloc:NO];
return(theAttribute);
}
theCurrentNode = theCurrentNode->next;
@@ -92,45 +92,45 @@ - (CXMLNode *)attributeForName:(NSString *)name
//- (CXMLNode *)resolveNamespaceForName:(NSString *)name;
//- (NSString *)resolvePrefixForNamespaceURI:(NSString *)namespaceURI;
-- (NSString*)_XMLStringWithOptions:(NSUInteger)options appendingToString:(NSMutableString*)str
-{
-NSString* name = [self name];
-[str appendString:[NSString stringWithFormat:@"<%@", name]];
-
-for (id attribute in [self attributes] )
- {
- [attribute _XMLStringWithOptions:options appendingToString:str];
- }
-
-if ( ! _node->children )
- {
- bool isEmpty = NO;
- NSArray *emptyTags = [NSArray arrayWithObjects: @"br", @"area", @"link", @"img", @"param", @"hr", @"input", @"col", @"base", @"meta", nil ];
- for (id s in emptyTags)
- {
- if ( [s isEqualToString:@"base"] )
- {
- isEmpty = YES;
- break;
- }
- }
- if ( isEmpty )
- {
- [str appendString:@"/>"];
- return str;
- }
- }
-
-[str appendString:@">"];
-
-if ( _node->children )
- {
- for (id child in [self children])
- [child _XMLStringWithOptions:options appendingToString:str];
- }
-[str appendString:[NSString stringWithFormat:@"</%@>", name]];
-return str;
-}
+//- (NSString*)_XMLStringWithOptions:(NSUInteger)options appendingToString:(NSMutableString*)str
+//{
+//NSString* name = [self name];
+//[str appendString:[NSString stringWithFormat:@"<%@", name]];
+//
+//for (id attribute in [self attributes] )
+// {
+// [attribute _XMLStringWithOptions:options appendingToString:str];
+// }
+//
+//if ( ! _node->children )
+// {
+// bool isEmpty = NO;
+// NSArray *emptyTags = [NSArray arrayWithObjects: @"br", @"area", @"link", @"img", @"param", @"hr", @"input", @"col", @"base", @"meta", nil ];
+// for (id s in emptyTags)
+// {
+// if ( [s isEqualToString:@"base"] )
+// {
+// isEmpty = YES;
+// break;
+// }
+// }
+// if ( isEmpty )
+// {
+// [str appendString:@"/>"];
+// return str;
+// }
+// }
+//
+//[str appendString:@">"];
+//
+//if ( _node->children )
+// {
+// for (id child in [self children])
+// [child _XMLStringWithOptions:options appendingToString:str];
+// }
+//[str appendString:[NSString stringWithFormat:@"</%@>", name]];
+//return str;
+//}
- (NSString *)description
{
View
3 Common/Source/CXMLNode.h
@@ -51,6 +51,7 @@ typedef enum {
// NSXMLNode
@interface CXMLNode : NSObject <NSCopying> {
xmlNodePtr _node;
+ BOOL _freeNodeOnRelease;
}
- (CXMLNodeKind)kind;
@@ -79,6 +80,4 @@ typedef enum {
- (NSString *)XMLStringWithOptions:(NSUInteger)options;
//- (NSString *)canonicalXMLStringPreservingComments:(BOOL)comments;
- (NSArray *)nodesForXPath:(NSString *)xpath error:(NSError **)error;
-
-- (NSString*)_XMLStringWithOptions:(NSUInteger)options appendingToString:(NSMutableString*)str;
@end
View
74 Common/Source/CXMLNode.m
@@ -35,6 +35,11 @@
#include <libxml/xpath.h>
#include <libxml/xpathInternals.h>
+#include <libxml/xmlIO.h>
+
+static int MyXmlOutputWriteCallback(void * context, const char * buffer, int len);
+static int MyXmlOutputCloseCallback(void * context);
+
@implementation CXMLNode
@@ -44,6 +49,12 @@ - (void)dealloc
{
if (_node->_private == self)
_node->_private = NULL;
+
+ if (_freeNodeOnRelease)
+ {
+ xmlFreeNode(_node);
+ }
+
_node = NULL;
}
//
@@ -53,7 +64,7 @@ - (void)dealloc
- (id)copyWithZone:(NSZone *)zone;
{
xmlNodePtr theNewNode = xmlCopyNode(_node, 1);
-CXMLNode *theNode = [[[self class] alloc] initWithLibXMLNode:theNewNode];
+CXMLNode *theNode = [[[self class] alloc] initWithLibXMLNode:theNewNode freeOnDealloc:YES];
theNewNode->_private = theNode;
return(theNode);
}
@@ -160,7 +171,7 @@ - (NSArray *)children
xmlNodePtr theCurrentNode = _node->children;
while (theCurrentNode != NULL)
{
- CXMLNode *theNode = [CXMLNode nodeWithLibXMLNode:theCurrentNode];
+ CXMLNode *theNode = [CXMLNode nodeWithLibXMLNode:theCurrentNode freeOnDealloc:NO];
[theChildren addObject:theNode];
theCurrentNode = theCurrentNode->next;
}
@@ -176,7 +187,7 @@ - (CXMLNode *)childAtIndex:(NSUInteger)index
for (N = 0; theCurrentNode != NULL && N != index; ++N, theCurrentNode = theCurrentNode->next)
;
if (theCurrentNode)
- return([CXMLNode nodeWithLibXMLNode:theCurrentNode]);
+ return([CXMLNode nodeWithLibXMLNode:theCurrentNode freeOnDealloc:NO]);
return(NULL);
}
@@ -187,7 +198,7 @@ - (CXMLNode *)previousSibling
if (_node->prev == NULL)
return(NULL);
else
- return([CXMLNode nodeWithLibXMLNode:_node->prev]);
+ return([CXMLNode nodeWithLibXMLNode:_node->prev freeOnDealloc:NO]);
}
- (CXMLNode *)nextSibling
@@ -197,7 +208,7 @@ - (CXMLNode *)nextSibling
if (_node->next == NULL)
return(NULL);
else
- return([CXMLNode nodeWithLibXMLNode:_node->next]);
+ return([CXMLNode nodeWithLibXMLNode:_node->next freeOnDealloc:NO]);
}
//- (CXMLNode *)previousNode;
@@ -246,34 +257,21 @@ - (NSString *)XMLString
return([self XMLStringWithOptions:0]);
}
-- (NSString*)_XMLStringWithOptions:(NSUInteger)options appendingToString:(NSMutableString*)str
+- (NSString *)XMLStringWithOptions:(NSUInteger)options
{
-#pragma unused (options)
+NSMutableData *theData = [[NSMutableData alloc] init];
-id value = NULL;
-switch([self kind])
- {
- case CXMLAttributeKind:
- value = [NSMutableString stringWithString:[self stringValue]];
- [value replaceOccurrencesOfString:@"\"" withString:@"&quot;" options:0 range:NSMakeRange(0, [value length])];
- [str appendFormat:@" %@=\"%@\"", [self name], value];
- break;
- case CXMLTextKind:
- [str appendString:[self stringValue]];
- break;
- case XML_COMMENT_NODE:
- case XML_CDATA_SECTION_NODE:
- // TODO: NSXML does not have XML_CDATA_SECTION_NODE correspondent.
- break;
- default:
- NSAssert1(NO, @"TODO not implemented type (%d).", [self kind]);
- }
-return str;
-}
+xmlOutputBufferPtr theOutputBuffer = xmlOutputBufferCreateIO(MyXmlOutputWriteCallback, MyXmlOutputCloseCallback, theData, NULL);
-- (NSString *)XMLStringWithOptions:(NSUInteger)options
-{
-return [self _XMLStringWithOptions:options appendingToString:[NSMutableString string]];
+xmlNodeDumpOutput(theOutputBuffer, _node->doc, _node, 0, 0, "utf-8");
+
+xmlOutputBufferFlush(theOutputBuffer);
+
+NSString *theString = [[[NSString alloc] initWithData:theData encoding:NSUTF8StringEncoding] autorelease];
+
+xmlOutputBufferClose(theOutputBuffer);
+
+return(theString);
}
//- (NSString *)canonicalXMLStringPreservingComments:(BOOL)comments;
@@ -305,7 +303,7 @@ - (NSArray *)nodesForXPath:(NSString *)xpath error:(NSError **)error
for (N = 0; N < theXPathObject->nodesetval->nodeNr; N++)
{
xmlNodePtr theNode = theXPathObject->nodesetval->nodeTab[N];
- [theArray addObject:[CXMLNode nodeWithLibXMLNode:theNode]];
+ [theArray addObject:[CXMLNode nodeWithLibXMLNode:theNode freeOnDealloc:NO]];
}
theResult = theArray;
@@ -321,3 +319,17 @@ - (NSArray *)nodesForXPath:(NSString *)xpath error:(NSError **)error
@end
+
+static int MyXmlOutputWriteCallback(void * context, const char * buffer, int len)
+{
+NSMutableData *theData = context;
+[theData appendBytes:buffer length:len];
+return(len);
+}
+
+static int MyXmlOutputCloseCallback(void * context)
+{
+NSMutableData *theData = context;
+[theData release];
+return(0);
+}
View
4 Common/Source/CXMLNode_PrivateExtensions.h
@@ -33,8 +33,8 @@
@property (readonly, nonatomic, assign) xmlNodePtr node;
-- (id)initWithLibXMLNode:(xmlNodePtr)inLibXMLNode;
+- (id)initWithLibXMLNode:(xmlNodePtr)inLibXMLNode freeOnDealloc:(BOOL)infreeOnDealloc;
-+ (id)nodeWithLibXMLNode:(xmlNodePtr)inLibXMLNode;
++ (id)nodeWithLibXMLNode:(xmlNodePtr)inLibXMLNode freeOnDealloc:(BOOL)infreeOnDealloc;
@end
View
7 Common/Source/CXMLNode_PrivateExtensions.m
@@ -38,16 +38,17 @@ @implementation CXMLNode (CXMLNode_PrivateExtensions)
-- (id)initWithLibXMLNode:(xmlNodePtr)inLibXMLNode;
+- (id)initWithLibXMLNode:(xmlNodePtr)inLibXMLNode freeOnDealloc:(BOOL)infreeOnDealloc
{
if ((self = [super init]) != NULL)
{
_node = inLibXMLNode;
+ _freeNodeOnRelease = infreeOnDealloc;
}
return(self);
}
-+ (id)nodeWithLibXMLNode:(xmlNodePtr)inLibXMLNode
++ (id)nodeWithLibXMLNode:(xmlNodePtr)inLibXMLNode freeOnDealloc:(BOOL)infreeOnDealloc
{
// TODO more checking.
if (inLibXMLNode->_private)
@@ -72,7 +73,7 @@ + (id)nodeWithLibXMLNode:(xmlNodePtr)inLibXMLNode
return(NULL);
}
-CXMLNode *theNode = [[[theClass alloc] initWithLibXMLNode:inLibXMLNode] autorelease];
+CXMLNode *theNode = [[[theClass alloc] initWithLibXMLNode:inLibXMLNode freeOnDealloc:infreeOnDealloc] autorelease];
if (inLibXMLNode->doc != NULL)
View
2 Common/Source/CXMLNode_XPathExtensions.m
@@ -64,7 +64,7 @@ - (NSArray *)nodesForXPath:(NSString *)xpath namespaceMappings:(NSDictionary *)i
for (N = 0; N < theXPathObject->nodesetval->nodeNr; N++)
{
xmlNodePtr theNode = theXPathObject->nodesetval->nodeTab[N];
- [theArray addObject:[CXMLNode nodeWithLibXMLNode:theNode]];
+ [theArray addObject:[CXMLNode nodeWithLibXMLNode:theNode freeOnDealloc:NO]];
}
theResult = theArray;
View
14 Common/Source/Creation/CXMLNode_CreationExtensions.m
@@ -40,7 +40,7 @@ + (id)document;
{
xmlDocPtr theDocumentNode = xmlNewDoc((const xmlChar *)"1.0");
NSAssert(theDocumentNode != NULL, @"xmlNewDoc failed");
-CXMLDocument *theDocument = [[[CXMLDocument alloc] initWithLibXMLNode:(xmlNodePtr)theDocumentNode] autorelease];
+CXMLDocument *theDocument = [[[CXMLDocument alloc] initWithLibXMLNode:(xmlNodePtr)theDocumentNode freeOnDealloc:NO] autorelease];
return(theDocument);
}
@@ -49,15 +49,15 @@ + (id)documentWithRootElement:(CXMLElement *)element;
xmlDocPtr theDocumentNode = xmlNewDoc((const xmlChar *)"1.0");
NSAssert(theDocumentNode != NULL, @"xmlNewDoc failed");
xmlDocSetRootElement(theDocumentNode, element.node);
-CXMLDocument *theDocument = [[[CXMLDocument alloc] initWithLibXMLNode:(xmlNodePtr)theDocumentNode] autorelease];
+CXMLDocument *theDocument = [[[CXMLDocument alloc] initWithLibXMLNode:(xmlNodePtr)theDocumentNode freeOnDealloc:NO] autorelease];
[theDocument.nodePool addObject:element];
return(theDocument);
}
+ (id)elementWithName:(NSString *)name
{
xmlNodePtr theElementNode = xmlNewNode(NULL, (const xmlChar *)[name UTF8String]);
-CXMLElement *theElement = [[[CXMLElement alloc] initWithLibXMLNode:(xmlNodePtr)theElementNode] autorelease];
+CXMLElement *theElement = [[[CXMLElement alloc] initWithLibXMLNode:(xmlNodePtr)theElementNode freeOnDealloc:NO] autorelease];
return(theElement);
}
@@ -67,14 +67,14 @@ + (id)elementWithName:(NSString *)name URI:(NSString *)URI
xmlNsPtr theNSNode = xmlNewNs(theElementNode, (const xmlChar *)[URI UTF8String], NULL);
theElementNode->ns = theNSNode;
-CXMLElement *theElement = [[[CXMLElement alloc] initWithLibXMLNode:(xmlNodePtr)theElementNode] autorelease];
+CXMLElement *theElement = [[[CXMLElement alloc] initWithLibXMLNode:(xmlNodePtr)theElementNode freeOnDealloc:NO] autorelease];
return(theElement);
}
+ (id)elementWithName:(NSString *)name stringValue:(NSString *)string
{
xmlNodePtr theElementNode = xmlNewNode(NULL, (const xmlChar *)[name UTF8String]);
-CXMLElement *theElement = [[[CXMLElement alloc] initWithLibXMLNode:(xmlNodePtr)theElementNode] autorelease];
+CXMLElement *theElement = [[[CXMLElement alloc] initWithLibXMLNode:(xmlNodePtr)theElementNode freeOnDealloc:NO] autorelease];
theElement.stringValue = string;
return(theElement);
}
@@ -83,15 +83,15 @@ + (id)namespaceWithName:(NSString *)name stringValue:(NSString *)stringValue
{
xmlNsPtr theNode = xmlNewNs(NULL, (const xmlChar *)[stringValue UTF8String], (const xmlChar *)[name UTF8String]);
NSAssert(theNode != NULL, @"xmlNewNs failed");
-CXMLNode *theNodeObject = [[[CXMLNode alloc] initWithLibXMLNode:(xmlNodePtr)theNode] autorelease];
+CXMLNode *theNodeObject = [[[CXMLNode alloc] initWithLibXMLNode:(xmlNodePtr)theNode freeOnDealloc:NO] autorelease];
return(theNodeObject);
}
+ (id)processingInstructionWithName:(NSString *)name stringValue:(NSString *)stringValue;
{
xmlNodePtr theNode = xmlNewPI((const xmlChar *)[name UTF8String], (const xmlChar *)[stringValue UTF8String]);
NSAssert(theNode != NULL, @"xmlNewPI failed");
-CXMLNode *theNodeObject = [[[CXMLNode alloc] initWithLibXMLNode:theNode] autorelease];
+CXMLNode *theNodeObject = [[[CXMLNode alloc] initWithLibXMLNode:theNode freeOnDealloc:NO] autorelease];
return(theNodeObject);
}
View
9 Demo/Source/main.m
@@ -36,13 +36,8 @@ int main(int argc, char *argv[])
NSError *theError = NULL;
-CXMLDocument *theXMLDocument = [[[CXMLDocument alloc] initWithXMLString:@"<xml><![CDATA[<sender>John Smith</sender>]]></xml>" options:0 error:&theError] autorelease];
-NSLog(@"%@", theXMLDocument);
-CXMLElement *theRootElement = [theXMLDocument rootElement];
-NSLog(@"%@", [theRootElement name]);
-NSLog(@"%@", [theRootElement stringValue]);
-CXMLNode *theNode = [[theRootElement children] lastObject];
-NSLog(@"%@", [theNode stringValue]);
+CXMLDocument *theXMLDocument = [[[CXMLDocument alloc] initWithXMLString:@"<enclosure url=\"http://www.flickr.com/apps/video/stewart.swf?v=71377&amp;photo_id=4445665165\" type=\"application/x-shockwave-flash\"/>" options:0 error:&theError] autorelease];
+NSLog(@"%@", [[theXMLDocument rootElement] XMLString]);
[pool release];

0 comments on commit ccf6525

Please sign in to comment.
Something went wrong with that request. Please try again.