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"
|
|
|
|
|
|
|
|
#import <PlaylistEntry.h>
|
|
|
|
|
2013-10-11 12:03:55 +00:00
|
|
|
#import "Logging.h"
|
|
|
|
|
2013-10-09 15:45:16 +00:00
|
|
|
@implementation XmlContainer
|
|
|
|
|
|
|
|
+ (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;
|
|
|
|
}
|
|
|
|
}
|
2013-10-11 12:03:55 +00:00
|
|
|
DLog(@"Fragment: %@", fragment);
|
2013-10-09 15:45:16 +00:00
|
|
|
|
|
|
|
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]];
|
|
|
|
[unixPath release];
|
|
|
|
return url;
|
|
|
|
}
|
|
|
|
|
2013-10-10 08:43:04 +00:00
|
|
|
+ (NSDictionary *)entriesForContainerURL:(NSURL *)url
|
2013-10-09 15:45:16 +00:00
|
|
|
{
|
|
|
|
if (![url isFileURL])
|
2014-12-04 06:34:13 +00:00
|
|
|
return [NSDictionary dictionary];
|
2013-10-09 15:45:16 +00:00
|
|
|
|
|
|
|
NSError *nserr;
|
|
|
|
|
|
|
|
NSString *error;
|
|
|
|
|
|
|
|
NSString *filename = [url path];
|
|
|
|
|
|
|
|
NSString * contents = [NSString stringWithContentsOfFile:filename encoding:NSUTF8StringEncoding error:&nserr];
|
|
|
|
|
|
|
|
NSData* plistData = [contents dataUsingEncoding:NSUTF8StringEncoding];
|
|
|
|
|
|
|
|
NSPropertyListFormat format;
|
2013-10-09 23:14:23 +00:00
|
|
|
id plist = [NSPropertyListSerialization propertyListFromData:plistData mutabilityOption:NSPropertyListImmutable format:&format errorDescription:&error];
|
2013-10-09 15:45:16 +00:00
|
|
|
if(!plist){
|
2013-10-11 12:03:55 +00:00
|
|
|
ALog(@"Error: %@",error);
|
2013-10-09 15:45:16 +00:00
|
|
|
[error release];
|
|
|
|
return nil;
|
|
|
|
}
|
2013-10-09 23:14:23 +00:00
|
|
|
|
|
|
|
BOOL isArray = [plist isKindOfClass:[NSArray class]];
|
|
|
|
BOOL isDict = [plist isKindOfClass:[NSDictionary class]];
|
|
|
|
|
|
|
|
if (!isDict && !isArray) return nil;
|
|
|
|
|
|
|
|
NSArray * items = (isArray) ? (NSArray*)plist : [(NSDictionary *)plist objectForKey:@"items"];
|
2013-10-09 15:45:16 +00:00
|
|
|
|
2013-10-09 23:14:23 +00:00
|
|
|
NSDictionary *albumArt = (isArray) ? nil : [(NSDictionary *)plist objectForKey:@"albumArt"];
|
2013-10-10 08:43:04 +00:00
|
|
|
|
|
|
|
NSArray *queueList = (isArray) ? [NSArray array] : [(NSDictionary *)plist objectForKey:@"queue"];
|
|
|
|
|
2013-10-09 15:45:16 +00:00
|
|
|
NSMutableArray *entries = [NSMutableArray array];
|
|
|
|
|
2013-10-10 08:43:04 +00:00
|
|
|
for (NSDictionary *entry in items)
|
2013-10-09 15:45:16 +00:00
|
|
|
{
|
|
|
|
NSMutableDictionary * preparedEntry = [NSMutableDictionary dictionaryWithDictionary:entry];
|
|
|
|
|
|
|
|
[preparedEntry setObject:[self urlForPath:[preparedEntry objectForKey:@"URL"] relativeTo:filename] forKey:@"URL"];
|
|
|
|
|
2013-10-09 23:14:23 +00:00
|
|
|
if (albumArt && [preparedEntry objectForKey:@"albumArt"])
|
|
|
|
[preparedEntry setObject:[albumArt objectForKey:[preparedEntry objectForKey:@"albumArt"]] forKey:@"albumArt"];
|
|
|
|
|
2013-10-09 15:45:16 +00:00
|
|
|
[entries addObject:[NSDictionary dictionaryWithDictionary:preparedEntry]];
|
|
|
|
}
|
2013-10-10 08:43:04 +00:00
|
|
|
|
|
|
|
return [NSDictionary dictionaryWithObjectsAndKeys:entries, @"entries", queueList, @"queue", nil];
|
2013-10-09 15:45:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|