Exemplo n.º 1
0
        /// <summary>
        /// Supplementary class that handles XmlWrite of individual UrlAddress as a fragment.
        /// </summary>
        /// <param name="writer">
        /// Used as XmlWriter.
        /// </param>
        /// <param name="address">
        /// UrlAddress instance.
        /// </param>
        private void WriteAddress(XmlWriter writer, UrlAddress address)
        {
            writer.WriteStartElement("urlAddress");
            writer.WriteStartElement("host");

            writer.WriteAttributeString("name", address.Host);
            writer.WriteEndElement();

            if (address.Uri != null && address.Uri.Length > 0)
            {
                writer.WriteStartElement("uri");

                foreach (var segment in address.Uri)
                {
                    XElement element = new XElement("segment", segment);
                    element.WriteTo(writer);
                }

                writer.WriteEndElement();
            }

            if (address.Parameters != null && address.Parameters.Count > 0)
            {
                writer.WriteStartElement("parameters");

                foreach (var item in address.Parameters)
                {
                    XElement element = new XElement("parameter", new XAttribute("key", item.Key), new XAttribute("value", item.Value));
                    element.WriteTo(writer);
                }

                writer.WriteEndElement();
            }

            writer.WriteEndElement();
        }
Exemplo n.º 2
0
        /// <summary>
        /// IXmlSerializable interface implementation of ReadXml method.
        /// </summary>
        /// <param name="reader">
        /// Used as XmlReader.
        /// </param>
        public void ReadXml(XmlReader reader)
        {
            List <UrlAddress> addresses = new List <UrlAddress>();

            reader.ReadStartElement();

            while (reader.Name == "urlAddress")
            {
                UrlAddress address = new UrlAddress();

                reader.ReadStartElement();

                XElement host = (XElement)XNode.ReadFrom(reader);
                address.Host = (string)host.Attribute("name");

                if (reader.Name == "uri")
                {
                    XElement uri = (XElement)XNode.ReadFrom(reader);
                    address.Uri = uri.Elements("segment").Select(element => element.Value).ToArray();
                }

                if (reader.Name == "parameters")
                {
                    XElement parameters = (XElement)XNode.ReadFrom(reader);
                    address.Parameters = parameters.Elements("parameter").ToDictionary(element => (string)element.Attribute("key"), element => (string)element.Attribute("value"));
                }

                reader.ReadEndElement();

                addresses.Add(address);
            }

            reader.ReadEndElement();

            this.Addresses = addresses;
        }
Exemplo n.º 3
0
        /// <summary>
        /// Parse method.
        /// </summary>
        /// <param name="urls">
        /// Array of urls as strings to parse.
        /// </param>
        /// <returns>
        /// Asignes bunch of parsed Urls as UrlAddresses object to IXmlSerializable interface.
        /// </returns>
        public IXmlSerializable Parse(string[] urls)
        {
            List <UrlAddress> addresses = new List <UrlAddress>();

            for (int k = 0; k < urls.Length; k++)
            {
                Uri siteUri = null;

                try
                {
                    siteUri = new Uri(urls[k]);
                }
                catch (Exception ex)
                {
                    this.OnInvalid(this, new ParserEventArgs(ex.Message, k));
                    continue;
                }

                UrlAddress parsedUrl = new UrlAddress();
                parsedUrl.Host = siteUri.Host;

                string[] segments = null;

                if (siteUri.Segments.Length > 1)
                {
                    segments = new string[siteUri.Segments.Length - 1];
                    for (int i = 0; i < siteUri.Segments.Length - 1; i++)
                    {
                        segments[i] = siteUri.Segments[i + 1];
                        int lastIndex = segments[i].Length - 1;
                        if (segments[i][lastIndex] == '/')
                        {
                            segments[i] = segments[i].Substring(0, lastIndex);
                        }
                    }
                }

                parsedUrl.Uri = segments;

                if (!string.IsNullOrWhiteSpace(siteUri.Query))
                {
                    string   fullQuery = siteUri.Query.Substring(1);
                    string[] queries   = fullQuery.Split('&', StringSplitOptions.RemoveEmptyEntries);
                    Dictionary <string, string> parameters = new Dictionary <string, string>();

                    foreach (var s in queries)
                    {
                        string[] parametersArray = s.Split('=', 2, StringSplitOptions.RemoveEmptyEntries);

                        if (parametersArray.Length >= 2 && !string.IsNullOrWhiteSpace(parametersArray[0]) && !string.IsNullOrWhiteSpace(parametersArray[1]))
                        {
                            if (!parameters.ContainsKey(parametersArray[0]))
                            {
                                parameters.Add(parametersArray[0], parametersArray[1]);
                            }
                        }
                    }

                    if (parameters.Count > 0)
                    {
                        parsedUrl.Parameters = parameters;
                    }
                }

                addresses.Add(parsedUrl);
            }

            return(new UrlAddresses(addresses));
        }