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