OCXMLServerErrorsParser.m 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. //
  2. // OCXMLServerErrorsParser.m
  3. // ownCloud iOS library
  4. //
  5. // Created by Gonzalo González on 1/6/15.
  6. //
  7. // Copyright (C) 2016, ownCloud GmbH. ( http://www.owncloud.org/ )
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining a copy
  10. // of this software and associated documentation files (the "Software"), to deal
  11. // in the Software without restriction, including without limitation the rights
  12. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. // copies of the Software, and to permit persons to whom the Software is
  14. // furnished to do so, subject to the following conditions:
  15. // The above copyright notice and this permission notice shall be included in
  16. // all copies or substantial portions of the Software.
  17. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. // THE SOFTWARE.
  24. //
  25. #import "OCXMLServerErrorsParser.h"
  26. #import "UtilsFramework.h"
  27. #import "OCErrorMsg.h"
  28. #define k_excepcion_element @"s:exception"
  29. #define k_message_element @"s:message"
  30. #define k_forbidden_character_error @"InvalidPath"
  31. NSString *OCErrorException = @"oc_exception";
  32. NSString *OCErrorMessage = @"oc_message";
  33. @implementation OCXMLServerErrorsParser
  34. - (void) startToParseWithData:(NSData *)data withCompleteBlock: (void(^)(NSError *error)) completeBlock{
  35. self.finishBlock = ^(NSError *err) {
  36. completeBlock(err);
  37. };
  38. if (!data) {
  39. NSError *error = nil;
  40. self.finishBlock(error);
  41. }else{
  42. NSXMLParser *parser = [[NSXMLParser alloc] initWithData:data];
  43. [parser setDelegate:self];
  44. [parser parse];
  45. if (self.resultDict.count == 0) {
  46. NSError *error = nil;
  47. self.finishBlock(error);
  48. }
  49. }
  50. }
  51. #pragma mark - XML Parser Delegate Methods
  52. - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
  53. if (!self.xmlString) {
  54. self.xmlString = [NSMutableString string];
  55. }
  56. // NSLog(@"xml String: %@", self.xmlString);
  57. if (!self.resultDict) {
  58. self.resultDict = [NSMutableDictionary dictionary];
  59. }
  60. }
  61. - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
  62. // NSLog(@"elementName: %@:%@", elementName,self.xmlString);
  63. if ([elementName isEqualToString:k_excepcion_element]) {
  64. if (self.xmlString) {
  65. //Get the lastObject where is the exception name
  66. NSArray *splitedUrl = [self.xmlString componentsSeparatedByString:@"\\"];
  67. [self.resultDict setObject:[splitedUrl lastObject] forKey:OCErrorException];
  68. }
  69. }
  70. if ([elementName isEqualToString:k_message_element]) {
  71. if (self.xmlString) {
  72. [self.resultDict setObject:self.xmlString forKey:OCErrorMessage];
  73. }
  74. }
  75. }
  76. - (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
  77. self.xmlString = string;
  78. }
  79. - (void)parserDidEndDocument:(NSXMLParser *)parser{
  80. // NSLog(@"Finish: %@", self.resultDict);
  81. [self checkTheResultLookingForErrors];
  82. }
  83. //Method that check the ressult in order to find server errors
  84. - (void) checkTheResultLookingForErrors{
  85. NSError *error = nil;
  86. if ([[self.resultDict objectForKey:OCErrorException] isEqualToString:k_forbidden_character_error]) {
  87. error = [UtilsFramework getErrorByCodeId:OCServerErrorForbiddenCharacters];
  88. }
  89. self.finishBlock(error);
  90. }
  91. @end