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

CDVInvokedUrlCommand.m 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 "CDVInvokedUrlCommand.h"
  18. #import "CDVJSON_private.h"
  19. @implementation CDVInvokedUrlCommand
  20. @synthesize arguments = _arguments;
  21. @synthesize callbackId = _callbackId;
  22. @synthesize className = _className;
  23. @synthesize methodName = _methodName;
  24. + (CDVInvokedUrlCommand*)commandFromJson:(NSArray*)jsonEntry
  25. {
  26. return [[CDVInvokedUrlCommand alloc] initFromJson:jsonEntry];
  27. }
  28. - (id)initFromJson:(NSArray*)jsonEntry
  29. {
  30. id tmp = [jsonEntry objectAtIndex:0];
  31. NSString* callbackId = tmp == [NSNull null] ? nil : tmp;
  32. NSString* className = [jsonEntry objectAtIndex:1];
  33. NSString* methodName = [jsonEntry objectAtIndex:2];
  34. NSMutableArray* arguments = [jsonEntry objectAtIndex:3];
  35. return [self initWithArguments:arguments
  36. callbackId:callbackId
  37. className:className
  38. methodName:methodName];
  39. }
  40. - (id)initWithArguments:(NSArray*)arguments
  41. callbackId:(NSString*)callbackId
  42. className:(NSString*)className
  43. methodName:(NSString*)methodName
  44. {
  45. self = [super init];
  46. if (self != nil) {
  47. _arguments = arguments;
  48. _callbackId = callbackId;
  49. _className = className;
  50. _methodName = methodName;
  51. }
  52. [self massageArguments];
  53. return self;
  54. }
  55. - (void)massageArguments
  56. {
  57. NSMutableArray* newArgs = nil;
  58. for (NSUInteger i = 0, count = [_arguments count]; i < count; ++i) {
  59. id arg = [_arguments objectAtIndex:i];
  60. if (![arg isKindOfClass:[NSDictionary class]]) {
  61. continue;
  62. }
  63. NSDictionary* dict = arg;
  64. NSString* type = [dict objectForKey:@"CDVType"];
  65. if (!type || ![type isEqualToString:@"ArrayBuffer"]) {
  66. continue;
  67. }
  68. NSString* data = [dict objectForKey:@"data"];
  69. if (!data) {
  70. continue;
  71. }
  72. if (newArgs == nil) {
  73. newArgs = [NSMutableArray arrayWithArray:_arguments];
  74. _arguments = newArgs;
  75. }
  76. [newArgs replaceObjectAtIndex:i withObject:[[NSData alloc] initWithBase64EncodedString:data options:0]];
  77. }
  78. }
  79. - (id)argumentAtIndex:(NSUInteger)index
  80. {
  81. return [self argumentAtIndex:index withDefault:nil];
  82. }
  83. - (id)argumentAtIndex:(NSUInteger)index withDefault:(id)defaultValue
  84. {
  85. return [self argumentAtIndex:index withDefault:defaultValue andClass:nil];
  86. }
  87. - (id)argumentAtIndex:(NSUInteger)index withDefault:(id)defaultValue andClass:(Class)aClass
  88. {
  89. if (index >= [_arguments count]) {
  90. return defaultValue;
  91. }
  92. id ret = [_arguments objectAtIndex:index];
  93. if (ret == [NSNull null]) {
  94. ret = defaultValue;
  95. }
  96. if ((aClass != nil) && ![ret isKindOfClass:aClass]) {
  97. ret = defaultValue;
  98. }
  99. return ret;
  100. }
  101. @end