-/*\r
- Copyright (c) 2008, Adobe Systems Incorporated\r
- All rights reserved.\r
-\r
- Redistribution and use in source and binary forms, with or without \r
- modification, are permitted provided that the following conditions are\r
- met:\r
-\r
- * Redistributions of source code must retain the above copyright notice, \r
- this list of conditions and the following disclaimer.\r
- \r
- * Redistributions in binary form must reproduce the above copyright\r
- notice, this list of conditions and the following disclaimer in the \r
- documentation and/or other materials provided with the distribution.\r
- \r
- * Neither the name of Adobe Systems Incorporated nor the names of its \r
- contributors may be used to endorse or promote products derived from \r
- this software without specific prior written permission.\r
-\r
- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS\r
- IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,\r
- THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR\r
- PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR \r
- CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,\r
- EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,\r
- PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\r
- PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\r
- LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING\r
- NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r
- SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
-*/\r
-\r
-package com.adobe.net\r
-{\r
- import flash.utils.ByteArray;\r
- \r
- /**\r
- * This class implements functions and utilities for working with URI's\r
- * (Universal Resource Identifiers). For technical description of the\r
- * URI syntax, please see RFC 3986 at http://www.ietf.org/rfc/rfc3986.txt\r
- * or do a web search for "rfc 3986".\r
- * \r
- * <p>The most important aspect of URI's to understand is that URI's\r
- * and URL's are not strings. URI's are complex data structures that\r
- * encapsulate many pieces of information. The string version of a\r
- * URI is the serialized representation of that data structure. This\r
- * string serialization is used to provide a human readable\r
- * representation and a means to transport the data over the network\r
- * where it can then be parsed back into its' component parts.</p>\r
- * \r
- * <p>URI's fall into one of three categories:\r
- * <ul>\r
- * <li><scheme>:<scheme-specific-part>#<fragment> (non-hierarchical)</li>\r
- * <li><scheme>:<authority><path>?<query>#<fragment> (hierarchical)</li>\r
- * <li><path>?<query>#<fragment> (relative hierarchical)</li>\r
- * </ul></p>\r
- * \r
- * <p>The query and fragment parts are optional.</p>\r
- * \r
- * <p>This class supports both non-hierarchical and hierarchical URI's</p>\r
- * \r
- * <p>This class is intended to be used "as-is" for the vast majority\r
- * of common URI's. However, if your application requires a custom\r
- * URI syntax (e.g. custom query syntax or special handling of\r
- * non-hierarchical URI's), this class can be fully subclassed. If you\r
- * intended to subclass URI, please see the source code for complete\r
- * documation on protected members and protected fuctions.</p>\r
- * \r
- * @langversion ActionScript 3.0\r
- * @playerversion Flash 9.0 \r
- */\r
- public class URI\r
- { \r
- // Here we define which characters must be escaped for each\r
- // URI part. The characters that must be escaped for each\r
- // part differ depending on what would cause ambiguous parsing.\r
- // RFC 3986 sec. 2.4 states that characters should only be\r
- // encoded when they would conflict with subcomponent delimiters.\r
- // We don't want to over-do the escaping. We only want to escape\r
- // the minimum needed to prevent parsing problems.\r
- \r
- // space and % must be escaped in all cases. '%' is the delimiter\r
- // for escaped characters.\r
- public static const URImustEscape:String = " %";\r
- \r
- // Baseline of what characters must be escaped\r
- public static const URIbaselineEscape:String = URImustEscape + ":?#/@";\r
- \r
- // Characters that must be escaped in the part part.\r
- public static const URIpathEscape:String = URImustEscape + "?#";\r
- \r
- // Characters that must be escaped in the query part, if setting\r
- // the query as a whole string. If the query is set by\r
- // name/value, URIqueryPartEscape is used instead.\r
- public static const URIqueryEscape:String = URImustEscape + "#";\r
- \r
- // This is what each name/value pair must escape "&=" as well\r
- // so they don't conflict with the "param=value¶m2=value2"\r
- // syntax.\r
- public static const URIqueryPartEscape:String = URImustEscape + "#&=";\r
- \r
- // Non-hierarchical URI's can have query and fragment parts, but\r
- // we also want to prevent '/' otherwise it might end up looking\r
- // like a hierarchical URI to the parser.\r
- public static const URInonHierEscape:String = URImustEscape + "?#/";\r
- \r
- // Baseline uninitialized setting for the URI scheme.\r
- public static const UNKNOWN_SCHEME:String = "unknown";\r
- \r
- // The following bitmaps are used for performance enhanced\r
- // character escaping.\r
- \r
- // Baseline characters that need to be escaped. Many parts use\r
- // this.\r
- protected static const URIbaselineExcludedBitmap:URIEncodingBitmap =\r
- new URIEncodingBitmap(URIbaselineEscape);\r
- \r
- // Scheme escaping bitmap\r
- protected static const URIschemeExcludedBitmap:URIEncodingBitmap = \r
- URIbaselineExcludedBitmap;\r
- \r
- // User/pass escaping bitmap\r
- protected static const URIuserpassExcludedBitmap:URIEncodingBitmap =\r
- URIbaselineExcludedBitmap;\r
- \r
- // Authority escaping bitmap\r
- protected static const URIauthorityExcludedBitmap:URIEncodingBitmap =\r
- URIbaselineExcludedBitmap;\r
- \r
- // Port escaping bitmap\r
- protected static const URIportExludedBitmap:URIEncodingBitmap = \r
- URIbaselineExcludedBitmap;\r
- \r
- // Path escaping bitmap\r
- protected static const URIpathExcludedBitmap:URIEncodingBitmap =\r
- new URIEncodingBitmap(URIpathEscape);\r
- \r
- // Query (whole) escaping bitmap\r
- protected static const URIqueryExcludedBitmap:URIEncodingBitmap =\r
- new URIEncodingBitmap(URIqueryEscape);\r
- \r
- // Query (individual parts) escaping bitmap\r
- protected static const URIqueryPartExcludedBitmap:URIEncodingBitmap =\r
- new URIEncodingBitmap(URIqueryPartEscape);\r
- \r
- // Fragments are the last part in the URI. They only need to\r
- // escape space, '#', and '%'. Turns out that is what query\r
- // uses too.\r
- protected static const URIfragmentExcludedBitmap:URIEncodingBitmap =\r
- URIqueryExcludedBitmap;\r
- \r
- // Characters that need to be escaped in the non-hierarchical part\r
- protected static const URInonHierexcludedBitmap:URIEncodingBitmap =\r
- new URIEncodingBitmap(URInonHierEscape);\r
- \r
- // Values used by getRelation()\r
- public static const NOT_RELATED:int = 0;\r
- public static const CHILD:int = 1;\r
- public static const EQUAL:int = 2;\r
- public static const PARENT:int = 3;\r
-\r
- //-------------------------------------------------------------------\r
- // protected class members\r
- //-------------------------------------------------------------------\r
- protected var _valid:Boolean = false;\r
- protected var _relative:Boolean = false;\r
- protected var _scheme:String = "";\r
- protected var _authority:String = "";\r
- protected var _username:String = "";\r
- protected var _password:String = "";\r
- protected var _port:String = "";\r
- protected var _path:String = "";\r
- protected var _query:String = "";\r
- protected var _fragment:String = "";\r
- protected var _nonHierarchical:String = "";\r
- protected static var _resolver:IURIResolver = null;\r
-\r
-\r
- /**\r
- * URI Constructor. If no string is given, this will initialize\r
- * this URI object to a blank URI.\r
- */\r
- public function URI(uri:String = null) : void \r
- {\r
- if (uri == null)\r
- initialize();\r
- else\r
- constructURI(uri);\r
- }\r
-\r
- \r
- /**\r
- * @private\r
- * Method that loads the URI from the given string.\r
- */\r
- protected function constructURI(uri:String) : Boolean\r
- {\r
- if (!parseURI(uri))\r
- _valid = false;\r
- \r
- return isValid();\r
- }\r
- \r
- \r
- /**\r
- * @private Private initializiation.\r
- */\r
- protected function initialize() : void\r
- {\r
- _valid = false;\r
- _relative = false;\r
- \r
- _scheme = UNKNOWN_SCHEME;\r
- _authority = "";\r
- _username = "";\r
- _password = "";\r
- _port = "";\r
- _path = "";\r
- _query = "";\r
- _fragment = "";\r
- \r
- _nonHierarchical = "";\r
- } \r
- \r
- /**\r
- * @private Accessor to explicitly set/get the hierarchical\r
- * state of the URI.\r
- */\r
- protected function set hierState(state:Boolean) : void\r
- {\r
- if (state)\r
- {\r
- // Clear the non-hierarchical data\r
- _nonHierarchical = "";\r
- \r
- // Also set the state vars while we are at it\r
- if (_scheme == "" || _scheme == UNKNOWN_SCHEME)\r
- _relative = true;\r
- else\r
- _relative = false;\r
- \r
- if (_authority.length == 0 && _path.length == 0)\r
- _valid = false;\r
- else\r
- _valid = true;\r
- }\r
- else\r
- {\r
- // Clear the hierarchical data\r
- _authority = "";\r
- _username = "";\r
- _password = "";\r
- _port = "";\r
- _path = "";\r
- \r
- _relative = false;\r
- \r
- if (_scheme == "" || _scheme == UNKNOWN_SCHEME)\r
- _valid = false;\r
- else\r
- _valid = true;\r
- }\r
- }\r
- protected function get hierState() : Boolean\r
- {\r
- return (_nonHierarchical.length == 0);\r
- }\r
- \r
- \r
- /**\r
- * @private Functions that performs some basic consistency validation.\r
- */\r
- protected function validateURI() : Boolean\r
- {\r
- // Check the scheme\r
- if (isAbsolute())\r
- {\r
- if (_scheme.length <= 1 || _scheme == UNKNOWN_SCHEME)\r
- {\r
- // we probably parsed a C:\ type path or no scheme\r
- return false;\r
- }\r
- else if (verifyAlpha(_scheme) == false)\r
- return false; // Scheme contains bad characters\r
- }\r
- \r
- if (hierState)\r
- {\r
- if (_path.search('\\') != -1)\r
- return false; // local path\r
- else if (isRelative() == false && _scheme == UNKNOWN_SCHEME)\r
- return false; // It's an absolute URI, but it has a bad scheme\r
- }\r
- else\r
- {\r
- if (_nonHierarchical.search('\\') != -1)\r
- return false; // some kind of local path\r
- }\r
- \r
- // Looks like it's ok.\r
- return true;\r
- }\r
- \r
- \r
- /**\r
- * @private\r
- *\r
- * Given a URI in string format, parse that sucker into its basic\r
- * components and assign them to this object. A URI is of the form:\r
- * <scheme>:<authority><path>?<query>#<fragment>\r
- *\r
- * For simplicity, we parse the URI in the following order:\r
- * \r
- * 1. Fragment (anchors)\r
- * 2. Query (CGI stuff)\r
- * 3. Scheme ("http")\r
- * 4. Authority (host name)\r
- * 5. Username/Password (if any)\r
- * 6. Port (server port if any)\r
- * 7. Path (/homepages/mypage.html)\r
- *\r
- * The reason for this order is to minimize any parsing ambiguities.\r
- * Fragments and queries can contain almost anything (they are parts\r
- * that can contain custom data with their own syntax). Parsing\r
- * them out first removes a large chance of parsing errors. This\r
- * method expects well formed URI's, but performing the parse in\r
- * this order makes us a little more tolerant of user error.\r
- * \r
- * REGEXP\r
- * Why doesn't this use regular expressions to parse the URI? We\r
- * have found that in a real world scenario, URI's are not always\r
- * well formed. Sometimes characters that should have been escaped\r
- * are not, and those situations would break a regexp pattern. This\r
- * function attempts to be smart about what it is parsing based on\r
- * location of characters relative to eachother. This function has\r
- * been proven through real-world use to parse the vast majority\r
- * of URI's correctly.\r
- *\r
- * NOTE\r
- * It is assumed that the string in URI form is escaped. This function\r
- * does not escape anything. If you constructed the URI string by\r
- * hand, and used this to parse in the URI and still need it escaped,\r
- * call forceEscape() on your URI object.\r
- *\r
- * Parsing Assumptions\r
- * This routine assumes that the URI being passed is well formed.\r
- * Passing things like local paths, malformed URI's, and the such\r
- * will result in parsing errors. This function can handle\r
- * - absolute hierarchical (e.g. "http://something.com/index.html),\r
- * - relative hierarchical (e.g. "../images/flower.gif"), or\r
- * - non-hierarchical URIs (e.g. "mailto:jsmith@fungoo.com").\r
- * \r
- * Anything else will probably result in a parsing error, or a bogus\r
- * URI object.\r
- * \r
- * Note that non-hierarchical URIs *MUST* have a scheme, otherwise\r
- * they will be mistaken for relative URI's.\r
- * \r
- * If you are not sure what is being passed to you (like manually\r
- * entered text from UI), you can construct a blank URI object and\r
- * call unknownToURI() passing in the unknown string.\r
- * \r
- * @return true if successful, false if there was some kind of\r
- * parsing error\r
- */\r
- protected function parseURI(uri:String) : Boolean\r
- {\r
- var baseURI:String = uri;\r
- var index:int, index2:int;\r
- \r
- // Make sure this object is clean before we start. If it was used\r
- // before and we are now parsing a new URI, we don't want any stale\r
- // info lying around.\r
- initialize();\r
- \r
- // Remove any fragments (anchors) from the URI\r
- index = baseURI.indexOf("#");\r
- if (index != -1)\r
- {\r
- // Store the fragment piece if any\r
- if (baseURI.length > (index + 1)) // +1 is to skip the '#'\r
- _fragment = baseURI.substr(index + 1, baseURI.length - (index + 1)); \r
- \r
- // Trim off the fragment\r
- baseURI = baseURI.substr(0, index);\r
- }\r
- \r
- // We need to strip off any CGI parameters (eg '?param=bob')\r
- index = baseURI.indexOf("?");\r
- if (index != -1)\r
- {\r
- if (baseURI.length > (index + 1))\r
- _query = baseURI.substr(index + 1, baseURI.length - (index + 1)); // +1 is to skip the '?'\r
- \r
- // Trim off the query\r
- baseURI = baseURI.substr(0, index);\r
- }\r
- \r
- // Now try to find the scheme part\r
- index = baseURI.search(':');\r
- index2 = baseURI.search('/');\r
- \r
- var containsColon:Boolean = (index != -1);\r
- var containsSlash:Boolean = (index2 != -1);\r
- \r
- // This value is indeterminate if "containsColon" is false.\r
- // (if there is no colon, does the slash come before or\r
- // after said non-existing colon?)\r
- var colonBeforeSlash:Boolean = (!containsSlash || index < index2);\r
- \r
- // If it has a colon and it's before the first slash, we will treat\r
- // it as a scheme. If a slash is before a colon, there must be a\r
- // stray colon in a path or something. In which case, the colon is\r
- // not the separator for the scheme. Technically, we could consider\r
- // this an error, but since this is not an ambiguous state (we know\r
- // 100% that this has no scheme), we will keep going.\r
- if (containsColon && colonBeforeSlash)\r
- {\r
- // We found a scheme\r
- _scheme = baseURI.substr(0, index);\r
- \r
- // Normalize the scheme\r
- _scheme = _scheme.toLowerCase();\r
- \r
- baseURI = baseURI.substr(index + 1);\r
- \r
- if (baseURI.substr(0, 2) == "//")\r
- {\r
- // This is a hierarchical URI\r
- _nonHierarchical = "";\r
- \r
- // Trim off the "//"\r
- baseURI = baseURI.substr(2, baseURI.length - 2);\r
- }\r
- else\r
- {\r
- // This is a non-hierarchical URI like "mailto:bob@mail.com"\r
- _nonHierarchical = baseURI;\r
- \r
- if ((_valid = validateURI()) == false)\r
- initialize(); // Bad URI. Clear it.\r
- \r
- // No more parsing to do for this case\r
- return isValid();\r
- }\r
- }\r
- else\r
- {\r
- // No scheme. We will consider this a relative URI\r
- _scheme = "";\r
- _relative = true;\r
- _nonHierarchical = "";\r
- }\r
- \r
- // Ok, what we have left is everything after the <scheme>://\r
- \r
- // Now that we have stripped off any query and fragment parts, we\r
- // need to split the authority from the path\r
- \r
- if (isRelative())\r
- {\r
- // Don't bother looking for the authority. It's a relative URI\r
- _authority = "";\r
- _port = "";\r
- _path = baseURI;\r
- }\r
- else\r
- {\r
- // Check for malformed UNC style file://///server/type/path/\r
- // By the time we get here, we have already trimmed the "file://"\r
- // so baseURI will be ///server/type/path. If baseURI only\r
- // has one slash, we leave it alone because that is valid (that\r
- // is the case of "file:///path/to/file.txt" where there is no\r
- // server - implicit "localhost").\r
- if (baseURI.substr(0, 2) == "//")\r
- {\r
- // Trim all leading slashes\r
- while(baseURI.charAt(0) == "/")\r
- baseURI = baseURI.substr(1, baseURI.length - 1);\r
- }\r
- \r
- index = baseURI.search('/');\r
- if (index == -1)\r
- {\r
- // No path. We must have passed something like "http://something.com"\r
- _authority = baseURI;\r
- _path = "";\r
- }\r
- else\r
- {\r
- _authority = baseURI.substr(0, index);\r
- _path = baseURI.substr(index, baseURI.length - index);\r
- }\r
- \r
- // Check to see if the URI has any username or password information.\r
- // For example: ftp://username:password@server.com\r
- index = _authority.search('@');\r
- if (index != -1)\r
- {\r
- // We have a username and possibly a password\r
- _username = _authority.substr(0, index);\r
- \r
- // Remove the username/password from the authority\r
- _authority = _authority.substr(index + 1); // Skip the '@'\r
- \r
- // Now check to see if the username also has a password\r
- index = _username.search(':');\r
- if (index != -1)\r
- {\r
- _password = _username.substring(index + 1, _username.length);\r
- _username = _username.substr(0, index);\r
- }\r
- else\r
- _password = "";\r
- }\r
- else\r
- {\r
- _username = "";\r
- _password = "";\r
- }\r
- \r
- // Lastly, check to see if the authorty has a port number.\r
- // This is parsed after the username/password to avoid conflicting\r
- // with the ':' in the 'username:password' if one exists.\r
- index = _authority.search(':');\r
- if (index != -1)\r
- {\r
- _port = _authority.substring(index + 1, _authority.length); // skip the ':'\r
- _authority = _authority.substr(0, index);\r
- }\r
- else\r
- {\r
- _port = "";\r
- }\r
- \r
- // Lastly, normalize the authority. Domain names\r
- // are case insensitive.\r
- _authority = _authority.toLowerCase();\r
- }\r
- \r
- if ((_valid = validateURI()) == false)\r
- initialize(); // Bad URI. Clear it\r
- \r
- return isValid();\r
- }\r
- \r
- \r
- /********************************************************************\r
- * Copy function.\r
- */\r
- public function copyURI(uri:URI) : void\r
- {\r
- this._scheme = uri._scheme;\r
- this._authority = uri._authority;\r
- this._username = uri._username;\r
- this._password = uri._password;\r
- this._port = uri._port;\r
- this._path = uri._path;\r
- this._query = uri._query;\r
- this._fragment = uri._fragment;\r
- this._nonHierarchical = uri._nonHierarchical;\r
- \r
- this._valid = uri._valid;\r
- this._relative = uri._relative;\r
- }\r
- \r
- \r
- /**\r
- * @private\r
- * Checks if the given string only contains a-z or A-Z.\r
- */\r
- protected function verifyAlpha(str:String) : Boolean\r
- {\r
- var pattern:RegExp = /[^a-z]/;\r
- var index:int;\r
- \r
- str = str.toLowerCase();\r
- index = str.search(pattern);\r
- \r
- if (index == -1)\r
- return true;\r
- else\r
- return false;\r
- }\r
- \r
- /**\r
- * Is this a valid URI?\r
- * \r
- * @return true if this object represents a valid URI, false\r
- * otherwise.\r
- */\r
- public function isValid() : Boolean\r
- { \r
- return this._valid;\r
- }\r
- \r
- \r
- /**\r
- * Is this URI an absolute URI? An absolute URI is a complete, fully\r
- * qualified reference to a resource. e.g. http://site.com/index.htm\r
- * Non-hierarchical URI's are always absolute.\r
- */\r
- public function isAbsolute() : Boolean\r
- { \r
- return !this._relative;\r
- }\r
- \r
- \r
- /**\r
- * Is this URI a relative URI? Relative URI's do not have a scheme\r
- * and only contain a relative path with optional anchor and query\r
- * parts. e.g. "../reports/index.htm". Non-hierarchical URI's\r
- * will never be relative.\r
- */\r
- public function isRelative() : Boolean\r
- { \r
- return this._relative;\r
- }\r
- \r
- \r
- /**\r
- * Does this URI point to a resource that is a directory/folder?\r
- * The URI specification dictates that any path that ends in a slash\r
- * is a directory. This is needed to be able to perform correct path\r
- * logic when combining relative URI's with absolute URI's to\r
- * obtain the correct absolute URI to a resource.\r
- * \r
- * @see URI.chdir\r
- * \r
- * @return true if this URI represents a directory resource, false\r
- * if this URI represents a file resource.\r
- */\r
- public function isDirectory() : Boolean\r
- {\r
- if (_path.length == 0)\r
- return false;\r
- \r
- return (_path.charAt(path.length - 1) == '/');\r
- }\r
- \r
- \r
- /**\r
- * Is this URI a hierarchical URI? URI's can be \r
- */\r
- public function isHierarchical() : Boolean\r
- { \r
- return hierState;\r
- }\r
- \r
- \r
- /**\r
- * The scheme of the URI.\r
- */\r
- public function get scheme() : String\r
- { \r
- return URI.unescapeChars(_scheme);\r
- }\r
- public function set scheme(schemeStr:String) : void\r
- {\r
- // Normalize the scheme\r
- var normalized:String = schemeStr.toLowerCase();\r
- _scheme = URI.fastEscapeChars(normalized, URI.URIschemeExcludedBitmap);\r
- }\r
- \r
- \r
- /**\r
- * The authority (host) of the URI. Only valid for\r
- * hierarchical URI's. If the URI is relative, this will\r
- * be an empty string. When setting this value, the string\r
- * given is assumed to be unescaped. When retrieving this\r
- * value, the resulting string is unescaped.\r
- */\r
- public function get authority() : String\r
- { \r
- return URI.unescapeChars(_authority);\r
- }\r
- public function set authority(authorityStr:String) : void\r
- {\r
- // Normalize the authority\r
- authorityStr = authorityStr.toLowerCase();\r
- \r
- _authority = URI.fastEscapeChars(authorityStr,\r
- URI.URIauthorityExcludedBitmap);\r
- \r
- // Only hierarchical URI's can have an authority, make\r
- // sure this URI is of the proper format.\r
- this.hierState = true;\r
- }\r
- \r
- \r
- /**\r
- * The username of the URI. Only valid for hierarchical\r
- * URI's. If the URI is relative, this will be an empty\r
- * string.\r
- * \r
- * <p>The URI specification allows for authentication\r
- * credentials to be embedded in the URI as such:</p>\r
- * \r
- * <p>http://user:passwd@host/path/to/file.htm</p>\r
- * \r
- * <p>When setting this value, the string\r
- * given is assumed to be unescaped. When retrieving this\r
- * value, the resulting string is unescaped.</p>\r
- */\r
- public function get username() : String\r
- {\r
- return URI.unescapeChars(_username);\r
- }\r
- public function set username(usernameStr:String) : void\r
- {\r
- _username = URI.fastEscapeChars(usernameStr, URI.URIuserpassExcludedBitmap);\r
- \r
- // Only hierarchical URI's can have a username.\r
- this.hierState = true;\r
- }\r
- \r
- \r
- /**\r
- * The password of the URI. Similar to username.\r
- * @see URI.username\r
- */\r
- public function get password() : String\r
- {\r
- return URI.unescapeChars(_password);\r
- }\r
- public function set password(passwordStr:String) : void\r
- {\r
- _password = URI.fastEscapeChars(passwordStr,\r
- URI.URIuserpassExcludedBitmap);\r
- \r
- // Only hierarchical URI's can have a password.\r
- this.hierState = true;\r
- }\r
- \r
- \r
- /**\r
- * The host port number. Only valid for hierarchical URI's. If\r
- * the URI is relative, this will be an empty string. URI's can\r
- * contain the port number of the remote host:\r
- * \r
- * <p>http://site.com:8080/index.htm</p>\r
- */\r
- public function get port() : String\r
- { \r
- return URI.unescapeChars(_port);\r
- }\r
- public function set port(portStr:String) : void\r
- {\r
- _port = URI.escapeChars(portStr);\r
- \r
- // Only hierarchical URI's can have a port.\r
- this.hierState = true;\r
- }\r
- \r
- \r
- /**\r
- * The path portion of the URI. Only valid for hierarchical\r
- * URI's. When setting this value, the string\r
- * given is assumed to be unescaped. When retrieving this\r
- * value, the resulting string is unescaped.\r
- * \r
- * <p>The path portion can be in one of two formats. 1) an absolute\r
- * path, or 2) a relative path. An absolute path starts with a\r
- * slash ('/'), a relative path does not.</p>\r
- * \r
- * <p>An absolute path may look like:</p>\r
- * <listing>/full/path/to/my/file.htm</listing>\r
- * \r
- * <p>A relative path may look like:</p>\r
- * <listing>\r
- * path/to/my/file.htm\r
- * ../images/logo.gif\r
- * ../../reports/index.htm\r
- * </listing>\r
- * \r
- * <p>Paths can be absolute or relative. Note that this not the same as\r
- * an absolute or relative URI. An absolute URI can only have absolute\r
- * paths. For example:</p>\r
- * \r
- * <listing>http:/site.com/path/to/file.htm</listing>\r
- * \r
- * <p>This absolute URI has an absolute path of "/path/to/file.htm".</p>\r
- * \r
- * <p>Relative URI's can have either absolute paths or relative paths.\r
- * All of the following relative URI's are valid:</p>\r
- * \r
- * <listing>\r
- * /absolute/path/to/file.htm\r
- * path/to/file.htm\r
- * ../path/to/file.htm\r
- * </listing>\r
- */\r
- public function get path() : String\r
- { \r
- return URI.unescapeChars(_path);\r
- }\r
- public function set path(pathStr:String) : void\r
- { \r
- this._path = URI.fastEscapeChars(pathStr, URI.URIpathExcludedBitmap);\r
- \r
- if (this._scheme == UNKNOWN_SCHEME)\r
- {\r
- // We set the path. This is a valid URI now.\r
- this._scheme = "";\r
- }\r
- \r
- // Only hierarchical URI's can have a path.\r
- hierState = true;\r
- }\r
- \r
- \r
- /**\r
- * The query (CGI) portion of the URI. This part is valid for\r
- * both hierarchical and non-hierarchical URI's.\r
- * \r
- * <p>This accessor should only be used if a custom query syntax\r
- * is used. This URI class supports the common "param=value"\r
- * style query syntax via the get/setQueryValue() and\r
- * get/setQueryByMap() functions. Those functions should be used\r
- * instead if the common syntax is being used.\r
- * \r
- * <p>The URI RFC does not specify any particular\r
- * syntax for the query part of a URI. It is intended to allow\r
- * any format that can be agreed upon by the two communicating hosts.\r
- * However, most systems have standardized on the typical CGI\r
- * format:</p>\r
- * \r
- * <listing>http://site.com/script.php?param1=value1¶m2=value2</listing>\r
- * \r
- * <p>This class has specific support for this query syntax</p>\r
- * \r
- * <p>This common query format is an array of name/value\r
- * pairs with its own syntax that is different from the overall URI\r
- * syntax. The query has its own escaping logic. For a query part\r
- * to be properly escaped and unescaped, it must be split into its\r
- * component parts. This accessor escapes/unescapes the entire query\r
- * part without regard for it's component parts. This has the\r
- * possibliity of leaving the query string in an ambiguious state in\r
- * regards to its syntax. If the contents of the query part are\r
- * important, it is recommended that get/setQueryValue() or\r
- * get/setQueryByMap() are used instead.</p>\r
- * \r
- * If a different query syntax is being used, a subclass of URI\r
- * can be created to handle that specific syntax.\r
- * \r
- * @see URI.getQueryValue, URI.getQueryByMap\r
- */\r
- public function get query() : String\r
- { \r
- return URI.unescapeChars(_query);\r
- }\r
- public function set query(queryStr:String) : void\r
- {\r
- _query = URI.fastEscapeChars(queryStr, URI.URIqueryExcludedBitmap);\r
- \r
- // both hierarchical and non-hierarchical URI's can\r
- // have a query. Do not set the hierState.\r
- }\r
- \r
- /**\r
- * Accessor to the raw query data. If you are using a custom query\r
- * syntax, this accessor can be used to get and set the query part\r
- * directly with no escaping/unescaping. This should ONLY be used\r
- * if your application logic is handling custom query logic and\r
- * handling the proper escaping of the query part.\r
- */\r
- public function get queryRaw() : String\r
- {\r
- return _query;\r
- }\r
- public function set queryRaw(queryStr:String) : void\r
- {\r
- _query = queryStr;\r
- }\r
-\r
-\r
- /**\r
- * The fragment (anchor) portion of the URI. This is valid for\r
- * both hierarchical and non-hierarchical URI's.\r
- */\r
- public function get fragment() : String\r
- { \r
- return URI.unescapeChars(_fragment);\r
- }\r
- public function set fragment(fragmentStr:String) : void\r
- {\r
- _fragment = URI.fastEscapeChars(fragmentStr, URIfragmentExcludedBitmap);\r
-\r
- // both hierarchical and non-hierarchical URI's can\r
- // have a fragment. Do not set the hierState.\r
- }\r
- \r
- \r
- /**\r
- * The non-hierarchical part of the URI. For example, if\r
- * this URI object represents "mailto:somebody@company.com",\r
- * this will contain "somebody@company.com". This is valid only\r
- * for non-hierarchical URI's. \r
- */\r
- public function get nonHierarchical() : String\r
- { \r
- return URI.unescapeChars(_nonHierarchical);\r
- }\r
- public function set nonHierarchical(nonHier:String) : void\r
- {\r
- _nonHierarchical = URI.fastEscapeChars(nonHier, URInonHierexcludedBitmap);\r
- \r
- // This is a non-hierarchical URI.\r
- this.hierState = false;\r
- }\r
- \r
- \r
- /**\r
- * Quick shorthand accessor to set the parts of this URI.\r
- * The given parts are assumed to be in unescaped form. If\r
- * the URI is non-hierarchical (e.g. mailto:) you will need\r
- * to call SetScheme() and SetNonHierarchical().\r
- */\r
- public function setParts(schemeStr:String, authorityStr:String,\r
- portStr:String, pathStr:String, queryStr:String,\r
- fragmentStr:String) : void\r
- {\r
- this.scheme = schemeStr;\r
- this.authority = authorityStr;\r
- this.port = portStr;\r
- this.path = pathStr;\r
- this.query = queryStr;\r
- this.fragment = fragmentStr;\r
-\r
- hierState = true;\r
- }\r
- \r
- \r
- /**\r
- * URI escapes the given character string. This is similar in function\r
- * to the global encodeURIComponent() function in ActionScript, but is\r
- * slightly different in regards to which characters get escaped. This\r
- * escapes the characters specified in the URIbaselineExluded set (see class\r
- * static members). This is needed for this class to work properly.\r
- * \r
- * <p>If a different set of characters need to be used for the escaping,\r
- * you may use fastEscapeChars() and specify a custom URIEncodingBitmap\r
- * that contains the characters your application needs escaped.</p>\r
- * \r
- * <p>Never pass a full URI to this function. A URI can only be properly\r
- * escaped/unescaped when split into its component parts (see RFC 3986\r
- * section 2.4). This is due to the fact that the URI component separators\r
- * could be characters that would normally need to be escaped.</p>\r
- * \r
- * @param unescaped character string to be escaped.\r
- * \r
- * @return escaped character string\r
- * \r
- * @see encodeURIComponent\r
- * @see fastEscapeChars\r
- */\r
- static public function escapeChars(unescaped:String) : String\r
- {\r
- // This uses the excluded set by default.\r
- return fastEscapeChars(unescaped, URI.URIbaselineExcludedBitmap);\r
- }\r
- \r
-\r
- /**\r
- * Unescape any URI escaped characters in the given character\r
- * string.\r
- * \r
- * <p>Never pass a full URI to this function. A URI can only be properly\r
- * escaped/unescaped when split into its component parts (see RFC 3986\r
- * section 2.4). This is due to the fact that the URI component separators\r
- * could be characters that would normally need to be escaped.</p>\r
- * \r
- * @param escaped the escaped string to be unescaped.\r
- * \r
- * @return unescaped string.\r
- */\r
- static public function unescapeChars(escaped:String /*, onlyHighASCII:Boolean = false*/) : String\r
- {\r
- // We can just use the default AS function. It seems to\r
- // decode everything correctly\r
- var unescaped:String;\r
- unescaped = decodeURIComponent(escaped);\r
- return unescaped;\r
- }\r
- \r
- /**\r
- * Performance focused function that escapes the given character\r
- * string using the given URIEncodingBitmap as the rule for what\r
- * characters need to be escaped. This function is used by this\r
- * class and can be used externally to this class to perform\r
- * escaping on custom character sets.\r
- * \r
- * <p>Never pass a full URI to this function. A URI can only be properly\r
- * escaped/unescaped when split into its component parts (see RFC 3986\r
- * section 2.4). This is due to the fact that the URI component separators\r
- * could be characters that would normally need to be escaped.</p>\r
- * \r
- * @param unescaped the unescaped string to be escaped\r
- * @param bitmap the set of characters that need to be escaped\r
- * \r
- * @return the escaped string.\r
- */\r
- static public function fastEscapeChars(unescaped:String, bitmap:URIEncodingBitmap) : String\r
- {\r
- var escaped:String = "";\r
- var c:String;\r
- var x:int, i:int;\r
- \r
- for (i = 0; i < unescaped.length; i++)\r
- {\r
- c = unescaped.charAt(i);\r
- \r
- x = bitmap.ShouldEscape(c);\r
- if (x)\r
- {\r
- c = x.toString(16);\r
- if (c.length == 1)\r
- c = "0" + c;\r
- \r
- c = "%" + c;\r
- c = c.toUpperCase();\r
- }\r
- \r
- escaped += c;\r
- }\r
- \r
- return escaped;\r
- }\r
-\r
- \r
- /**\r
- * Is this URI of a particular scheme type? For example,\r
- * passing "http" to a URI object that represents the URI\r
- * "http://site.com/" would return true.\r
- * \r
- * @param scheme scheme to check for\r
- * \r
- * @return true if this URI object is of the given type, false\r
- * otherwise.\r
- */\r
- public function isOfType(scheme:String) : Boolean\r
- {\r
- // Schemes are never case sensitive. Ignore case.\r
- scheme = scheme.toLowerCase();\r
- return (this._scheme == scheme);\r
- }\r
-\r
-\r
- /**\r
- * Get the value for the specified named in the query part. This\r
- * assumes the query part of the URI is in the common\r
- * "name1=value1&name2=value2" syntax. Do not call this function\r
- * if you are using a custom query syntax.\r
- * \r
- * @param name name of the query value to get.\r
- * \r
- * @return the value of the query name, empty string if the\r
- * query name does not exist.\r
- */\r
- public function getQueryValue(name:String) : String\r
- {\r
- var map:Object;\r
- var item:String;\r
- var value:String;\r
- \r
- map = getQueryByMap();\r
- \r
- for (item in map)\r
- {\r
- if (item == name)\r
- {\r
- value = map[item];\r
- return value;\r
- }\r
- }\r
- \r
- // Didn't find the specified key\r
- return new String("");\r
- }\r
- \r
-\r
- /**\r
- * Set the given value on the given query name. If the given name\r
- * does not exist, it will automatically add this name/value pair\r
- * to the query. If null is passed as the value, it will remove\r
- * the given item from the query.\r
- * \r
- * <p>This automatically escapes any characters that may conflict with\r
- * the query syntax so that they are "safe" within the query. The\r
- * strings passed are assumed to be literal unescaped name and value.</p>\r
- * \r
- * @param name name of the query value to set\r
- * @param value value of the query item to set. If null, this will\r
- * force the removal of this item from the query.\r
- */\r
- public function setQueryValue(name:String, value:String) : void\r
- {\r
- var map:Object;\r
-\r
- map = getQueryByMap();\r
- \r
- // If the key doesn't exist yet, this will create a new pair in\r
- // the map. If it does exist, this will overwrite the previous\r
- // value, which is what we want.\r
- map[name] = value;\r
- \r
- setQueryByMap(map);\r
- }\r
-\r
- \r
- /**\r
- * Get the query of the URI in an Object class that allows for easy\r
- * access to the query data via Object accessors. For example:\r
- * \r
- * <listing>\r
- * var query:Object = uri.getQueryByMap();\r
- * var value:String = query["param"]; // get a value\r
- * query["param2"] = "foo"; // set a new value\r
- * </listing>\r
- * \r
- * @return Object that contains the name/value pairs of the query.\r
- * \r
- * @see #setQueryByMap\r
- * @see #getQueryValue\r
- * @see #setQueryValue\r
- */\r
- public function getQueryByMap() : Object\r
- {\r
- var queryStr:String;\r
- var pair:String;\r
- var pairs:Array;\r
- var item:Array;\r
- var name:String, value:String;\r
- var index:int;\r
- var map:Object = new Object();\r
- \r
- \r
- // We need the raw query string, no unescaping.\r
- queryStr = this._query;\r
- \r
- pairs = queryStr.split('&');\r
- for each (pair in pairs)\r
- {\r
- if (pair.length == 0)\r
- continue;\r
- \r
- item = pair.split('=');\r
- \r
- if (item.length > 0)\r
- name = item[0];\r
- else\r
- continue; // empty array\r
- \r
- if (item.length > 1)\r
- value = item[1];\r
- else\r
- value = "";\r
- \r
- name = queryPartUnescape(name);\r
- value = queryPartUnescape(value);\r
- \r
- map[name] = value;\r
- }\r
- \r
- return map;\r
- }\r
- \r
-\r
- /**\r
- * Set the query part of this URI using the given object as the\r
- * content source. Any member of the object that has a value of\r
- * null will not be in the resulting query.\r
- * \r
- * @param map object that contains the name/value pairs as\r
- * members of that object.\r
- * \r
- * @see #getQueryByMap\r
- * @see #getQueryValue\r
- * @see #setQueryValue\r
- */\r
- public function setQueryByMap(map:Object) : void\r
- {\r
- var item:String;\r
- var name:String, value:String;\r
- var queryStr:String = "";\r
- var tmpPair:String;\r
- var foo:String;\r
- \r
- for (item in map)\r
- {\r
- name = item;\r
- value = map[item];\r
- \r
- if (value == null)\r
- value = "";\r
- \r
- // Need to escape the name/value pair so that they\r
- // don't conflict with the query syntax (specifically\r
- // '=', '&', and <whitespace>).\r
- name = queryPartEscape(name);\r
- value = queryPartEscape(value);\r
- \r
- tmpPair = name;\r
- \r
- if (value.length > 0)\r
- {\r
- tmpPair += "=";\r
- tmpPair += value;\r
- }\r
-\r
- if (queryStr.length != 0)\r
- queryStr += '&'; // Add the separator\r
- \r
- queryStr += tmpPair;\r
- }\r
- \r
- // We don't want to escape. We already escaped the\r
- // individual name/value pairs. If we escaped the\r
- // query string again by assigning it to "query",\r
- // we would have double escaping.\r
- _query = queryStr;\r
- }\r
- \r
- \r
- /**\r
- * Similar to Escape(), except this also escapes characters that\r
- * would conflict with the name/value pair query syntax. This is\r
- * intended to be called on each individual "name" and "value"\r
- * in the query making sure that nothing in the name or value\r
- * strings contain characters that would conflict with the query\r
- * syntax (e.g. '=' and '&').\r
- * \r
- * @param unescaped unescaped string that is to be escaped.\r
- * \r
- * @return escaped string.\r
- * \r
- * @see #queryUnescape\r
- */\r
- static public function queryPartEscape(unescaped:String) : String\r
- {\r
- var escaped:String = unescaped;\r
- escaped = URI.fastEscapeChars(unescaped, URI.URIqueryPartExcludedBitmap);\r
- return escaped;\r
- }\r
- \r
-\r
- /**\r
- * Unescape the individual name/value string pairs.\r
- * \r
- * @param escaped escaped string to be unescaped\r
- * \r
- * @return unescaped string\r
- * \r
- * @see #queryEscape\r
- */\r
- static public function queryPartUnescape(escaped:String) : String\r
- {\r
- var unescaped:String = escaped;\r
- unescaped = unescapeChars(unescaped);\r
- return unescaped;\r
- }\r
- \r
- /**\r
- * Output this URI as a string. The resulting string is properly\r
- * escaped and well formed for machine processing.\r
- */\r
- public function toString() : String\r
- {\r
- if (this == null)\r
- return "";\r
- else\r
- return toStringInternal(false);\r
- }\r
- \r
- /**\r
- * Output the URI as a string that is easily readable by a human.\r
- * This outputs the URI with all escape sequences unescaped to\r
- * their character representation. This makes the URI easier for\r
- * a human to read, but the URI could be completely invalid\r
- * because some unescaped characters may now cause ambiguous parsing.\r
- * This function should only be used if you want to display a URI to\r
- * a user. This function should never be used outside that specific\r
- * case.\r
- * \r
- * @return the URI in string format with all escape sequences\r
- * unescaped.\r
- * \r
- * @see #toString\r
- */\r
- public function toDisplayString() : String\r
- {\r
- return toStringInternal(true);\r
- }\r
- \r
- \r
- /**\r
- * @private\r
- * \r
- * The guts of toString()\r
- */\r
- protected function toStringInternal(forDisplay:Boolean) : String\r
- {\r
- var uri:String = "";\r
- var part:String = "";\r
- \r
- if (isHierarchical() == false)\r
- {\r
- // non-hierarchical URI\r
- \r
- uri += (forDisplay ? this.scheme : _scheme);\r
- uri += ":";\r
- uri += (forDisplay ? this.nonHierarchical : _nonHierarchical);\r
- }\r
- else\r
- {\r
- // Hierarchical URI\r
- \r
- if (isRelative() == false)\r
- {\r
- // If it is not a relative URI, then we want the scheme and\r
- // authority parts in the string. If it is relative, we\r
- // do NOT want this stuff.\r
- \r
- if (_scheme.length != 0)\r
- {\r
- part = (forDisplay ? this.scheme : _scheme);\r
- uri += part + ":";\r
- }\r
- \r
- if (_authority.length != 0 || isOfType("file"))\r
- {\r
- uri += "//";\r
- \r
- // Add on any username/password associated with this\r
- // authority\r
- if (_username.length != 0)\r
- {\r
- part = (forDisplay ? this.username : _username);\r
- uri += part;\r
- \r
- if (_password.length != 0)\r
- {\r
- part = (forDisplay ? this.password : _password);\r
- uri += ":" + part;\r
- }\r
- \r
- uri += "@";\r
- }\r
- \r
- // add the authority\r
- part = (forDisplay ? this.authority : _authority);\r
- uri += part;\r
- \r
- // Tack on the port number, if any\r
- if (port.length != 0)\r
- uri += ":" + port;\r
- }\r
- }\r
- \r
- // Tack on the path\r
- part = (forDisplay ? this.path : _path);\r
- uri += part;\r
- \r
- } // end hierarchical part\r
- \r
- // Both non-hier and hierarchical have query and fragment parts\r
- \r
- // Add on the query and fragment parts\r
- if (_query.length != 0)\r
- {\r
- part = (forDisplay ? this.query : _query);\r
- uri += "?" + part;\r
- }\r
- \r
- if (fragment.length != 0)\r
- {\r
- part = (forDisplay ? this.fragment : _fragment);\r
- uri += "#" + part;\r
- }\r
- \r
- return uri;\r
- }\r
- \r
- /**\r
- * Forcefully ensure that this URI is properly escaped.\r
- * \r
- * <p>Sometimes URI's are constructed by hand using strings outside\r
- * this class. In those cases, it is unlikely the URI has been\r
- * properly escaped. This function forcefully escapes this URI\r
- * by unescaping each part and then re-escaping it. If the URI\r
- * did not have any escaping, the first unescape will do nothing\r
- * and then the re-escape will properly escape everything. If\r
- * the URI was already escaped, the unescape and re-escape will\r
- * essentally be a no-op. This provides a safe way to make sure\r
- * a URI is in the proper escaped form.</p>\r
- */\r
- public function forceEscape() : void\r
- {\r
- // The accessors for each of the members will unescape\r
- // and then re-escape as we get and assign them.\r
- \r
- // Handle the parts that are common for both hierarchical\r
- // and non-hierarchical URI's\r
- this.scheme = this.scheme;\r
- this.setQueryByMap(this.getQueryByMap());\r
- this.fragment = this.fragment;\r
- \r
- if (isHierarchical())\r
- {\r
- this.authority = this.authority;\r
- this.path = this.path;\r
- this.port = this.port;\r
- this.username = this.username;\r
- this.password = this.password;\r
- }\r
- else\r
- {\r
- this.nonHierarchical = this.nonHierarchical;\r
- }\r
- }\r
- \r
- \r
- /**\r
- * Does this URI point to a resource of the given file type?\r
- * Given a file extension (or just a file name, this will strip the\r
- * extension), check to see if this URI points to a file of that\r
- * type.\r
- * \r
- * @param extension string that contains a file extension with or\r
- * without a dot ("html" and ".html" are both valid), or a file\r
- * name with an extension (e.g. "index.html").\r
- * \r
- * @return true if this URI points to a resource with the same file\r
- * file extension as the extension provided, false otherwise.\r
- */\r
- public function isOfFileType(extension:String) : Boolean\r
- {\r
- var thisExtension:String;\r
- var index:int;\r
- \r
- index = extension.lastIndexOf(".");\r
- if (index != -1)\r
- {\r
- // Strip the extension\r
- extension = extension.substr(index + 1);\r
- }\r
- else\r
- {\r
- // The caller passed something without a dot in it. We\r
- // will assume that it is just a plain extension (e.g. "html").\r
- // What they passed is exactly what we want\r
- }\r
- \r
- thisExtension = getExtension(true);\r
- \r
- if (thisExtension == "")\r
- return false;\r
- \r
- // Compare the extensions ignoring case\r
- if (compareStr(thisExtension, extension, false) == 0)\r
- return true;\r
- else\r
- return false;\r
- }\r
- \r
- \r
- /**\r
- * Get the ".xyz" file extension from the filename in the URI.\r
- * For example, if we have the following URI:\r
- * \r
- * <listing>http://something.com/path/to/my/page.html?form=yes&name=bob#anchor</listing>\r
- * \r
- * <p>This will return ".html".</p>\r
- * \r
- * @param minusDot If true, this will strip the dot from the extension.\r
- * If true, the above example would have returned "html".\r
- * \r
- * @return the file extension\r
- */\r
- public function getExtension(minusDot:Boolean = false) : String\r
- {\r
- var filename:String = getFilename();\r
- var extension:String;\r
- var index:int;\r
- \r
- if (filename == "")\r
- return String("");\r
- \r
- index = filename.lastIndexOf(".");\r
- \r
- // If it doesn't have an extension, or if it is a "hidden" file,\r
- // it doesn't have an extension. Hidden files on unix start with\r
- // a dot (e.g. ".login").\r
- if (index == -1 || index == 0)\r
- return String("");\r
- \r
- extension = filename.substr(index);\r
- \r
- // If the caller does not want the dot, remove it.\r
- if (minusDot && extension.charAt(0) == ".")\r
- extension = extension.substr(1);\r
- \r
- return extension;\r
- }\r
- \r
- /**\r
- * Quick function to retrieve the file name off the end of a URI.\r
- * \r
- * <p>For example, if the URI is:</p>\r
- * <listing>http://something.com/some/path/to/my/file.html</listing>\r
- * <p>this function will return "file.html".</p>\r
- * \r
- * @param minusExtension true if the file extension should be stripped\r
- * \r
- * @return the file name. If this URI is a directory, the return\r
- * value will be empty string.\r
- */\r
- public function getFilename(minusExtension:Boolean = false) : String\r
- {\r
- if (isDirectory())\r
- return String("");\r
- \r
- var pathStr:String = this.path;\r
- var filename:String;\r
- var index:int;\r
- \r
- // Find the last path separator.\r
- index = pathStr.lastIndexOf("/");\r
- \r
- if (index != -1)\r
- filename = pathStr.substr(index + 1);\r
- else\r
- filename = pathStr;\r
- \r
- if (minusExtension)\r
- {\r
- // The caller has requested that the extension be removed\r
- index = filename.lastIndexOf(".");\r
- \r
- if (index != -1)\r
- filename = filename.substr(0, index);\r
- }\r
- \r
- return filename;\r
- }\r
- \r
- \r
- /**\r
- * @private\r
- * Helper function to compare strings.\r
- * \r
- * @return true if the two strings are identical, false otherwise.\r
- */\r
- static protected function compareStr(str1:String, str2:String,\r
- sensitive:Boolean = true) : Boolean\r
- {\r
- if (sensitive == false)\r
- {\r
- str1 = str1.toLowerCase();\r
- str2 = str2.toLowerCase();\r
- }\r
- \r
- return (str1 == str2)\r
- }\r
- \r
- /**\r
- * Based on the type of this URI (http, ftp, etc.) get\r
- * the default port used for that protocol. This is\r
- * just intended to be a helper function for the most\r
- * common cases.\r
- */\r
- public function getDefaultPort() : String\r
- {\r
- if (_scheme == "http")\r
- return String("80");\r
- else if (_scheme == "ftp")\r
- return String("21");\r
- else if (_scheme == "file")\r
- return String("");\r
- else if (_scheme == "sftp")\r
- return String("22"); // ssh standard port\r
- else\r
- {\r
- // Don't know the port for this URI type\r
- return String("");\r
- }\r
- }\r
- \r
- /**\r
- * @private\r
- * \r
- * This resolves the given URI if the application has a\r
- * resolver interface defined. This function does not\r
- * modify the passed in URI and returns a new URI.\r
- */\r
- static protected function resolve(uri:URI) : URI\r
- {\r
- var copy:URI = new URI();\r
- copy.copyURI(uri);\r
- \r
- if (_resolver != null)\r
- {\r
- // A resolver class has been registered. Call it.\r
- return _resolver.resolve(copy);\r
- }\r
- else\r
- {\r
- // No resolver. Nothing to do, but we don't\r
- // want to reuse the one passed in.\r
- return copy;\r
- }\r
- }\r
- \r
- /**\r
- * Accessor to set and get the resolver object used by all URI\r
- * objects to dynamically resolve URI's before comparison.\r
- */\r
- static public function set resolver(resolver:IURIResolver) : void\r
- {\r
- _resolver = resolver;\r
- }\r
- static public function get resolver() : IURIResolver\r
- {\r
- return _resolver;\r
- }\r
- \r
- /**\r
- * Given another URI, return this URI object's relation to the one given.\r
- * URI's can have 1 of 4 possible relationships. They can be unrelated,\r
- * equal, parent, or a child of the given URI.\r
- * \r
- * @param uri URI to compare this URI object to.\r
- * @param caseSensitive true if the URI comparison should be done\r
- * taking case into account, false if the comparison should be\r
- * performed case insensitive.\r
- * \r
- * @return URI.NOT_RELATED, URI.CHILD, URI.PARENT, or URI.EQUAL\r
- */\r
- public function getRelation(uri:URI, caseSensitive:Boolean = true) : int\r
- {\r
- // Give the app a chance to resolve these URI's before we compare them.\r
- var thisURI:URI = URI.resolve(this);\r
- var thatURI:URI = URI.resolve(uri);\r
- \r
- if (thisURI.isRelative() || thatURI.isRelative())\r
- {\r
- // You cannot compare relative URI's due to their lack of context.\r
- // You could have two relative URI's that look like:\r
- // ../../images/\r
- // ../../images/marketing/logo.gif\r
- // These may appear related, but you have no overall context\r
- // from which to make the comparison. The first URI could be\r
- // from one site and the other URI could be from another site.\r
- return URI.NOT_RELATED;\r
- }\r
- else if (thisURI.isHierarchical() == false || thatURI.isHierarchical() == false)\r
- {\r
- // One or both of the URI's are non-hierarchical.\r
- if (((thisURI.isHierarchical() == false) && (thatURI.isHierarchical() == true)) ||\r
- ((thisURI.isHierarchical() == true) && (thatURI.isHierarchical() == false)))\r
- {\r
- // XOR. One is hierarchical and the other is\r
- // non-hierarchical. They cannot be compared.\r
- return URI.NOT_RELATED;\r
- }\r
- else\r
- {\r
- // They are both non-hierarchical\r
- if (thisURI.scheme != thatURI.scheme)\r
- return URI.NOT_RELATED;\r
- \r
- if (thisURI.nonHierarchical != thatURI.nonHierarchical)\r
- return URI.NOT_RELATED;\r
- \r
- // The two non-hierarcical URI's are equal.\r
- return URI.EQUAL;\r
- }\r
- }\r
- \r
- // Ok, this URI and the one we are being compared to are both\r
- // absolute hierarchical URI's.\r
- \r
- if (thisURI.scheme != thatURI.scheme)\r
- return URI.NOT_RELATED;\r
- \r
- if (thisURI.authority != thatURI.authority)\r
- return URI.NOT_RELATED;\r
- \r
- var thisPort:String = thisURI.port;\r
- var thatPort:String = thatURI.port;\r
- \r
- // Different ports are considered completely different servers.\r
- if (thisPort == "")\r
- thisPort = thisURI.getDefaultPort();\r
- if (thatPort == "")\r
- thatPort = thatURI.getDefaultPort();\r
- \r
- // Check to see if the port is the default port.\r
- if (thisPort != thatPort)\r
- return URI.NOT_RELATED;\r
- \r
- if (compareStr(thisURI.path, thatURI.path, caseSensitive))\r
- return URI.EQUAL;\r
- \r
- // Special case check. If we are here, the scheme, authority,\r
- // and port match, and it is not a relative path, but the\r
- // paths did not match. There is a special case where we\r
- // could have:\r
- // http://something.com/\r
- // http://something.com\r
- // Technically, these are equal. So lets, check for this case.\r
- var thisPath:String = thisURI.path;\r
- var thatPath:String = thatURI.path;\r
- \r
- if ( (thisPath == "/" || thatPath == "/") &&\r
- (thisPath == "" || thatPath == "") )\r
- {\r
- // We hit the special case. These two are equal.\r
- return URI.EQUAL;\r
- }\r
- \r
- // Ok, the paths do not match, but one path may be a parent/child\r
- // of the other. For example, we may have:\r
- // http://something.com/path/to/homepage/\r
- // http://something.com/path/to/homepage/images/logo.gif\r
- // In this case, the first is a parent of the second (or the second\r
- // is a child of the first, depending on which you compare to the\r
- // other). To make this comparison, we must split the path into\r
- // its component parts (split the string on the '/' path delimiter).\r
- // We then compare the \r
- var thisParts:Array, thatParts:Array;\r
- var thisPart:String, thatPart:String;\r
- var i:int;\r
- \r
- thisParts = thisPath.split("/");\r
- thatParts = thatPath.split("/");\r
- \r
- if (thisParts.length > thatParts.length)\r
- {\r
- thatPart = thatParts[thatParts.length - 1];\r
- if (thatPart.length > 0)\r
- {\r
- // if the last part is not empty, the passed URI is\r
- // not a directory. There is no way the passed URI\r
- // can be a parent.\r
- return URI.NOT_RELATED;\r
- }\r
- else\r
- {\r
- // Remove the empty trailing part\r
- thatParts.pop();\r
- }\r
- \r
- // This may be a child of the one passed in\r
- for (i = 0; i < thatParts.length; i++)\r
- {\r
- thisPart = thisParts[i];\r
- thatPart = thatParts[i];\r
- \r
- if (compareStr(thisPart, thatPart, caseSensitive) == false)\r
- return URI.NOT_RELATED;\r
- }\r
- \r
- return URI.CHILD;\r
- }\r
- else if (thisParts.length < thatParts.length)\r
- {\r
- thisPart = thisParts[thisParts.length - 1];\r
- if (thisPart.length > 0)\r
- {\r
- // if the last part is not empty, this URI is not a\r
- // directory. There is no way this object can be\r
- // a parent.\r
- return URI.NOT_RELATED;\r
- }\r
- else\r
- {\r
- // Remove the empty trailing part\r
- thisParts.pop();\r
- }\r
- \r
- // This may be the parent of the one passed in\r
- for (i = 0; i < thisParts.length; i++)\r
- {\r
- thisPart = thisParts[i];\r
- thatPart = thatParts[i];\r
- \r
- if (compareStr(thisPart, thatPart, caseSensitive) == false)\r
- return URI.NOT_RELATED;\r
- }\r
- \r
- return URI.PARENT;\r
- }\r
- else\r
- {\r
- // Both URI's have the same number of path components, but\r
- // it failed the equivelence check above. This means that\r
- // the two URI's are not related.\r
- return URI.NOT_RELATED;\r
- }\r
- \r
- // If we got here, the scheme and authority are the same,\r
- // but the paths pointed to two different locations that\r
- // were in different parts of the file system tree\r
- return URI.NOT_RELATED;\r
- }\r
- \r
- /**\r
- * Given another URI, return the common parent between this one\r
- * and the provided URI.\r
- * \r
- * @param uri the other URI from which to find a common parent\r
- * @para caseSensitive true if this operation should be done\r
- * with case sensitive comparisons.\r
- * \r
- * @return the parent URI if successful, null otherwise.\r
- */\r
- public function getCommonParent(uri:URI, caseSensitive:Boolean = true) : URI\r
- {\r
- var thisURI:URI = URI.resolve(this);\r
- var thatURI:URI = URI.resolve(uri);\r
- \r
- if(!thisURI.isAbsolute() || !thatURI.isAbsolute() ||\r
- thisURI.isHierarchical() == false ||\r
- thatURI.isHierarchical() == false)\r
- {\r
- // Both URI's must be absolute hierarchical for this to\r
- // make sense.\r
- return null;\r
- }\r
- \r
- var relation:int = thisURI.getRelation(thatURI);\r
- if (relation == URI.NOT_RELATED)\r
- {\r
- // The given URI is not related to this one. No\r
- // common parent.\r
- return null;\r
- }\r
- \r
- thisURI.chdir(".");\r
- thatURI.chdir(".");\r
- \r
- var strBefore:String, strAfter:String;\r
- do\r
- {\r
- relation = thisURI.getRelation(thatURI, caseSensitive);\r
- if(relation == URI.EQUAL || relation == URI.PARENT)\r
- break;\r
- \r
- // If strBefore and strAfter end up being the same,\r
- // we know we are at the root of the path because\r
- // chdir("..") is doing nothing.\r
- strBefore = thisURI.toString();\r
- thisURI.chdir("..");\r
- strAfter = thisURI.toString();\r
- }\r
- while(strBefore != strAfter);\r
- \r
- return thisURI;\r
- }\r
- \r
- \r
- /**\r
- * This function is used to move around in a URI in a way similar\r
- * to the 'cd' or 'chdir' commands on Unix. These operations are\r
- * completely string based, using the context of the URI to\r
- * determine the position within the path. The heuristics used\r
- * to determine the action are based off Appendix C in RFC 2396.\r
- * \r
- * <p>URI paths that end in '/' are considered paths that point to\r
- * directories, while paths that do not end in '/' are files. For\r
- * example, if you execute chdir("d") on the following URI's:<br/>\r
- * 1. http://something.com/a/b/c/ (directory)<br/>\r
- * 2. http://something.com/a/b/c (not directory)<br/>\r
- * you will get:<br/>\r
- * 1. http://something.com/a/b/c/d<br/>\r
- * 2. http://something.com/a/b/d<br/></p>\r
- * \r
- * <p>See RFC 2396, Appendix C for more info.</p>\r
- * \r
- * @param reference the URI or path to "cd" to.\r
- * @param escape true if the passed reference string should be URI\r
- * escaped before using it.\r
- * \r
- * @return true if the chdir was successful, false otherwise.\r
- */\r
- public function chdir(reference:String, escape:Boolean = false) : Boolean\r
- {\r
- var uriReference:URI;\r
- var ref:String = reference;\r
- \r
- if (escape)\r
- ref = URI.escapeChars(reference);\r
- \r
- if (ref == "")\r
- {\r
- // NOOP\r
- return true;\r
- }\r
- else if (ref.substr(0, 2) == "//")\r
- {\r
- // Special case. This is an absolute URI but without the scheme.\r
- // Take the scheme from this URI and tack it on. This is\r
- // intended to make working with chdir() a little more\r
- // tolerant.\r
- var final:String = this.scheme + ":" + ref;\r
- \r
- return constructURI(final);\r
- }\r
- else if (ref.charAt(0) == "?")\r
- {\r
- // A relative URI that is just a query part is essentially\r
- // a "./?query". We tack on the "./" here to make the rest\r
- // of our logic work.\r
- ref = "./" + ref;\r
- }\r
- \r
- // Parse the reference passed in as a URI. This way we\r
- // get any query and fragments parsed out as well.\r
- uriReference = new URI(ref);\r
- \r
- if (uriReference.isAbsolute() ||\r
- uriReference.isHierarchical() == false)\r
- {\r
- // If the URI given is a full URI, it replaces this one.\r
- copyURI(uriReference);\r
- return true;\r
- }\r
- \r
- \r
- var thisPath:String, thatPath:String;\r
- var thisParts:Array, thatParts:Array;\r
- var thisIsDir:Boolean = false, thatIsDir:Boolean = false;\r
- var thisIsAbs:Boolean = false, thatIsAbs:Boolean = false;\r
- var lastIsDotOperation:Boolean = false;\r
- var curDir:String;\r
- var i:int;\r
- \r
- thisPath = this.path;\r
- thatPath = uriReference.path;\r
- \r
- if (thisPath.length > 0)\r
- thisParts = thisPath.split("/");\r
- else\r
- thisParts = new Array();\r
- \r
- if (thatPath.length > 0)\r
- thatParts = thatPath.split("/");\r
- else\r
- thatParts = new Array();\r
- \r
- if (thisParts.length > 0 && thisParts[0] == "")\r
- {\r
- thisIsAbs = true;\r
- thisParts.shift(); // pop the first one off the array\r
- }\r
- if (thisParts.length > 0 && thisParts[thisParts.length - 1] == "")\r
- {\r
- thisIsDir = true;\r
- thisParts.pop(); // pop the last one off the array\r
- }\r
- \r
- if (thatParts.length > 0 && thatParts[0] == "")\r
- {\r
- thatIsAbs = true;\r
- thatParts.shift(); // pop the first one off the array\r
- }\r
- if (thatParts.length > 0 && thatParts[thatParts.length - 1] == "")\r
- {\r
- thatIsDir = true;\r
- thatParts.pop(); // pop the last one off the array\r
- }\r
- \r
- if (thatIsAbs)\r
- {\r
- // The reference is an absolute path (starts with a slash).\r
- // It replaces this path wholesale.\r
- this.path = uriReference.path;\r
- \r
- // And it inherits the query and fragment\r
- this.queryRaw = uriReference.queryRaw;\r
- this.fragment = uriReference.fragment;\r
- \r
- return true;\r
- }\r
- else if (thatParts.length == 0 && uriReference.query == "")\r
- {\r
- // The reference must have only been a fragment. Fragments just\r
- // get appended to whatever the current path is. We don't want\r
- // to overwrite any query that may already exist, so this case\r
- // only takes on the new fragment.\r
- this.fragment = uriReference.fragment;\r
- return true;\r
- }\r
- else if (thisIsDir == false && thisParts.length > 0)\r
- {\r
- // This path ends in a file. It goes away no matter what.\r
- thisParts.pop();\r
- }\r
- \r
- // By default, this assumes the query and fragment of the reference\r
- this.queryRaw = uriReference.queryRaw;\r
- this.fragment = uriReference.fragment;\r
- \r
- // Append the parts of the path from the passed in reference\r
- // to this object's path.\r
- thisParts = thisParts.concat(thatParts);\r
- \r
- for(i = 0; i < thisParts.length; i++)\r
- {\r
- curDir = thisParts[i];\r
- lastIsDotOperation = false;\r
- \r
- if (curDir == ".")\r
- {\r
- thisParts.splice(i, 1);\r
- i = i - 1; // account for removing this item\r
- lastIsDotOperation = true;\r
- }\r
- else if (curDir == "..")\r
- {\r
- if (i >= 1)\r
- {\r
- if (thisParts[i - 1] == "..")\r
- {\r
- // If the previous is a "..", we must have skipped\r
- // it due to this URI being relative. We can't\r
- // collapse leading ".."s in a relative URI, so\r
- // do nothing.\r
- }\r
- else\r
- {\r
- thisParts.splice(i - 1, 2);\r
- i = i - 2; // move back to account for the 2 we removed\r
- }\r
- }\r
- else\r
- {\r
- // This is the first thing in the path.\r
- \r
- if (isRelative())\r
- {\r
- // We can't collapse leading ".."s in a relative\r
- // path. Do noting.\r
- }\r
- else\r
- {\r
- // This is an abnormal case. We have dot-dotted up\r
- // past the base of our "file system". This is a\r
- // case where we had a /path/like/this.htm and were\r
- // given a path to chdir to like this:\r
- // ../../../../../../mydir\r
- // Obviously, it has too many ".." and will take us\r
- // up beyond the top of the URI. However, according\r
- // RFC 2396 Appendix C.2, we should try to handle\r
- // these abnormal cases appropriately. In this case,\r
- // we will do what UNIX command lines do if you are\r
- // at the root (/) of the filesystem and execute:\r
- // # cd ../../../../../bin\r
- // Which will put you in /bin. Essentially, the extra\r
- // ".."'s will just get eaten.\r
- \r
- thisParts.splice(i, 1);\r
- i = i - 1; // account for the ".." we just removed\r
- }\r
- }\r
- \r
- lastIsDotOperation = true;\r
- }\r
- }\r
- \r
- var finalPath:String = "";\r
- \r
- // If the last thing in the path was a "." or "..", then this thing is a\r
- // directory. If the last thing isn't a dot-op, then we don't want to \r
- // blow away any information about the directory (hence the "|=" binary\r
- // assignment).\r
- thatIsDir = thatIsDir || lastIsDotOperation;\r
- \r
- // Reconstruct the path with the abs/dir info we have\r
- finalPath = joinPath(thisParts, thisIsAbs, thatIsDir);\r
- \r
- // Set the path (automatically escaping it)\r
- this.path = finalPath;\r
- \r
- return true;\r
- }\r
- \r
- /**\r
- * @private\r
- * Join an array of path parts back into a URI style path string.\r
- * This is used by the various path logic functions to recombine\r
- * a path. This is different than the standard Array.join()\r
- * function because we need to take into account the starting and\r
- * ending path delimiters if this is an absolute path or a\r
- * directory.\r
- * \r
- * @param parts the Array that contains strings of each path part.\r
- * @param isAbs true if the given path is absolute\r
- * @param isDir true if the given path is a directory\r
- * \r
- * @return the combined path string.\r
- */\r
- protected function joinPath(parts:Array, isAbs:Boolean, isDir:Boolean) : String\r
- {\r
- var pathStr:String = "";\r
- var i:int;\r
- \r
- for (i = 0; i < parts.length; i++)\r
- {\r
- if (pathStr.length > 0)\r
- pathStr += "/";\r
- \r
- pathStr += parts[i];\r
- }\r
- \r
- // If this path is a directory, tack on the directory delimiter,\r
- // but only if the path contains something. Adding this to an\r
- // empty path would make it "/", which is an absolute path that\r
- // starts at the root.\r
- if (isDir && pathStr.length > 0)\r
- pathStr += "/";\r
- \r
- if (isAbs)\r
- pathStr = "/" + pathStr;\r
- \r
- return pathStr;\r
- }\r
- \r
- /**\r
- * Given an absolute URI, make this relative URI absolute using\r
- * the given URI as a base. This URI instance must be relative\r
- * and the base_uri must be absolute.\r
- * \r
- * @param base_uri URI to use as the base from which to make\r
- * this relative URI into an absolute URI.\r
- * \r
- * @return true if successful, false otherwise.\r
- */\r
- public function makeAbsoluteURI(base_uri:URI) : Boolean\r
- {\r
- if (isAbsolute() || base_uri.isRelative())\r
- {\r
- // This URI needs to be relative, and the base needs to be\r
- // absolute otherwise we won't know what to do!\r
- return false;\r
- }\r
- \r
- // Make a copy of the base URI. We don't want to modify\r
- // the passed URI.\r
- var base:URI = new URI();\r
- base.copyURI(base_uri);\r
- \r
- // ChDir on the base URI. This will preserve any query\r
- // and fragment we have.\r
- if (base.chdir(toString()) == false)\r
- return false;\r
- \r
- // It worked, so copy the base into this one\r
- copyURI(base);\r
- \r
- return true;\r
- }\r
- \r
- \r
- /**\r
- * Given a URI to use as a base from which this object should be\r
- * relative to, convert this object into a relative URI. For example,\r
- * if you have:\r
- * \r
- * <listing>\r
- * var uri1:URI = new URI("http://something.com/path/to/some/file.html");\r
- * var uri2:URI = new URI("http://something.com/path/to/another/file.html");\r
- * \r
- * uri1.MakeRelativePath(uri2);</listing>\r
- * \r
- * <p>uri1 will have a final value of "../some/file.html"</p>\r
- * \r
- * <p>Note! This function is brute force. If you have two URI's\r
- * that are completely unrelated, this will still attempt to make\r
- * the relative URI. In that case, you will most likely get a\r
- * relative path that looks something like:</p>\r
- * \r
- * <p>../../../../../../some/path/to/my/file.html</p>\r
- * \r
- * @param base_uri the URI from which to make this URI relative\r
- * \r
- * @return true if successful, false if the base_uri and this URI\r
- * are not related, of if error.\r
- */\r
- public function makeRelativeURI(base_uri:URI, caseSensitive:Boolean = true) : Boolean\r
- {\r
- var base:URI = new URI();\r
- base.copyURI(base_uri);\r
- \r
- var thisParts:Array, thatParts:Array;\r
- var finalParts:Array = new Array();\r
- var thisPart:String, thatPart:String, finalPath:String;\r
- var pathStr:String = this.path;\r
- var queryStr:String = this.queryRaw;\r
- var fragmentStr:String = this.fragment;\r
- var i:int;\r
- var diff:Boolean = false;\r
- var isDir:Boolean = false;\r
- \r
- if (isRelative())\r
- {\r
- // We're already relative.\r
- return true;\r
- }\r
- \r
- if (base.isRelative())\r
- {\r
- // The base is relative. A relative base doesn't make sense.\r
- return false;\r
- }\r
- \r
- \r
- if ( (isOfType(base_uri.scheme) == false) ||\r
- (this.authority != base_uri.authority) )\r
- {\r
- // The schemes and/or authorities are different. We can't\r
- // make a relative path to something that is completely\r
- // unrelated.\r
- return false;\r
- }\r
- \r
- // Record the state of this URI\r
- isDir = isDirectory();\r
- \r
- // We are based of the directory of the given URI. We need to\r
- // make sure the URI is pointing to a directory. Changing\r
- // directory to "." will remove any file name if the base is\r
- // not a directory.\r
- base.chdir(".");\r
- \r
- thisParts = pathStr.split("/");\r
- thatParts = base.path.split("/");\r
- \r
- if (thisParts.length > 0 && thisParts[0] == "")\r
- thisParts.shift();\r
- \r
- if (thisParts.length > 0 && thisParts[thisParts.length - 1] == "")\r
- {\r
- isDir = true;\r
- thisParts.pop();\r
- }\r
- \r
- if (thatParts.length > 0 && thatParts[0] == "")\r
- thatParts.shift();\r
- if (thatParts.length > 0 && thatParts[thatParts.length - 1] == "")\r
- thatParts.pop();\r
- \r
- \r
- // Now that we have the paths split into an array of directories,\r
- // we can compare the two paths. We start from the left of side\r
- // of the path and start comparing. When we either run out of\r
- // directories (one path is longer than the other), or we find\r
- // a directory that is different, we stop. The remaining parts\r
- // of each path is then used to determine the relative path. For\r
- // example, lets say we have:\r
- // path we want to make relative: /a/b/c/d/e.txt\r
- // path to use as base for relative: /a/b/f/\r
- //\r
- // This loop will start at the left, and remove directories\r
- // until we get a mismatch or run off the end of one of them.\r
- // In this example, the result will be:\r
- // c/d/e.txt\r
- // f\r
- //\r
- // For every part left over in the base path, we prepend a ".."\r
- // to the relative to get the final path:\r
- // ../c/d/e.txt\r
- while(thatParts.length > 0)\r
- {\r
- if (thisParts.length == 0)\r
- {\r
- // we matched all there is to match, we are done.\r
- // This is the case where "this" object is a parent\r
- // path of the given URI. eg:\r
- // this.path = /a/b/ (thisParts)\r
- // base.path = /a/b/c/d/e/ (thatParts)\r
- break;\r
- }\r
- \r
- thisPart = thisParts[0];\r
- thatPart = thatParts[0];\r
- \r
- if (compareStr(thisPart, thatPart, caseSensitive))\r
- {\r
- thisParts.shift();\r
- thatParts.shift();\r
- }\r
- else\r
- break;\r
- }\r
- \r
- // If there are any path info left from the base URI, that means\r
- // **this** object is above the given URI in the file tree. For\r
- // each part left over in the given URI, we need to move up one\r
- // directory to get where we are.\r
- var dotdot:String = "..";\r
- for (i = 0; i < thatParts.length; i++)\r
- {\r
- finalParts.push(dotdot);\r
- }\r
- \r
- // Append the parts of this URI to any dot-dot's we have\r
- finalParts = finalParts.concat(thisParts);\r
- \r
- // Join the parts back into a path\r
- finalPath = joinPath(finalParts, false /* not absolute */, isDir);\r
- \r
- if (finalPath.length == 0)\r
- {\r
- // The two URI's are exactly the same. The proper relative\r
- // path is:\r
- finalPath = "./";\r
- }\r
- \r
- // Set the parts of the URI, preserving the original query and\r
- // fragment parts.\r
- setParts("", "", "", finalPath, queryStr, fragmentStr);\r
- \r
- return true;\r
- }\r
- \r
- /**\r
- * Given a string, convert it to a URI. The string could be a\r
- * full URI that is improperly escaped, a malformed URI (e.g.\r
- * missing a protocol like "www.something.com"), a relative URI,\r
- * or any variation there of.\r
- * \r
- * <p>The intention of this function is to take anything that a\r
- * user might manually enter as a URI/URL and try to determine what\r
- * they mean. This function differs from the URI constructor in\r
- * that it makes some assumptions to make it easy to import user\r
- * entered URI data.</p>\r
- * \r
- * <p>This function is intended to be a helper function.\r
- * It is not all-knowning and will probably make mistakes\r
- * when attempting to parse a string of unknown origin. If\r
- * your applicaiton is receiving input from the user, your\r
- * application should already have a good idea what the user\r
- * should be entering, and your application should be\r
- * pre-processing the user's input to make sure it is well formed\r
- * before passing it to this function.</p>\r
- * \r
- * <p>It is assumed that the string given to this function is\r
- * something the user may have manually entered. Given this,\r
- * the URI string is probably unescaped or improperly escaped.\r
- * This function will attempt to properly escape the URI by\r
- * using forceEscape(). The result is that a toString() call\r
- * on a URI that was created from unknownToURI() may not match\r
- * the input string due to the difference in escaping.</p>\r
- *\r
- * @param unknown a potental URI string that should be parsed\r
- * and loaded into this object.\r
- * @param defaultScheme if it is determined that the passed string\r
- * looks like a URI, but it is missing the scheme part, this\r
- * string will be used as the missing scheme.\r
- * \r
- * @return true if the given string was successfully parsed into\r
- * a valid URI object, false otherwise.\r
- */\r
- public function unknownToURI(unknown:String, defaultScheme:String = "http") : Boolean\r
- {\r
- var temp:String;\r
- \r
- if (unknown.length == 0)\r
- {\r
- this.initialize();\r
- return false;\r
- }\r
- \r
- // Some users love the backslash key. Fix it.\r
- unknown = unknown.replace(/\\/g, "/");\r
- \r
- // Check for any obviously missing scheme.\r
- if (unknown.length >= 2)\r
- {\r
- temp = unknown.substr(0, 2);\r
- if (temp == "//")\r
- unknown = defaultScheme + ":" + unknown;\r
- }\r
- \r
- if (unknown.length >= 3)\r
- {\r
- temp = unknown.substr(0, 3);\r
- if (temp == "://")\r
- unknown = defaultScheme + unknown;\r
- }\r
-\r
- // Try parsing it as a normal URI\r
- var uri:URI = new URI(unknown);\r
- \r
- if (uri.isHierarchical() == false)\r
- {\r
- if (uri.scheme == UNKNOWN_SCHEME)\r
- {\r
- this.initialize();\r
- return false;\r
- }\r
- \r
- // It's a non-hierarchical URI\r
- copyURI(uri);\r
- forceEscape();\r
- return true;\r
- }\r
- else if ((uri.scheme != UNKNOWN_SCHEME) &&\r
- (uri.scheme.length > 0))\r
- {\r
- if ( (uri.authority.length > 0) ||\r
- (uri.scheme == "file") )\r
- {\r
- // file://... URI\r
- copyURI(uri);\r
- forceEscape(); // ensure proper escaping\r
- return true;\r
- }\r
- else if (uri.authority.length == 0 && uri.path.length == 0)\r
- {\r
- // It's is an incomplete URI (eg "http://")\r
- \r
- setParts(uri.scheme, "", "", "", "", "");\r
- return false;\r
- }\r
- }\r
- else\r
- {\r
- // Possible relative URI. We can only detect relative URI's\r
- // that start with "." or "..". If it starts with something\r
- // else, the parsing is ambiguous.\r
- var path:String = uri.path;\r
- \r
- if (path == ".." || path == "." || \r
- (path.length >= 3 && path.substr(0, 3) == "../") ||\r
- (path.length >= 2 && path.substr(0, 2) == "./") )\r
- {\r
- // This is a relative URI.\r
- copyURI(uri);\r
- forceEscape();\r
- return true;\r
- }\r
- }\r
- \r
- // Ok, it looks like we are just a normal URI missing the scheme. Tack\r
- // on the scheme.\r
- uri = new URI(defaultScheme + "://" + unknown);\r
- \r
- // Check to see if we are good now\r
- if (uri.scheme.length > 0 && uri.authority.length > 0)\r
- {\r
- // It was just missing the scheme.\r
- copyURI(uri);\r
- forceEscape(); // Make sure we are properly encoded.\r
- return true;\r
- }\r
- \r
- // don't know what this is\r
- this.initialize();\r
- return false;\r
- }\r
- \r
- } // end URI class\r
-} // end package
\ No newline at end of file