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