1 /* NSString+Utilities.m - this file is part of SOGo
3 * Copyright (C) 2006 Inverse group conseil
5 * Author: Wolfgang Sourdeau <wsourdeau@inverse.ca>
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)
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.
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.
23 #import <Foundation/NSArray.h>
24 #import <Foundation/NSCharacterSet.h>
25 #import <Foundation/NSEnumerator.h>
27 #import "NSArray+Utilities.h"
28 #import "NSDictionary+URL.h"
30 #import "NSString+Utilities.h"
32 static NSMutableCharacterSet *urlNonEndingChars = nil;
33 static NSMutableCharacterSet *urlAfterEndingChars = nil;
35 @implementation NSString (SOGoURLExtension)
37 - (NSString *) composeURLWithAction: (NSString *) action
38 parameters: (NSDictionary *) urlParameters
39 andHash: (BOOL) useHash
41 NSMutableString *completeURL;
43 completeURL = [NSMutableString new];
44 [completeURL autorelease];
46 [completeURL appendString: [self urlWithoutParameters]];
47 if (![completeURL hasSuffix: @"/"])
48 [completeURL appendString: @"/"];
49 [completeURL appendString: action];
50 [completeURL appendString: [urlParameters asURLParameters]];
52 [completeURL appendString: @"#"];
57 - (NSString *) hostlessURL
60 NSRange hostR, locationR;
62 if ([self hasPrefix: @"/"])
69 hostR = [self rangeOfString: @"://"];
70 locationR = [[self substringFromIndex: (hostR.location + hostR.length)]
72 newURL = [self substringFromIndex: (hostR.location + hostR.length + locationR.location)];
78 - (NSString *) urlWithoutParameters;
83 r = [self rangeOfString:@"?" options: NSBackwardsSearch];
85 newUrl = [self substringToIndex: NSMaxRange(r) - 1];
92 - (NSString *) davMethodToObjC
94 NSMutableString *newName;
95 NSEnumerator *components;
98 newName = [NSMutableString stringWithString: @"dav"];
99 components = [[self componentsSeparatedByString: @"-"] objectEnumerator];
100 component = [components nextObject];
103 [newName appendString: [component capitalizedString]];
104 component = [components nextObject];
110 - (NSRange) _rangeOfURLInRange: (NSRange) refRange
115 if (!urlNonEndingChars)
117 urlNonEndingChars = [NSMutableCharacterSet new];
118 [urlNonEndingChars addCharactersInString: @">&=,.:;\t \r\n"];
120 if (!urlAfterEndingChars)
122 urlAfterEndingChars = [NSMutableCharacterSet new];
123 [urlAfterEndingChars addCharactersInString: @"()[]{}&;<\t \r\n"];
126 start = refRange.location;
128 && ![urlAfterEndingChars characterIsMember:
129 [self characterAtIndex: 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;
140 && [urlNonEndingChars characterIsMember:
141 [self characterAtIndex: (start + length - 1)]])
144 return NSMakeRange (start, length);
147 - (void) _handleURLs: (NSMutableString *) selfCopy
148 textToMatch: (NSString *) match
149 prefix: (NSString *) prefix
150 inRanges: (NSMutableArray *) ranges
152 NSRange httpRange, currentURL, rest;
153 NSString *urlText, *newUrlText;
154 unsigned int length, matchLength;
156 matchLength = [match length];
157 httpRange = [selfCopy rangeOfString: match];
158 while (httpRange.location != NSNotFound)
160 if ([ranges hasRangeIntersection: httpRange])
161 rest.location = NSMaxRange (httpRange);
164 currentURL = [selfCopy _rangeOfURLInRange: httpRange];
165 urlText = [selfCopy substringFromRange: currentURL];
166 if ([urlText length] > matchLength)
168 newUrlText = [NSString stringWithFormat: @"<a href=\"%@%@\">%@</a>",
169 prefix, urlText, urlText];
170 [selfCopy replaceCharactersInRange: currentURL
171 withString: newUrlText];
173 = NSMakeRange (currentURL.location, [newUrlText length]);
174 [ranges addRange: currentURL];
176 rest.location = NSMaxRange (currentURL);
178 length = [selfCopy length];
179 rest.length = length - rest.location;
180 httpRange = [selfCopy rangeOfString: match
181 options: 0 range: rest];
185 - (NSString *) stringByDetectingURLs
187 NSMutableString *selfCopy;
188 NSMutableArray *ranges;
190 ranges = [NSMutableArray new];
191 selfCopy = [NSMutableString stringWithString: self];
192 [self _handleURLs: selfCopy
196 [self _handleURLs: selfCopy
205 - (NSString *) jsonRepresentation
207 NSMutableString *representation;
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"];
219 return [NSString stringWithFormat: @"\"%@\"", representation];
222 #if LIB_FOUNDATION_LIBRARY
225 return !([self isEqualToString: @"0"]
226 || [self isEqualToString: @"NO"]);