Beispiel #1
0
        /// <summary>Transforms the current request in a collection of corresponding key-value pairs.</summary>
        /// <returns>The collection of key-value pairs.</returns>
        NameValueCollection Ows.IRequest.ToKeyValuePairs()
        {
            var ret = new NameValueCollection();

            ret.Add("request", "GetRecords");
            ret.Add("service", HttpUtility.UrlEncode(this.Content.service));
            ret.Add("version", HttpUtility.UrlEncode(this.Content.version));

            // namespace
            var        namespaceManager = new XmlNamespaceManager(new NameTable());
            XNamespace dn = this.Untyped.GetDefaultNamespace();

            if (dn != XNamespace.None)
            {
                namespaceManager.AddNamespace(string.Empty, dn.NamespaceName);
            }
            var namespaces = from at in this.Untyped.Attributes()
                             where at.IsNamespaceDeclaration
                             select new {
                Prefix = at.Parent.GetPrefixOfNamespace(at.Value),
                Uri    = at.Value
            };

            namespaces.ToList().ForEach(n => namespaceManager.AddNamespace(n.Prefix, n.Uri));
            ret.Add(
                "namespace",
                string.Join(
                    ",",
                    namespaceManager.Cast <string>()
                    .Where(s => !string.IsNullOrEmpty(namespaceManager.LookupNamespace(s ?? string.Empty)))
                    .Select(s => string.Concat("xmlns(", !string.IsNullOrEmpty(s) ? string.Concat(s, "=") : string.Empty, HttpUtility.UrlEncode(namespaceManager.LookupNamespace(s ?? string.Empty)), ")"))
                    )
                );

            if (!string.IsNullOrEmpty(this.resultType))
            {
                ret.Add("resulttype", HttpUtility.UrlEncode(this.resultType));
            }
            if (this.requestId != null)
            {
                ret.Add("requestid", HttpUtility.UrlEncode(this.requestId.ToString()));
            }
            if (!string.IsNullOrEmpty(this.outputFormat))
            {
                ret.Add("outputformat", HttpUtility.UrlEncode(this.outputFormat));
            }
            if (outputSchema != null)
            {
                ret.Add("outputschema", HttpUtility.UrlEncode(outputSchema.ToString()));
            }
            if (startPosition != 1)
            {
                ret.Add("startposition", this.startPosition.ToString("F0", CultureInfo.InvariantCulture));
            }
            if (maxRecords != 10)
            {
                ret.Add("maxrecords", this.maxRecords.ToString("F0", CultureInfo.InvariantCulture));
            }

            var query = this.AbstractQuery as Query;

            if (query != null)
            {
                var tnElement = Content.AbstractQuery.Untyped.Attribute("typeNames");
                if (tnElement != null)
                {
                    ret.Add("typenames", string.Join(",", tnElement.Value.Split(' ').Select(s => HttpUtility.UrlEncode(s))));
                }

                if (query.Untyped.Elements("{http://www.opengis.net/cat/csw/2.0.2}ElementSetName").Any <XElement>() && !string.IsNullOrEmpty(query.ElementSetName.TypedValue))
                {
                    ret.Add("elementsetname", HttpUtility.UrlEncode(query.ElementSetName.TypedValue));
                }
                if ((query.ElementName != null) && (query.ElementName.Count > 0))
                {
                    var elementNames = from el in query.Untyped.Descendants()
                                       where el.Name == "{http://www.opengis.net/cat/csw/2.0.2}ElementName"
                                       select HttpUtility.UrlEncode(el.Value);

                    ret.Add("elementname", string.Join(",", elementNames));
                }

                if (query.Untyped.Element("{http://www.opengis.net/cat/csw/2.0.2}Constraint") != null)
                {
                    if (query.Constraint.Untyped.Element("{http://www.opengis.net/ogc}Filter") != null)
                    {
                        ret.Add("constraintlanguage", "FILTER");
                        ret.Add("constraint", HttpUtility.UrlEncode(query.Constraint.Filter.Untyped.ToString(SaveOptions.OmitDuplicateNamespaces)));
                    }
                    else if (!string.IsNullOrWhiteSpace(query.Constraint.CqlText))
                    {
                        ret.Add("constraintlanguage", "CQL_TEXT");
                        ret.Add("constraint", string.Concat("\"", HttpUtility.UrlEncode(query.Constraint.CqlText), "\""));
                    }
                    if (!string.IsNullOrEmpty(query.Constraint.version))
                    {
                        ret.Add("constraint_language_version", query.Constraint.version);
                    }
                }
            }

            return(ret);
        }