as3corelib Tutorial:How to Use URI in Flex

penguin-256x256 IMO, the major usage of this class is parsing a URL to retrieve the information such as host, port, file name, file extension, query, etc. It’s really a convenient way instead of using regular expression to parse the target information.

According to the documentation of as3corelib:

This class implements functions and utilities for working with URI’s (Universal Resource Identifiers). For technical description of the URI syntax, please see RFC 3986 at http://www.ietf.org/rfc/rfc3986.txt or do a web search for “RFC 3986″.

The most important aspect of URI’s to understand is that URI’s and URL’s are not strings. URI’s are complex data structures that encapsulate many pieces of information. The string version of a URI is the serialized representation of that data structure. This string serialization is used to provide a human readable representation and a means to transport the data over the network where it can then be parsed back into its’ component parts.

URI’s fall into one of three categories:

<scheme>:<scheme-specific-part>#<fragment> (non-hierarchical)
<scheme>:<authority><path>?<query>#<fragment> (hierarchical)=""></authority><path>?<query>#<fragment>>
<path>?<query>#<fragment> (relative hierarchical)

The query and fragment parts are optional.

This class supports both non-hierarchical and hierarchical URI’s

Search-256x256 Demo | DownloadDownload Full Project

This class is intended to be used “as-is” for the vast majority of common URI’s. However, if your application requires a custom URI syntax (e.g. custom query syntax or special handling of non-hierarchical URI’s), this class can be fully sub-classed. If you intended to subclass URI, please see the source code for complete documentation on protected members and protected functions.

For example:

var uri:URI = new URI(Application.application.url);
userService.url = “http://”+uri.authority+ (uri.port==””?””:”:”+uri.port) + ”/FileManage/servlet”;

Here, the URI class is used to retrieve the host value and port value of the server which reserves the requested flex application.

The documentation of this class is attached at the end of this article.

Screenshot of this demo:

clip_image002

Following is the source code of URIExample.xml

<?xml version="1.0" encoding="utf-8"?>
<uris>
	<uri value="ftp://tom:goodday@ftp.is.co.za:22/"/>
	<uri value="ftp://ftp.is.co.za/rfc/rfc1808.html"/>
	<uri value="http://www.ietf.org/rfc/rfc2396.txt#anchor1"/>
	<uri value="http://www.ietf.org:8080/rfc/rfc2396.txt"/>
	<uri value="http://www.ietf.org:8080/rfc/"/>
	<uri value="http://something.com/a/b/c/"/>
	<uri value="http://www.ietf.org:8080/rfc"/>
	<uri value="www.ietf.org:8080/rfc/rfc2396.txt"/>
	<uri value="/rfc/rfc2396.txt"/>
	<uri value="rfc/background.png"/>
	<uri value="../rfc/rfc2396.txt"/>
	<uri value="file:///F:/opensource/as3corelib-.92.1/as3corelib-.92.1/docs/index.html"/>
	<uri value="F:/opensource/as3corelib-.92.1/as3corelib-.92.1/docs/index.html"/>
	<uri value="F:\\opensource\\as3corelib-.92.1\\as3corelib-.92.1\\docs\\index.html"/>
	<uri value="ldap://[2001:db8::7]/c=GB?objectClass?one"/>
	<uri value="mailto:John.Doe@example.com"/>
	<uri value="news:comp.infosystems.www.servers.unix"/>
	<uri value="tel:+1-816-555-1212"/>
	<uri value="telnet://192.0.2.16:80/"/>
	<uri value="urn:oasis:names:specification:docbook:dtd:xml:4.1.2"/>
</uris>

And the source code of URIDemo.mxml

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
	<mx:Script>
		<![CDATA[
			import com.adobe.net.URI;	

			[Bindable]
			private var uri:URI;

			private function selectURI(event:MouseEvent):void
			{
				uri = new URI(event.currentTarget.text);
			}
		]]>
	</mx:Script>

	<mx:XML id="uris" source="URIExample.xml"/>

	<mx:Panel
		layout="horizontal"
		width="100%"
		height="100%"
		title="URI Demo"
		>
		<mx:VBox>
			<mx:Repeater id="uriList" dataProvider="{uris.uri}">
				<mx:Label
					text="{uriList.currentItem.@value}"
					buttonMode="true"
					mouseChildren="false"
					click="selectURI(event);"/>
			</mx:Repeater>
		</mx:VBox>

		<mx:Form
			paddingTop="0">
			<mx:FormItem label="is valid">
				<mx:Label text="{uri.isValid()}"/>
			</mx:FormItem>

			<mx:FormItem label="to display string">
				<mx:Label text="{uri.toDisplayString()}"/>
			</mx:FormItem>

			<mx:FormItem label="to string">
				<mx:Label text="{uri.toString()}"/>
			</mx:FormItem>

			<mx:FormItem label="schema">
				<mx:Label text="{uri.scheme}"/>
			</mx:FormItem>

			<mx:FormItem label="username">
				<mx:Label text="{uri.username}"/>
			</mx:FormItem>

			<mx:FormItem label="password">
				<mx:Label text="{uri.password}"/>
			</mx:FormItem>

			<mx:FormItem label="authority">
				<mx:Label text="{uri.authority}"/>
			</mx:FormItem>

			<mx:FormItem label="default port">
				<mx:Label text="{uri.getDefaultPort()}"/>
			</mx:FormItem>

			<mx:FormItem label="port">
				<mx:Label text="{uri.port}"/>
			</mx:FormItem>

			<mx:FormItem label="path">
				<mx:Label text="{uri.path}"/>
			</mx:FormItem>

			<mx:FormItem label="file name">
				<mx:Label text="{uri.getFilename(true)}"/>
			</mx:FormItem>

			<mx:FormItem label="extension">
				<mx:Label text="{uri.getExtension(true)}"/>
			</mx:FormItem>

			<mx:FormItem label="query">
				<mx:Label text="{uri.query}"/>
			</mx:FormItem>

			<mx:FormItem label="queryRaw">
				<mx:Label text="{uri.queryRaw}"/>
			</mx:FormItem>

			<mx:FormItem label="fragment">
				<mx:Label text="{uri.fragment}"/>
			</mx:FormItem>

			<mx:FormItem label="nonHierarchical">
				<mx:Label text="{uri.nonHierarchical}"/>
			</mx:FormItem>

			<mx:FormItem label="is absolute uri">
				<mx:Label text="{uri.isAbsolute()}"/>
			</mx:FormItem>

			<mx:FormItem label="is relative uri">
				<mx:Label text="{uri.isRelative()}"/>
			</mx:FormItem>

			<mx:FormItem label="is directory">
				<mx:Label text="{uri.isDirectory()}"/>
			</mx:FormItem>

			<mx:FormItem label="is hierarchical">
				<mx:Label text="{uri.isHierarchical()}"/>
			</mx:FormItem>
			<!--
				it seems isOfFileType return the opposite
				result. maybe it's a bug of this method.
			-->
			<mx:FormItem label="is of file type(html)">
				<mx:Label text="{uri.getExtension(true)==''?false:!uri.isOfFileType('html')}"/>
			</mx:FormItem>
		</mx:Form>

		<mx:ControlBar>
			<mx:Form>
				<mx:FormItem label="custom uri">
					<mx:TextInput id="customURI"/>
				</mx:FormItem>

				<mx:FormItem>
					<mx:Button  id="parseButton"
						label="parse"
						click="uri = new URI(customURI.text)"/>
				</mx:FormItem>
			</mx:Form>
		</mx:ControlBar>
	</mx:Panel>
</mx:Application>
Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • DZone
  • Reddit
  • Technorati
  • StumbleUpon
  • Twitter
RSS Enjoy this Post? Subscribe to Ntt.cc

RSS Feed   RSS Feed     Email Feed  Email Feed Follow us Follow us
You can leave a response, or trackback from your own site.

2 Responses to “as3corelib Tutorial:How to Use URI in Flex”

  1. Steve says:

    Hi There

    Great tutorial. I was wondering if you could point me in the right direction as this tutorial seems to be what i am after – but might be a bit much for the simple feature i am trying to acheive.

    I have a external .swf file which will get loaded by either swfloader or navigateToURL via a flex app. the .swf file is not part of the flex framework, and it has a URI datainput field with a clickable button.

    What i am trying to do is basically move that URI datainput field from the external .swf file and put it in the flex app. When the clickable button is pressed i need a variable with the URI value sent to the .swf, so that when the .swf fires it also looks for and loads the URI variable data.

    Any help guys would be really appreciated, this thing is pulling my hair out.

    Regards

    Steve

  2. [...] 原文:http://ntt.cc/2009/02/16/as3corelib-tutorialhow-to-use-uri-class-in-flex.html Tags: AS3, as3Corelib, Flash, Flex You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site. [...]

Leave a Reply