﻿var U={};
U.URLParser=function(url){
	/**
	后面的值是相对与正则表达式的：
	*/
	this._fields = {
		'URL' : 0,
		'Protocol' : 2,
		'Username' : 4,
		'Password' : 5,
		'Host' : 6,
		'Port' : 7,
		'Pathname' : 8,
		'Querystring' : 9,
		'Fragment' : 10
	};
	this._values = {};
	/**
	匹配URL
	*/
	this._regex = /^((\w+):\/\/)?((\w+):?(\w+)?@)?([^\/\?:]+):?(\d+)?(\/?[^\?#]+)?\??([^#]+)?#?(\w*)/;
	
	/**
		得到相应的方法,getURL,getProtocol,getUsername,getPassword,getHost,
		getPort,getPathname,getQuerystring,getFragment;
	*/
	for(var f in this._fields){
		this['get' + f] = this._makeGetter(f);
	}
	/**
	初始化参数
	*/
	if (typeof url != 'undefined'){
		this._parse(url);
	}
};
U.URLParser.prototype.setURL = function(url) {
	this._parse(url);
};
/**
初始化各个属性，设置值为空；
*/
U.URLParser.prototype._initValues = function() {
	for(var f in this._fields){
		this._values[f] = '';
	}
}

U.URLParser.prototype._parse = function(url) {
	this._initValues();
	var r = this._regex.exec(url);
	if (!r) throw "DPURLParser::_parse -> Invalid URL";
 
	for(var f in this._fields) if (typeof r[this._fields[f]] != 'undefined'){
	/**
	根据正则得到相应的值，最后给this._values[f]
	*/
		this._values[f] = r[this._fields[f]];
	}
};
U.URLParser.prototype._makeGetter = function(field) {
	return function() {
		return this._values[field];
	}
};