Advertisement
Guest User

Untitled

a guest
Apr 9th, 2011
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.33 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.Collections;
  10. using System.Text;
  11. using System.IO;
  12.  
  13. namespace HTMLTreeTest
  14. {
  15.     public partial class Form1 : Form
  16.     {
  17.         public Form1()
  18.         {
  19.             InitializeComponent();
  20.         }
  21.  
  22.         private void Form1_Load(object sender, EventArgs e)
  23.         {
  24.             comboBox1.SelectedIndex = 0;
  25.         }
  26.  
  27.         private void button1_Click(object sender, EventArgs e)
  28.         {
  29.             exportFileDialog.FileName = "";
  30.             exportFileDialog.FilterIndex = 1;
  31.  
  32.             if (exportFileDialog.ShowDialog() != DialogResult.Cancel)
  33.             {
  34.                 System.IO.StreamWriter strWri = new System.IO.StreamWriter(exportFileDialog.FileName);
  35.                 buildHTMLTree(strWri);
  36.  
  37.                 strWri.Close();
  38.  
  39.                 MessageBox.Show(
  40.                         "Saved to file " + exportFileDialog.FileName + "!",
  41.                         "",
  42.                         MessageBoxButtons.OK,
  43.                         MessageBoxIcon.Information);
  44.             }
  45.         }
  46.  
  47.         private ArrayList simulateInput()
  48.         {
  49.             ArrayList rows = new ArrayList();
  50.  
  51.             for (int i = 1; i <= 9; i++)
  52.             {
  53.                 if (i == 1)
  54.                     rows.Add(new KeywordRows("keyword_" + i.ToString(), "keyword_" + i.ToString()));
  55.  
  56.                 for (int x = 1; x <= (Convert.ToInt32(comboBox1.Text) / 9); x++)
  57.                 {
  58.                     if (i == 1)
  59.                         rows.Add(new KeywordRows("keyword_" + i.ToString() + "-" + x.ToString(), "keyword_" + i.ToString()));
  60.                     else
  61.                         rows.Add(new KeywordRows("keyword_" + i.ToString() + "-" + x.ToString(), "keyword_" + (i - 1).ToString() + "-" + x.ToString()));
  62.                 }
  63.             }
  64.  
  65.             return rows;
  66.         }
  67.  
  68.         private void buildHTMLTree(StreamWriter strWri)
  69.         {
  70.             string template = Properties.Resources.htmlTree_tmpl;
  71.             string[] templateParts = template.Split(new[] { "{KEYWORDS}" }, StringSplitOptions.None);
  72.  
  73.             Dictionary<string, string> parents = new Dictionary<string, string>();
  74.             Dictionary<string, string> childs = new Dictionary<string, string>();
  75.             ArrayList array = new ArrayList();
  76.             array = simulateInput();
  77.  
  78.            
  79.  
  80.             foreach (KeywordRows kwd in array)
  81.             {
  82.                 if (kwd.root_keyword == kwd.keyword)
  83.                 {
  84.                     if (!parents.ContainsKey(kwd.keyword))
  85.                         parents.Add(kwd.keyword, kwd.root_keyword);
  86.                 }
  87.                 else
  88.                 {
  89.                     if (!childs.ContainsKey(kwd.keyword))
  90.                         childs.Add(kwd.keyword, kwd.root_keyword);
  91.                 }
  92.             }
  93.  
  94.             strWri.Write(templateParts[0]);
  95.             strWri.Write("<ul id=\"parents\">");
  96.             foreach (string parent in parents.Values)
  97.             {
  98.                 strWri.Write("<li id=\"{0}\">{0}", parent);
  99.                 if (childs.ContainsValue(parent))
  100.                 {
  101.                     strWri.Write("<ul id=\"parents\">");
  102.                     process(ref childs,strWri, parent);
  103.                     strWri.Write("</ul>");
  104.                 }
  105.  
  106.                 strWri.Write("</li>");
  107.             }
  108.             strWri.Write("</ul>");
  109.  
  110.             strWri.Write(templateParts[1]);
  111.  
  112.             //return Properties.Resources.htmlTree_tmpl.Replace("{KEYWORDS}", sb.ToString());
  113.         }
  114.  
  115.         public void process(ref Dictionary<string, string> _childs, StreamWriter strWri, string parent)
  116.         {
  117.             var getChilds =
  118.             from o in _childs
  119.             where (o.Value == parent)
  120.             select o.Key;
  121.  
  122.             foreach (var tmp in getChilds)
  123.             {
  124.                 string child = tmp.ToString();
  125.  
  126.                 if (_childs.ContainsValue(child))
  127.                 {
  128.                     strWri.Write("<li id=\"{0}\">{0}<ul id=\"{0}\">", child);
  129.                     process(ref _childs, strWri, child);
  130.                     strWri.Write("</ul></li>");
  131.                 }
  132.                 else
  133.                 {
  134.                     strWri.Write("<li id=\"{0}\">{0}</li>", child);
  135.                 }
  136.             }
  137.  
  138.             return;
  139.         }
  140.  
  141.         public class KeywordRows
  142.         {
  143.             private string _keyword;
  144.             private string _root_keyword;
  145.  
  146.             public KeywordRows(string keyword, string root_keyword)
  147.             {
  148.                 _keyword = keyword;
  149.                 _root_keyword = root_keyword;
  150.             }
  151.  
  152.             public string keyword
  153.             {
  154.                 get
  155.                 {
  156.                     return _keyword;
  157.                 }
  158.                 set
  159.                 {
  160.                     _keyword = value;
  161.                 }
  162.             }
  163.  
  164.             public string root_keyword
  165.             {
  166.                 get
  167.                 {
  168.                     return _root_keyword;
  169.                 }
  170.                 set
  171.                 {
  172.                     _root_keyword = value;
  173.                 }
  174.             }
  175.         }
  176.     }
  177. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement