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); }
public static void Main(string[] args) { Console.WriteLine("Simple RSS Compiler v2.0"); Console.WriteLine(); // Process command line if (args.Length != 2) { Console.WriteLine("usage: rssdl.exe url-or-file outputcode.cs"); return; } string url = args[0]; string codeFilename = args[1]; string classNamePrefix = Path.GetFileNameWithoutExtension(codeFilename); // Load the channel data from supplied url string codeString; try { using (Stream feedStream = DownloadManager.GetFeed(url)) { using (XmlTextReader reader = new XmlTextReader(feedStream)) { codeString = RssXmlHelper.ConvertToRssXml(reader); try { // Open the output code file using (TextWriter codeWriter = new StreamWriter(codeFilename, false)) { // Get the language from file extension string lang = Path.GetExtension(codeFilename); if (lang != null && lang.Length > 1 && lang.StartsWith(".")) { lang = lang.Substring(1).ToUpperInvariant(); } else { lang = "CS"; } // Generate source try { RssCodeGenerator.GenerateCode(codeString, url, lang, string.Empty, classNamePrefix, codeWriter, true); Console.WriteLine("Done -- generated '{0}'.", codeFilename); return; } catch (Exception e) { Console.WriteLine("*** Error generating '{0}' *** {1}: {2}", codeFilename, e.GetType().Name, e.Message); } } File.Delete(codeFilename); } catch (Exception e) { Console.WriteLine("*** Failed to open '{0}' for writing *** {1}: {2}", codeFilename, e.GetType().Name, e.Message); } } } } catch (Exception e) { Console.WriteLine("*** Failed to load '{0}' *** {1}: {2}", url, e.GetType().Name, e.Message); } }
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); } }