To generate dictionary used in code generation
Esempio n. 1
0
        /// <summary>
        /// Generates the code DOM tree.
        /// </summary>
        /// <param name="codeXml">The code XML.</param>
        /// <param name="url">The URL.</param>
        /// <param name="namespaceName">Name of the namespace.</param>
        /// <param name="classNamePrefix">The class name prefix.</param>
        /// <param name="outputCodeCompileUnit">The output code compile unit.</param>
        /// <param name="useBaseClass">if set to <c>true</c> [use base class].</param>
        public static void GenerateCodeDomTree(
            string codeXml,
            string url,
            string namespaceName,
            string classNamePrefix,
            CodeCompileUnit outputCodeCompileUnit,
            bool useBaseClass)
        {
            if (string.IsNullOrEmpty(codeXml))
            {
                throw new ArgumentException(string.Format(Resources.RssToolkit.Culture, Resources.RssToolkit.ArgmentException, "codeXml"));
            }

            if (namespaceName == null)
            {
                throw new ArgumentNullException("namespaceName");
            }

            if (string.IsNullOrEmpty(classNamePrefix))
            {
                throw new ArgumentException(string.Format(Resources.RssToolkit.Culture, Resources.RssToolkit.ArgmentException, "classNamePrefix"));
            }

            if (outputCodeCompileUnit == null)
            {
                throw new ArgumentNullException("outputCodeCompileUnit");
            }

            if (outputCodeCompileUnit.Namespaces == null)
            {
                throw new ArgumentNullException("outputCodeCompileUnit.Namespaces");
            }

            RssCodeTreeGenerator           codeTree        = new RssCodeTreeGenerator();
            Dictionary <string, ClassInfo> classDictionary = codeTree.ConvertToDictionary(codeXml);

            // generate namespace
            CodeNamespace generatedNamespace = new CodeNamespace(namespaceName);

            generatedNamespace.Imports.Add(new CodeNamespaceImport("System"));
            generatedNamespace.Imports.Add(new CodeNamespaceImport("System.Xml"));
            generatedNamespace.Imports.Add(new CodeNamespaceImport("System.Xml.Serialization"));
            generatedNamespace.Imports.Add(new CodeNamespaceImport("System.Collections.Generic"));
            generatedNamespace.Imports.Add(new CodeNamespaceImport(NamespaceConstant));

            foreach (string classKey in classDictionary.Keys)
            {
                ClassInfo classInfo = classDictionary[classKey];
                if (classInfo.Properties.Count > 0)
                {
                    string className             = classNamePrefix + CodeNameFromRssName(classInfo.Name);
                    CodeTypeDeclaration itemType = new CodeTypeDeclaration(className);

                    if (classInfo.Name.Equals(RssConstant, StringComparison.OrdinalIgnoreCase))
                    {
                        itemType.CustomAttributes.Add(new CodeAttributeDeclaration("System.Serializable"));
                        itemType.CustomAttributes.Add(new CodeAttributeDeclaration("System.Xml.Serialization.XmlRoot", new CodeAttributeArgument(new CodePrimitiveExpression("rss"))));

                        if (useBaseClass)
                        {
                            itemType.BaseTypes.Add(new CodeTypeReference(RssNamespaceConstant));
                        }

                        if (!string.IsNullOrEmpty(url))
                        {
                            string itemTypeName = classNamePrefix + "Item";

                            //// LoadRss (and LoadRssItems) method
                            GenerateLoad(itemType, url);
                            GenerateLoadRssByUrl(itemType);
                            GenerateLoadRssByXmlTextReader(itemType);
                            GenerateLoadRssByXml(itemType);
                            GenerateLoadRssItems(itemType, itemTypeName);
                        }

                        GenerateToXml(itemType);
                        GenerateToDataSet(itemType);
                    }

                    if (!NamespaceContainsType(generatedNamespace, itemType.Name))
                    {
                        generatedNamespace.Types.Add(itemType);
                    }

                    foreach (string propertyKey in classInfo.Properties.Keys)
                    {
                        PropertyInfo property = classInfo.Properties[propertyKey];

                        string propertyName = classNamePrefix +
                                              CodeNameFromRssName(property.Name);
                        AddCodeProperty(
                            property.Name,
                            property.IsAttribute ? XmlNodeType.Attribute : XmlNodeType.Element,
                            (classDictionary.ContainsKey(property.Name) && classDictionary[property.Name].Properties.Count > 0) ? new CodeTypeReference(propertyName) : new CodeTypeReference("System.String"),
                            itemType,
                            property.Occurrences > 1 ? true : false,
                            (classDictionary.ContainsKey(property.Name) ? classDictionary[property.Name].Namespace : string.Empty));
                    }

                    if (classInfo.IsText)
                    {
                        AddCodeProperty("Text", XmlNodeType.Text, new CodeTypeReference("System.String"), itemType, false, string.Empty);
                    }
                }
            }

            if (useBaseClass)
            {
                // generate http handler base class
                string handlerTypeName          = classNamePrefix + "HttpHandlerBase";
                CodeTypeDeclaration handlerType = new CodeTypeDeclaration(handlerTypeName);

                handlerType.BaseTypes.Add(new CodeTypeReference(HttpHandlerNamespaceConstant, new CodeTypeReference[1] {
                    new CodeTypeReference(classNamePrefix + RssConstant)
                }));

                generatedNamespace.Types.Add(handlerType);
            }

            // add the generated namespace to the code compile unit
            outputCodeCompileUnit.Namespaces.Add(generatedNamespace);
        }
Esempio n. 2
0
        /// <summary>
        /// Generates the code DOM tree.
        /// </summary>
        /// <param name="codeXml">The code XML.</param>
        /// <param name="url">The URL.</param>
        /// <param name="namespaceName">Name of the namespace.</param>
        /// <param name="classNamePrefix">The class name prefix.</param>
        /// <param name="outputCodeCompileUnit">The output code compile unit.</param>
        /// <param name="useBaseClass">if set to <c>true</c> [use base class].</param>
        public static void GenerateCodeDomTree(
            string codeXml, 
            string url,
            string namespaceName, 
            string classNamePrefix,
            CodeCompileUnit outputCodeCompileUnit, 
            bool useBaseClass)
        {
            if (string.IsNullOrEmpty(codeXml))
            {
                throw new ArgumentException(string.Format(Resources.RssToolkit.Culture, Resources.RssToolkit.ArgmentException, "codeXml"));
            }

            if (namespaceName == null)
            {
                throw new ArgumentNullException("namespaceName");
            }

            if (string.IsNullOrEmpty(classNamePrefix))
            {
                throw new ArgumentException(string.Format(Resources.RssToolkit.Culture, Resources.RssToolkit.ArgmentException, "classNamePrefix"));
            }

            if (outputCodeCompileUnit == null)
            {
                throw new ArgumentNullException("outputCodeCompileUnit");
            }

            if (outputCodeCompileUnit.Namespaces == null)
            {
                throw new ArgumentNullException("outputCodeCompileUnit.Namespaces");
            }

            RssCodeTreeGenerator codeTree = new RssCodeTreeGenerator();
            Dictionary<string, ClassInfo> classDictionary = codeTree.ConvertToDictionary(codeXml);

            // generate namespace
            CodeNamespace generatedNamespace = new CodeNamespace(namespaceName);
            generatedNamespace.Imports.Add(new CodeNamespaceImport("System"));
            generatedNamespace.Imports.Add(new CodeNamespaceImport("System.Xml"));
            generatedNamespace.Imports.Add(new CodeNamespaceImport("System.Xml.Serialization"));
            generatedNamespace.Imports.Add(new CodeNamespaceImport("System.Collections.Generic"));
            generatedNamespace.Imports.Add(new CodeNamespaceImport(NamespaceConstant));

            foreach (string classKey in classDictionary.Keys)
            {
                ClassInfo classInfo = classDictionary[classKey];
                if (classInfo.Properties.Count > 0)
                {
                    string className = classNamePrefix + CodeNameFromRssName(classInfo.Name);
                    CodeTypeDeclaration itemType = new CodeTypeDeclaration(className);

                    if (classInfo.Name.Equals(RssConstant, StringComparison.OrdinalIgnoreCase))
                    {
                        itemType.CustomAttributes.Add(new CodeAttributeDeclaration("System.Serializable"));
                        itemType.CustomAttributes.Add(new CodeAttributeDeclaration("System.Xml.Serialization.XmlRoot", new CodeAttributeArgument(new CodePrimitiveExpression("rss"))));

                        if (useBaseClass)
                        {
                            itemType.BaseTypes.Add(new CodeTypeReference(RssNamespaceConstant));
                        }

                        if (!string.IsNullOrEmpty(url))
                        {
                            string itemTypeName = classNamePrefix + "Item";

                            //// LoadRss (and LoadRssItems) method
                            GenerateLoad(itemType, url);
                            GenerateLoadRssByUrl(itemType);
                            GenerateLoadRssByXmlTextReader(itemType);
                            GenerateLoadRssByXml(itemType);
                            GenerateLoadRssItems(itemType, itemTypeName);
                        }

                        GenerateToXml(itemType);
                        GenerateToDataSet(itemType);
                    }

                    if (!NamespaceContainsType(generatedNamespace, itemType.Name))
                    {
                        generatedNamespace.Types.Add(itemType);
                    }

                    foreach (string propertyKey in classInfo.Properties.Keys)
                    {
                        PropertyInfo property = classInfo.Properties[propertyKey];

                        string propertyName = classNamePrefix +
                            CodeNameFromRssName(property.Name);
                        AddCodeProperty(
                            property.Name,
                            property.IsAttribute ? XmlNodeType.Attribute : XmlNodeType.Element,
                            (classDictionary.ContainsKey(property.Name) && classDictionary[property.Name].Properties.Count > 0) ? new CodeTypeReference(propertyName) : new CodeTypeReference("System.String"),
                            itemType,
                            property.Occurrences > 1 ? true : false,
                            (classDictionary.ContainsKey(property.Name) ? classDictionary[property.Name].Namespace : string.Empty));
                    }

                    if (classInfo.IsText)
                    {
                        AddCodeProperty("Text", XmlNodeType.Text, new CodeTypeReference("System.String"), itemType, false, string.Empty);
                    }
                }
            }

            if (useBaseClass)
            {
                // generate http handler base class
                string handlerTypeName = classNamePrefix + "HttpHandlerBase";
                CodeTypeDeclaration handlerType = new CodeTypeDeclaration(handlerTypeName);

                handlerType.BaseTypes.Add(new CodeTypeReference(HttpHandlerNamespaceConstant, new CodeTypeReference[1] { new CodeTypeReference(classNamePrefix + RssConstant) }));

                generatedNamespace.Types.Add(handlerType);
            }

            // add the generated namespace to the code compile unit
            outputCodeCompileUnit.Namespaces.Add(generatedNamespace);
        }