]> err.no Git - scalable-opengroupware.org/blob - SoObjects/SOGo/NSString+Utilities.m
git-svn-id: http://svn.opengroupware.org/SOGo/inverse/trunk@1088 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 "NSArray+Utilities.h"
28 #import "NSDictionary+URL.h"
29
30 #import "NSString+Utilities.h"
31
32 static NSMutableCharacterSet *urlNonEndingChars = nil;
33 static NSMutableCharacterSet *urlAfterEndingChars = nil;
34
35 @implementation NSString (SOGoURLExtension)
36
37 - (NSString *) composeURLWithAction: (NSString *) action
38                          parameters: (NSDictionary *) urlParameters
39                             andHash: (BOOL) useHash
40 {
41   NSMutableString *completeURL;
42
43   completeURL = [NSMutableString new];
44   [completeURL autorelease];
45
46   [completeURL appendString: [self urlWithoutParameters]];
47   if (![completeURL hasSuffix: @"/"])
48     [completeURL appendString: @"/"];
49   [completeURL appendString: action];
50   [completeURL appendString: [urlParameters asURLParameters]];
51   if (useHash)
52     [completeURL appendString: @"#"];
53
54   return completeURL;
55 }
56
57 - (NSString *) hostlessURL
58 {
59   NSString *newURL;
60   NSRange hostR, locationR;
61
62   if ([self hasPrefix: @"/"])
63     {
64       newURL = [self copy];
65       [newURL autorelease];
66     }
67   else
68     {
69       hostR = [self rangeOfString: @"://"];
70       locationR = [[self substringFromIndex: (hostR.location + hostR.length)]
71                     rangeOfString: @"/"];
72       newURL = [self substringFromIndex: (hostR.location + hostR.length + locationR.location)];
73     }
74
75   return newURL;
76 }
77
78 - (NSString *) urlWithoutParameters;
79 {
80   NSRange r;
81   NSString *newUrl;
82   
83   r = [self rangeOfString:@"?" options: NSBackwardsSearch];
84   if (r.length > 0)
85     newUrl = [self substringToIndex: NSMaxRange(r) - 1];
86   else
87     newUrl = self;
88
89   return newUrl;
90 }
91
92 - (NSString *) davMethodToObjC
93 {
94   NSMutableString *newName;
95   NSEnumerator *components;
96   NSString *component;
97
98   newName = [NSMutableString stringWithString: @"dav"];
99   components = [[self componentsSeparatedByString: @"-"] objectEnumerator];
100   component = [components nextObject];
101   while (component)
102     {
103       [newName appendString: [component capitalizedString]];
104       component = [components nextObject];
105     }
106
107   return newName;
108 }
109
110 - (NSRange) _rangeOfURLInRange: (NSRange) refRange
111 {
112   int start, length;
113   NSRange workRange;
114
115   if (!urlNonEndingChars)
116     {
117       urlNonEndingChars = [NSMutableCharacterSet new];
118       [urlNonEndingChars addCharactersInString: @">&=,.:;\t \r\n"];
119     }
120   if (!urlAfterEndingChars)
121     {
122       urlAfterEndingChars = [NSMutableCharacterSet new];
123       [urlAfterEndingChars addCharactersInString: @"()[]{}&;<\t \r\n"];
124     }
125
126   start = refRange.location;
127   while (start > -1
128          && ![urlAfterEndingChars characterIsMember:
129                                     [self characterAtIndex: start]])
130     start--;
131   start++;
132   length = [self length] - start;
133   workRange = NSMakeRange (start, length);
134   workRange = [self rangeOfCharacterFromSet: urlAfterEndingChars
135                     options: NSLiteralSearch range: workRange];
136   if (workRange.location != NSNotFound)
137     length = workRange.location - start;
138   while
139     (length > 0
140      && [urlNonEndingChars characterIsMember:
141                              [self characterAtIndex: (start + length - 1)]])
142     length--;
143
144   return NSMakeRange (start, length);
145 }
146
147 - (void) _handleURLs: (NSMutableString *) selfCopy
148          textToMatch: (NSString *) match
149               prefix: (NSString *) prefix
150             inRanges: (NSMutableArray *) ranges
151 {
152   NSRange httpRange, currentURL, rest;
153   NSString *urlText, *newUrlText;
154   unsigned int length, matchLength;
155
156   matchLength = [match length];
157   httpRange = [selfCopy rangeOfString: match];
158   while (httpRange.location != NSNotFound)
159     {
160       if ([ranges hasRangeIntersection: httpRange])
161         rest.location = NSMaxRange (httpRange);
162       else
163         {
164           currentURL = [selfCopy _rangeOfURLInRange: httpRange];
165           urlText = [selfCopy substringFromRange: currentURL];
166           if ([urlText length] > matchLength)
167             {
168               newUrlText = [NSString stringWithFormat: @"<a href=\"%@%@\">%@</a>",
169                                      prefix, urlText, urlText];
170               [selfCopy replaceCharactersInRange: currentURL
171                         withString: newUrlText];
172               currentURL
173                 = NSMakeRange (currentURL.location, [newUrlText length]);
174               [ranges addRange: currentURL];
175             }
176           rest.location = NSMaxRange (currentURL);
177         }
178       length = [selfCopy length];
179       rest.length = length - rest.location;
180       httpRange = [selfCopy rangeOfString: match
181                             options: 0 range: rest];
182     }
183 }
184
185 - (NSString *) stringByDetectingURLs
186 {
187   NSMutableString *selfCopy;
188   NSMutableArray *ranges;
189
190   ranges = [NSMutableArray new];
191   selfCopy = [NSMutableString stringWithString: self];
192   [self _handleURLs: selfCopy
193         textToMatch: @"://"
194         prefix: @""
195         inRanges: ranges];
196   [self _handleURLs: selfCopy
197         textToMatch: @"@"
198         prefix: @"mailto:"
199         inRanges: ranges];
200   [ranges release];
201
202   return selfCopy;
203 }
204
205 - (NSString *) jsonRepresentation
206 {
207   NSMutableString *representation;
208
209   representation = [NSMutableString stringWithString: self];
210   [representation replaceString: @"\\" withString: @"\\\\"];
211   [representation replaceString: @"\"" withString: @"\\\""];
212   [representation replaceString: @"/" withString: @"\\/"];
213   [representation replaceString: @"\b" withString: @"\\b"];
214   [representation replaceString: @"\f" withString: @"\\f"];
215   [representation replaceString: @"\n" withString: @"\\n"];
216   [representation replaceString: @"\r" withString: @"\\r"];
217   [representation replaceString: @"\t" withString: @"\\t"];
218
219   return [NSString stringWithFormat: @"\"%@\"", representation];
220 }
221
222 #if LIB_FOUNDATION_LIBRARY
223 - (BOOL) boolValue
224 {
225   return !([self isEqualToString: @"0"]
226            || [self isEqualToString: @"NO"]);
227 }
228 #endif
229
230 @end