1/* NSString+DAV.m - this file is part of SOGo
2 *
3 * Copyright (C) 2008 Inverse inc.
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/NSDictionary.h>
24
25#import <NGExtensions/NSString+misc.h>
26
27#import "NSArray+Utilities.h"
28
29#import "NSString+DAV.h"
30
31@implementation NSString (SOGoWebDAVExtensions)
32
33- (NSString *)
34 asWebDavStringWithNamespaces: (NSMutableDictionary *) namespaces
35{
36  return [self stringByEscapingXMLString];
37}
38
39- (NSString *) asDAVPropertyDescription
40{
41  NSArray *components;
42
43  components = [[self componentsSeparatedByString: @"-"]
44                 resultsOfSelector: @selector (capitalizedString)];
45
46  return [components componentsJoinedByString: @" "];
47}
48
49#warning we should use the same nomenclature as the webdav values...
50- (NSMutableDictionary *) asWebDAVTuple
51{
52  NSString *namespace, *nodeName;
53  NSRange nsEnd;
54
55  nsEnd = [self rangeOfString: @"}"];
56  namespace = [self substringFromRange: NSMakeRange (1, nsEnd.location - 1)];
57  nodeName = [self substringFromIndex: nsEnd.location + 1];
58
59  return [NSMutableDictionary
60           dictionaryWithObjectsAndKeys: namespace, @"ns",
61           nodeName, @"method", nil];
62}
63
64
65- (NSString *) davMethodToObjC
66{
67  NSMutableString *newName;
68  NSArray *components;
69
70  newName = [NSMutableString stringWithString: @"dav"];
71  components = [[self componentsSeparatedByString: @"-"]
72                 resultsOfSelector: @selector (capitalizedString)];
73  [newName appendString: [components componentsJoinedByString: @""]];
74
75  return newName;
76}
77
78- (NSString *) davSetterName
79{
80  unichar firstLetter;
81  NSString *firstString, *property, *davPrefix;
82
83  property = [[self asDavInvocation] objectForKey: @"method"];
84  if (!property)
85    property = self;
86  firstLetter = [property characterAtIndex: 0];
87  firstString = [[NSString stringWithCharacters: &firstLetter length: 1]
88		  uppercaseString];
89  if ([property length] > 3
90      && [[property substringWithRange: NSMakeRange (0, 3)]
91           caseInsensitiveCompare: @"dav"] == NSOrderedSame)
92    davPrefix = @"";
93  else
94    davPrefix = @"Dav";
95
96  return [NSString stringWithFormat: @"set%@%@%@:", davPrefix,
97		   firstString, [property substringFromIndex: 1]];
98}
99
100- (NSDictionary *) asDavInvocation
101{
102  NSMutableDictionary *davInvocation;
103  NSRange nsEnclosing, methodEnclosing;
104  unsigned int length;
105
106  davInvocation = nil;
107  if ([self hasPrefix: @"{"])
108    {
109      nsEnclosing = [self rangeOfString: @"}"];
110      length = [self length];
111      if (nsEnclosing.length > 0 && nsEnclosing.location < (length - 1))
112	{
113	  methodEnclosing = NSMakeRange (nsEnclosing.location + 1,
114                                         length - nsEnclosing.location - 1);
115	  nsEnclosing.length = nsEnclosing.location - 1;
116	  nsEnclosing.location = 1;
117	  davInvocation = [NSMutableDictionary dictionaryWithCapacity: 2];
118	  [davInvocation setObject: [self substringWithRange: nsEnclosing]
119			 forKey: @"ns"];
120	  [davInvocation setObject: [self substringWithRange: methodEnclosing]
121			 forKey: @"method"];
122	}
123    }
124
125  return davInvocation;
126}
127
128- (NSMutableDictionary *) asWebDAVTupleWithContent: (id) content
129{
130  NSMutableDictionary *tuple;
131
132  tuple = [self asWebDAVTuple];
133  [tuple setObject: content forKey: @"content"];
134
135  return tuple;
136}
137
138- (NSString *) removeOutsideTags
139{
140  NSString *newString;
141  NSRange range;
142
143  newString = nil;
144
145  /* we extract the text between >XXX</, but in a bad way */
146  range = [self rangeOfString: @">"];
147  if (range.location != NSNotFound)
148    {
149      newString = [self substringFromIndex: range.location + 1];
150      range = [newString rangeOfString: @"<" options: NSBackwardsSearch];
151      if (range.location != NSNotFound)
152        newString = [newString substringToIndex: range.location];
153      else
154        newString = nil;
155    }
156  else
157    newString = nil;
158
159  return newString;
160}
161
162@end
163