Skip to content

Commit

Permalink
Moved utils.makeURI(), utils.makePath() & isAbsolutePath() to xjsfl.f…
Browse files Browse the repository at this point in the history
…ile, and updated dependent files
  • Loading branch information
davestewart committed Jul 5, 2011
1 parent b62b2f2 commit 543834e
Show file tree
Hide file tree
Showing 8 changed files with 144 additions and 143 deletions.
4 changes: 2 additions & 2 deletions core/jsfl/libraries/config.jsfl
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
//TODO decide if this is what we want. Should null configs be allowed?
if(uri == null)
{
uri = xjsfl.utils.makeURI('user/config/' + configPath + '.xml');
uri = xjsfl.file.makeURI('user/config/' + configPath + '.xml');
}
}

Expand Down Expand Up @@ -164,7 +164,7 @@

toString:function(asXML)
{
var path = this.file ? xjsfl.utils.makePath(this.file.uri, true) : '';
var path = this.file ? xjsfl.file.makePath(this.file.uri, true) : '';
var nodes = this.xml ? this.xml.*.length() : 0;
return asXML ? this.xml.toXMLString() : '[object Config path="' +path+ '" nodes=' +nodes+ ']';
}
Expand Down
4 changes: 2 additions & 2 deletions core/jsfl/libraries/filesystem.jsfl
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

if(pathOrUri)
{
this.uri = xjsfl.utils.makeURI(pathOrUri);
this.uri = xjsfl.file.makeURI(pathOrUri);
}
if(this.uri)
{
Expand Down Expand Up @@ -505,7 +505,7 @@
{
uriCopy = uriCopy.replace(/\/*$/, '/') + this.name;
}
uriCopy = xjsfl.utils.makeURI(uriCopy);
uriCopy = xjsfl.file.makeURI(uriCopy);

// remove target if file should overwrite
if(overWrite)
Expand Down
4 changes: 2 additions & 2 deletions core/jsfl/libraries/module.jsfl
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
this.name = name;
this.uri = xjsfl.uri + 'modules/' + escape(this.name) + '/';

// register module so path is added to global paths before config is created
// register module so the module path is added to global paths before config is created
xjsfl.modules.register(this);

// instantiate settings and data
Expand Down Expand Up @@ -66,7 +66,7 @@

// accessors
get key(){ return this.name.toLowerCase().replace(/\W/g, ''); },
get path(){ return xjsfl.utils.makePath(this.uri, true); },
get path(){ return xjsfl.file.makePath(this.uri, true); },

// methods
getURI:function(folder, file)
Expand Down
2 changes: 1 addition & 1 deletion core/jsfl/libraries/template.jsfl
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@
}
else
{
xjsfl.output.warn('The template "' +xjsfl.utils.makePath(uri, true)+ '" was not saved');
xjsfl.output.warn('The template "' +xjsfl.file.makePath(uri, true)+ '" was not saved');
}
}
else
Expand Down
263 changes: 132 additions & 131 deletions core/jsfl/libraries/xjsfl.jsfl
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
function()
{
var stack = xjsfl.utils.getStack();
return xjsfl.utils.makeURI(stack[3].path);
return xjsfl.file.makeURI(stack[3].path);
}
);

Expand Down Expand Up @@ -351,129 +351,6 @@
return String(string || '').replace(/(^\s*|\s*$)/g, '');
},

/**
* Create a valid URI from a supplied string
* Function has the same internal functionality as makePath()
*
* @param str {String} An absolute path, relative path, or uri
* @param context {String} An optional context (uri or path), from which to start the URI
* @param context {Boolean} An alternative optional Boolean indicating to automatically derive the URI from the calling function's file location
* @returns {String} An absolute URI
* @see xjsfl.utils#makePath
*/
makeURI:function(str, context)
{
// if str is already a URI, no need to convert so return immediately
if(str.indexOf('file:') == 0)
{
return str;
}

// if an additional filepath is passed in, the returned URI will be relative to it
if(typeof context === 'string')
{
context = context.replace(/[^\/\\]+$/, '');
str = xjsfl.utils.makePath(context) + str;
}

// if context is true, then the returned URI will be relative to the calling script
// if str is true, the returned URI will be the folder of the calling script
else if(context === true || str === true)
{
var stack = xjsfl.utils.getStack();
var path = stack[1].path;
str = xjsfl.utils.makePath(path) + (str === true ? '' : str);
}

//TODO IMPORTANT! Throw error / passback false on empty string
//TODO If an empty string is passed back, the system assumes the URI is the root. This could be dangerous (especialy if files are to be deleted!) so consider throwing an error, or passing back xJSFL core
// Also, if a recursive operation is to be called, this could freeze flash if too many files

// return the final URI using the system FLfile commands
return str ? FLfile.platformPathToURI(xjsfl.utils.makePath(str)) : '';
},


/**
* Create a valid path from a supplied string
*
* Function will:
*
* - convert file:/// to paths
* - convert {xjsfl} and {config} tokens
* - convert relative paths to absolute from xJSFL folder
* - replace multiple / and \ with /
* - resolve ../ tokens to correct parent folder
*
* @param str {String} An absolute path, relative path, or uri
* @param shorten {Boolean} An optional boolean to return a path with {xjsfl} or {config} swapped out from the actual path
* @returns {String} An absolute or shortened path
*/
makePath:function(str, shorten)
{
// if a URI is passed in, just convert it
if(str.match(/^file:\/\/\//))
{
path = FLfile.uriToPlatformPath(String(str));
}
else
{
path = String(str);
}

// convert {config} and {xjsfl} tokens
path = path
.replace(/.*{config}/g, xjsfl.settings.folders.config)
.replace(/.*{xjsfl}/g, xjsfl.settings.folders.xjsfl);

// if a relative path is passed in, convert it to absolute from the xJSFL root
if( ! xjsfl.utils.isAbsolutePath(path))
{
path = xjsflPath + path;
}

// replace backslashes
path = path.replace(/\\+/g, '/');

// replace double-slashes
path = path.replace(/\/+/g, '/');

// resolve ../
while(path.indexOf('../') > -1)
{
path = path.replace(/\/[^\/]+\/\.\.\//, "/");
}

// optionally, shorten path
if(shorten)
{
path = path
.replace(xjsfl.settings.folders.config, 'Configuration/')
.replace(xjsfl.settings.folders.xjsfl, 'xJSFL/');
}

// return
return path
},

/**
* Checks if a path is absolute or not
*
* @param path {String} The path to the file
* @returns {Boolean} True (absolute) or False (relative)
*/
isAbsolutePath:function(path)
{
if(xjsfl.settings.platform == 'mac')
{
return path.substr(0, 1).replace('\\', '/') == '/';
}
else
{
return path.match(/^[A-Z]:/i);
}
},

/**
* Checks if the object is an array or not
*
Expand Down Expand Up @@ -1009,8 +886,8 @@
}

// template uris
var uriErrors = xjsfl.utils.makeURI('core/config/templates/errors/errors.txt');
var uriError = xjsfl.utils.makeURI('core/config/templates/errors/error.txt');
var uriErrors = xjsfl.file.makeURI('core/config/templates/errors/errors.txt');
var uriError = xjsfl.file.makeURI('core/config/templates/errors/error.txt');

// build errors
var content = '';
Expand Down Expand Up @@ -1141,7 +1018,7 @@
// check all paths for files
for(var i = 0; i < paths.length; i++)
{
var uri = xjsfl.utils.makeURI(paths[i] + path);
var uri = xjsfl.file.makeURI(paths[i] + path);
if(FLfile.exists(uri))
{
uris.push(uri);
Expand Down Expand Up @@ -1212,7 +1089,7 @@
if(name == null || name === true || name === false)
{
var path = type;
var uri = xjsfl.utils.makeURI(path);
var uri = xjsfl.file.makeURI(path);
result = FLfile.exists(uri) ? uri : null;
}

Expand Down Expand Up @@ -1251,7 +1128,7 @@
{
// variables
var uri = uris[i];
var path = xjsfl.utils.makePath(uri, true);
var path = xjsfl.file.makePath(uri, true);
var ext = uri.match(/(\w+)$/)[1];

// debug
Expand Down Expand Up @@ -1299,7 +1176,7 @@
// if the type was a module, ensure any panels are copied to the WindowSWF folder
if(type.match(/modules?/))
{
var folder = new xjsfl.classes.Folder(xjsfl.utils.makeURI('modules/' + name + '/ui/'));
var folder = new xjsfl.classes.Folder(xjsfl.file.makeURI('modules/' + name + '/ui/'));
for each(var file in folder.contents)
{
if(file.extension == 'swf')
Expand Down Expand Up @@ -1329,6 +1206,130 @@

// return
return this;
},


/**
* Create a valid URI from a supplied string
* Function has the same internal functionality as makePath()
*
* @param str {String} An absolute path, relative path, or uri
* @param context {String} An optional context (uri or path), from which to start the URI
* @param context {Boolean} An alternative optional Boolean indicating to automatically derive the URI from the calling function's file location
* @returns {String} An absolute URI
* @see xjsfl.file.makePath
*/
makeURI:function(str, context)
{
// if str is already a URI, no need to convert so return immediately
if(str.indexOf('file:') == 0)
{
return str;
}

// if an additional filepath is passed in, the returned URI will be relative to it
if(typeof context === 'string')
{
context = context.replace(/[^\/\\]+$/, '');
str = xjsfl.file.makePath(context) + str;
}

// if context is true, then the returned URI will be relative to the calling script
// if str is true, the returned URI will be the folder of the calling script
else if(context === true || str === true)
{
var stack = xjsfl.utils.getStack();
var path = stack[1].path;
str = xjsfl.file.makePath(path) + (str === true ? '' : str);
}

//TODO IMPORTANT! Throw error / passback false on empty string
//TODO If an empty string is passed back, the system assumes the URI is the root. This could be dangerous (especialy if files are to be deleted!) so consider throwing an error, or passing back xJSFL core
// Also, if a recursive operation is to be called, this could freeze flash if too many files

// return the final URI using the system FLfile commands
return str ? FLfile.platformPathToURI(xjsfl.file.makePath(str)) : '';
},


/**
* Create a valid path from a supplied string
*
* Function will:
*
* - convert file:/// to paths
* - convert {xjsfl} and {config} tokens
* - convert relative paths to absolute from xJSFL folder
* - replace multiple / and \ with /
* - resolve ../ tokens to correct parent folder
*
* @param str {String} An absolute path, relative path, or uri
* @param shorten {Boolean} An optional boolean to return a path with {xjsfl} or {config} swapped out from the actual path
* @returns {String} An absolute or shortened path
*/
makePath:function(str, shorten)
{
// if a URI is passed in, just convert it
if(str.match(/^file:\/\/\//))
{
path = FLfile.uriToPlatformPath(String(str));
}
else
{
path = String(str);
}

// convert {config} and {xjsfl} tokens
path = path
.replace(/.*{config}/g, xjsfl.settings.folders.config)
.replace(/.*{xjsfl}/g, xjsfl.settings.folders.xjsfl);

// if a relative path is passed in, convert it to absolute from the xJSFL root
if( ! xjsfl.file.isAbsolutePath(path))
{
path = xjsflPath + path;
}

// replace backslashes
path = path.replace(/\\+/g, '/');

// replace double-slashes
path = path.replace(/\/+/g, '/');

// resolve ../
while(path.indexOf('../') > -1)
{
path = path.replace(/\/[^\/]+\/\.\.\//, "/");
}

// optionally, shorten path
if(shorten)
{
path = path
.replace(xjsfl.settings.folders.config, 'Configuration/')
.replace(xjsfl.settings.folders.xjsfl, 'xJSFL/');
}

// return
return path
},

/**
* Checks if a path is absolute or not
*
* @param path {String} The path to the file
* @returns {Boolean} True (absolute) or False (relative)
*/
isAbsolutePath:function(path)
{
if(xjsfl.settings.platform == 'mac')
{
return path.substr(0, 1).replace('\\', '/') == '/';
}
else
{
return path.match(/^[A-Z]:/i);
}
}

}
Expand Down Expand Up @@ -1551,7 +1552,7 @@
.replace(/xjsfl.ui.handleEvent\(0,/g, 'xjsfl.ui.handleEvent(' +xul.id+ ',');

// save XML to dialog.xml
var uri = xul.uri || xjsfl.utils.makeURI('core/ui/dialog.xml');
var uri = xul.uri || xjsfl.file.makeURI('core/ui/dialog.xml');
new File(uri, xml);

// register XUL
Expand Down
Loading

0 comments on commit 543834e

Please sign in to comment.