This article is about an alternative for the URLVariables class in the flash.net namespace.
In the current situation one can create a URLVariables object by passing a parameter string to it. The URLVariables object will call the method decode() to parse the given string to name-value pairs. The toString() method will return the name-value pairs as a parameter string (the order of the parameters seems to be randomized). Everything works fine untill one needs to interact with the name-value pairs. There is no way you can retrieve the names of the pairs that are held by the URLVariables object. Also there is no way to influence the randomized order you’re getting the toString() result in. Especially for cache managers like Akamai, randomly placed parameters is a hassle, since it will treat each URL as a unique one although the URL’s content is the same.
This is why I’ve created my own little workaround for this, because I needed to interact with the available name-value pairs and create new ones if needed. Also I needed the toString() method to return a parameter string which was alphabetically sorted. This is why I came up with the following two classes URLParameters and URLParameter (the code can be seen below).
package com.ciber.framework.url
{
/**
* This class provided means to store URL parameters based upon a given String
*
* @author CIBER Netherlands
* @author geert.zijlmans
*/
public class URLParameters
{
/** The delimiter used between the objects */
public var delimiter : String = "&";
/** The url parameter objects */
public var urlParameters : Array = [];
/**
* Constructor
* @param parameters String - The given String representation of the parameters.
* @param delimiter String - The delimiter used between the parameters.
*/
public function URLParameters(parameters : String, delimiter: String)
{
this.delimiter = delimiter;
var array : Array = parameters.split(this.delimiter);
for each (var parameter : String in array)
{
var urlParameter : URLParameter = new URLParameter(parameter);
urlParameters.push(urlParameter);
}
urlParameters.sortOn("name");
}
/**
* This method outputs the parameter String for the URLparameters
*/
public function toString():String
{
var s : String = "";
for (var i : int = 0; i < urlParameters.length; i++)
{
var urlParameter : URLParameter = urlParameters[i] as URLParameter;
if (0 < i)
s+= delimiter;
s += urlParameter.toString();
}
return s;
}
/**
* This method returns the value of the parameter.
* @param name String - The name of the parameter.
* @return String - The value of the parameter, otherwise null.
*/
public function getParameter(name : String):String
{
for each (var urlParameter : URLParameter in urlParameters)
{
if (urlParameter.name == name)
{
return urlParameter.value;
}
}
return null;
}
/**
* This method sets the value of the existing parameter or creates one for it.
* @param name String - The name of the parameter.
* @param value String - The value of the parameter.
*/
public function setParameter(name : String, value : String):void
{
var urlParameter : URLParameter;
var found : Boolean = false;
for each (urlParameter in urlParameters)
{
if (urlParameter.name == name)
{
urlParameter.value = value;
found = true;
break;
}
}
if (!found)
{
urlParameter = new URLParameter(name + "=" + value);
urlParameters.push(urlParameter);
urlParameters.sortOn("name");
}
}
}
}
Code 1: URLParameters.as
package com.ciber.framework.url
{
/**
* This class provided means to store a URL parameter
*
* @author CIBER Netherlands
* @author geert.zijlmans
*/
public class URLParameter
{
/** The delimiter between name and value */
private static const DELIMITER : String = "=";
/** The name of the URL parameter */
public var name : String;
/** The value of the URL parameter */
public var value : String;
/**
* Constructor
* @param name String - This is the name of the parameter.
* @param value String - This is the value of the parameter.
*/
public function URLParameter(parameter : String)
{
var array : Array = parameter.split(DELIMITER);
this.name = array[0];
this.value = array[1];
}
/**
* Overriding the toString() of Object.
* This method outputs the parameter's name + DELIMITER + parameter's value.
*/
public function toString():String
{
return this.name + DELIMITER + this.value;
}
}
}
Code 2: URLParameter.as
Below you’ll find an example how these two classes can be used to interact with a name-value pair in the parameter string of a given URL. The URL will be split into the base part and the parameter string.
var myPattern:RegExp = /.*\?/; var s:String = __model.xmlLocation; s = s.replace(myPattern, ""); var base : String = __model.xmlLocation; myPattern = /\?.*/; base = base.replace(myPattern, ""); var urlParameters : URLParameters = new URLParameters(s, "&"); urlParameters.setParameter(PARAMETER_SORTING, stateSorting + ""); __model.xmlLocation = base + "?" + urlParameters.toString();
Code 3: Example of the use of URLParameters class
Naturally these classes can be extended to provide more rich features. But this worked for me