Skip to content

Instantly share code, notes, and snippets.

@farcaller
Created February 9, 2009 13:46
Show Gist options
  • Save farcaller/60791 to your computer and use it in GitHub Desktop.
Save farcaller/60791 to your computer and use it in GitHub Desktop.
@import <Foundation/CPObject.j>
/*!
Helper function to load xml documents
*/
function XmlFromString(xmlData)
{
if (window.ActiveXObject) {
//for IE
xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async="false";
xmlDoc.loadXML(xmlData);
return xmlDoc;
} else if (document.implementation && document.implementation.createDocument) {
//for Mozila
parser=new DOMParser();
xmlDoc=parser.parseFromString(xmlData,"text/xml");
return xmlDoc;
}
}
/*!
Core class that manages address book. It loads given ab.xml, converts it
into plist and stores in contacts.
*/
@implementation ABManager : CPObject
{
CPArray _contacts;
}
/*!
Used to initialize ABController
@param path path to XML document with contacts data
*/
- (id)initWithXML:(CPString)path
{
self = [super init];
var xml = [CPURLConnection
sendSynchronousRequest:[CPURLRequest requestWithURL:path]
returningResponse:nil
error:nil];
if(xml == nil)
return nil;
var xslt = [CPURLConnection
sendSynchronousRequest:[CPURLRequest requestWithURL:@"abplist.xsl"]
returningResponse:nil
error:nil];
if(xslt == nil)
return nil;
// now some js crap to convert xml :)
var xmlDoc = XmlFromString([xml string]);
var xsltDoc = XmlFromString([xslt string]);
var plist = nil;
if (window.ActiveXObject) {
plist = xmlDoc.transformNode(xsltDoc);
plist = plist.xml;
} else if (document.implementation&& document.implementation.createDocument) {
xsltProcessor = new XSLTProcessor();
xsltProcessor.importStylesheet(xsltDoc);
plist = xsltProcessor.transformToFragment(xmlDoc, document);
plist = (new XMLSerializer()).serializeToString(plist);
} else
return nil;
_contacts = [[[CPData alloc] initWithString:plist] plistObject];
return self;
}
/*!
Just a getter method
*/
- (CPArray)contacts
{
return _contacts;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment