2013-10-09 15:45:16 +00:00
|
|
|
//
|
|
|
|
// XmlContainer.m
|
|
|
|
// Xml
|
|
|
|
//
|
|
|
|
// Created by Christopher Snowhill on 10/9/13.
|
|
|
|
// Copyright 2013 __NoWork, Inc__. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#import "XmlContainer.h"
|
|
|
|
|
2013-10-11 12:03:55 +00:00
|
|
|
#import "Logging.h"
|
|
|
|
|
2013-10-09 15:45:16 +00:00
|
|
|
@implementation XmlContainer
|
|
|
|
|
2021-01-30 23:14:08 +00:00
|
|
|
+ (NSURL *)urlForPath:(NSString *)path relativeTo:(NSString *)baseFilename {
|
|
|
|
NSRange protocolRange = [path rangeOfString:@"://"];
|
|
|
|
if (protocolRange.location != NSNotFound) {
|
|
|
|
return [NSURL URLWithString:path];
|
|
|
|
}
|
|
|
|
|
|
|
|
NSMutableString *unixPath = [path mutableCopy];
|
|
|
|
|
|
|
|
//Get the fragment
|
|
|
|
NSString *fragment = @"";
|
|
|
|
NSScanner *scanner = [NSScanner scannerWithString:unixPath];
|
|
|
|
NSCharacterSet *characterSet = [NSCharacterSet characterSetWithCharactersInString:@"#1234567890"];
|
|
|
|
while (![scanner isAtEnd]) {
|
|
|
|
NSString *possibleFragment;
|
|
|
|
[scanner scanUpToString:@"#" intoString:nil];
|
|
|
|
|
|
|
|
if ([scanner scanCharactersFromSet:characterSet intoString:&possibleFragment] && [scanner isAtEnd]) {
|
|
|
|
fragment = possibleFragment;
|
|
|
|
[unixPath deleteCharactersInRange:NSMakeRange([scanner scanLocation] - [possibleFragment length], [possibleFragment length])];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
DLog(@"Fragment: %@", fragment);
|
|
|
|
|
|
|
|
if (![unixPath hasPrefix:@"/"]) {
|
|
|
|
//Only relative paths would have windows backslashes.
|
|
|
|
[unixPath replaceOccurrencesOfString:@"\\" withString:@"/" options:0 range:NSMakeRange(0, [unixPath length])];
|
|
|
|
|
|
|
|
NSString *basePath = [[[baseFilename stringByStandardizingPath] stringByDeletingLastPathComponent] stringByAppendingString:@"/"];
|
|
|
|
|
|
|
|
[unixPath insertString:basePath atIndex:0];
|
|
|
|
}
|
|
|
|
|
|
|
|
//Append the fragment
|
|
|
|
NSURL *url = [NSURL URLWithString:[[[NSURL fileURLWithPath:unixPath] absoluteString] stringByAppendingString:fragment]];
|
|
|
|
return url;
|
2013-10-09 15:45:16 +00:00
|
|
|
}
|
|
|
|
|
2021-01-30 23:14:08 +00:00
|
|
|
+ (NSDictionary *)entriesForContainerURL:(NSURL *)url {
|
|
|
|
if (![url isFileURL])
|
|
|
|
return nil;
|
|
|
|
|
|
|
|
NSError *error;
|
|
|
|
|
|
|
|
NSString *filename = [url path];
|
|
|
|
|
|
|
|
NSString *contents = [NSString stringWithContentsOfFile:filename
|
|
|
|
encoding:NSUTF8StringEncoding
|
|
|
|
error:&error];
|
|
|
|
if (!contents) {
|
|
|
|
ALog(@"Error: %@", error);
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
NSData *plistData = [contents dataUsingEncoding:NSUTF8StringEncoding];
|
|
|
|
|
2013-10-09 15:45:16 +00:00
|
|
|
NSPropertyListFormat format;
|
2021-01-30 23:14:08 +00:00
|
|
|
id plist = [NSPropertyListSerialization propertyListWithData:plistData
|
|
|
|
options:NSPropertyListImmutable
|
|
|
|
format:&format
|
|
|
|
error:&error];
|
|
|
|
if (!plist) {
|
|
|
|
ALog(@"Error: %@", error);
|
2013-10-09 15:45:16 +00:00
|
|
|
return nil;
|
|
|
|
}
|
2021-01-30 23:14:08 +00:00
|
|
|
|
2013-10-09 23:14:23 +00:00
|
|
|
BOOL isArray = [plist isKindOfClass:[NSArray class]];
|
|
|
|
BOOL isDict = [plist isKindOfClass:[NSDictionary class]];
|
2021-01-30 23:14:08 +00:00
|
|
|
|
2013-10-09 23:14:23 +00:00
|
|
|
if (!isDict && !isArray) return nil;
|
2021-01-30 23:14:08 +00:00
|
|
|
|
|
|
|
NSArray *items;
|
|
|
|
NSDictionary *albumArt;
|
|
|
|
NSArray *queueList;
|
|
|
|
if (isArray) {
|
|
|
|
items = (NSArray *) plist;
|
|
|
|
albumArt = nil;
|
2022-01-19 02:12:57 +00:00
|
|
|
queueList = @[];
|
2021-01-30 23:14:08 +00:00
|
|
|
} else {
|
|
|
|
NSDictionary *dict = (NSDictionary *) plist;
|
|
|
|
items = dict[@"items"];
|
|
|
|
albumArt = dict[@"albumArt"];
|
|
|
|
queueList = dict[@"queue"];
|
|
|
|
}
|
|
|
|
|
|
|
|
NSMutableArray *entries = [NSMutableArray array];
|
|
|
|
|
|
|
|
for (NSDictionary *entry in items) {
|
|
|
|
NSMutableDictionary *preparedEntry = [NSMutableDictionary dictionaryWithDictionary:entry];
|
|
|
|
|
|
|
|
preparedEntry[@"URL"] = [self urlForPath:preparedEntry[@"URL"] relativeTo:filename];
|
|
|
|
|
|
|
|
if (albumArt && preparedEntry[@"albumArt"])
|
|
|
|
preparedEntry[@"albumArt"] = albumArt[preparedEntry[@"albumArt"]];
|
|
|
|
|
2013-10-09 15:45:16 +00:00
|
|
|
[entries addObject:[NSDictionary dictionaryWithDictionary:preparedEntry]];
|
2021-01-30 23:14:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return @{@"entries": entries, @"queue": queueList};
|
2013-10-09 15:45:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|