Plugins/Bonjour/libezv/Private Classes/AWEzvXMLNode.m
author Evan Schoenberg
Sat Oct 31 12:18:11 2009 -0500 (2009-10-31)
changeset 2705 da02036bbb7a
parent 2502 88f26ed1ecfe
child 2708 9834b574299e
permissions -rw-r--r--
Prevent attempting to set a nil property on an AWEzvXMLNode. This turns a crash into a warning.

One such case could occur, and seems to have at least once for one user in #11799, if AWEzvContactManager's myInstanceName is nil. This needs further investigation.

Fixes #11799
David@0
     1
/*
David@0
     2
 * Project:     Libezv
David@0
     3
 * File:        AWEzvXMLNode.m
David@0
     4
 *
David@0
     5
 * Version:     1.0
David@0
     6
 * Author:      Andrew Wellington <proton[at]wiretapped.net>
David@0
     7
 *
David@0
     8
 * License:
David@0
     9
 * Copyright (C) 2004-2005 Andrew Wellington.
David@0
    10
 * All rights reserved.
David@0
    11
 * 
David@0
    12
 * Redistribution and use in source and binary forms, with or without
David@0
    13
 * modification, are permitted provided that the following conditions are met:
David@0
    14
 * 
David@0
    15
 * 1. Redistributions of source code must retain the above copyright notice,
David@0
    16
 * this list of conditions and the following disclaimer.
David@0
    17
 * 2. Redistributions in binary form must reproduce the above copyright notice,
David@0
    18
 * this list of conditions and the following disclaimer in the documentation
David@0
    19
 * and/or other materials provided with the distribution.
David@0
    20
 * 
David@0
    21
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED
David@0
    22
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
David@0
    23
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
David@0
    24
 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
David@0
    25
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
David@0
    26
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
David@0
    27
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
David@0
    28
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
David@0
    29
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
David@0
    30
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
David@0
    31
 */
David@0
    32
David@0
    33
#import "AWEzvXMLNode.h"
David@0
    34
David@0
    35
#define DEFAULT_CAPACITY	10
David@0
    36
David@0
    37
@implementation AWEzvXMLNode
David@0
    38
David@0
    39
David@0
    40
- (id) initWithType:(int)theType name:(NSString *)theName 
David@0
    41
{
David@0
    42
    if ((self = [super init])) {
David@0
    43
		type = theType;
David@0
    44
		name = [theName copy];
David@0
    45
		children = [[NSMutableArray alloc] initWithCapacity:DEFAULT_CAPACITY];
David@0
    46
		attributes = [[NSMutableDictionary alloc] initWithCapacity:DEFAULT_CAPACITY];
David@0
    47
	}
David@0
    48
    
David@0
    49
    return self;
David@0
    50
}
David@0
    51
David@0
    52
- (void)dealloc
David@0
    53
{
catfish@2496
    54
	[children release]; children = nil;
catfish@2496
    55
	[attributes release]; attributes = nil;
catfish@2496
    56
	[name release]; name = nil;
David@0
    57
	
David@0
    58
	[super dealloc];
David@0
    59
}
David@0
    60
catfish@2502
    61
- (int) type {
catfish@2502
    62
    return type;
catfish@2502
    63
}
David@0
    64
- (NSArray *)children {
David@0
    65
    return [[children copy] autorelease];
David@0
    66
}
David@0
    67
- (void) addChild:(AWEzvXMLNode *)node {
David@0
    68
    [children addObject:node];
David@0
    69
}
David@0
    70
David@0
    71
- (NSDictionary *)attributes {
David@0
    72
    return [[attributes copy] autorelease];
David@0
    73
}
David@0
    74
David@0
    75
- (void) addAttribute:(NSString *)property withValue:(NSString *)value {
Evan@2705
    76
	if (value && property)
Evan@2705
    77
		[attributes setObject:value forKey:property];
Evan@2705
    78
	else {
Evan@2705
    79
		NSLog(@"WARNING: %@ attempted to set %@ for %@", self, value, propery);
Evan@2705
    80
		AILogWithSignature(@"WARNING: %@ attempted to set %@ for %@", self, value, propery);
Evan@2705
    81
	}
Evan@2705
    82
David@0
    83
}
David@0
    84
catfish@2502
    85
- (NSString *)name {
catfish@2502
    86
    return name;
catfish@2502
    87
}
catfish@2502
    88
catfish@2502
    89
- (void) setName:(NSString *)theName {
catfish@2502
    90
	if (name != theName) {
catfish@2502
    91
        [name release];
catfish@2502
    92
		name = [theName retain];
catfish@2502
    93
	}
catfish@2502
    94
}
catfish@2502
    95
David@0
    96
- (NSString *)xmlString {
David@0
    97
    NSMutableString	*string;
David@0
    98
    NSString		*key;
David@0
    99
    AWEzvXMLNode	*node;
David@0
   100
    
David@0
   101
    if (type == AWEzvXMLText) {
David@0
   102
        string = [[name mutableCopy] autorelease];
David@0
   103
        [string replaceOccurrencesOfString:@"&" withString:@"&amp;" 
David@0
   104
								   options:NSLiteralSearch range:NSMakeRange(0, [string length])];
David@0
   105
        [string replaceOccurrencesOfString:@"<" withString:@"&lt;" 
David@0
   106
								   options:NSLiteralSearch range:NSMakeRange(0, [string length])];
David@0
   107
        [string replaceOccurrencesOfString:@">" withString:@"&gt;" 
David@0
   108
								   options:NSLiteralSearch range:NSMakeRange(0, [string length])];
David@0
   109
        return [[string copy] autorelease];
David@0
   110
David@0
   111
    } else if (type == AWEzvXMLRaw) {
David@0
   112
		return [[name copy] autorelease];
David@0
   113
    }
David@0
   114
    
David@0
   115
    string = [NSMutableString stringWithString:@"<"];
David@0
   116
    [string appendString:name];
David@0
   117
    
David@1616
   118
		for (key in [attributes keyEnumerator]) {
David@0
   119
        [string appendFormat:@" %@=\"%@\"", key, [attributes objectForKey:key]];
David@0
   120
    }
David@0
   121
    
David@0
   122
    [string appendString:@">"];
David@0
   123
    
David@75
   124
    for (node in children) {
David@0
   125
		NSString	*xmlString;
David@0
   126
		if ((xmlString = [node xmlString])) {
David@0
   127
			[string appendString:xmlString];
David@0
   128
		}
David@0
   129
    }
David@0
   130
    
David@0
   131
    [string appendFormat:@"</%@>", name];
David@0
   132
    
David@0
   133
    return [[string copy] autorelease];
David@0
   134
}
David@0
   135
David@0
   136
- (NSString *)description
David@0
   137
{
David@0
   138
	NSMutableString *string, *key;
David@0
   139
	AWEzvXMLNode	*node;
David@0
   140
	string = [NSMutableString stringWithString:@"<"];
David@0
   141
	[string appendString:name];
David@0
   142
	
David@1616
   143
	for (key in [attributes keyEnumerator]) {
David@0
   144
		[string appendFormat:@" %@=\"%@\"", key, [attributes objectForKey:key]];
David@0
   145
	}
David@0
   146
	
David@0
   147
	[string appendString:@">"];
David@0
   148
	
David@75
   149
	for (node in children) {
David@0
   150
		NSString	*xmlString;
David@0
   151
		if ((xmlString = [node xmlString])) {
David@0
   152
			[string appendString:xmlString];
David@0
   153
		}
David@0
   154
	}
David@0
   155
	
David@0
   156
	[string appendFormat:@"</%@>", name];
David@0
   157
David@0
   158
	return [NSString stringWithFormat:@"<AWEzvXMLNode %x:type %i:\"%@\">",self,type,string];
David@0
   159
}
David@0
   160
@end