Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // EZGenericViewController.m
- //
- - (void)loadDevice
- {
- NSData *XMLData = [[EZXMLTemplateBuilder instance] XMLDataAppropriateDevice:self.device];
- if (XMLData == nil)
- {
- [self showValidationFailedAlert:@"Need generic XML"];
- return;
- }
- [SVProgressHUD showHUDWithStatus:kLoadingStatus];
- NSString *pathXSD = [[NSBundle mainBundle] pathForResource:@"EZDataItemMarkup" ofType:@"xsd"];
- NSURL *schemaAtURL = [NSURL URLWithString:pathXSD];
- NSError *dataError;
- BOOL successValidateXMLData = [[EZXMLValidator instance] validateXMLData:XMLData schemaAtURL:schemaAtURL error:&dataError];
- NSLog(@"Success Validate XML from data %@", successValidateXMLData ? @"YES" : @"NO");
- // parsing and mapping here
- }
- //
- // EZXMLTemplateBuilder.h
- //
- @interface EZXMLTemplateBuilder : NSObject
- + (instancetype)instance;
- - (NSData *)XMLDataAppropriateDevice:(EZDevice *)aDevice;
- @end
- //
- // EZXMLTemplateBuilder.m
- //
- static NSString * const kPlistFileName = @"devicetype";
- static NSString * const kTemplateDeviceID = @"$MARK_DATAITEM_ID";
- static NSString * const kTemplateTempUnit = @"$MARK_TEMP_UNIT";
- static NSString * const kTemplateTempFormat = @"$MARK_TEMP_FORMAT";
- static NSString * const kTemplateTempMin = @"$MARK_TEMP_MIN_VALUE";
- static NSString * const kTemplateTempMax = @"$MARK_TEMP_MAX_VALUE";
- static NSString * const kTemplateUnit = @"$MARK_UNIT";
- static NSString * const kTemplateFormat = @"$MARK_FORMAT";
- static NSString * const kTemplateMinValue = @"$MARK_MIN_VALUE";
- static NSString * const kTemplateMaxValue = @"$MARK_MAX_VALUE";
- static NSString * const kTemplateMinTitle = @"$MARK_MIN_TITLE";
- static NSString * const kTemplateMaxTitle = @"$MARK_MAX_TITLE";
- static NSString * const kTemplateTitle = @"$MARK_TITLE";
- static NSString * const kTemplateGetter = @"$MARK_GETTER";
- static NSString * const kTemplateSetter = @"$MARK_SETTER";
- static NSString * const kTemplatePositive = @"$MARK_YES";
- static NSString * const kTemplateNegative = @"$MARK_NO";
- @interface EZXMLNode : NSObject
- {
- NSMutableDictionary* mAttributes;
- NSMutableArray* mSubnodes;
- }
- @property (nonatomic, readwrite, strong) NSString* name;
- @property (nonatomic, readwrite, strong) NSString* content;
- @property (nonatomic, readonly, strong) NSDictionary* attributes;
- @property (nonatomic, readonly, strong) NSArray* subnodes;
- - (id)init;
- - (id)initWithName:(NSString*)aName;
- + (EZXMLNode*)createNew:(NSString*)aName;
- - (void)clearAttributes;
- - (void)addAttribute:(NSString*)aAttribute value:(NSString*)aValue;
- - (void)clearSubnodes;
- - (void)addSubnode:(EZXMLNode*)aNode;
- @end
- @implementation EZXMLNode
- + (EZXMLNode*)createNew:(NSString*)aName
- {
- return [[EZXMLNode alloc] initWithName:aName];
- }
- - (id)init
- {
- if (self = [super init])
- {
- self.name = @"";
- mAttributes = [[NSMutableDictionary alloc] init];
- mSubnodes = [[NSMutableArray alloc] init];
- }
- return self;
- }
- - (id)initWithName:(NSString*)aName
- {
- if (self = [super init])
- {
- self.name = aName;
- mAttributes = [[NSMutableDictionary alloc] init];
- mSubnodes = [[NSMutableArray alloc] init];
- }
- return self;
- }
- - (NSDictionary*)attributes
- {
- return mAttributes;
- }
- - (NSArray*)subnodes
- {
- return mSubnodes;
- }
- - (BOOL)writeToString:(NSMutableString*)aString
- {
- if (aString.length == 0)
- return NO;
- if (self.name.length == 0)
- return NO;
- if (self.content.length > 0)
- {
- [aString appendFormat:@"%@", self.content];
- return YES;
- }
- [aString appendFormat:@"<%@", self.name];
- NSArray* attributeKeys = [self.attributes allKeys];
- for (NSString* attributeKey in attributeKeys)
- {
- NSString* attributeValue = [self.attributes objectForKey:attributeKey];
- if (attributeKey.length == 0 || attributeValue.length == 0)
- continue;
- [aString appendFormat:@" %@=\"%@\"", attributeKey, attributeValue];
- }
- BOOL hasContent = NO;
- if (self.subnodes.count > 0)
- hasContent = YES;
- if (hasContent)
- {
- [aString appendString:@">"];
- for (EZXMLNode* node in self.subnodes)
- {
- if (![node writeToString:aString])
- return NO;
- }
- [aString appendFormat:@"</%@>", self.name];
- }
- else
- {
- [aString appendString:@"/>"];
- }
- return YES;
- }
- - (void)clearAttributes
- {
- [mAttributes removeAllObjects];
- }
- - (void)addAttribute:(NSString*)aAttribute value:(NSString*)aValue
- {
- if (aAttribute.length == 0 || aValue.length == 0)
- return;
- if (![aAttribute isKindOfClass:[NSString class]])
- return;
- if (![aValue isKindOfClass:[NSString class]])
- return;
- [mAttributes setObject:aValue forKey:aAttribute];
- }
- - (void)clearSubnodes
- {
- [mSubnodes removeAllObjects];
- }
- - (void)addSubnode:(EZXMLNode*)aNode
- {
- if (aNode == nil)
- return;
- if (![aNode isKindOfClass:[EZXMLNode class]])
- return;
- [mSubnodes addObject:aNode];
- }
- @end
- @interface EZXMLTemplateBuilder ()
- {
- NSMutableDictionary *mXMLmap;
- }
- @end
- @implementation EZXMLTemplateBuilder
- + (instancetype)instance
- {
- static EZXMLTemplateBuilder *mInstance = nil;
- static dispatch_once_t onceToken;
- dispatch_once(&onceToken, ^
- {
- mInstance = [[EZXMLTemplateBuilder alloc] init];
- });
- return mInstance;
- }
- - (instancetype)init
- {
- self = [super init];
- if (self)
- {
- mXMLmap = [NSMutableDictionary dictionary];
- [self loadPlist];
- }
- return self;
- }
- - (void)loadPlist
- {
- NSString *pathOfFile = [[NSBundle mainBundle] pathForResource:kPlistFileName ofType:@"plist"];
- NSData *plistData = [NSData dataWithContentsOfFile:pathOfFile];
- if (!plistData)
- {
- NSLog(@"error reading from file: %@", pathOfFile);
- return;
- }
- NSPropertyListFormat format;
- NSError *error = nil;
- id plist = [NSPropertyListSerialization propertyListWithData:plistData options:NSPropertyListMutableContainersAndLeaves format:&format error:&error];
- if (!error)
- {
- mXMLmap = plist;
- NSLog(@"loaded data:\n%@", mXMLmap);
- }
- else
- {
- NSLog(@"error: %@", error);
- }
- }
- - (NSString *)fileNameAppropriateDevice:(EZDevice *)aDevice
- {
- for (NSString *key in mXMLmap.allKeys)
- {
- NSDictionary *deviceInfoDict = [mXMLmap objectForKey:key];
- EZDeviceInfoType *deviceInfoType = [[EZDeviceInfoType alloc] initWithOptions:deviceInfoDict];
- if ([deviceInfoType isEqual:aDevice.info.type])
- {
- return key;
- }
- }
- return nil;
- }
- - (NSData*)generateTemplateForDevice:(EZDevice*)aDevice
- {
- static NSString* const kAttribute_Orientation = @"orientation";
- NSMutableString* template = [NSMutableString stringWithString:@"<?xml version=\"1.0\" encoding=\"UTF-8\"?>"];
- EZXMLNode* nRoot = [EZXMLNode createNew:@"root"];
- EZXMLNode* nContent = [EZXMLNode createNew:@"content"];
- [nContent addAttribute:kAttribute_Orientation value:@"portrait"];
- EZFunctionalityObject* battery = nil;
- for (EZFunctionalityObject* fo in aDevice.functionalityObjects)
- {
- EZGetterMethod* getter = fo.methodGet;
- EZSetterMethod* setter = fo.methodSet;
- NSString* foMethodType = getter.type;
- if (foMethodType == nil)
- {
- foMethodType = setter.type;
- }
- else
- {
- if (setter.type && ![foMethodType isEqualToString:setter.type])
- foMethodType = nil;
- }
- if (foMethodType == nil)
- continue;
- if (getter.deviceType == EZDeviceTypeBattery)
- {
- battery = fo;
- continue;
- }
- EZDeviceType deviceType = fo.methodGet.deviceType;
- if ([foMethodType isEqualToString:kMethodTypeBool])
- {
- NSString* rowTemplateURLStr = [[NSBundle mainBundle] pathForResource:@"row_bool" ofType:@"xml"];
- if (rowTemplateURLStr == nil)
- continue;
- NSError* error = nil;
- NSString* rowTemplate = [[NSString alloc] initWithContentsOfFile:rowTemplateURLStr
- encoding:NSUTF8StringEncoding
- error:&error];
- if (rowTemplate == nil)
- continue;
- {
- NSString* node = @"";
- if (getter != nil)
- {
- node = [NSString stringWithFormat:@"<getter dataItemId=\"%@\" methodId=\"%@\" instanceId=\"%@\"/>",
- aDevice.deviceId,
- getter.signature,
- fo.instance.instanceId];
- }
- rowTemplate = [rowTemplate stringByReplacingOccurrencesOfString:kTemplateGetter
- withString:node];
- }
- {
- NSString* node = @"";
- if (setter != nil)
- {
- node = [NSString stringWithFormat:@"<setter dataItemId=\"%@\" methodId=\"%@\" instanceId=\"%@\"/>",
- aDevice.deviceId,
- setter.signature,
- fo.instance.instanceId];
- }
- rowTemplate = [rowTemplate stringByReplacingOccurrencesOfString:kTemplateSetter
- withString:node];
- }
- rowTemplate = [rowTemplate stringByReplacingOccurrencesOfString:kTemplateTitle
- withString:fo.instance.title];
- if (getter != nil)
- {
- NSString* positiveValue = [EZMethod positiveTextForDeviceType:deviceType];
- rowTemplate = [rowTemplate stringByReplacingOccurrencesOfString:kTemplatePositive
- withString:positiveValue];
- NSString* negativeValue = [EZMethod negativeTextForDeviceType:deviceType];
- rowTemplate = [rowTemplate stringByReplacingOccurrencesOfString:kTemplateNegative
- withString:negativeValue];
- }
- else
- {
- continue;
- }
- EZXMLNode* nRow = [EZXMLNode createNew:@"row"];
- nRow.content = rowTemplate;
- [nContent addSubnode:nRow];
- }
- else if ([foMethodType isEqualToString:kMethodTypeInt] || [foMethodType isEqualToString:kMethodTypeFloat])
- {
- NSString* rowTemplateURLStr = [[NSBundle mainBundle] pathForResource:@"row_float" ofType:@"xml"];
- if (rowTemplateURLStr == nil)
- continue;
- NSError* error = nil;
- NSString* rowTemplate = [[NSString alloc] initWithContentsOfFile:rowTemplateURLStr
- encoding:NSUTF8StringEncoding
- error:&error];
- if (rowTemplate == nil)
- continue;
- {
- NSString* node = @"";
- if (getter != nil)
- {
- node = [NSString stringWithFormat:@"<getter dataItemId=\"%@\" methodId=\"%@\" instanceId=\"%@\"/>",
- aDevice.deviceId,
- getter.signature,
- fo.instance.instanceId];
- }
- rowTemplate = [rowTemplate stringByReplacingOccurrencesOfString:kTemplateGetter
- withString:node];
- }
- {
- NSString* node = @"";
- if (setter != nil)
- {
- node = [NSString stringWithFormat:@"<setter dataItemId=\"%@\" methodId=\"%@\" instanceId=\"%@\"/>",
- aDevice.deviceId,
- setter.signature,
- fo.instance.instanceId];
- }
- rowTemplate = [rowTemplate stringByReplacingOccurrencesOfString:kTemplateSetter
- withString:node];
- }
- rowTemplate = [rowTemplate stringByReplacingOccurrencesOfString:kTemplateTitle
- withString:fo.instance.title];
- rowTemplate = [rowTemplate stringByReplacingOccurrencesOfString:kTemplateMinTitle
- withString:[EZMethod minValueForDeviceType:deviceType]];
- rowTemplate = [rowTemplate stringByReplacingOccurrencesOfString:kTemplateMaxTitle
- withString:[EZMethod maxValueForDeviceType:deviceType]];
- rowTemplate = [rowTemplate stringByReplacingOccurrencesOfString:kTemplateMinValue
- withString:[EZMethod minValueForDeviceType:deviceType]];
- rowTemplate = [rowTemplate stringByReplacingOccurrencesOfString:kTemplateMaxValue
- withString:[EZMethod maxValueForDeviceType:deviceType]];
- rowTemplate = [rowTemplate stringByReplacingOccurrencesOfString:kTemplateFormat
- withString:[EZMethod formatDefaultForDeviceType:deviceType]];
- rowTemplate = [rowTemplate stringByReplacingOccurrencesOfString:kTemplateUnit
- withString:[EZMethod scaleDefaultForDeviceType:deviceType]];
- EZXMLNode* nRow = [EZXMLNode createNew:@"row"];
- nRow.content = rowTemplate;
- [nContent addSubnode:nRow];
- }
- else
- {
- }
- }
- if (battery != nil)
- {
- EZXMLNode* nCharge = [EZXMLNode createNew:@"charge"];
- nCharge.content = [NSString stringWithFormat:@"<charge><getter dataItemId=\"%@\" methodId=\"%@\" instanceId=\"%@\"/></charge>",
- aDevice.deviceId,
- battery.methodGet.signature,
- battery.instance.instanceId];
- [nRoot addSubnode:nCharge];
- }
- [nRoot addSubnode:nContent];
- [nRoot writeToString:template];
- return [template dataUsingEncoding:NSUTF8StringEncoding];
- }
- - (NSData*)XMLDataAppropriateDevice:(EZDevice*)aDevice
- {
- NSString *nameXMLContent = [self fileNameAppropriateDevice:aDevice];
- if (nameXMLContent == nil)
- {
- return [self generateTemplateForDevice:aDevice];
- }
- NSString *pathXML = [[NSBundle mainBundle] pathForResource:nameXMLContent ofType:@"xml"];
- NSURLComponents *components = [NSURLComponents new];
- components.scheme = @"file";
- components.host = nil;
- components.path = pathXML;
- NSURL *XMLAtURL = [components URL];
- NSData *XMLData = [NSData dataWithContentsOfURL:XMLAtURL];
- return [self replaceTemplateOccurrencesInData:XMLData withDeviceID:aDevice.deviceId];
- }
- - (NSData *)replaceTemplateOccurrencesInData:(NSData *)aData withDeviceID:(NSString *)aDeviceID
- {
- NSMutableString *XMLString = [[NSMutableString alloc] initWithData:aData encoding:NSUTF8StringEncoding];
- [XMLString replaceOccurrencesOfString:kTemplateDeviceID withString:aDeviceID
- options:NSCaseInsensitiveSearch range:NSMakeRange(0, [XMLString length])];
- [XMLString replaceOccurrencesOfString:kTemplateTempFormat withString:[EZMethod formatDefaultForDeviceType:EZDeviceTypeTemperature]
- options:NSCaseInsensitiveSearch range:NSMakeRange(0, [XMLString length])];
- [XMLString replaceOccurrencesOfString:kTemplateTempUnit withString:[EZMethod scaleDefaultForDeviceType:EZDeviceTypeTemperature]
- options:NSCaseInsensitiveSearch range:NSMakeRange(0, [XMLString length])];
- [XMLString replaceOccurrencesOfString:kTemplateTempMin withString:[EZMethod minValueForDeviceType:EZDeviceTypeTemperature]
- options:NSCaseInsensitiveSearch range:NSMakeRange(0, [XMLString length])];
- [XMLString replaceOccurrencesOfString:kTemplateTempMax withString:[EZMethod maxValueForDeviceType:EZDeviceTypeTemperature]
- options:NSCaseInsensitiveSearch range:NSMakeRange(0, [XMLString length])];
- return [XMLString dataUsingEncoding:NSUTF8StringEncoding];
- }
- @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement