/// <summary>
        /// Builds the request URL for template.
        /// </summary>
        /// <returns>The request URL for template.</returns>
        /// <param name="remoteUrlTemplate">Remote URL template.</param>
        /// <param name="searchParameters">Search parameters.</param>
        public static OpenSearchUrl BuildRequestUrlForTemplate(OpenSearchDescriptionUrl remoteUrlTemplate, NameValueCollection searchParameters, QuerySettings querySettings)
        {
            // container for the final query url
            UriBuilder finalUrl = new UriBuilder(remoteUrlTemplate.Template);
            // parameters for final query
            NameValueCollection finalQueryParameters = new NameValueCollection();

            // Parse the possible parametrs of the remote urls
            NameValueCollection remoteParametersDef = HttpUtility.ParseQueryString(finalUrl.Query);

            // For each parameter requested
            foreach (string parameter_id in searchParameters.AllKeys) {
                if (remoteParametersDef[parameter_id] == null)
                {
                    // if forced, set the param
                    if (querySettings.ForceUnspecifiedParameters)
                    {
                        if (!(querySettings.SkipNullOrEmptyQueryStringParameters && string.IsNullOrEmpty(searchParameters[parameter_id])))
                            finalQueryParameters.Set(parameter_id, searchParameters[parameter_id]);
                    }
                    continue;
                }
                // first find the defintion of the parameter in the url template
                foreach (var key in remoteParametersDef.GetValues(parameter_id)) {
                    Match matchParamDef = Regex.Match(key, @"^{([^?]+)\??}$");
                    // If parameter does not exist, continue
                    if (!matchParamDef.Success)
                        continue;
                    // We have the parameter defintion
                    string paramDef = matchParamDef.Groups[1].Value;
                    string paramValue = searchParameters[parameter_id];

                    if ( !(querySettings.SkipNullOrEmptyQueryStringParameters && string.IsNullOrEmpty(paramValue)))
                        finalQueryParameters.Set(parameter_id, paramValue);
                }

            }

            string[] queryString = Array.ConvertAll(finalQueryParameters.AllKeys, key => string.Format("{0}={1}", key, HttpUtility.UrlEncode(finalQueryParameters[key])));
            finalUrl.Query = string.Join("&", queryString);

            return new OpenSearchUrl(finalUrl.Uri);
        }
        public OpenSearchDescription GetOpenSearchDescription()
        {
            OpenSearchDescription osd = new OpenSearchDescription();
            osd.ShortName = this.Identifier;
            osd.Description = this.Description;
            osd.SyndicationRight = "open";
            osd.AdultContent = "false";
            osd.Language = "en-us";
            osd.OutputEncoding = "UTF-8";
            osd.InputEncoding = "UTF-8";
            osd.Developer = "Terradue GeoSpatial Development Team";

            // Create the union Link

            OpenSearchDescriptionUrl url = new OpenSearchDescriptionUrl("application/atom+xml", "dummy://dummy", "results");

            osd.Url = new OpenSearchDescriptionUrl[1];
            osd.Url[0] = url;

            return osd;
        }
 //---------------------------------------------------------------------------------------------------------------------
 /// <summary>Creates an OpenSearchParameterValueSet instance based on the specified OpenSearchDescriptionUrl object.</summary>
 /// <returns>The created OpenSearchParameterValueSet instance.</returns>
 /// <param name="osdUrl">An OpenSearchDescriptionUrl object from a deserialized OpenSearch description document.</param>
 public static OpenSearchParameterValueSet FromOpenSearchDescription(OpenSearchDescriptionUrl osdUrl, XmlSerializerNamespaces namespaces = null)
 {
     OpenSearchParameterValueSet result = FromOpenSearchDescription(osdUrl.Template, osdUrl.Parameters, namespaces);
     result.ContentType = osdUrl.Type;
     return result;
 }
        public OpenSearchDescription GetOpenSearchDescription()
        {
            OpenSearchDescription osd = new OpenSearchDescription();
            osd.ShortName = "test";
            osd.Contact = "*****@*****.**";
            osd.SyndicationRight = "open";
            osd.AdultContent = "false";
            osd.Language = "en-us";
            osd.OutputEncoding = "UTF-8";
            osd.InputEncoding = "UTF-8";
            osd.Developer = "Terradue OpenSearch Development Team";
            osd.Attribution = "Terradue";

            List<OpenSearchDescriptionUrl> urls = new List<OpenSearchDescriptionUrl>();

            UriBuilder urlb = new UriBuilder("dummy://localhost/test/description");

            OpenSearchDescriptionUrl url = new OpenSearchDescriptionUrl("application/opensearchdescription+xml", urlb.ToString(), "self");
            url.Parameters = OpenSearchFactory.GetDefaultParametersDescription(100).ToArray();
            urls.Add(url);

            urlb = new UriBuilder("dummy://localhost/test/search");
            NameValueCollection query = GetOpenSearchParameters("application/atom+xml");

            string[] queryString = Array.ConvertAll(query.AllKeys, key => string.Format("{0}={1}", key, query[key]));
            urlb.Query = string.Join("&", queryString);
            url = new OpenSearchDescriptionUrl("application/atom+xml", urlb.ToString(), "search");
            url.IndexOffset = 1;
            urls.Add(url);

            osd.Url = urls.ToArray();

            return osd;
        }
        public static NameValueCollection ReplaceTemplateByIdentifier(NameValueCollection osParameters, OpenSearchDescriptionUrl osdUrl)
        {
            NameValueCollection dic = osdUrl.GetIdentifierDictionary();
            NameValueCollection newNvc = new NameValueCollection();
            foreach (var templateKey in osParameters.AllKeys) {
                if (dic[templateKey] == null)
                    continue;
                newNvc.Add(dic[templateKey], osParameters[templateKey]);

            }
            return newNvc;
        }
 /// <summary>
 /// Gets the open search parameters.
 /// </summary>
 /// <returns>The open search parameters.</returns>
 /// <param name="osUrl">Os URL.</param>
 public static NameValueCollection GetOpenSearchParameters(OpenSearchDescriptionUrl osUrl)
 {
     Uri finalUrl = new Uri(osUrl.Template);
     return HttpUtility.ParseQueryString(finalUrl.Query);
 }
        /// <summary>
        /// Builds the request URL for template.
        /// </summary>
        /// <returns>The request URL for template.</returns>
        /// <param name="remoteUrlTemplate">Remote URL template.</param>
        /// <param name="searchParameters">Search parameters.</param>
        /// <param name="urlTemplateDef">URL template def.</param>
        public static OpenSearchUrl BuildRequestUrlForTemplate(OpenSearchDescriptionUrl remoteUrlTemplate, NameValueCollection searchParameters, NameValueCollection requestUrlTemplateDef, QuerySettings querySettings)
        {
            // container for the final query url
            UriBuilder finalUrl = new UriBuilder(remoteUrlTemplate.Template);
            // parameters for final query
            NameValueCollection finalQueryParameters = new NameValueCollection();

            // Parse the possible parameters of the remote url template
            NameValueCollection remoteParametersDef = HttpUtility.ParseQueryString(finalUrl.Query);

            // control duplicates
            foreach (string key in remoteParametersDef.AllKeys) {
                if (string.IsNullOrEmpty(key))
                    continue;
                int count = remoteParametersDef.GetValues(key).Count();
                if (count > 1) {
                    var value = remoteParametersDef.GetValues(key)[0];
                    remoteParametersDef.Remove(key);
                    remoteParametersDef.Add(key, value);
                }
            }

            // For each parameter id set for search
            foreach (string parameter_id in searchParameters.AllKeys) {
                // skip if parameter is not in the template unless it is forced
                if (requestUrlTemplateDef[parameter_id] == null)
                {
                    // if forced, set the param
                    if (querySettings.ForceUnspecifiedParameters)
                    {
                        if (!(querySettings.SkipNullOrEmptyQueryStringParameters && string.IsNullOrEmpty(searchParameters[parameter_id])))
                        finalQueryParameters.Set(parameter_id, searchParameters[parameter_id]);
                    }
                    continue;
                }
                // first find the defintion of the parameter in the url template
                foreach (var key in requestUrlTemplateDef.GetValues(parameter_id)) {
                    Match matchParamDef = Regex.Match(key, @"^{([^?]+)\??}$");
                    // If parameter is not respecting OpenSearch template spec, skip
                    if (!matchParamDef.Success)
                        continue;
                    // We have the parameter defintion
                    string paramDef = matchParamDef.Groups[1].Value;
                    string paramValue = searchParameters[parameter_id];

                    // Find the paramdef in the remote URL template
                    foreach (string keyDef in remoteParametersDef.AllKeys) {
                        foreach (var key2 in remoteParametersDef.GetValues(keyDef)) {
                            Match remoteMatchParamDef = Regex.Match(key2, @"^{(" + paramDef + @")\??}$");
                            // if match is successful
                            if (remoteMatchParamDef.Success) {
                                // then add the parameter with the right key
                                if (!(querySettings.SkipNullOrEmptyQueryStringParameters && string.IsNullOrEmpty(paramValue)))
                                    finalQueryParameters.Set(keyDef, paramValue);
                            }
                        }
                    }
                }

            }

            // All other remote query parameters
            foreach (string parameter in remoteParametersDef.AllKeys) {
                Match matchParamDef = Regex.Match(remoteParametersDef[parameter], @"^{([^?]+)\??}$");
                // If parameter does not exist, continue
                if (!matchParamDef.Success && !string.IsNullOrEmpty(parameter))
                {
                    if (!(querySettings.SkipNullOrEmptyQueryStringParameters && string.IsNullOrEmpty(remoteParametersDef[parameter])))
                        finalQueryParameters.Set(parameter, remoteParametersDef[parameter]);
                }
            }

            //finalQueryParameters.Set("enableSourceproduct", "true");
            string[] queryString = Array.ConvertAll(finalQueryParameters.AllKeys, key => string.Format("{0}={1}", key, HttpUtility.UrlEncode(finalQueryParameters[key])));
            finalUrl.Query = string.Join("&", queryString);

            return new OpenSearchUrl(finalUrl.Uri);
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="Terradue.OpenSearch.OpenSearchUrl"/> class.
 /// </summary>
 /// <param name="osdu">Osdu.</param>
 public OpenSearchUrl(OpenSearchDescriptionUrl osdu)
     : base(osdu.Template)
 {
     this.osdIndexOffset = osdu.IndexOffset;
     this.osdPageOffset = osdu.PageOffset;
 }