]> err.no Git - scalable-opengroupware.org/blob - SoObjects/SOGo/NSString+Utilities.m
ae0b79e85181fd98855b1d44a56ea593ec1cb9a5
[scalable-opengroupware.org] / SoObjects / SOGo / NSString+Utilities.m
1 /* NSString+URL.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 "NSString+Utilities.h"
24 #import "NSDictionary+URL.h"
25
26 @implementation NSString (SOGoURLExtension)
27
28 - (NSString *) composeURLWithAction: (NSString *) action
29                          parameters: (NSDictionary *) urlParameters
30                             andHash: (BOOL) useHash
31 {
32   NSMutableString *completeURL;
33
34   completeURL = [NSMutableString new];
35   [completeURL autorelease];
36
37   [completeURL appendString: [self urlWithoutParameters]];
38   if (![completeURL hasSuffix: @"/"])
39     [completeURL appendString: @"/"];
40   [completeURL appendString: action];
41   [completeURL appendString: [urlParameters asURLParameters]];
42   if (useHash)
43     [completeURL appendString: @"#"];
44
45   return completeURL;
46 }
47
48 - (NSString *) hostlessURL
49 {
50   NSString *newURL;
51   NSRange hostR, locationR;
52
53   if ([self hasPrefix: @"/"])
54     {
55       newURL = [self copy];
56       [newURL autorelease];
57     }
58   else
59     {
60       hostR = [self rangeOfString: @"://"];
61       locationR = [[self substringFromIndex: (hostR.location + hostR.length)]
62                     rangeOfString: @"/"];
63       newURL = [self substringFromIndex: (hostR.location + hostR.length + locationR.location)];
64     }
65
66   return newURL;
67 }
68
69 - (NSString *) urlWithoutParameters;
70 {
71   NSRange r;
72   NSString *newUrl;
73   
74   r = [self rangeOfString:@"?" options: NSBackwardsSearch];
75   if (r.length > 0)
76     newUrl = [self substringToIndex: NSMaxRange(r) - 1];
77   else
78     newUrl = self;
79
80   return newUrl;
81 }
82
83 - (NSString *) davMethodToObjC
84 {
85   NSMutableString *newName;
86   NSEnumerator *components;
87   NSString *component;
88
89   newName = [NSMutableString stringWithString: @"dav"];
90   components = [[self componentsSeparatedByString: @"-"] objectEnumerator];
91   component = [components nextObject];
92   while (component)
93     {
94       [newName appendString: [component capitalizedString]];
95       component = [components nextObject];
96     }
97
98   return newName;
99 }
100
101 @end