URLParser: a parser to get value of host, protocol, port, parameters from an URL using regular expression

Filetype-URL Problem Summary: Once I need to get the host, protocol, port, parameters from a given URL with Action-Script. I googled for a class named HTTPUtil developed by members of flexpasta.com, and got some tips about URLUtil of Flex SDK. However, HTTPUtil is used to resolve information from the URL of the page the SWF file embed in. URLUtil is useful, but a little complex. I need a simple method to resolve a given URL, and use the resolved information directly without extra processing.

Solution Summary: I decide to write a class named URLParser myself to satisfy my requirement using regular expression of action-script.

Search-256x256 Demo | DownloadDownload Full Project

URLParser

Download: URLParser.as
  1. package
  2. {
  3.     import mx.managers.ISystemManager;
  4.    
  5.     [Bindable]
  6.     public class URLParser
  7.     {
  8.         public static var url:String;
  9.        
  10.         public static var host:String = "";
  11.         public static var port:String = "";
  12.         public static var protocol:String = "";
  13.         public static var path:String = "";
  14.         public static var parameters:Object;   
  15.        
  16.          public static function parse( url:String ) : void    
  17.         {
  18.             URLParser.url = url;
  19.             var reg:RegExp = /(?P<protocol>[a-zA-Z]+) : \/\/  (?P<host>[^:\/]*) (:(?P<port>\d+))?  ((?P<path>[^?]*))? ((?P<parameters>.*))? /x;
  20.             var results:Array = reg.exec(url);
  21.            
  22.             protocol = results.protocol
  23.             host = results.host;
  24.             port = results.port;
  25.             path = results.path;
  26.             var paramsStr:String = results.parameters;
  27.             if(paramsStr!="")
  28.             {
  29.                 parameters = null;
  30.                 parameters = new Object();
  31.                
  32.                 if(paramsStr.charAt(0) == "?")
  33.                 {
  34.                     paramsStr = paramsStr.substring(1);
  35.                 }
  36.                 var params:Array = paramsStr.split("&");
  37.                 for each(var paramStr:String in params)
  38.                 {
  39.                     var param:Array = paramStr.split("=");
  40.                     parameters[param[0]] = param[1];
  41.                 }                               
  42.             }   
  43.         }
  44.     }
  45. }

Description: I use regular expression to resolve what I want from the given URL. The explanation of the expression is below:

As the url is “http://localhost:8080/URLParserDemo.swf?debug=true”

  1. (?P<protocol>[a-zA-Z]+) match the protocol
  2.  
  3. http
  4.  
  5. : \/\/ match “://”
  6.  
  7. ://
  8.  
  9. (?P<host>[^:\/]*) match the host domain
  10.  
  11. localhost
  12.  
  13. (:(?P<port>\d+))? match the port, if any
  14.  
  15. :8080
  16.  
  17. ((?P<path>[^?]*))? match the requested resource path of the host, if any
  18.  
  19. URLParserDemo.swf
  20.  
  21. ((?P<parameters>.*))? match the request parameters, if any
  22.  
  23. ?debug=true

Following is the source of URLParserDemo.mxml:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <!-- http://ntt.cc -->
  3. <mx:Application 
  4.     xmlns:mx="http://www.adobe.com/2006/mxml" 
  5.     layout="absolute"
  6.     creationComplete="initApp()" 
  7.     >
  8.     <mx:Script>
  9.         <![CDATA[
  10.             import mx.controls.Alert;
  11.             import mx.utils.ObjectUtil;
  12.            
  13.             [Bindable]
  14.             private var params:Array;
  15.            
  16.             private function initApp():void
  17.             {
  18.                 URLParser.parse(systemManager.loaderInfo.url);
  19.                 var classInfo:Object = ObjectUtil.getClassInfo(URLParser.parameters);
  20.                 if(classInfo.properties!=null && classInfo.properties.length>0)
  21.                     params = classInfo.properties;
  22.             }
  23.            
  24.         ]]>
  25.     </mx:Script>
  26.     <mx:Panel width="100%" height="100%" title="HttpUtil Demo窶披€粘how the host info of the loaded swf">
  27.         <mx:Form width="100%" height="100%">
  28.             <mx:FormItem label="url">
  29.                 <mx:Label text="{ URLParser.url}"/>
  30.             </mx:FormItem>
  31.             <mx:FormItem label="host">
  32.                 <mx:Label text="{ URLParser.host}"/>
  33.             </mx:FormItem>
  34.             <mx:FormItem label="path">
  35.                 <mx:Label text="{ URLParser.path}"/>
  36.             </mx:FormItem>
  37.             <mx:FormItem label="port">
  38.                 <mx:Label text="{ URLParser.port}"/>
  39.             </mx:FormItem>
  40.             <mx:FormItem label="protocol">
  41.                 <mx:Label text="{ URLParser.protocol}"/>
  42.             </mx:FormItem>
  43.             <mx:FormItem label="parameters" direction="horizontal">
  44.                  <mx:Repeater dataProvider="{params}" id="paramList">
  45.                      <mx:Label text="{paramList.currentItem.localName}"/>
  46.                      <mx:Label text="{URLParser.parameters[paramList.currentItem.localName]}"/>
  47.                  </mx:Repeater>
  48.             </mx:FormItem>
  49.         </mx:Form>
  50.     </mx:Panel>   
  51. </mx:Application>

*You can find more HTTPUtil info on: http://www.flexpasta.com

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
You can leave a response, or trackback from your own site.

2 Responses to “URLParser: a parser to get value of host, protocol, port, parameters from an URL using regular expression”

  1. Great class! I like it more than mx.utils.URLUtil which does the same type of thing but on a smaller scale.

    I am using URLUtil for the parameters though. If your URL is “something.html?id=3&debug=true” you can use
    var params:String = URLUtil.objectToString(URLParser.parameters, “&”, true);
    This will result in “id=3&debug=true”

  2. [...] http://ntt.cc/2008/08/07/urlparser-a-parser-to-get-value-of-host-protocol-port-parameters-from-an-ur... カテゴリー: as3, flex入門 タグ: as3, flex3, url解析 コメント (0) トラックバック (0) コメントをどうぞ トラックバックURL [...]

Leave a Reply