From 8b02b7fe5115189dc7d21f71088dcd327e91866a Mon Sep 17 00:00:00 2001 From: Wolfgang Sourdeau Date: Fri, 25 May 2007 17:30:24 +0000 Subject: [PATCH] Monotone-Parent: 8d642918307c4ec207760dac241eca7219d0074a Monotone-Revision: 2303b51b776f8ba14da83e3a05e92f43b13279cc Monotone-Author: wsourdeau@inverse.ca Monotone-Date: 2007-05-25T17:30:24 Monotone-Branch: ca.inverse.sogo --- ChangeLog | 9 ++++ SoObjects/SOGo/NSArray+Utilities.h | 2 + SoObjects/SOGo/NSArray+Utilities.m | 23 +++++++++++ SoObjects/SOGo/NSDictionary+Utilities.h | 36 ++++++++++++++++ SoObjects/SOGo/NSDictionary+Utilities.m | 55 +++++++++++++++++++++++++ SoObjects/SOGo/NSObject+Utilities.h | 36 ++++++++++++++++ SoObjects/SOGo/NSObject+Utilities.m | 36 ++++++++++++++++ SoObjects/SOGo/NSString+Utilities.h | 2 + SoObjects/SOGo/NSString+Utilities.m | 17 ++++++++ 9 files changed, 216 insertions(+) create mode 100644 SoObjects/SOGo/NSDictionary+Utilities.h create mode 100644 SoObjects/SOGo/NSDictionary+Utilities.m create mode 100644 SoObjects/SOGo/NSObject+Utilities.h create mode 100644 SoObjects/SOGo/NSObject+Utilities.m diff --git a/ChangeLog b/ChangeLog index bb9b8fbc3..0c7a106ad 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,12 @@ +2007-05-25 Wolfgang Sourdeau + + * SoObjects/SOGo/NSDictionary+Utilities.[hm]: new extension + module. + + * SoObjects/SOGo/NSObject+Utilities.m ([NSObject + -jsonRepresentation]): new interface method meant to be overriden + by the primitive classes. + 2007-05-23 Wolfgang Sourdeau * UI/MailerUI/UIxMailSplashView.m: removed. diff --git a/SoObjects/SOGo/NSArray+Utilities.h b/SoObjects/SOGo/NSArray+Utilities.h index 2617538d3..1f8c1bd29 100644 --- a/SoObjects/SOGo/NSArray+Utilities.h +++ b/SoObjects/SOGo/NSArray+Utilities.h @@ -29,6 +29,8 @@ @interface NSArray (SOGoArrayUtilities) +- (NSString *) jsonRepresentation; + - (NSArray *) stringsWithFormat: (NSString *) format; #ifdef GNUSTEP_BASE_LIBRARY diff --git a/SoObjects/SOGo/NSArray+Utilities.m b/SoObjects/SOGo/NSArray+Utilities.m index 75dec348c..d30f93ac3 100644 --- a/SoObjects/SOGo/NSArray+Utilities.m +++ b/SoObjects/SOGo/NSArray+Utilities.m @@ -58,6 +58,29 @@ withObject: object2]; } +- (NSString *) jsonRepresentation +{ + id currentElement; + NSMutableArray *jsonElements; + NSEnumerator *elements; + NSString *representation; + + jsonElements = [NSMutableArray new]; + + elements = [self objectEnumerator]; + currentElement = [elements nextObject]; + while (currentElement) + { + [jsonElements addObject: [currentElement jsonRepresentation]]; + currentElement = [elements nextObject]; + } + representation = [NSString stringWithFormat: @"[%@]", + [jsonElements componentsJoinedByString: @", "]]; + [jsonElements release]; + + return representation; +} + @end @implementation NSMutableArray (SOGoArrayUtilities) diff --git a/SoObjects/SOGo/NSDictionary+Utilities.h b/SoObjects/SOGo/NSDictionary+Utilities.h new file mode 100644 index 000000000..df40a3427 --- /dev/null +++ b/SoObjects/SOGo/NSDictionary+Utilities.h @@ -0,0 +1,36 @@ +/* NSDictionary+Utilities.h - this file is part of SOGo + * + * Copyright (C) 2007 Inverse groupe conseil + * + * Author: Wolfgang Sourdeau + * + * This file is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2, or (at your option) + * any later version. + * + * This file is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + */ + +#ifndef NSDICTIONARY_UTILITIES_H +#define NSDICTIONARY_UTILITIES_H + +#import + +@class NSString; + +@interface NSDictionary (SOGoDictionaryUtilities) + +- (NSString *) jsonRepresentation; + +@end + +#endif /* NSDICTIONARY_UTILITIES_H */ diff --git a/SoObjects/SOGo/NSDictionary+Utilities.m b/SoObjects/SOGo/NSDictionary+Utilities.m new file mode 100644 index 000000000..5b21ce2c9 --- /dev/null +++ b/SoObjects/SOGo/NSDictionary+Utilities.m @@ -0,0 +1,55 @@ +/* NSDictionary+Utilities.m - this file is part of SOGo + * + * Copyright (C) 2007 Inverse groupe conseil + * + * Author: Wolfgang Sourdeau + * + * This file is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2, or (at your option) + * any later version. + * + * This file is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + */ + +#import +#import + +#import "NSObject+Utilities.h" +#import "NSDictionary+Utilities.h" + +@implementation NSDictionary (SOGoDictionaryUtilities) + +- (NSString *) jsonRepresentation +{ + NSMutableArray *values; + NSString *representation, *currentKey, *currentValue, *currentPair; + NSEnumerator *keys; + + values = [NSMutableArray new]; + keys = [[self allKeys] objectEnumerator]; + currentKey = [keys nextObject]; + while (currentKey) + { + currentValue = [[self objectForKey: currentKey] jsonRepresentation]; + currentPair = [NSString stringWithFormat: @"%@: %@", + [currentKey jsonRepresentation], currentValue]; + [values addObject: currentPair]; + currentKey = [keys nextObject]; + } + representation = [NSString stringWithFormat: @"{%@}", + [values componentsJoinedByString: @", "]]; + [values release]; + + return representation; +} + +@end diff --git a/SoObjects/SOGo/NSObject+Utilities.h b/SoObjects/SOGo/NSObject+Utilities.h new file mode 100644 index 000000000..11610aaba --- /dev/null +++ b/SoObjects/SOGo/NSObject+Utilities.h @@ -0,0 +1,36 @@ +/* NSObject+Utilities.h - this file is part of SOGo + * + * Copyright (C) 2007 Inverse groupe conseil + * + * Author: Wolfgang Sourdeau + * + * This file is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2, or (at your option) + * any later version. + * + * This file is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + */ + +#ifndef NSOBJECT_UTILITIES_H +#define NSOBJECT_UTILITIES_H + +#import + +@class NSString; + +@interface NSObject (SOGoObjectUtilities) + +- (NSString *) jsonRepresentation; + +@end + +#endif /* NSOBJECT+UTILITIES_H */ diff --git a/SoObjects/SOGo/NSObject+Utilities.m b/SoObjects/SOGo/NSObject+Utilities.m new file mode 100644 index 000000000..d013f09cd --- /dev/null +++ b/SoObjects/SOGo/NSObject+Utilities.m @@ -0,0 +1,36 @@ +/* NSObject+Utilities.m - this file is part of SOGo + * + * Copyright (C) 2007 Inverse groupe conseil + * + * Author: Wolfgang Sourdeau + * + * This file is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2, or (at your option) + * any later version. + * + * This file is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + */ + +#import + +#import "NSObject+Utilities.h" + +@implementation NSObject (SOGoObjectUtilities) + +- (NSString *) jsonRepresentation +{ + [self subclassResponsibility: _cmd]; + + return nil; +} + +@end diff --git a/SoObjects/SOGo/NSString+Utilities.h b/SoObjects/SOGo/NSString+Utilities.h index 01728e9e3..29679f532 100644 --- a/SoObjects/SOGo/NSString+Utilities.h +++ b/SoObjects/SOGo/NSString+Utilities.h @@ -40,6 +40,8 @@ - (NSString *) stringByDetectingURLs; +- (NSString *) jsonRepresentation; + #ifndef GNUSTEP_BASE_LIBRARY - (BOOL) boolValue; #endif diff --git a/SoObjects/SOGo/NSString+Utilities.m b/SoObjects/SOGo/NSString+Utilities.m index 987ad47c7..4535212e5 100644 --- a/SoObjects/SOGo/NSString+Utilities.m +++ b/SoObjects/SOGo/NSString+Utilities.m @@ -184,6 +184,23 @@ static NSMutableCharacterSet *urlAfterEndingChars = nil; return selfCopy; } +- (NSString *) jsonRepresentation +{ + NSMutableString *representation; + + representation = [NSMutableString stringWithString: self]; + [representation replaceString: @"\\" withString: @"\\\\"]; + [representation replaceString: @"\"" withString: @"\\\""]; + [representation replaceString: @"/" withString: @"\\/"]; + [representation replaceString: @"\b" withString: @"\\b"]; + [representation replaceString: @"\f" withString: @"\\f"]; + [representation replaceString: @"\n" withString: @"\\n"]; + [representation replaceString: @"\r" withString: @"\\r"]; + [representation replaceString: @"\t" withString: @"\\t"]; + + return [NSString stringWithFormat: @"\"%@\"", representation]; +} + #if LIB_FOUNDATION_LIBRARY - (BOOL) boolValue {