package com.ericfeminella.net 
{
    import com.adobe.net.URI;
    import flash.net.navigateToURL;
    import flash.net.URLRequest;
    import mx.utils.StringUtil;
    /**
     * 
     * All static class which Provides an API for loading a URI based 
     * on a particular scheme
     * 
     * @see com.adobe.net.URI
     * @see flash.net.navigateToURL
     * 
     */
    public final class URIRequestHelper 
    {
        /**
         * Defines the Exception message for an invalid URI protocol
         */        
        private static const SCHEME_ERROR:String = "Invalid URI protocol encountered. Expected {0}";
        
        /**
         * 
         * Loads a linked document to the specified target location
         * 
         * @param rawURI which has not been pre-processed or validated 
         *        against a particular scheme
         * @param target in which the document is to load
         * 
         */
        public static function getURI(rawURI:String, target:String = "_blank") : void
        {
            var request:URLRequest = new URLRequest(rawURI);
            navigateToURL(request, target);
        }
        
        /**
         * 
         * Handles loading a linked document to a specified target based 
         * on a specific protocol scheme such as http, ftp etc
         * 
         * @param  rawURI which has not been validated
         * @param  target in which the document is to load
         * @return valid com.adobe.net.URI object
         * 
         */
        public static function getSchemeBasedURI(rawURI:String, target:String = "_blank", protocolScheme:String = "http") : void
        {
            var uri:URI = new URI( rawURI );
            
            if ( uri.isOfType(protocolScheme) )
            {
                var request:URLRequest = new URLRequest(rawURI);
                navigateToURL(request, target);
            }
            else
            {
                throw new Error( StringUtil.substitute(SCHEME_ERROR, protocolScheme) );
            }
        }
        
        /**
         * 
         * Creates a new <code>URI</code> object and returns based
         * on the specified URL
         * 
         * @param  rawURI in which to create the URI object
         * @return valid com.adobe.net.URI object
         * 
         */
        public static function getURIClassObject(rawURI:String) : URI
        {
            var uri:URI = new URI( rawURI );
            return uri;
        }
    }
}