650 likes | 795 Views
实例教程:一个组件开发. 需求说明 结构设计 代码实现 测试 文档生成 混淆. 需求说明. 一个用于生成 HTML 文件的 .NET 组件 支持用户以编程的方式生成 HTML 文件 支持的 HTML 元素包括文本和图像,其它内容的支持容易通过进一步的扩展来实现 输出为磁盘文件或内存流 接口清晰,方便使用 用于电子商务中的数据展示. Document. Paragraphs. Text. Image. 结构设计. 设计思路:面向用户的文档结构. 建模. 建模工具- UML UML 图的工具 Rational Rose
E N D
实例教程:一个组件开发 • 需求说明 • 结构设计 • 代码实现 • 测试 • 文档生成 • 混淆
需求说明 • 一个用于生成HTML文件的.NET组件 • 支持用户以编程的方式生成HTML文件 • 支持的HTML元素包括文本和图像,其它内容的支持容易通过进一步的扩展来实现 • 输出为磁盘文件或内存流 • 接口清晰,方便使用 • 用于电子商务中的数据展示
Document Paragraphs Text Image 结构设计 • 设计思路:面向用户的文档结构
建模 • 建模工具-UML • UML图的工具 • Rational Rose • Microsoft Visio
类设计-Paragraph • 抽象类 • 属性 • ID:该paragraph的唯一标识
类设计-Paragraphs • 集合类,Paragraph对象的集合 • 继承CollectionBase • 方法 • public void Add(Paragraph paragraph) • public Paragraph this[int index] • public Paragraph this[string paragraphID] • public int IndexOf(Paragraph paragraph)
类设计-Document • 属性 • public Paragraphs Paragraphs • Public string Title • 方法 • public void Save(string path) • public void Save(stream outStream)
类设计-Text • 继承Paragraph • 属性 • public string Content • public string FontName • public int FontSize • public string Color • 方法 • public Text(string content)
类设计-Image • 继承Paragraph • 属性 • public string Src • public int Width • public int Height • 方法 • public Image(string src) • public Image(string src,int width,int height)
类设计-DocumentGenerator • 属性 • 方法 • internal MemoryStream GenerateHTML(Document doc) • private void WriteHead() • private void WriteTitle() • private void WriteParagraphs() • private void WriteTail()
类设计-TextGenerator • 属性 • 方法 • internal string GenerateTextTag(HTMLWriter.Text text)
类设计-ImageGenerator • 属性 • 方法 • internal string GenerateImageTag(HTMLWriter.Image img)
代码实现-Paragraph /// <summary> /// Represents a paragraph in the HTML document. /// </summary> public abstract class Paragraph { /// <summary> /// Initializes a new instance of the <see cref="Paragraph"/> class. /// </summary> public Paragraph() { }
代码实现-Paragraph /// <summary> /// The ID of the paragraph. /// </summary> private string paraID; /// <summary> /// Gets or sets a string that indicates the ID of the paragraph. /// </summary> public string ID { get { return this.paraID; } set { this.paraID = value; } }
代码实现-Paragraphs /// <summary> /// Represents a collection of <see cref="Paragraph"/> objects. /// </summary> public class Paragraphs : CollectionBase { /// <summary> /// Initializes a new instance of the <see cref="Paragraphs"/> class. /// </summary> public Paragraphs() { }
代码实现-Paragraphs /// <summary> /// Adds a new <see cref="Paragraph"/> object into the collection. /// </summary> /// <param name="paragraph">The <see cref="Paragraph"/> object to be added.</param> public void Add(Paragraph paragraph) { this.List.Add(paragraph); }
代码实现-Paragraphs /// <summary> /// Gets or sets a <see cref="Paragraph"/> object from the collection according to Paragraph index. /// </summary> public Paragraph this[int index] { get { if (index >= 0 && index < this.Count) return (Paragraph)this.List[index]; else throw new ArgumentException("Invalid index in Paragraphs indexer: " + index.ToString()); }
代码实现-Paragraphs set { if (index >= 0 && index < this.Count) this.List[index] = value; else throw new ArgumentException("Invalid index in Paragraphs indexer: " + index.ToString()); } }
代码实现-Paragraphs /// <summary> /// Gets or sets a <see cref="Paragraph"/> object from the collection according to Paragraph ID. /// </summary> public Paragraph this[string paragraphID] { get { Paragraph resultParagraph = null; foreach (Paragraph curParagraph in this) { if (String.Compare(curParagraph.ID, paragraphID,false) == 0) { resultParagraph = curParagraph; break; } }
代码实现-Paragraphs if (resultParagraph != null) return resultParagraph; else throw new ArgumentException("Paragraph ID not found in Paragraphs indexer: " + paragraphID); }
代码实现-Paragraphs set { Paragraph resultParagraph = null; foreach (Paragraph curParagraph in this) { if (String.Compare(curParagraph.ID, paragraphID, false) == 0) { resultParagraph = curParagraph; break; } } if (resultParagraph != null) resultParagraph = value; else throw new ArgumentException("Paragraph ID not found in Paragraphs indexer: " + paragraphID); } }
代码实现-Paragraphs /// <summary> /// Gets the index of a specified <see cref="Paragraph"/> object in the collection. /// </summary> /// <param name="paragraph">The specified paragraph.</param> /// <returns>The index value.</returns> public int IndexOf(Paragraph paragraph) { return this.List.IndexOf(paragraph); }
代码实现-Document /// <summary> /// Represents a HTML document. /// </summary> public class Document { /// <summary> /// Initializes a new instance of the <see cref="Document"/> class. /// </summary> public Document() { }
代码实现-Document /// <summary> /// The title of the HTML. /// </summary> private string htmlTitle; /// <summary> /// Gets or sets a string that indicates the Title of the HTML. /// </summary> public string Title { get { return htmlTitle; } set { htmlTitle = value; } }
代码实现-Document /// <summary> /// The paragraphs in the HTML. /// </summary> private Paragraphs parasInHTML; /// <summary> /// Gets or sets a <see cref="Paragraphs"/> object that indicates the paragraphs in the HTML. /// </summary> public Paragraphs Paragraphs { get { return parasInHTML; } set { parasInHTML = value; } }
代码实现-Document /// <summary> /// Save the HTML into file. /// </summary> /// <param name="path">The path of the HTML file.</param> public void Save(string path) { } /// <summary> /// Save the HTML into stream. /// </summary> /// <param name="outStream">The stream that the HTML is saved to.</param> public void Save(Stream outStream) { }
代码实现-Text /// <summary> /// Represents a text in HTML document. /// </summary> public class Text : Paragraph { /// <summary> /// Initializes a new instance of the <see cref="Text"/> class. /// </summary> public Text() { } /// <summary> /// Initializes a new instance of the <see cref="Text"/> class. /// </summary> public Text(string content) { textContent = content; }
代码实现-Text /// <summary> /// The content of the Text. /// </summary> private string textContent; /// <summary> /// Gets or sets a string that indicates the content of the Text. /// </summary> public string Content { get { return textContent; } set { textContent = value; } }
代码实现-Text /// <summary> /// The font name of the Text. /// </summary> private string textFontName; /// <summary> /// Gets or sets a string that indicates the font name of the Text. /// </summary> public string FontName { get { return textFontName; } set { textFontName = value; } }
代码实现-Text /// <summary> /// The font size of the Text. /// </summary> private int textFontSize= 0; /// <summary> /// Gets or sets a string that indicates the font size of the Text. /// </summary> public int FontSize { get { return textFontSize; } set { textFontSize = value; } }
代码实现-Text /// <summary> /// The color of the Text. /// </summary> private string textColor; /// <summary> /// Gets or sets a string that indicates the color of the Text. /// </summary> public string Color { get { return textColor; } set { textColor = value; } }
代码实现-Image /// <summary> /// Represents an image in HTML document. /// </summary> public class Image : Paragraph { /// <summary> /// Initializes a new instance of the <see cref="Image"/> class. /// </summary> public Image() { }
代码实现-Image /// <summary> /// The source of the Image. /// </summary> private string source; /// <summary> /// Gets or sets a string that indicates the source of the Image. /// </summary> public string Src { get { return source; } set { source = value; } }
代码实现-Image /// <summary> /// The width of the image. /// </summary> private int imageWidth= 0; /// <summary> /// Gets or sets a string that indicates the width of the image. The unit is pixel. /// </summary> public int Width { get { return imageWidth; } set { imageWidth = value; } }
代码实现-Image /// <summary> /// The height of the image. /// </summary> private int imageHeight= 0; /// <summary> /// Gets or sets a string that indicates the height of the image. The unit is pixel. /// </summary> public int Height { get { return imageHeight; } set { imageHeight = value; } }
代码实现-Image /// <summary> /// Initializes a new instance of the <see cref="Image"/> class. /// </summary> /// <param name="src">The source of the image.</param> public Image(string src) { this.source = src; }
代码实现-Image /// <summary> /// Initializes a new instance of the <see cref="Image"/> class. /// </summary> /// <param name="src">The source of the image.</param> /// <param name="width">The width of the image</param> /// <param name="height">The height of the image.</param> public Image(string src,int width,int height) { this.source = src; this.Width = width; this.Height = height; }
代码实现-TextGenerator namespace HTMLGenerator { internal class TextGenerator { internal string GenerateTextTag(HTMLWriter.Text text) { string result = ""; result += "<span"; string style = ""; if(text.FontSize > 0) style = style + " font-size: " + text.FontSize.ToString() + "pt"; if (text.FontName != null) { if (style.Length > 0) style += ";"; style = style + " font-family: " + text.FontName; }
代码实现-TextGenerator if (text.Color != null) { if (style.Length > 0) style += ";"; style = style + " color: " + text.Color; } if (style.Length > 0) result = result + "style=\"" + style + "\""; result += ">"; result += text.Content; result += "</span>\n"; return result; }
代码实现-ImageGenerator internal class ImageGenerator { internal string GenerateImageTag(HTMLWriter.Image img) { string result = "<img Src=\"" + img.Src + "\""; if (img.Width > 0) result = result + " width=\"" + img.Width.ToString() + "\""; if (img.Height > 0) result = result + " height=\"" + img.Height.ToString() + "\""; result += " />\n"; return result; } }
代码实现-DocumentGenerator internal class DocumentGenerator { internal DocumentGenerator(){} private Document htmlDoc; private MemoryStream htmlStream; private StreamWriter sw; private void WriteHead() { sw.Write("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n"); sw.Write("<html xmlns=\"http://www.w3.org/1999/xhtml\" >\n"); }
代码实现-DocumentGenerator private void WriteTitle() { if (htmlDoc.Title != null) { sw.Write("<head>\n"); sw.Write("<title>" + htmlDoc.Title + "</title>\n"); sw.Write("</head>\n"); } } private void WriteTail() { sw.Write("</html>"); }
代码实现-DocumentGenerator private void WriteParagraphs() { sw.Write("<body>\n"); foreach(Paragraph para in this.htmlDoc.Paragraphs) { if (para is HTMLWriter.Text) { HTMLWriter.Text text = para as HTMLWriter.Text; TextGenerator tg = new TextGenerator(); sw.Write(tg.GenerateTextTag(text)); } else if (para is HTMLWriter.Image) { HTMLWriter.Image img = para as HTMLWriter.Image; ImageGenerator ig = new ImageGenerator(); sw.Write(ig.GenerateImageTag(img)); } sw.Write("<br />"); } sw.Write("</body>\n"); }
代码实现-DocumentGenerator internal MemoryStream GenerateHTML(Document doc) { htmlDoc = doc; htmlStream = new MemoryStream(); sw = new StreamWriter(htmlStream); WriteHead(); WriteTitle(); WriteParagraphs(); WriteTail(); sw.Flush(); return htmlStream; }
代码实现-Document public void Save(string path) { HTMLGenerator.DocumentGenerator dg = new HTMLGenerator.DocumentGenerator(); MemoryStream ms = dg.GenerateHTML(this); ms.WriteTo(new FileStream(path, FileMode.Create)); ms.Close(); } public void Save(Stream outStream) { HTMLGenerator.DocumentGenerator dg = new HTMLGenerator.DocumentGenerator(); MemoryStream ms = dg.GenerateHTML(this); ms.WriteTo(outStream); ms.Close(); }
测试 添加一个新的Console项目,并在项目中添加组件的引用