/// <summary>
        /// Returns a Template created from a locator string generated by CreateLocator.
        /// </summary>
        /// <param name="locator">A locator string provided by CreateLocator.</param>
        /// <returns></returns>
        public static Template Locate(string locator)
        {
            if (string.IsNullOrEmpty(locator))
            {
                throw new ArgumentNullException("locator");
            }

            string decryptedLocator = Util.DecryptString(locator);

            string[] tokens = decryptedLocator.Split('|');
            if (tokens.Length != 4)
            {
                throw new Exception("Invalid template locator.");
            }

            string fileName        = tokens[0];
            string switches        = tokens[1];
            string key             = tokens[2];
            string locationLocator = tokens[3];

            Template template = new Template(fileName, TemplateLocation.Locate(locationLocator), switches);

            template.Key = key;
            template.UpdateFileName();
            return(template);
        }
Beispiel #2
0
        /// <summary>
        /// Create a <c>TemplateLocation</c> object from an encrypted locator string returned by <c>TemplateLocation.CreateLocator</c>.
        /// </summary>
        /// <param name="encodedLocator">An encoded template locator.</param>
        /// <returns>A <c>TemplateLocation</c> object.</returns>
        public static TemplateLocation Locate(string encodedLocator)
        {
            string locator  = Util.DecryptString(encodedLocator);
            int    stampLen = locator.IndexOf('|');

            if (stampLen == -1)
            {
                return(null);
            }
            string stamp   = locator.Substring(0, stampLen);
            string content = locator.Substring(stampLen + 1, locator.Length - (stampLen + 1));

            foreach (Type type in _registeredTypes)
            {
                if (stamp == type.ToString())
                {
                    object           obj = System.Runtime.Serialization.FormatterServices.GetSafeUninitializedObject(type);
                    TemplateLocation templateLocation = obj as TemplateLocation;
                    if (templateLocation == null)
                    {
                        throw new Exception("Invalid template location.");
                    }

                    try
                    {
                        templateLocation.DeserializeContent(content);
                    }
                    catch (Exception)
                    {
                        throw new Exception("Invalid template location.");
                    }
                    return(templateLocation);
                }
            }
            throw new Exception("The type " + stamp + " is not registered as a TemplateLocation. Call TemplateLocation.RegisterLocation at application start-up.");
        }