Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

CDVPluginResult.m 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*
  2. Licensed to the Apache Software Foundation (ASF) under one
  3. or more contributor license agreements. See the NOTICE file
  4. distributed with this work for additional information
  5. regarding copyright ownership. The ASF licenses this file
  6. to you under the Apache License, Version 2.0 (the
  7. "License"); you may not use this file except in compliance
  8. with the License. You may obtain a copy of the License at
  9. http://www.apache.org/licenses/LICENSE-2.0
  10. Unless required by applicable law or agreed to in writing,
  11. software distributed under the License is distributed on an
  12. "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  13. KIND, either express or implied. See the License for the
  14. specific language governing permissions and limitations
  15. under the License.
  16. */
  17. #import "CDVPluginResult.h"
  18. #import "CDVJSON_private.h"
  19. #import "CDVDebug.h"
  20. // This exists to preserve compatibility with early Swift plugins, who are
  21. // using CDVCommandStatus as ObjC-style constants rather than as Swift enum
  22. // values.
  23. // These constants alias the enum values back to their previous names.
  24. #define SWIFT_ENUM_COMPAT_HACK(enumVal) const CDVCommandStatus SWIFT_##enumVal NS_SWIFT_NAME(enumVal) = enumVal
  25. SWIFT_ENUM_COMPAT_HACK(CDVCommandStatus_NO_RESULT);
  26. SWIFT_ENUM_COMPAT_HACK(CDVCommandStatus_OK);
  27. SWIFT_ENUM_COMPAT_HACK(CDVCommandStatus_CLASS_NOT_FOUND_EXCEPTION);
  28. SWIFT_ENUM_COMPAT_HACK(CDVCommandStatus_ILLEGAL_ACCESS_EXCEPTION);
  29. SWIFT_ENUM_COMPAT_HACK(CDVCommandStatus_INSTANTIATION_EXCEPTION);
  30. SWIFT_ENUM_COMPAT_HACK(CDVCommandStatus_MALFORMED_URL_EXCEPTION);
  31. SWIFT_ENUM_COMPAT_HACK(CDVCommandStatus_IO_EXCEPTION);
  32. SWIFT_ENUM_COMPAT_HACK(CDVCommandStatus_INVALID_ACTION);
  33. SWIFT_ENUM_COMPAT_HACK(CDVCommandStatus_JSON_EXCEPTION);
  34. SWIFT_ENUM_COMPAT_HACK(CDVCommandStatus_ERROR);
  35. #undef SWIFT_ENUM_COMPAT_HACK
  36. @interface CDVPluginResult ()
  37. - (CDVPluginResult*)initWithStatus:(CDVCommandStatus)statusOrdinal message:(id)theMessage;
  38. @end
  39. @implementation CDVPluginResult
  40. @synthesize status, message, keepCallback, associatedObject;
  41. static NSArray* org_apache_cordova_CommandStatusMsgs;
  42. id messageFromArrayBuffer(NSData* data)
  43. {
  44. return @{
  45. @"CDVType" : @"ArrayBuffer",
  46. @"data" :[data base64EncodedStringWithOptions:0]
  47. };
  48. }
  49. id massageMessage(id message)
  50. {
  51. if ([message isKindOfClass:[NSData class]]) {
  52. return messageFromArrayBuffer(message);
  53. }
  54. return message;
  55. }
  56. id messageFromMultipart(NSArray* theMessages)
  57. {
  58. NSMutableArray* messages = [NSMutableArray arrayWithArray:theMessages];
  59. for (NSUInteger i = 0; i < messages.count; ++i) {
  60. [messages replaceObjectAtIndex:i withObject:massageMessage([messages objectAtIndex:i])];
  61. }
  62. return @{
  63. @"CDVType" : @"MultiPart",
  64. @"messages" : messages
  65. };
  66. }
  67. + (void)initialize
  68. {
  69. org_apache_cordova_CommandStatusMsgs = [[NSArray alloc] initWithObjects:@"No result",
  70. @"OK",
  71. @"Class not found",
  72. @"Illegal access",
  73. @"Instantiation error",
  74. @"Malformed url",
  75. @"IO error",
  76. @"Invalid action",
  77. @"JSON error",
  78. @"Error",
  79. nil];
  80. }
  81. - (CDVPluginResult*)init
  82. {
  83. return [self initWithStatus:CDVCommandStatus_NO_RESULT message:nil];
  84. }
  85. - (CDVPluginResult*)initWithStatus:(CDVCommandStatus)statusOrdinal message:(id)theMessage
  86. {
  87. self = [super init];
  88. if (self) {
  89. status = @(statusOrdinal);
  90. message = theMessage;
  91. keepCallback = [NSNumber numberWithBool:NO];
  92. }
  93. return self;
  94. }
  95. + (CDVPluginResult*)resultWithStatus:(CDVCommandStatus)statusOrdinal
  96. {
  97. return [[self alloc] initWithStatus:statusOrdinal message:nil];
  98. }
  99. + (CDVPluginResult*)resultWithStatus:(CDVCommandStatus)statusOrdinal messageAsString:(NSString*)theMessage
  100. {
  101. return [[self alloc] initWithStatus:statusOrdinal message:theMessage];
  102. }
  103. + (CDVPluginResult*)resultWithStatus:(CDVCommandStatus)statusOrdinal messageAsArray:(NSArray*)theMessage
  104. {
  105. return [[self alloc] initWithStatus:statusOrdinal message:theMessage];
  106. }
  107. + (CDVPluginResult*)resultWithStatus:(CDVCommandStatus)statusOrdinal messageAsInt:(int)theMessage
  108. {
  109. return [[self alloc] initWithStatus:statusOrdinal message:[NSNumber numberWithInt:theMessage]];
  110. }
  111. + (CDVPluginResult*)resultWithStatus:(CDVCommandStatus)statusOrdinal messageAsNSInteger:(NSInteger)theMessage
  112. {
  113. return [[self alloc] initWithStatus:statusOrdinal message:[NSNumber numberWithInteger:theMessage]];
  114. }
  115. + (CDVPluginResult*)resultWithStatus:(CDVCommandStatus)statusOrdinal messageAsNSUInteger:(NSUInteger)theMessage
  116. {
  117. return [[self alloc] initWithStatus:statusOrdinal message:[NSNumber numberWithUnsignedInteger:theMessage]];
  118. }
  119. + (CDVPluginResult*)resultWithStatus:(CDVCommandStatus)statusOrdinal messageAsDouble:(double)theMessage
  120. {
  121. return [[self alloc] initWithStatus:statusOrdinal message:[NSNumber numberWithDouble:theMessage]];
  122. }
  123. + (CDVPluginResult*)resultWithStatus:(CDVCommandStatus)statusOrdinal messageAsBool:(BOOL)theMessage
  124. {
  125. return [[self alloc] initWithStatus:statusOrdinal message:[NSNumber numberWithBool:theMessage]];
  126. }
  127. + (CDVPluginResult*)resultWithStatus:(CDVCommandStatus)statusOrdinal messageAsDictionary:(NSDictionary*)theMessage
  128. {
  129. return [[self alloc] initWithStatus:statusOrdinal message:theMessage];
  130. }
  131. + (CDVPluginResult*)resultWithStatus:(CDVCommandStatus)statusOrdinal messageAsArrayBuffer:(NSData*)theMessage
  132. {
  133. return [[self alloc] initWithStatus:statusOrdinal message:messageFromArrayBuffer(theMessage)];
  134. }
  135. + (CDVPluginResult*)resultWithStatus:(CDVCommandStatus)statusOrdinal messageAsMultipart:(NSArray*)theMessages
  136. {
  137. return [[self alloc] initWithStatus:statusOrdinal message:messageFromMultipart(theMessages)];
  138. }
  139. + (CDVPluginResult*)resultWithStatus:(CDVCommandStatus)statusOrdinal messageToErrorObject:(int)errorCode
  140. {
  141. NSDictionary* errDict = @{@"code" :[NSNumber numberWithInt:errorCode]};
  142. return [[self alloc] initWithStatus:statusOrdinal message:errDict];
  143. }
  144. - (void)setKeepCallbackAsBool:(BOOL)bKeepCallback
  145. {
  146. [self setKeepCallback:[NSNumber numberWithBool:bKeepCallback]];
  147. }
  148. - (NSString*)argumentsAsJSON
  149. {
  150. id arguments = (self.message == nil ? [NSNull null] : self.message);
  151. NSArray* argumentsWrappedInArray = [NSArray arrayWithObject:arguments];
  152. NSString* argumentsJSON = [argumentsWrappedInArray cdv_JSONString];
  153. argumentsJSON = [argumentsJSON substringWithRange:NSMakeRange(1, [argumentsJSON length] - 2)];
  154. return argumentsJSON;
  155. }
  156. static BOOL gIsVerbose = NO;
  157. + (void)setVerbose:(BOOL)verbose
  158. {
  159. gIsVerbose = verbose;
  160. }
  161. + (BOOL)isVerbose
  162. {
  163. return gIsVerbose;
  164. }
  165. @end