Exemplo n.º 1
0
        public static GenericRssChannel LoadChannel(XmlDocument doc)
        {
            GenericRssChannel channel = new GenericRssChannel();

            channel.LoadFromXml(doc);
            return(channel);
        }
Exemplo n.º 2
0
        public static GenericRssChannel LoadChannel(string url)
        {
            GenericRssChannel channel = new GenericRssChannel();

            channel.LoadFromUrl(url);
            return(channel);
        }
        private void button1_Click(object sender, EventArgs e)
        {
            string url = urlComboBox.Text;

            if (url != _dataSource.Url)
            {
                try {
                    // validate URL
                    GenericRssChannel.LoadChannel(url);
                    AddToHistory(url);
                }
                catch {
                    MessageBox.Show(this, "Failed to load RSS feed for the specified URL", "RssDataSource Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
            }

            _dataSource.Url = url;
            DialogResult    = DialogResult.OK;
        }
Exemplo n.º 4
0
        public static void GenerateCode(GenericRssChannel channelDefinition,
                                        string outputLanguage, string namespaceName, string classNamePrefix,
                                        TextWriter outputCode)
        {
            // get the CodeDom provider for the language
            CodeDomProvider provider = CodeDomProvider.CreateProvider(outputLanguage);

            // generate the CodeDom tree
            CodeCompileUnit unit = new CodeCompileUnit();

            GenerateCodeDomTree(channelDefinition, namespaceName, classNamePrefix, unit);

            // generate source
            CodeGeneratorOptions options = new CodeGeneratorOptions();

            options.BlankLinesBetweenMembers = true;
            options.BracingStyle             = "Block";
            options.ElseOnClosing            = false;
            options.IndentString             = "    ";

            provider.GenerateCodeFromCompileUnit(unit, outputCode, options);
        }
Exemplo n.º 5
0
        public override void GenerateCode(AssemblyBuilder assemblyBuilder)
        {
            // get name and namespace from the filename
            string fname = VirtualPathUtility.GetFileName(VirtualPath);

            fname = fname.Substring(0, fname.LastIndexOf('.')); // no extension
            int i = fname.LastIndexOf('.');

            string name, ns;

            if (i < 0)
            {
                name = fname;
                ns   = string.Empty;
            }
            else
            {
                name = fname.Substring(i + 1);
                ns   = fname.Substring(0, i);
            }

            // load as XML
            XmlDocument doc = new XmlDocument();

            using (Stream s = OpenStream(VirtualPath)) {
                doc.Load(s);
            }

            // create the channel
            GenericRssChannel channel = GenericRssChannel.LoadChannel(doc);

            // compile the channel
            CodeCompileUnit ccu = new CodeCompileUnit();

            RssCodeGenerator.GenerateCodeDomTree(channel, ns, name, ccu);
            assemblyBuilder.AddCodeCompileUnit(this, ccu);
        }
Exemplo n.º 6
0
        public override void GenerateCode(AssemblyBuilder assemblyBuilder)
        {
            // load as XML
            XmlDocument doc = new XmlDocument();

            using (Stream s = OpenStream(VirtualPath)) {
                doc.Load(s);
            }

            // valide root rssdl node
            XmlNode root = doc.DocumentElement;

            if (root.Name != "rssdl")
            {
                throw new InvalidDataException(
                          string.Format("Unexpected root node '{0}' -- expected root 'rssdl' node", root.Name));
            }

            // iterate through rss nodes
            for (XmlNode n = root.FirstChild; n != null; n = n.NextSibling)
            {
                if (n.NodeType != XmlNodeType.Element)
                {
                    continue;
                }

                if (n.Name != "rss")
                {
                    throw new InvalidDataException(
                              string.Format("Unexpected node '{0}' -- expected root 'rss' node", root.Name));
                }

                string name = string.Empty;
                string file = string.Empty;
                string url  = string.Empty;
                string ns   = string.Empty;

                foreach (XmlAttribute attr in n.Attributes)
                {
                    switch (attr.Name)
                    {
                    case "name":
                        name = attr.Value;
                        break;

                    case "url":
                        if (!string.IsNullOrEmpty(file))
                        {
                            throw new InvalidDataException("Only one of 'file' and 'url' can be specified");
                        }

                        url = attr.Value;
                        break;

                    case "file":
                        if (!string.IsNullOrEmpty(url))
                        {
                            throw new InvalidDataException("Only one of 'file' and 'url' can be specified");
                        }

                        file = VirtualPathUtility.Combine(VirtualPathUtility.GetDirectory(VirtualPath), attr.Value);
                        break;

                    case "namespace":
                        ns = attr.Value;
                        break;

                    default:
                        throw new InvalidDataException(
                                  string.Format("Unexpected attribute '{0}'", attr.Name));
                    }
                }

                if (string.IsNullOrEmpty(name))
                {
                    throw new InvalidDataException("Missing 'name' attribute");
                }

                if (string.IsNullOrEmpty(url) && string.IsNullOrEmpty(file))
                {
                    throw new InvalidDataException("Missing 'url' or 'file' attribute - one must be specified");
                }

                // load channel
                GenericRssChannel channel = null;

                if (!string.IsNullOrEmpty(url))
                {
                    channel = GenericRssChannel.LoadChannel(url);
                }
                else
                {
                    XmlDocument rssDoc = new XmlDocument();

                    using (Stream s = OpenStream(file)) {
                        rssDoc.Load(s);
                    }

                    channel = GenericRssChannel.LoadChannel(rssDoc);
                }

                // compile channel
                CodeCompileUnit ccu = new CodeCompileUnit();
                RssCodeGenerator.GenerateCodeDomTree(channel, ns, name, ccu);
                assemblyBuilder.AddCodeCompileUnit(this, ccu);
            }
        }
Exemplo n.º 7
0
        public static void GenerateCodeDomTree(GenericRssChannel channelDefinition,
                                               string namespaceName, string classNamePrefix,
                                               CodeCompileUnit outputCodeCompileUnit)
        {
            // generate namespace

            CodeNamespace generatedNamespace = new CodeNamespace(namespaceName);

            generatedNamespace.Imports.Add(new CodeNamespaceImport("System"));

            // generate item class

            string itemTypeName          = classNamePrefix + "Item";
            CodeTypeDeclaration itemType = new CodeTypeDeclaration(itemTypeName);

            itemType.BaseTypes.Add(typeof(RssElementBase));

            if (channelDefinition.Items.Count > 0)
            {
                // item ctors (one default and one that takes attributes)
                GenerateCtorsFromAttributes(itemType, channelDefinition.Items[0]);

                // item atributes as explicit properties
                GenerateAttributes(itemType, channelDefinition.Items[0]);
            }

            generatedNamespace.Types.Add(itemType);

            // generate image class

            bool   hasImage = (channelDefinition.Image != null);
            string imageTypeName;

            if (hasImage)
            {
                imageTypeName = classNamePrefix + "Image";
                CodeTypeDeclaration imageType = new CodeTypeDeclaration(imageTypeName);
                imageType.BaseTypes.Add(typeof(RssElementBase));

                // image atributes as explicit properties
                GenerateAttributes(imageType, channelDefinition.Image);

                // generate SetDefaults method for image attribute
                GenerateSetDefaultsMethod(imageType, channelDefinition.Image);

                generatedNamespace.Types.Add(imageType);
            }
            else
            {
                imageTypeName = typeof(GenericRssElement).FullName;
            }

            // generate channel class

            string channelTypeName          = classNamePrefix + "Channel";
            CodeTypeDeclaration channelType = new CodeTypeDeclaration(channelTypeName);

            channelType.BaseTypes.Add(new CodeTypeReference("RssToolkit.RssChannelBase",
                                                            new CodeTypeReference[2] {
                new CodeTypeReference(itemTypeName), new CodeTypeReference(imageTypeName)
            }));

            // channel atributes as explicit properties
            GenerateAttributes(channelType, channelDefinition);

            if (hasImage)
            {
                // generate public Image property only if the channel has an image
                GenerateImageProperty(channelType, imageTypeName);
            }

            // generate SetDefaults method
            GenerateSetDefaultsMethod(channelType, channelDefinition);

            if (!string.IsNullOrEmpty(channelDefinition.Url))
            {
                // LoadChannel (and LoadChannelItems) method
                GenerateLoadChannel(channelType, channelDefinition.Url);
                GenerateLoadChannelByUrl(channelType);
                GenerateLoadChannelItems(channelType, itemTypeName);
            }

            generatedNamespace.Types.Add(channelType);

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

            handlerType.BaseTypes.Add(new CodeTypeReference("RssToolkit.RssHttpHandlerBase",
                                                            new CodeTypeReference[3] {
                new CodeTypeReference(channelTypeName),
                new CodeTypeReference(itemTypeName),
                new CodeTypeReference(imageTypeName)
            }));

            generatedNamespace.Types.Add(handlerType);


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