Advertisement
Guest User

Untitled

a guest
Feb 9th, 2016
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.26 KB | None | 0 0
  1. //
  2. // EZGenericViewController.m
  3. //
  4.  
  5. - (void)loadDevice
  6. {
  7. NSData *XMLData = [[EZXMLTemplateBuilder instance] XMLDataAppropriateDevice:self.device];
  8. if (XMLData == nil)
  9. {
  10. [self showValidationFailedAlert:@"Need generic XML"];
  11. return;
  12. }
  13.  
  14. [SVProgressHUD showHUDWithStatus:kLoadingStatus];
  15.  
  16. NSString *pathXSD = [[NSBundle mainBundle] pathForResource:@"EZDataItemMarkup" ofType:@"xsd"];
  17. NSURL *schemaAtURL = [NSURL URLWithString:pathXSD];
  18. NSError *dataError;
  19. BOOL successValidateXMLData = [[EZXMLValidator instance] validateXMLData:XMLData schemaAtURL:schemaAtURL error:&dataError];
  20. NSLog(@"Success Validate XML from data %@", successValidateXMLData ? @"YES" : @"NO");
  21.  
  22. // parsing and mapping here
  23. }
  24.  
  25.  
  26. //
  27. // EZXMLTemplateBuilder.h
  28. //
  29. @interface EZXMLTemplateBuilder : NSObject
  30.  
  31. + (instancetype)instance;
  32. - (NSData *)XMLDataAppropriateDevice:(EZDevice *)aDevice;
  33.  
  34. @end
  35.  
  36.  
  37. //
  38. // EZXMLTemplateBuilder.m
  39. //
  40. static NSString * const kPlistFileName = @"devicetype";
  41.  
  42. static NSString * const kTemplateDeviceID = @"$MARK_DATAITEM_ID";
  43. static NSString * const kTemplateTempUnit = @"$MARK_TEMP_UNIT";
  44. static NSString * const kTemplateTempFormat = @"$MARK_TEMP_FORMAT";
  45. static NSString * const kTemplateTempMin = @"$MARK_TEMP_MIN_VALUE";
  46. static NSString * const kTemplateTempMax = @"$MARK_TEMP_MAX_VALUE";
  47. static NSString * const kTemplateUnit = @"$MARK_UNIT";
  48. static NSString * const kTemplateFormat = @"$MARK_FORMAT";
  49. static NSString * const kTemplateMinValue = @"$MARK_MIN_VALUE";
  50. static NSString * const kTemplateMaxValue = @"$MARK_MAX_VALUE";
  51. static NSString * const kTemplateMinTitle = @"$MARK_MIN_TITLE";
  52. static NSString * const kTemplateMaxTitle = @"$MARK_MAX_TITLE";
  53. static NSString * const kTemplateTitle = @"$MARK_TITLE";
  54. static NSString * const kTemplateGetter = @"$MARK_GETTER";
  55. static NSString * const kTemplateSetter = @"$MARK_SETTER";
  56. static NSString * const kTemplatePositive = @"$MARK_YES";
  57. static NSString * const kTemplateNegative = @"$MARK_NO";
  58.  
  59.  
  60. @interface EZXMLNode : NSObject
  61. {
  62. NSMutableDictionary* mAttributes;
  63. NSMutableArray* mSubnodes;
  64. }
  65.  
  66. @property (nonatomic, readwrite, strong) NSString* name;
  67. @property (nonatomic, readwrite, strong) NSString* content;
  68. @property (nonatomic, readonly, strong) NSDictionary* attributes;
  69. @property (nonatomic, readonly, strong) NSArray* subnodes;
  70.  
  71. - (id)init;
  72. - (id)initWithName:(NSString*)aName;
  73. + (EZXMLNode*)createNew:(NSString*)aName;
  74. - (void)clearAttributes;
  75. - (void)addAttribute:(NSString*)aAttribute value:(NSString*)aValue;
  76. - (void)clearSubnodes;
  77. - (void)addSubnode:(EZXMLNode*)aNode;
  78.  
  79. @end
  80.  
  81. @implementation EZXMLNode
  82.  
  83. + (EZXMLNode*)createNew:(NSString*)aName
  84. {
  85. return [[EZXMLNode alloc] initWithName:aName];
  86. }
  87.  
  88. - (id)init
  89. {
  90. if (self = [super init])
  91. {
  92. self.name = @"";
  93. mAttributes = [[NSMutableDictionary alloc] init];
  94. mSubnodes = [[NSMutableArray alloc] init];
  95. }
  96. return self;
  97. }
  98.  
  99. - (id)initWithName:(NSString*)aName
  100. {
  101. if (self = [super init])
  102. {
  103. self.name = aName;
  104. mAttributes = [[NSMutableDictionary alloc] init];
  105. mSubnodes = [[NSMutableArray alloc] init];
  106. }
  107. return self;
  108. }
  109.  
  110. - (NSDictionary*)attributes
  111. {
  112. return mAttributes;
  113. }
  114.  
  115. - (NSArray*)subnodes
  116. {
  117. return mSubnodes;
  118. }
  119.  
  120. - (BOOL)writeToString:(NSMutableString*)aString
  121. {
  122. if (aString.length == 0)
  123. return NO;
  124.  
  125. if (self.name.length == 0)
  126. return NO;
  127.  
  128. if (self.content.length > 0)
  129. {
  130. [aString appendFormat:@"%@", self.content];
  131. return YES;
  132. }
  133.  
  134. [aString appendFormat:@"<%@", self.name];
  135. NSArray* attributeKeys = [self.attributes allKeys];
  136. for (NSString* attributeKey in attributeKeys)
  137. {
  138. NSString* attributeValue = [self.attributes objectForKey:attributeKey];
  139. if (attributeKey.length == 0 || attributeValue.length == 0)
  140. continue;
  141. [aString appendFormat:@" %@=\"%@\"", attributeKey, attributeValue];
  142. }
  143.  
  144. BOOL hasContent = NO;
  145. if (self.subnodes.count > 0)
  146. hasContent = YES;
  147.  
  148. if (hasContent)
  149. {
  150. [aString appendString:@">"];
  151. for (EZXMLNode* node in self.subnodes)
  152. {
  153. if (![node writeToString:aString])
  154. return NO;
  155. }
  156. [aString appendFormat:@"</%@>", self.name];
  157. }
  158. else
  159. {
  160. [aString appendString:@"/>"];
  161. }
  162.  
  163. return YES;
  164. }
  165.  
  166. - (void)clearAttributes
  167. {
  168. [mAttributes removeAllObjects];
  169. }
  170.  
  171. - (void)addAttribute:(NSString*)aAttribute value:(NSString*)aValue
  172. {
  173. if (aAttribute.length == 0 || aValue.length == 0)
  174. return;
  175. if (![aAttribute isKindOfClass:[NSString class]])
  176. return;
  177. if (![aValue isKindOfClass:[NSString class]])
  178. return;
  179.  
  180. [mAttributes setObject:aValue forKey:aAttribute];
  181. }
  182.  
  183. - (void)clearSubnodes
  184. {
  185. [mSubnodes removeAllObjects];
  186. }
  187.  
  188. - (void)addSubnode:(EZXMLNode*)aNode
  189. {
  190. if (aNode == nil)
  191. return;
  192. if (![aNode isKindOfClass:[EZXMLNode class]])
  193. return;
  194. [mSubnodes addObject:aNode];
  195. }
  196.  
  197. @end
  198.  
  199. @interface EZXMLTemplateBuilder ()
  200. {
  201. NSMutableDictionary *mXMLmap;
  202. }
  203.  
  204. @end
  205.  
  206. @implementation EZXMLTemplateBuilder
  207.  
  208. + (instancetype)instance
  209. {
  210. static EZXMLTemplateBuilder *mInstance = nil;
  211. static dispatch_once_t onceToken;
  212. dispatch_once(&onceToken, ^
  213. {
  214. mInstance = [[EZXMLTemplateBuilder alloc] init];
  215. });
  216. return mInstance;
  217. }
  218.  
  219. - (instancetype)init
  220. {
  221. self = [super init];
  222. if (self)
  223. {
  224. mXMLmap = [NSMutableDictionary dictionary];
  225. [self loadPlist];
  226. }
  227. return self;
  228. }
  229.  
  230. - (void)loadPlist
  231. {
  232. NSString *pathOfFile = [[NSBundle mainBundle] pathForResource:kPlistFileName ofType:@"plist"];
  233.  
  234. NSData *plistData = [NSData dataWithContentsOfFile:pathOfFile];
  235. if (!plistData)
  236. {
  237. NSLog(@"error reading from file: %@", pathOfFile);
  238. return;
  239. }
  240. NSPropertyListFormat format;
  241. NSError *error = nil;
  242. id plist = [NSPropertyListSerialization propertyListWithData:plistData options:NSPropertyListMutableContainersAndLeaves format:&format error:&error];
  243. if (!error)
  244. {
  245. mXMLmap = plist;
  246. NSLog(@"loaded data:\n%@", mXMLmap);
  247. }
  248. else
  249. {
  250. NSLog(@"error: %@", error);
  251. }
  252. }
  253.  
  254. - (NSString *)fileNameAppropriateDevice:(EZDevice *)aDevice
  255. {
  256. for (NSString *key in mXMLmap.allKeys)
  257. {
  258. NSDictionary *deviceInfoDict = [mXMLmap objectForKey:key];
  259. EZDeviceInfoType *deviceInfoType = [[EZDeviceInfoType alloc] initWithOptions:deviceInfoDict];
  260. if ([deviceInfoType isEqual:aDevice.info.type])
  261. {
  262. return key;
  263. }
  264. }
  265. return nil;
  266. }
  267.  
  268. - (NSData*)generateTemplateForDevice:(EZDevice*)aDevice
  269. {
  270. static NSString* const kAttribute_Orientation = @"orientation";
  271.  
  272. NSMutableString* template = [NSMutableString stringWithString:@"<?xml version=\"1.0\" encoding=\"UTF-8\"?>"];
  273.  
  274. EZXMLNode* nRoot = [EZXMLNode createNew:@"root"];
  275.  
  276. EZXMLNode* nContent = [EZXMLNode createNew:@"content"];
  277. [nContent addAttribute:kAttribute_Orientation value:@"portrait"];
  278.  
  279. EZFunctionalityObject* battery = nil;
  280.  
  281. for (EZFunctionalityObject* fo in aDevice.functionalityObjects)
  282. {
  283. EZGetterMethod* getter = fo.methodGet;
  284. EZSetterMethod* setter = fo.methodSet;
  285. NSString* foMethodType = getter.type;
  286. if (foMethodType == nil)
  287. {
  288. foMethodType = setter.type;
  289. }
  290. else
  291. {
  292. if (setter.type && ![foMethodType isEqualToString:setter.type])
  293. foMethodType = nil;
  294. }
  295.  
  296. if (foMethodType == nil)
  297. continue;
  298.  
  299. if (getter.deviceType == EZDeviceTypeBattery)
  300. {
  301. battery = fo;
  302. continue;
  303. }
  304.  
  305. EZDeviceType deviceType = fo.methodGet.deviceType;
  306.  
  307. if ([foMethodType isEqualToString:kMethodTypeBool])
  308. {
  309. NSString* rowTemplateURLStr = [[NSBundle mainBundle] pathForResource:@"row_bool" ofType:@"xml"];
  310. if (rowTemplateURLStr == nil)
  311. continue;
  312.  
  313. NSError* error = nil;
  314. NSString* rowTemplate = [[NSString alloc] initWithContentsOfFile:rowTemplateURLStr
  315. encoding:NSUTF8StringEncoding
  316. error:&error];
  317. if (rowTemplate == nil)
  318. continue;
  319.  
  320. {
  321.  
  322. NSString* node = @"";
  323. if (getter != nil)
  324. {
  325. node = [NSString stringWithFormat:@"<getter dataItemId=\"%@\" methodId=\"%@\" instanceId=\"%@\"/>",
  326. aDevice.deviceId,
  327. getter.signature,
  328. fo.instance.instanceId];
  329. }
  330. rowTemplate = [rowTemplate stringByReplacingOccurrencesOfString:kTemplateGetter
  331. withString:node];
  332. }
  333.  
  334. {
  335. NSString* node = @"";
  336. if (setter != nil)
  337. {
  338. node = [NSString stringWithFormat:@"<setter dataItemId=\"%@\" methodId=\"%@\" instanceId=\"%@\"/>",
  339. aDevice.deviceId,
  340. setter.signature,
  341. fo.instance.instanceId];
  342. }
  343. rowTemplate = [rowTemplate stringByReplacingOccurrencesOfString:kTemplateSetter
  344. withString:node];
  345. }
  346.  
  347. rowTemplate = [rowTemplate stringByReplacingOccurrencesOfString:kTemplateTitle
  348. withString:fo.instance.title];
  349.  
  350. if (getter != nil)
  351. {
  352. NSString* positiveValue = [EZMethod positiveTextForDeviceType:deviceType];
  353. rowTemplate = [rowTemplate stringByReplacingOccurrencesOfString:kTemplatePositive
  354. withString:positiveValue];
  355. NSString* negativeValue = [EZMethod negativeTextForDeviceType:deviceType];
  356. rowTemplate = [rowTemplate stringByReplacingOccurrencesOfString:kTemplateNegative
  357. withString:negativeValue];
  358. }
  359. else
  360. {
  361. continue;
  362. }
  363.  
  364. EZXMLNode* nRow = [EZXMLNode createNew:@"row"];
  365. nRow.content = rowTemplate;
  366. [nContent addSubnode:nRow];
  367. }
  368. else if ([foMethodType isEqualToString:kMethodTypeInt] || [foMethodType isEqualToString:kMethodTypeFloat])
  369. {
  370. NSString* rowTemplateURLStr = [[NSBundle mainBundle] pathForResource:@"row_float" ofType:@"xml"];
  371. if (rowTemplateURLStr == nil)
  372. continue;
  373.  
  374. NSError* error = nil;
  375. NSString* rowTemplate = [[NSString alloc] initWithContentsOfFile:rowTemplateURLStr
  376. encoding:NSUTF8StringEncoding
  377. error:&error];
  378. if (rowTemplate == nil)
  379. continue;
  380.  
  381. {
  382.  
  383. NSString* node = @"";
  384. if (getter != nil)
  385. {
  386. node = [NSString stringWithFormat:@"<getter dataItemId=\"%@\" methodId=\"%@\" instanceId=\"%@\"/>",
  387. aDevice.deviceId,
  388. getter.signature,
  389. fo.instance.instanceId];
  390. }
  391. rowTemplate = [rowTemplate stringByReplacingOccurrencesOfString:kTemplateGetter
  392. withString:node];
  393. }
  394.  
  395. {
  396. NSString* node = @"";
  397. if (setter != nil)
  398. {
  399. node = [NSString stringWithFormat:@"<setter dataItemId=\"%@\" methodId=\"%@\" instanceId=\"%@\"/>",
  400. aDevice.deviceId,
  401. setter.signature,
  402. fo.instance.instanceId];
  403. }
  404. rowTemplate = [rowTemplate stringByReplacingOccurrencesOfString:kTemplateSetter
  405. withString:node];
  406. }
  407.  
  408. rowTemplate = [rowTemplate stringByReplacingOccurrencesOfString:kTemplateTitle
  409. withString:fo.instance.title];
  410.  
  411. rowTemplate = [rowTemplate stringByReplacingOccurrencesOfString:kTemplateMinTitle
  412. withString:[EZMethod minValueForDeviceType:deviceType]];
  413. rowTemplate = [rowTemplate stringByReplacingOccurrencesOfString:kTemplateMaxTitle
  414. withString:[EZMethod maxValueForDeviceType:deviceType]];
  415. rowTemplate = [rowTemplate stringByReplacingOccurrencesOfString:kTemplateMinValue
  416. withString:[EZMethod minValueForDeviceType:deviceType]];
  417. rowTemplate = [rowTemplate stringByReplacingOccurrencesOfString:kTemplateMaxValue
  418. withString:[EZMethod maxValueForDeviceType:deviceType]];
  419. rowTemplate = [rowTemplate stringByReplacingOccurrencesOfString:kTemplateFormat
  420. withString:[EZMethod formatDefaultForDeviceType:deviceType]];
  421. rowTemplate = [rowTemplate stringByReplacingOccurrencesOfString:kTemplateUnit
  422. withString:[EZMethod scaleDefaultForDeviceType:deviceType]];
  423.  
  424. EZXMLNode* nRow = [EZXMLNode createNew:@"row"];
  425. nRow.content = rowTemplate;
  426. [nContent addSubnode:nRow];
  427. }
  428. else
  429. {
  430. }
  431. }
  432.  
  433. if (battery != nil)
  434. {
  435. EZXMLNode* nCharge = [EZXMLNode createNew:@"charge"];
  436. nCharge.content = [NSString stringWithFormat:@"<charge><getter dataItemId=\"%@\" methodId=\"%@\" instanceId=\"%@\"/></charge>",
  437. aDevice.deviceId,
  438. battery.methodGet.signature,
  439. battery.instance.instanceId];
  440. [nRoot addSubnode:nCharge];
  441. }
  442.  
  443. [nRoot addSubnode:nContent];
  444.  
  445. [nRoot writeToString:template];
  446. return [template dataUsingEncoding:NSUTF8StringEncoding];
  447. }
  448.  
  449. - (NSData*)XMLDataAppropriateDevice:(EZDevice*)aDevice
  450. {
  451. NSString *nameXMLContent = [self fileNameAppropriateDevice:aDevice];
  452. if (nameXMLContent == nil)
  453. {
  454. return [self generateTemplateForDevice:aDevice];
  455. }
  456.  
  457. NSString *pathXML = [[NSBundle mainBundle] pathForResource:nameXMLContent ofType:@"xml"];
  458.  
  459. NSURLComponents *components = [NSURLComponents new];
  460. components.scheme = @"file";
  461. components.host = nil;
  462. components.path = pathXML;
  463.  
  464. NSURL *XMLAtURL = [components URL];
  465.  
  466. NSData *XMLData = [NSData dataWithContentsOfURL:XMLAtURL];
  467.  
  468. return [self replaceTemplateOccurrencesInData:XMLData withDeviceID:aDevice.deviceId];
  469. }
  470.  
  471. - (NSData *)replaceTemplateOccurrencesInData:(NSData *)aData withDeviceID:(NSString *)aDeviceID
  472. {
  473. NSMutableString *XMLString = [[NSMutableString alloc] initWithData:aData encoding:NSUTF8StringEncoding];
  474.  
  475. [XMLString replaceOccurrencesOfString:kTemplateDeviceID withString:aDeviceID
  476. options:NSCaseInsensitiveSearch range:NSMakeRange(0, [XMLString length])];
  477. [XMLString replaceOccurrencesOfString:kTemplateTempFormat withString:[EZMethod formatDefaultForDeviceType:EZDeviceTypeTemperature]
  478. options:NSCaseInsensitiveSearch range:NSMakeRange(0, [XMLString length])];
  479. [XMLString replaceOccurrencesOfString:kTemplateTempUnit withString:[EZMethod scaleDefaultForDeviceType:EZDeviceTypeTemperature]
  480. options:NSCaseInsensitiveSearch range:NSMakeRange(0, [XMLString length])];
  481. [XMLString replaceOccurrencesOfString:kTemplateTempMin withString:[EZMethod minValueForDeviceType:EZDeviceTypeTemperature]
  482. options:NSCaseInsensitiveSearch range:NSMakeRange(0, [XMLString length])];
  483. [XMLString replaceOccurrencesOfString:kTemplateTempMax withString:[EZMethod maxValueForDeviceType:EZDeviceTypeTemperature]
  484. options:NSCaseInsensitiveSearch range:NSMakeRange(0, [XMLString length])];
  485.  
  486. return [XMLString dataUsingEncoding:NSUTF8StringEncoding];
  487. }
  488.  
  489. @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement