]> err.no Git - scalable-opengroupware.org/blob - SoObjects/SOGo/NSString+Utilities.m
git-svn-id: http://svn.opengroupware.org/SOGo/inverse/trunk@1168 d1b88da0-ebda-0310...
[scalable-opengroupware.org] / SoObjects / SOGo / NSString+Utilities.m
1 /* NSString+Utilities.m - this file is part of SOGo
2  *
3  * Copyright (C) 2006  Inverse group conseil
4  *
5  * Author: Wolfgang Sourdeau <wsourdeau@inverse.ca>
6  *
7  * This file is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2, or (at your option)
10  * any later version.
11  *
12  * This file is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; see the file COPYING.  If not, write to
19  * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22
23 #import <Foundation/NSArray.h>
24 #import <Foundation/NSCharacterSet.h>
25 #import <Foundation/NSEnumerator.h>
26
27 #import <NGExtensions/NGQuotedPrintableCoding.h>
28
29 #import "NSArray+Utilities.h"
30 #import "NSDictionary+URL.h"
31
32 #import "NSString+Utilities.h"
33
34 static NSMutableCharacterSet *urlNonEndingChars = nil;
35 static NSMutableCharacterSet *urlAfterEndingChars = nil;
36
37 @implementation NSString (SOGoURLExtension)
38
39 - (NSString *) composeURLWithAction: (NSString *) action
40                          parameters: (NSDictionary *) urlParameters
41                             andHash: (BOOL) useHash
42 {
43   NSMutableString *completeURL;
44
45   completeURL = [NSMutableString new];
46   [completeURL autorelease];
47
48   [completeURL appendString: [self urlWithoutParameters]];
49   if (![completeURL hasSuffix: @"/"])
50     [completeURL appendString: @"/"];
51   [completeURL appendString: action];
52   [completeURL appendString: [urlParameters asURLParameters]];
53   if (useHash)
54     [completeURL appendString: @"#"];
55
56   return completeURL;
57 }
58
59 - (NSString *) hostlessURL
60 {
61   NSString *newURL;
62   NSRange hostR, locationR;
63
64   if ([self hasPrefix: @"/"])
65     {
66       newURL = [self copy];
67       [newURL autorelease];
68     }
69   else
70     {
71       hostR = [self rangeOfString: @"://"];
72       locationR = [[self substringFromIndex: (hostR.location + hostR.length)]
73                     rangeOfString: @"/"];
74       newURL = [self substringFromIndex: (hostR.location + hostR.length
75                                           + locationR.location)];
76     }
77
78   return newURL;
79 }
80
81 - (NSString *) urlWithoutParameters;
82 {
83   NSRange r;
84   NSString *newUrl;
85   
86   r = [self rangeOfString:@"?" options: NSBackwardsSearch];
87   if (r.length > 0)
88     newUrl = [self substringToIndex: NSMaxRange(r) - 1];
89   else
90     newUrl = self;
91
92   return newUrl;
93 }
94
95 - (NSString *) davMethodToObjC
96 {
97   NSMutableString *newName;
98   NSEnumerator *components;
99   NSString *component;
100
101   newName = [NSMutableString stringWithString: @"dav"];
102   components = [[self componentsSeparatedByString: @"-"] objectEnumerator];
103   component = [components nextObject];
104   while (component)
105     {
106       [newName appendString: [component capitalizedString]];
107       component = [components nextObject];
108     }
109
110   return newName;
111 }
112
113 - (NSDictionary *) asDavInvocation
114 {
115   NSMutableDictionary *davInvocation;
116   NSRange nsEnclosing, methodEnclosing;
117   unsigned int length;
118
119   davInvocation = nil;
120   if ([self hasPrefix: @"{"])
121     {
122       nsEnclosing = [self rangeOfString: @"}"];
123       length = [self length];
124       if (nsEnclosing.length > 0
125           && nsEnclosing.location < (length - 1))
126         {
127           methodEnclosing = NSMakeRange(nsEnclosing.location + 1,
128                                         length - nsEnclosing.location - 1);
129           nsEnclosing.length = nsEnclosing.location - 1;
130           nsEnclosing.location = 1;
131           davInvocation = [NSMutableDictionary dictionaryWithCapacity: 2];
132           [davInvocation setObject: [self substringWithRange: nsEnclosing]
133                          forKey: @"ns"];
134           [davInvocation setObject: [self substringWithRange: methodEnclosing]
135                          forKey: @"method"];
136         }
137     }
138
139   return davInvocation;
140 }
141
142 - (NSRange) _rangeOfURLInRange: (NSRange) refRange
143 {
144   int start, length;
145   NSRange workRange;
146
147   if (!urlNonEndingChars)
148     {
149       urlNonEndingChars = [NSMutableCharacterSet new];
150       [urlNonEndingChars addCharactersInString: @">&=,.:;\t \r\n"];
151     }
152   if (!urlAfterEndingChars)
153     {
154       urlAfterEndingChars = [NSMutableCharacterSet new];
155       [urlAfterEndingChars addCharactersInString: @"()[]{}&;<\t \r\n"];
156     }
157
158   start = refRange.location;
159   while (start > -1
160          && ![urlAfterEndingChars characterIsMember:
161                                     [self characterAtIndex: start]])
162     start--;
163   start++;
164   length = [self length] - start;
165   workRange = NSMakeRange (start, length);
166   workRange = [self rangeOfCharacterFromSet: urlAfterEndingChars
167                     options: NSLiteralSearch range: workRange];
168   if (workRange.location != NSNotFound)
169     length = workRange.location - start;
170   while
171     (length > 0
172      && [urlNonEndingChars characterIsMember:
173                              [self characterAtIndex: (start + length - 1)]])
174     length--;
175
176   return NSMakeRange (start, length);
177 }
178
179 - (void) _handleURLs: (NSMutableString *) selfCopy
180          textToMatch: (NSString *) match
181               prefix: (NSString *) prefix
182             inRanges: (NSMutableArray *) ranges
183 {
184   NSRange httpRange, currentURL, rest;
185   NSString *urlText, *newUrlText;
186   unsigned int length, matchLength;
187
188   matchLength = [match length];
189   httpRange = [selfCopy rangeOfString: match];
190   while (httpRange.location != NSNotFound)
191     {
192       if ([ranges hasRangeIntersection: httpRange])
193         rest.location = NSMaxRange (httpRange);
194       else
195         {
196           currentURL = [selfCopy _rangeOfURLInRange: httpRange];
197           urlText = [selfCopy substringFromRange: currentURL];
198           if ([urlText length] > matchLength)
199             {
200               newUrlText = [NSString stringWithFormat: @"<a href=\"%@%@\">%@</a>",
201                                      prefix, urlText, urlText];
202               [selfCopy replaceCharactersInRange: currentURL
203                         withString: newUrlText];
204               currentURL
205                 = NSMakeRange (currentURL.location, [newUrlText length]);
206               [ranges addRange: currentURL];
207             }
208           rest.location = NSMaxRange (currentURL);
209         }
210       length = [selfCopy length];
211       rest.length = length - rest.location;
212       httpRange = [selfCopy rangeOfString: match
213                             options: 0 range: rest];
214     }
215 }
216
217 - (NSString *) stringByDetectingURLs
218 {
219   NSMutableString *selfCopy;
220   NSMutableArray *ranges;
221
222   ranges = [NSMutableArray new];
223   selfCopy = [NSMutableString stringWithString: self];
224   [self _handleURLs: selfCopy
225         textToMatch: @"://"
226         prefix: @""
227         inRanges: ranges];
228   [self _handleURLs: selfCopy
229         textToMatch: @"@"
230         prefix: @"mailto:"
231         inRanges: ranges];
232   [ranges release];
233
234   return selfCopy;
235 }
236
237 - (NSString *) jsonRepresentation
238 {
239   NSMutableString *representation;
240
241   representation = [NSMutableString stringWithString: self];
242   [representation replaceString: @"\\" withString: @"\\\\"];
243   [representation replaceString: @"\"" withString: @"\\\""];
244   [representation replaceString: @"/" withString: @"\\/"];
245   [representation replaceString: @"\b" withString: @"\\b"];
246   [representation replaceString: @"\f" withString: @"\\f"];
247   [representation replaceString: @"\n" withString: @"\\n"];
248   [representation replaceString: @"\r" withString: @"\\r"];
249   [representation replaceString: @"\t" withString: @"\\t"];
250
251   return [NSString stringWithFormat: @"\"%@\"", representation];
252 }
253
254 - (NSString *) pureEMailAddress
255 {
256   NSString *pureAddress;
257   NSRange delimiter;
258
259   delimiter = [self rangeOfString: @"<"];
260   if (delimiter.location == NSNotFound)
261     pureAddress = self;
262   else
263     {
264       pureAddress = [self substringFromIndex: NSMaxRange (delimiter)];
265       delimiter = [pureAddress rangeOfString: @">"];
266       if (delimiter.location != NSNotFound)
267         pureAddress = [pureAddress substringToIndex: delimiter.location];
268     }
269
270   return pureAddress;
271 }
272
273 - (NSString *) asQPSubjectString: (NSString *) encoding
274 {
275   NSString *qpString, *subjectString;
276   NSData *subjectData, *destSubjectData;
277
278   subjectData = [self dataUsingEncoding: NSUTF8StringEncoding];
279   destSubjectData = [subjectData dataByEncodingQuotedPrintable];
280
281   qpString = [[NSString alloc] initWithData: destSubjectData
282                                encoding: NSASCIIStringEncoding];
283   [qpString autorelease];
284   if ([qpString length] > [self length])
285     subjectString = [NSString stringWithFormat: @"=?%@?Q?%@?=",
286                               encoding, qpString];
287   else
288     subjectString = self;
289
290   return subjectString;
291 }
292
293 #if LIB_FOUNDATION_LIBRARY
294 - (BOOL) boolValue
295 {
296   return !([self isEqualToString: @"0"]
297            || [self isEqualToString: @"NO"]);
298 }
299 #endif
300
301 @end