/*
 Copyright (c) 2006 Eric J. Feminella  <eric@ericfeminella.com>
 All rights reserved.

 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is furnished
 to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in all
 copies or substantial portions of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
 INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
 PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

 @internal
 */
 
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;
        }
    }
}