📄 uri.as
字号:
}
/**
* Output the URI as a string that is easily readable by a human.
* This outputs the URI with all escape sequences unescaped to
* their character representation. This makes the URI easier for
* a human to read, but the URI could be completely invalid
* because some unescaped characters may now cause ambiguous parsing.
* This function should only be used if you want to display a URI to
* a user. This function should never be used outside that specific
* case.
*
* @return the URI in string format with all escape sequences
* unescaped.
*
* @see #toString
*/
public function toDisplayString() : String
{
return toStringInternal(true);
}
/**
* @private
*
* The guts of toString()
*/
protected function toStringInternal(forDisplay:Boolean) : String
{
var uri:String = "";
var part:String = "";
if (isHierarchical() == false)
{
// non-hierarchical URI
uri += (forDisplay ? this.scheme : _scheme);
uri += ":";
uri += (forDisplay ? this.nonHierarchical : _nonHierarchical);
}
else
{
// Hierarchical URI
if (isRelative() == false)
{
// If it is not a relative URI, then we want the scheme and
// authority parts in the string. If it is relative, we
// do NOT want this stuff.
if (_scheme.length != 0)
{
part = (forDisplay ? this.scheme : _scheme);
uri += part + ":";
}
if (_authority.length != 0 || isOfType("file"))
{
uri += "//";
// Add on any username/password associated with this
// authority
if (_username.length != 0)
{
part = (forDisplay ? this.username : _username);
uri += part;
if (_password.length != 0)
{
part = (forDisplay ? this.password : _password);
uri += ":" + part;
}
uri += "@";
}
// add the authority
part = (forDisplay ? this.authority : _authority);
uri += part;
// Tack on the port number, if any
if (port.length != 0)
uri += ":" + port;
}
}
// Tack on the path
part = (forDisplay ? this.path : _path);
uri += part;
} // end hierarchical part
// Both non-hier and hierarchical have query and fragment parts
// Add on the query and fragment parts
if (_query.length != 0)
{
part = (forDisplay ? this.query : _query);
uri += "?" + part;
}
if (fragment.length != 0)
{
part = (forDisplay ? this.fragment : _fragment);
uri += "#" + part;
}
return uri;
}
/**
* Forcefully ensure that this URI is properly escaped.
*
* <p>Sometimes URI's are constructed by hand using strings outside
* this class. In those cases, it is unlikely the URI has been
* properly escaped. This function forcefully escapes this URI
* by unescaping each part and then re-escaping it. If the URI
* did not have any escaping, the first unescape will do nothing
* and then the re-escape will properly escape everything. If
* the URI was already escaped, the unescape and re-escape will
* essentally be a no-op. This provides a safe way to make sure
* a URI is in the proper escaped form.</p>
*/
public function forceEscape() : void
{
// The accessors for each of the members will unescape
// and then re-escape as we get and assign them.
// Handle the parts that are common for both hierarchical
// and non-hierarchical URI's
this.scheme = this.scheme;
this.setQueryByMap(this.getQueryByMap());
this.fragment = this.fragment;
if (isHierarchical())
{
this.authority = this.authority;
this.path = this.path;
this.port = this.port;
this.username = this.username;
this.password = this.password;
}
else
{
this.nonHierarchical = this.nonHierarchical;
}
}
/**
* Does this URI point to a resource of the given file type?
* Given a file extension (or just a file name, this will strip the
* extension), check to see if this URI points to a file of that
* type.
*
* @param extension string that contains a file extension with or
* without a dot ("html" and ".html" are both valid), or a file
* name with an extension (e.g. "index.html").
*
* @return true if this URI points to a resource with the same file
* file extension as the extension provided, false otherwise.
*/
public function isOfFileType(extension:String) : Boolean
{
var thisExtension:String;
var index:int;
index = extension.lastIndexOf(".");
if (index != -1)
{
// Strip the extension
extension = extension.substr(index + 1);
}
else
{
// The caller passed something without a dot in it. We
// will assume that it is just a plain extension (e.g. "html").
// What they passed is exactly what we want
}
thisExtension = getExtension(true);
if (thisExtension == "")
return false;
// Compare the extensions ignoring case
if (compareStr(thisExtension, extension, false) == 0)
return true;
else
return false;
}
/**
* Get the ".xyz" file extension from the filename in the URI.
* For example, if we have the following URI:
*
* <listing>http://something.com/path/to/my/page.html?form=yes&name=bob#anchor</listing>
*
* <p>This will return ".html".</p>
*
* @param minusDot If true, this will strip the dot from the extension.
* If true, the above example would have returned "html".
*
* @return the file extension
*/
public function getExtension(minusDot:Boolean = false) : String
{
var filename:String = getFilename();
var extension:String;
var index:int;
if (filename == "")
return String("");
index = filename.lastIndexOf(".");
// If it doesn't have an extension, or if it is a "hidden" file,
// it doesn't have an extension. Hidden files on unix start with
// a dot (e.g. ".login").
if (index == -1 || index == 0)
return String("");
extension = filename.substr(index);
// If the caller does not want the dot, remove it.
if (minusDot && extension.charAt(0) == ".")
extension = extension.substr(1);
return extension;
}
/**
* Quick function to retrieve the file name off the end of a URI.
*
* <p>For example, if the URI is:</p>
* <listing>http://something.com/some/path/to/my/file.html</listing>
* <p>this function will return "file.html".</p>
*
* @param minusExtension true if the file extension should be stripped
*
* @return the file name. If this URI is a directory, the return
* value will be empty string.
*/
public function getFilename(minusExtension:Boolean = false) : String
{
if (isDirectory())
return String("");
var pathStr:String = this.path;
var filename:String;
var index:int;
// Find the last path separator.
index = pathStr.lastIndexOf("/");
if (index != -1)
filename = pathStr.substr(index + 1);
else
filename = pathStr;
if (minusExtension)
{
// The caller has requested that the extension be removed
index = filename.lastIndexOf(".");
if (index != -1)
filename = filename.substr(0, index);
}
return filename;
}
/**
* @private
* Helper function to compare strings.
*
* @return true if the two strings are identical, false otherwise.
*/
static protected function compareStr(str1:String, str2:String,
sensitive:Boolean = true) : Boolean
{
if (sensitive == false)
{
str1 = str1.toLowerCase();
str2 = str2.toLowerCase();
}
return (str1 == str2)
}
/**
* Based on the type of this URI (http, ftp, etc.) get
* the default port used for that protocol. This is
* just intended to be a helper function for the most
* common cases.
*/
public function getDefaultPort() : String
{
if (_scheme == "http")
return String("80");
else if (_scheme == "ftp")
return String("21");
else if (_scheme == "file")
return String("");
else if (_scheme == "sftp")
return String("22"); // ssh standard port
else
{
// Don't know the port for this URI type
return String("");
}
}
/**
* @private
*
* This resolves the given URI if the application has a
* resolver interface defined. This function does not
* modify the passed in URI and returns a new URI.
*/
static protected function resolve(uri:URI) : URI
{
var copy:URI = new URI();
copy.copyURI(uri);
if (_resolver != null)
{
// A resolver class has been registered. Call it.
return _resolver.resolve(copy);
}
else
{
// No resolver. Nothing to do, but we don't
// want to reuse the one passed in.
return copy;
}
}
/**
* Accessor to set and get the resolver object used by all URI
* objects to dynamically resolve URI's before comparison.
*/
static public function set resolver(resolver:IURIResolver) : void
{
_resolver = resolver;
}
static public function get resolver() : IURIResolver
{
return _resolver;
}
/**
* Given another URI, return this URI object's relation to the one given.
* URI's can have 1 of 4 possible relationships. They can be unrelated,
* equal, parent, or a child of the given URI.
*
* @param uri URI to compare this URI object to.
* @param caseSensitive true if the URI comparison should be done
* taking case into account, false if the comparison should be
* performed case insensitive.
*
* @return URI.NOT_RELATED, URI.CHILD, URI.PARENT, or URI.EQUAL
*/
public function getRelation(uri:URI, caseSensitive:Boolean = true) : int
{
// Give the app a chance to resolve these URI's before we compare them.
var thisURI:URI = URI.resolve(this);
var thatURI:URI = URI.resolve(uri);
if (thisURI.isRelative() || thatURI.isRelative())
{
// You cannot compare relative URI's due to their lack of context.
// You could have two relative URI's that look like:
// ../../images/
// ../../images/marketing/logo.gif
// These may appear related, but you have no overall context
// from which to make the comparison. The first URI could be
// from one site and the other URI could be from another site.
return URI.NOT_RELATED;
}
else if (thisURI.isHierarchical() == false || thatURI.isHierarchical() == false)
{
// One or both of the URI's are non-hierarchical.
if (((thisURI.isHierarchical() == false) && (thatURI.isHierarchical() == true)) ||
((thisURI.isHierarchical() == true) && (thatURI.isHierarchical() == false)))
{
// XOR. One is hierarchical and the other is
// non-hierarchical. They cannot be compared.
return URI.NOT_RELATED;
}
else
{
// They are both non-hierarchical
if (thisURI.scheme != thatURI.scheme)
return URI.NOT_RELATED;
if (thisURI.nonHierarchical != thatURI.nonHierarchical)
return URI.NOT_RELATED;
// The two non-hierarcical URI's are equal.
return URI.EQUAL;
}
}
// Ok, this URI and the one we are being compared to are both
// absolute hierarchical URI's.
if (thisURI.scheme != thatURI.scheme)
return URI.NOT_RELATED;
if (thisURI.authority != thatURI.authority)
return URI.NOT_RELATED;
var thisPort:String = thisURI.port;
var thatPort:String = thatURI.port;
// Different ports are considered completely different servers.
if (thisPort == "")
thisPort = thisURI.getDefaultPort();
if (thatPort == "")
thatPort = thatURI.getDefaultPort();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -