Wednesday, August 05, 2009

AS3 URI parser and code sequence to detect local file versus valid moniker

Sometimes when passing a file path to a SWF you would like to perform FileExistabce check before passing the file to an AS3 class. To do so you want to know if a URI is a file or an http URL or any other URI with specific protocol (moniker).
The following code will tell you if you are dealing with a local full or relative path.



var strURI:String;
var semicolumn:int = -1;
var bRemote:Boolean = false;
var bFileURI:Boolean = false;
var bUnknownURI:Boolean = false;
var bLocalFileExist:Boolean = false;
var uri:Object;
var p:String;
var strAuthority:String;
var strRelative:String;
var strProtocol:String;
var strFile:String;
var strQuery:String;
var strPath:String;
var strHost:String;
var strDirectory:String;
var strPort:String;
var strPassword:String;
var strUser:String;
var strUserInfo:String;
var strSource:String;
var strAnchor:String;
var _strURI:String = "c:\\folder\\subfolder\\test.flv";

uri = parseUri(_strURI);
for (p in uri)
{
switch (p)
{
case "authority":
strAuthority = uri[p];
break;
case "relative":
strRelative = uri[p];
break;
case "file":
strFile = uri[p];
break;
case "query":
strQuery = uri[p];
break;
case "path":
strPath = uri[p];
break;
case "host":
strHost = uri[p];
break;
case "directory":
strDirectory = uri[p];
break;
case "port":
strPort = uri[p];
break;
case "protocol":
strProtocol = uri[p];
break;
case "password":
strPassword = uri[p];
break;
case "user":
strUser = uri[p];
break;
case "userInfo":
strUserInfo = uri[p];
break;
case "source":
strSource = uri[p];
break;
case "anchor":
strAnchor = uri[p];
break;
default :
//trace(p + ": " + uri[p]);
break;
}
}

if ( (strProtocol.toLowerCase() == "http") ||
(strProtocol.toLowerCase().substr(0, 4) == "rtmp") )
bRemote = true;

if (strProtocol.toLowerCase() == "file")
bFileURI = true;

if (bRemote == false && bFileURI == false && strProtocol.toLowerCase() == "")
{
bUnknownURI = true;
// we have an unknown file moniker
// probably relative path we may have to reject
}

if (bUnknownURI || bFileURI)
{

semicolumn = _strVideoURL.lastIndexOf(":");
if ( (semicolumn>=0) && bFileURI ) {
strURI = _strVideoURL;
}
else
{
strURI = "file://" + _strVideoURL;
}

trace("URI = " + strURI);
}


public function parseUri(str:String, strictMode:Boolean=false):Object {
var o:Object = new Object();
o.strictMode = strictMode;
o.key = new Array("source","protocol","authority","userInfo","user","password","host","port","relative","path","directory","file","query","anchor");
o.q = new Object();
o.q.name = "queryKey";
o.q.parser = /(?:^|&)([^&=]*)=?([^&]*)/g
o.parser = new Object();
o.parser.strict = /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/
o.parser.loose = /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/

var m:Object = o.parser[o.strictMode ? "strict" : "loose"].exec(str);
var uri:Object = new Object();
var i:int = 14;
while (i--) uri[o.key[i]] = m[i] || "";
uri[o.q.name] = new Object();
uri[o.key[12]].replace(o.q.parser, function ($0, $1, $2){
if ($1) uri[o.q.name][$1] = $2;
}
);
return uri;
}


Enjoy !

No comments: