PageRenderTime 124ms CodeModel.GetById 50ms app.highlight 70ms RepoModel.GetById 1ms app.codeStats 0ms

/core/externals/update-engine/externals/gdata-objectivec-client/Source/Networking/GDataServerError.m

http://macfuse.googlecode.com/
Objective C | 350 lines | 229 code | 78 blank | 43 comment | 29 complexity | 3c3112c9d261f72e2a3d4b60227da225 MD5 | raw file
  1/* Copyright (c) 2008 Google Inc.
  2 *
  3 * Licensed under the Apache License, Version 2.0 (the "License");
  4 * you may not use this file except in compliance with the License.
  5 * You may obtain a copy of the License at
  6 *
  7 *     http://www.apache.org/licenses/LICENSE-2.0
  8 *
  9 * Unless required by applicable law or agreed to in writing, software
 10 * distributed under the License is distributed on an "AS IS" BASIS,
 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 12 * See the License for the specific language governing permissions and
 13 * limitations under the License.
 14 */
 15
 16#define GDATASERVERERROR_DEFINE_GLOBALS 1
 17#import "GDataServerError.h"
 18
 19#import "GDataObject.h"   // for namespace
 20#import "GDataUtilities.h" // for AreEqualOrBothNil
 21
 22//
 23// GDataServerErrorGroup
 24//
 25
 26@interface GDataServerErrorGroup (PrivateMethods)
 27- (NSArray *)serverErrorsWithData:(NSData *)data;
 28@end
 29
 30@implementation GDataServerErrorGroup
 31
 32- (id)initWithData:(NSData *)data {
 33
 34  self = [super init];
 35  if (self) {
 36
 37    NSArray *errors = [self serverErrorsWithData:data];
 38    if (errors) {
 39
 40      [self setErrors:errors];
 41
 42    } else {
 43      // failed to parse errors
 44      [self release];
 45      return nil;
 46    }
 47  }
 48  return self;
 49}
 50
 51- (NSString *)description {
 52  return [NSString stringWithFormat:@"%@ %p: {errors: %@}",
 53          [self class], self, [self errors]];
 54}
 55
 56- (NSUInteger)hash {
 57  return (NSUInteger) (void *) [GDataServerErrorGroup class];
 58}
 59
 60- (BOOL)isEqual:(GDataServerErrorGroup *)other {
 61  if (self == other) return YES;
 62  if (![other isKindOfClass:[GDataServerErrorGroup class]]) return NO;
 63
 64  return AreEqualOrBothNil([self errors], [other errors]);
 65}
 66
 67- (id)copyWithZone:(NSZone *)zone {
 68  GDataServerErrorGroup* newObj = [[[self class] allocWithZone:zone] init];
 69
 70  NSArray *errorsCopy = [[[NSArray alloc] initWithArray:[self errors]
 71                                              copyItems:YES] autorelease];
 72  [newObj setErrors:errorsCopy];
 73
 74  return newObj;
 75}
 76
 77- (void)dealloc {
 78  [errors_ release];
 79  [super dealloc];
 80}
 81
 82#pragma mark -
 83
 84- (NSArray *)errors {
 85  return errors_;
 86}
 87
 88- (void)setErrors:(NSArray *)array {
 89  [errors_ autorelease];
 90  errors_ = [array retain];
 91}
 92
 93- (GDataServerError *)mainError {
 94
 95  if ([errors_ count] > 0) {
 96    GDataServerError *obj = [errors_ objectAtIndex:0];
 97    return obj;
 98  }
 99  return nil;
100}
101
102#pragma mark -
103
104// -serverErrorsWithData is used by the initFromData: method
105//
106// convert the data to an XML document, step through the <error> elements,
107// and make each into a GDataServerError object
108//
109// returns nil if the data cannot be parsed or no error elements are found
110//
111// data is expected to look like:
112//
113//  <?xml version="1.0" ?>
114//  <errors xmlns="http://schemas.google.com/g/2005">
115//    <error>
116//      <domain>GData</domain>
117//      <code>invalidUri</code>
118//      <internalReason>Badly formatted URI</internalReason>
119//    </error>
120//  </errors>
121
122- (NSArray *)serverErrorsWithData:(NSData *)data {
123
124  NSMutableArray *serverErrors = [NSMutableArray array];
125
126  NSError *docError = nil;
127  NSXMLDocument *xmlDoc;
128
129  xmlDoc = [[[NSXMLDocument alloc] initWithData:data
130                                        options:0
131                                          error:&docError] autorelease];
132  if (xmlDoc != nil) {
133
134    // step through all <error> elements and make a GDataServerError instance
135    // for each
136    NSXMLElement *errorsElem = [xmlDoc rootElement];
137    NSArray *errorElems = [errorsElem elementsForLocalName:@"error"
138                                                       URI:kGDataNamespaceGData];
139
140    for (NSXMLElement *errorElem in errorElems) {
141
142      GDataServerError *serverError;
143
144      serverError = [[[GDataServerError alloc] initWithXMLElement:errorElem] autorelease];
145
146      if (serverError != nil) {
147        [serverErrors addObject:serverError];
148      }
149    }
150
151  }
152
153  if ([serverErrors count] > 0) {
154    return serverErrors;
155  }
156
157  // failed to parse, or found no errors
158#if DEBUG
159  NSString *errStr;
160  errStr = [[[NSString alloc] initWithData:data
161                                  encoding:NSUTF8StringEncoding] autorelease];
162  NSLog(@"Could not parse error: %@\n  Error XML data: %@", docError, errStr);
163#endif
164
165  return nil;
166}
167
168@end
169
170//
171// GDataServerError
172//
173
174@interface GDataServerError (PrivateMethods)
175+ (NSString *)errorStringForParentElement:(NSXMLElement *)parent
176                                childName:(NSString *)childName;
177@end
178
179@implementation GDataServerError
180
181- (id)initWithXMLElement:(NSXMLElement *)element {
182  self = [super init];
183  if (self) {
184    NSString *domain = [GDataServerError errorStringForParentElement:element
185                                                           childName:@"domain"];
186    [self setDomain:domain];
187
188    NSString *code = [GDataServerError errorStringForParentElement:element
189                                                         childName:@"code"];
190    [self setCode:code];
191
192    NSString *reason = [GDataServerError errorStringForParentElement:element
193                                                           childName:@"internalReason"];
194    [self setInternalReason:reason];
195
196    NSString *help = [GDataServerError errorStringForParentElement:element
197                                                         childName:@"extendedHelp"];
198    [self setExtendedHelpURI:help];
199
200    NSString *report = [GDataServerError errorStringForParentElement:element
201                                                           childName:@"sendReport"];
202    [self setSendReportURI:report];
203  }
204  return self;
205}
206
207- (NSString *)description {
208  NSMutableArray *arr = [NSMutableArray array];
209
210  if (domain_) {
211    [arr addObject:[NSString stringWithFormat:@"domain=\"%@\"", domain_]];
212  }
213  if (code_) {
214    [arr addObject:[NSString stringWithFormat:@"code=\"%@\"", code_]];
215  }
216  if (internalReason_) {
217    [arr addObject:[NSString stringWithFormat:@"reason=\"%@\"", internalReason_]];
218  }
219  if (extendedHelpURI_) {
220    [arr addObject:[NSString stringWithFormat:@"help=\"%@\"", extendedHelpURI_]];
221  }
222  if (sendReportURI_) {
223    [arr addObject:[NSString stringWithFormat:@"report=\"%@\"", sendReportURI_]];
224  }
225  NSString *str = [arr componentsJoinedByString:@" "];
226
227  return [NSString stringWithFormat:@"%@ %p: {%@}", [self class], self, str];
228}
229
230- (NSUInteger)hash {
231  return (NSUInteger) (void *) [GDataServerError class];
232}
233
234- (BOOL)isEqual:(GDataServerError *)other {
235  if (self == other) return YES;
236  if (![other isKindOfClass:[GDataServerError class]]) return NO;
237
238  return AreEqualOrBothNil([self domain], [other domain])
239    && AreEqualOrBothNil([self code], [other code])
240    && AreEqualOrBothNil([self internalReason], [other internalReason])
241    && AreEqualOrBothNil([self extendedHelpURI], [other extendedHelpURI])
242    && AreEqualOrBothNil([self sendReportURI], [other sendReportURI]);
243}
244
245- (id)copyWithZone:(NSZone *)zone {
246  GDataServerError* newObj = [[[self class] allocWithZone:zone] init];
247
248  [newObj setDomain:[self domain]];
249  [newObj setCode:[self code]];
250  [newObj setInternalReason:[self internalReason]];
251  [newObj setExtendedHelpURI:[self extendedHelpURI]];
252  [newObj setSendReportURI:[self sendReportURI]];
253
254  return newObj;
255}
256
257- (void)dealloc {
258  [domain_ release];
259  [code_ release];
260  [internalReason_ release];
261  [extendedHelpURI_ release];
262  [sendReportURI_ release];
263  [super dealloc];
264}
265
266#pragma mark -
267
268- (NSString *)summary {
269
270  NSString *summary;
271
272  NSString *domain = [self domain];
273  NSString *code = [self code];
274  NSString *internalReason = [self internalReason];
275
276  if ([internalReason length] > 0) {
277    summary = [NSString stringWithFormat:@"%@ error %@: \"%@\"",
278               domain, code, internalReason];
279  } else {
280    summary = [NSString stringWithFormat:@"%@ error %@", domain, code];
281  }
282  return summary;
283}
284
285#pragma mark -
286
287- (NSString *)domain {
288  return domain_;
289}
290
291- (void)setDomain:(NSString *)str {
292  [domain_ autorelease];
293  domain_ = [str copy];
294}
295
296- (NSString *)code {
297  return code_;
298}
299
300- (void)setCode:(NSString *)str {
301  [code_ autorelease];
302  code_ = [str copy];
303}
304
305- (NSString *)internalReason {
306  return internalReason_;
307}
308
309- (void)setInternalReason:(NSString *)str {
310  [internalReason_ autorelease];
311  internalReason_ = [str copy];
312}
313
314- (NSString *)extendedHelpURI {
315  return extendedHelpURI_;
316}
317
318- (void)setExtendedHelpURI:(NSString *)str {
319  [extendedHelpURI_ autorelease];
320  extendedHelpURI_ = [str copy];
321}
322
323- (NSString *)sendReportURI {
324  return sendReportURI_;
325}
326
327- (void)setSendReportURI:(NSString *)str {
328  [sendReportURI_ autorelease];
329  sendReportURI_ = [str copy];
330}
331
332#pragma mark -
333
334// internal utility routine to get the string value of the named child element,
335// assuming the GData namespace
336+ (NSString *)errorStringForParentElement:(NSXMLElement *)parent
337                                childName:(NSString *)childName {
338
339  NSArray *array = [parent elementsForLocalName:childName
340                                            URI:kGDataNamespaceGData];
341  if ([array count] > 0) {
342    NSXMLElement *elem = [array objectAtIndex:0];
343    NSString *str = [elem stringValue];
344    return str;
345  }
346  return nil;
347}
348
349@end
350