C# 아래의 Uri.Builder

5636 단어 C#Uri.Builder

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace Uri
{
    class Builder
    {
        private string scheme = "http";
        private string host = "";
        private StringBuilder path;
        private Boolean endwithSlash = false;
        private IDictionary<string, string> param = null;

        public Builder()
        {
            scheme = "http";
            host = "";
            path = new StringBuilder("");
            endwithSlash = false;

            param = new Dictionary<string, string>();
        }

        public Builder SetScheme(string scheme)
        {
            this.scheme = scheme;

            return this;
        }

        public Builder SetHost(string host)
        {
            this.host = host;

            return this;
        }

        public Builder Path(string path)
        {
            return AppendPath(ParsePath(path));
        }

        public Builder EncodePath(string path)
        {
            return AppendEncodePath(ParsePath(path));
        }

        public Builder AppendPath(string path)
        {
            if (path.Length > 0)
            {
                // start with '/'
                if (!endwithSlash && !path.StartsWith("/"))
                    this.path.Append("/");

                this.path.Append(path);

                if (path.EndsWith("/"))
                    endwithSlash = true;
                else
                    endwithSlash = false;
            }

            return this;
        }

        public Builder AppendEncodePath(string path)
        {
            return AppendPath(Encode(path));
        }

        private string ParsePath(string path)
        {
            int posScheme = path.IndexOf("://");
            if (posScheme > 0)
            {
                // get scheme
                scheme = path.Substring(0, posScheme);
                path = path.Substring(posScheme + 3);
            }

            int posHost = path.IndexOf("/");
            if (posHost == -1)
            {
                // not found '/'
                host = path;
                path = "";
            }
            else
            {
                host = path.Substring(0, posHost);
                path = path.Substring(posHost + 1);
            }

            return path;
        }

        public Builder AppendParameter(string key, string value)
        {
            param.Add(key, value);

            return this;
        }

        public Builder AppendEncodeParameter(string key, string value)
        {
            return AppendParameter(key, Encode(value));
        }

        public string ToString()
        {
            // generate URL string
            StringBuilder url = new StringBuilder("");

            // scheme
            url.Append(scheme).Append("://");

            // host
            url.Append(host);

            // path
            if (path.Length > 0)
                url.Append(path);

            // param
            if (param.Count > 0)
            {
                string pre = "?";
                foreach (KeyValuePair<string, string> p in param)
                {
                    url.Append(pre);

                    if (p.Value != null)
                        url.AppendFormat("{0}={1}", p.Key, p.Value);
                    else
                        url.Append(p.Key);

                    pre = "&";
                }
            }

            return url.ToString();
        }

        private string Encode(string value)
        {
            return HttpUtility.UrlEncode(value);
        }

        public Builder Parse(string url)
        {
            // seperate path and parameters
            string[] div = url.Split(new string[] { "?" }, 2, StringSplitOptions.None);
            Path(div[0]);
            if (div.Length == 2)
                ParseParameters(div[1]);

            return this;
        }

        private void ParseParameters(string param)
        {
            if (param.Length == 0)
                return;

            string[] para = param.Split(new string[] { "&" }, StringSplitOptions.None);
            foreach (string p in para)
            {
                string[] val = p.Split(new string[] { "=" }, 2, StringSplitOptions.None);
                if (val.Length == 2)
                    AppendParameter(val[0], val[1]);
                else
                    AppendParameter(val[0], null);
            }
        }
    }
}

좋은 웹페이지 즐겨찾기