コード例 #1
0
        static bool ComplainExcessAttributes(Directive dt, ParsedTemplate pt)
        {
            if (dt.Attributes.Count == 0)
            {
                return(false);
            }
            StringBuilder sb    = new StringBuilder("Unknown attributes ");
            bool          first = true;

            foreach (string key in dt.Attributes.Keys)
            {
                if (!first)
                {
                    sb.Append(", ");
                }
                else
                {
                    first = false;
                }
                sb.Append(key);
            }
            sb.Append(" found in ");
            sb.Append(dt.Name);
            sb.Append(" directive.");
            pt.LogWarning(sb.ToString(), dt.StartLocation);
            return(false);
        }
コード例 #2
0
        public static TemplateSettings GetSettings(ITextTemplatingEngineHost host, ParsedTemplate pt)
        {
            string language = null;

            TemplateSettings settings = new TemplateSettings();

            foreach (Directive dt in pt.Directives)
            {
                switch (dt.Name)
                {
                case "template":
                    string val = dt.Extract("language");
                    if (val != null)
                    {
                        language = val;
                    }
                    val = dt.Extract("debug");
                    if (val != null)
                    {
                        settings.Debug = string.Compare(val, "true", StringComparison.OrdinalIgnoreCase) == 0;
                    }
                    val = dt.Extract("inherits");
                    if (val != null)
                    {
                        settings.Inherits = val;
                    }
                    val = dt.Extract("culture");
                    if (val != null)
                    {
                        System.Globalization.CultureInfo culture = System.Globalization.CultureInfo.GetCultureInfo(val);
                        if (culture == null)
                        {
                            pt.LogWarning("Could not find culture '" + val + "'", dt.StartLocation);
                        }
                        else
                        {
                            settings.Culture = culture;
                        }
                    }
                    val = dt.Extract("hostspecific");
                    if (val != null)
                    {
                        settings.HostSpecific = string.Compare(val, "true", StringComparison.OrdinalIgnoreCase) == 0;
                    }
                    break;

                case "assembly":
                    string name = dt.Extract("name");
                    if (name == null)
                    {
                        pt.LogError("Missing name attribute in assembly directive", dt.StartLocation);
                    }
                    else
                    {
                        settings.Assemblies.Add(name);
                    }
                    break;

                case "import":
                    string namespac = dt.Extract("namespace");
                    if (namespac == null)
                    {
                        pt.LogError("Missing namespace attribute in import directive", dt.StartLocation);
                    }
                    else
                    {
                        settings.Imports.Add(namespac);
                    }
                    break;

                case "output":
                    settings.Extension = dt.Extract("extension");
                    string encoding = dt.Extract("encoding");
                    if (encoding != null)
                    {
                        settings.Encoding = Encoding.GetEncoding("encoding");
                    }
                    break;

                case "include":
                    throw new InvalidOperationException("Include is handled in the parser");

                default:
                    throw new NotImplementedException("Custom directives are not supported yet");
                }
                ComplainExcessAttributes(dt, pt);
            }

            if (settings.Name == null)
            {
                settings.Name = string.Format("GeneratedTextTransformation{0:x}", new System.Random().Next());
            }
            if (settings.Namespace == null)
            {
                settings.Namespace = typeof(TextTransformation).Namespace;
            }

            //resolve the CodeDOM provider
            if (String.IsNullOrEmpty(language))
            {
                pt.LogError("No language was specified for the template");
                return(settings);
            }

            if (language == "C#v3.5")
            {
                Dictionary <string, string> providerOptions = new Dictionary <string, string> ();
                providerOptions.Add("CompilerVersion", "v3.5");
                settings.Provider = new CSharpCodeProvider(providerOptions);
            }
            else
            {
                settings.Provider = CodeDomProvider.CreateProvider(language);
            }

            if (settings.Provider == null)
            {
                pt.LogError("A provider could not be found for the language '" + language + "'");
                return(settings);
            }

            return(settings);
        }