Home >
URI Manager Utility

Advanced Flash Tactics or AFTs are techniques that come from deep within the Flash Art Of War, the oldest Flash military treatise in the world. In this AFT I will go over how to build a - URI Manager Utility. This utility allows you to organize all of your URIs in one place. So before we get started lets talk about what a URI is:
In computing, a Uniform Resource Identifier (URI) consists of a string of characters used to identify or name a resource on the Internet. Such identification enables interaction with representations of the resource over a network (typically the World Wide Web) using specific protocols. - wikipedia
In an earlier post, I talked about using RegEx to create a utility for replacing ${tokens} in a string. We are going to take that functionality and use it every time you request a URI from the manager. The power of the TokenUtil allows use to create new URIs on the fly by replacing any tokens within them. Whether you are keeping track of 1 or 100+ URIs this simple utility will help make your life way easier.
You can get all the source code for the URIManagerUtil and an updated version of the TokenUtil here on GitHub. You can check it out through git or use the download button and get a copy of the source minus the version control resources. In the project you will find an ANT build to create a SWC, unit tests and a read me file with the following example to help you get up and running. Once you have all the files lets create a new project and begin setting it up.
Here is how to setup and run the build script:
- First, make sure you have ANT installed on your computer.
- Copy the build.template.properties file and rename it to build.properties.
- Change the path to your FLEX_HOME to where you put the Flex SDK
- The ANT file is automatically set up to run the Unit Test, generate AS Docs, and build a SWC.
Lets create an external xml file with a few URIs. I usually make this part of a master config.xml so I would call the file config.xml.
<?xml version="1.0" encoding="UTF-8"?>
<config>
<URIs handler="parseURIs">
<URI name="locations"><![CDATA[xml/locations/${filename}.xml]]></URI>
<URI name="fonts"><![CDATA[swfs/fonts/${filename}.swf]]></URI>
<URI name="css"><![CDATA[css/${filename}.css]]></URI>
<URI name="decalsheets"><![CDATA[xml/${filename}.xml]]></URI>
<URI name="selectContainer"><![CDATA[${location}/container/${id}/view/front]]></URI>
</URIs>
</config>
As you can see in the above xml, I have a few different URIs. They handle xml files, fonts, css, and images. I also have a special URI called selectContainer that represents an internal url my application uses to generate a clean SWFAddress url. Every external connection path I need to make gets place in this file. This is also great for 3rd party APIs such as Twitter, Facebook, a CMS, or anything else that you would load or save data from/to.
Now lets create our URIManagerUtil:
package com.flashartofwar.utils
{
/**
*
*/
public class URIManagerUtil
{
private static var __instance : URIManagerUtil;
private var uris : Array = [];
/**
* Constructor can only be called by the static instance method.
*
*/
public function URIManagerUtil(enforcer : SingletonEnforcer)
{
if (enforcer == null)
{
throw new Error( "Error: Instantiation failed: Use URIManagerUtil.instance instead." );
}
}
/**
* Creates a new instance of URIManagerUtil if one does not currently exist.
*
* @return URIManagerUtil
*/
public static function get instance() : URIManagerUtil
{
if(URIManagerUtil.__instance == null)
{
URIManagerUtil.__instance = new URIManagerUtil( new SingletonEnforcer( ) );
}
return URIManagerUtil.__instance;
}
/**
* Use this method to add a URI into the utility.
*
* @param name this is the name of the URI. You will need to use this
* when requesting a URI from the utility.
* @param path the path of the URI. You can put ${tokens} in the path
* to be used when requesting out later.
*/
public function addURI( name : String, path : String ) : void
{
uris[ name ] = path;
}
/**
* This returns a URI from the manager.
*
* @param name this is the name of the URI
* @param token this is the object that contains values to use to
* replace tokens in the url. Each property of the object will be
* tested against a ${token} with a similar name. If a match is
* found, the properties value will replace the token.
*/
public function getURI( name : String, token : Object = null ) : String
{
var uriMethod : String = "";
uriMethod = uris[ name ] ? uris[ name ] : "";
return TokenUtil.replaceTokens( uriMethod, token );
}
}
}
internal class SingletonEnforcer
{
}
The class is documented so I am not going to really go into what is going on under the hood. All you need to focus on are the following things:
- This utility is a singleton so you can access the reference of the class by calling it's instance getter. To use this class in your own code you would call URIManagerUtil.instance
- To add a URI you call addURI, it requires a name and the path.
- When you need to get a URI you simply call getURI. You will need to know the name of the URI and pass in an optional token object with values to replace any tokens you may have in the path. I am going to explain how this works next.
So lets create a simple doc class to test that this utility works. I called mine URIManagerExamples:
package
{
import com.flashartofwar.utils.URIManagerUtil;
import flash.net.URLRequest;
import flash.events.Event;
import flash.net.URLLoader;
import flash.display.Sprite;
/**
* @author jessefreeman
*/
public class URIManagerExamples extends Sprite
{
private var loader : URLLoader;
private var URIManager : URIManagerUtil;
public function URIManagerExamples()
{
loadConfig( );
}
private function loadConfig() : void
{
loader = new URLLoader( );
loader.addEventListener( Event.COMPLETE, onConfigLoad );
loader.load( new URLRequest( "xml/config.xml" ) );
}
private function onConfigLoad(event : Event) : void
{
parseURIs( XML( event.target.data ) );
}
private function parseURIs(xml : XML) : void
{
var URIList : XMLList = xml.URIs.*;
URIManager = URIManagerUtil.instance;
var URI : XML;
for each (URI in URIList)
{
URIManager.addURI( URI.@name, URI.toString( ) );
}
testURIManager( );
}
private function testURIManager() : void
{
// Test 1
trace( "locations \n", URIManager.getURI( "locations" ) + "\n", URIManager.getURI( "locations", {filename: "eastvillage"} ), "\n" );
// Test 2
trace( "fonts \n", URIManager.getURI( "fonts" ) + "\n", URIManager.getURI( "fonts", {filename: "FontLibrary"} ), "\n" );
// Test 3
trace( "selectContainer \n", URIManager.getURI( "selectContainer" ) + "\n", URIManager.getURI( "selectContainer", {location: "eastvillage", id:"1"} ), "\n" );
}
}
}
If you run the class you will see a few things get traced out:
locations
xml/locations/${filename}.xml
xml/locations/eastvillage.xml
fonts
swfs/fonts/${filename}.swf
swfs/fonts/FontLibrary.swf
selectContainer
${location}/container/${id}/view/front
eastvillage/container/1/view/front
In each trace you should see the name of the URI, its original value, and the value when the tokens have been replaced. This should be a good example of how to use this class. If you have any questions or want to talk about how you implemented it in your own project please leave a comment.




Facebook Application Development
Thanks, that looks useful.. good idea to abstract the representation of the resources from the application. Is this the reason you decided to call it URIManager instead of URLManager - to emphasize this abstraction ? I wish we could just call them URLs, as the distinction is rather academic.