CXMLElement.m 9.32 KB
Newer Older
Pierre's avatar
Pierre committed
1 2 3 4 5
//
//  CXMLElement.m
//  TouchCode
//
//  Created by Jonathan Wight on 03/07/08.
6
//  Copyright 2011 toxicsoftware.com. All rights reserved.
Pierre's avatar
Pierre committed
7
//
8 9
//  Redistribution and use in source and binary forms, with or without modification, are
//  permitted provided that the following conditions are met:
Pierre's avatar
Pierre committed
10
//
11 12
//     1. Redistributions of source code must retain the above copyright notice, this list of
//        conditions and the following disclaimer.
Pierre's avatar
Pierre committed
13
//
14 15 16
//     2. Redistributions in binary form must reproduce the above copyright notice, this list
//        of conditions and the following disclaimer in the documentation and/or other materials
//        provided with the distribution.
Pierre's avatar
Pierre committed
17
//
18 19 20 21 22 23 24 25 26 27 28 29 30
//  THIS SOFTWARE IS PROVIDED BY TOXICSOFTWARE.COM ``AS IS'' AND ANY EXPRESS OR IMPLIED
//  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
//  FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL TOXICSOFTWARE.COM OR
//  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
//  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
//  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
//  ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
//  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
//  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
//  The views and conclusions contained in the software and documentation are those of the
//  authors and should not be interpreted as representing official policies, either expressed
//  or implied, of toxicsoftware.com.
Pierre's avatar
Pierre committed
31 32 33 34

#import "CXMLElement.h"

#import "CXMLNode_PrivateExtensions.h"
35 36
#import "CXMLNode_CreationExtensions.h"
#import "CXMLNamespaceNode.h"
Pierre's avatar
Pierre committed
37 38 39 40

@implementation CXMLElement

- (NSArray *)elementsForName:(NSString *)name
41 42
    {
    NSMutableArray *theElements = [NSMutableArray array];
Pierre's avatar
Pierre committed
43

44 45
    // TODO -- native xml api?
    const xmlChar *theName = (const xmlChar *)[name UTF8String];
Pierre's avatar
Pierre committed
46

47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
    xmlNodePtr theCurrentNode = _node->children;
    while (theCurrentNode != NULL)
        {
        if (theCurrentNode->type == XML_ELEMENT_NODE && xmlStrcmp(theName, theCurrentNode->name) == 0)
            {
            CXMLNode *theNode = [CXMLNode nodeWithLibXMLNode:(xmlNodePtr)theCurrentNode freeOnDealloc:NO];
            [theElements addObject:theNode];
            }
        theCurrentNode = theCurrentNode->next;
        }
    return(theElements);
    }

- (NSArray *)elementsForLocalName:(NSString *)localName URI:(NSString *)URI
    {
	if (URI == nil || [URI length] == 0)
        {
		return [self elementsForName:localName];
        }

	NSMutableArray *theElements = [NSMutableArray array];
	const xmlChar *theLocalName = (const xmlChar *)[localName UTF8String];
	const xmlChar *theNamespaceName = (const xmlChar *)[URI UTF8String];

	xmlNodePtr theCurrentNode = _node->children;
	while (theCurrentNode != NULL)
        {
		if (theCurrentNode->type == XML_ELEMENT_NODE
			&& xmlStrcmp(theLocalName, theCurrentNode->name) == 0
			&& theCurrentNode->ns
			&& xmlStrcmp(theNamespaceName, theCurrentNode->ns->href) == 0)
            {
			CXMLNode *theNode = [CXMLNode nodeWithLibXMLNode:(xmlNodePtr)theCurrentNode freeOnDealloc:NO];
			[theElements addObject:theNode];
            }
		theCurrentNode = theCurrentNode->next;
        }
Pierre's avatar
Pierre committed
84

85 86
	return theElements;
    }
Pierre's avatar
Pierre committed
87 88

- (NSArray *)attributes
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
    {
    NSMutableArray *theAttributes = [NSMutableArray array];
    xmlAttrPtr theCurrentNode = _node->properties;
    while (theCurrentNode != NULL)
        {
        CXMLNode *theAttribute = [CXMLNode nodeWithLibXMLNode:(xmlNodePtr)theCurrentNode freeOnDealloc:NO];
        [theAttributes addObject:theAttribute];
        theCurrentNode = theCurrentNode->next;
        }
    return(theAttributes);
    }

- (CXMLNode *)attributeForName:(NSString *)name
    {
	// TODO -- look for native libxml2 function for finding a named attribute (like xmlGetProp)

	NSRange split = [name rangeOfString:@":"];

	xmlChar *theLocalName = NULL;
	xmlChar *thePrefix = NULL;

	if (split.length > 0)
        {
		theLocalName = (xmlChar *)[[name substringFromIndex:split.location + 1] UTF8String];
		thePrefix = (xmlChar *)[[name substringToIndex:split.location] UTF8String];
        }
	else
        {
		theLocalName = (xmlChar *)[name UTF8String];
        }

	xmlAttrPtr theCurrentNode = _node->properties;
	while (theCurrentNode != NULL)
        {
		if (xmlStrcmp(theLocalName, theCurrentNode->name) == 0)
            {
			if (thePrefix == NULL || (theCurrentNode->ns
                && theCurrentNode->ns->prefix
                && xmlStrcmp(thePrefix, theCurrentNode->ns->prefix) == 0))
                {
				CXMLNode *theAttribute = [CXMLNode nodeWithLibXMLNode:(xmlNodePtr)theCurrentNode freeOnDealloc:NO];
				return(theAttribute);
                }
            }
		theCurrentNode = theCurrentNode->next;
        }
	return(NULL);
    }

- (CXMLNode *)attributeForLocalName:(NSString *)localName URI:(NSString *)URI
    {
	if (URI == nil)
        {
		return [self attributeForName:localName];
        }

	// TODO -- look for native libxml2 function for finding a named attribute (like xmlGetProp)
	const xmlChar *theLocalName = (const xmlChar *)[localName UTF8String];
	const xmlChar *theNamespaceName = (const xmlChar *)[URI UTF8String];

	xmlAttrPtr theCurrentNode = _node->properties;
	while (theCurrentNode != NULL)
        {
		if (theCurrentNode->ns && theCurrentNode->ns->href &&
			xmlStrcmp(theLocalName, theCurrentNode->name) == 0 &&
			xmlStrcmp(theNamespaceName, theCurrentNode->ns->href) == 0)
            {
			CXMLNode *theAttribute = [CXMLNode nodeWithLibXMLNode:(xmlNodePtr)theCurrentNode freeOnDealloc:NO];
			return(theAttribute);
            }
		theCurrentNode = theCurrentNode->next;
        }
	return(NULL);
    }

- (NSArray *)namespaces
    {
166
	NSMutableArray *theNamespaces = [[NSMutableArray alloc] init];
167 168 169 170
	xmlNsPtr theCurrentNamespace = _node->nsDef;

	while (theCurrentNamespace != NULL)
        {
171 172
		NSString *thePrefix = theCurrentNamespace->prefix ? @((const char *)theCurrentNamespace->prefix) : @"";
		NSString *theURI = @((const char *)theCurrentNamespace->href);
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
		CXMLNamespaceNode *theNode = [[CXMLNamespaceNode alloc] initWithPrefix:thePrefix URI:theURI parentElement:self];
		[theNamespaces addObject:theNode];

		theCurrentNamespace = theCurrentNamespace->next;
        }

	return theNamespaces;
    }

- (CXMLNode *)namespaceForPrefix:(NSString *)name
    {
	const xmlChar *thePrefix = (const xmlChar *)[name UTF8String];
	xmlNsPtr theCurrentNamespace = _node->nsDef;

	while (theCurrentNamespace != NULL)
        {
		if (xmlStrcmp(theCurrentNamespace->prefix, thePrefix) == 0)
            {
191 192
			NSString *thePrefixString = theCurrentNamespace->prefix ? @((const char *)theCurrentNamespace->prefix) : @"";
			NSString *theURI = @((const char *)theCurrentNamespace->href);
193
			return [[CXMLNamespaceNode alloc] initWithPrefix:thePrefixString URI:theURI parentElement:self];
194 195 196 197 198 199 200
            }
		theCurrentNamespace = theCurrentNamespace->next;
        }
	return nil;
    }

- (CXMLNode *)resolveNamespaceForName:(NSString *)name
Pierre's avatar
Pierre committed
201
{
202 203 204 205 206 207 208 209
	NSRange split = [name rangeOfString:@":"];

	if (split.length > 0)
		return [self namespaceForPrefix:[name substringToIndex:split.location]];

	xmlNsPtr theCurrentNamespace = _node->nsDef;

	while (theCurrentNamespace != NULL)
Pierre's avatar
Pierre committed
210
	{
211 212 213
		if (theCurrentNamespace->prefix == 0
			|| (theCurrentNamespace->prefix)[0] == 0)
		{
214 215
			NSString *thePrefix = theCurrentNamespace->prefix ? @((const char *)theCurrentNamespace->prefix) : @"";
			NSString *theURI = @((const char *)theCurrentNamespace->href);
216
			return [[CXMLNamespaceNode alloc] initWithPrefix:thePrefix URI:theURI parentElement:self];
217 218
		}
		theCurrentNamespace = theCurrentNamespace->next;
Pierre's avatar
Pierre committed
219
	}
220 221

	return nil;
Pierre's avatar
Pierre committed
222 223
}

224
- (NSString *)resolvePrefixForNamespaceURI:(NSString *)namespaceURI
Pierre's avatar
Pierre committed
225
{
226 227 228
	const xmlChar *theXMLURI = (const xmlChar *)[namespaceURI UTF8String];

	xmlNsPtr theCurrentNamespace = _node->nsDef;
Pierre's avatar
Pierre committed
229

230
	while (theCurrentNamespace != NULL)
Pierre's avatar
Pierre committed
231
	{
232
		if (xmlStrcmp(theCurrentNamespace->href, theXMLURI) == 0)
Pierre's avatar
Pierre committed
233
		{
234
			if(theCurrentNamespace->prefix)
235
				return @((const char *)theCurrentNamespace->prefix);
236 237

			return @"";
Pierre's avatar
Pierre committed
238
		}
239
		theCurrentNamespace = theCurrentNamespace->next;
Pierre's avatar
Pierre committed
240
	}
241
	return nil;
Pierre's avatar
Pierre committed
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
}

//- (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:@">"];
274
//
Pierre's avatar
Pierre committed
275 276 277 278 279 280 281 282 283 284 285 286
//if ( _node->children )
//	{
//	for (id child in [self children])
//		[child _XMLStringWithOptions:options appendingToString:str];
//	}
//[str appendString:[NSString stringWithFormat:@"</%@>", name]];
//return str;
//}

- (NSString *)description
{
	NSAssert(_node != NULL, @"TODO");
287

Pierre's avatar
Pierre committed
288 289 290 291
	return([NSString stringWithFormat:@"<%@ %p [%p] %@ %@>", NSStringFromClass([self class]), self, self->_node, [self name], [self XMLStringWithOptions:0]]);
}

@end