public ToxySpreadsheet LoadData(DataContext context)
 {
     ParserContext pc = new ParserContext(context.Path);
     ISpreadsheetParser parser=ParserFactory.CreateSpreadsheet(pc);
     var ss=parser.Parse();
     return ss;
 }
		public async Task<bool> Process(ICrawler crawler, PropertyBag propertyBag)
		{
			if (propertyBag.StatusCode != HttpStatusCode.OK
				|| propertyBag.Response == null)
			{
				return true;
			}

			string extension = MapContentTypeToExtension(propertyBag.ContentType);
			if (extension.IsNullOrEmpty())
			{
				return true;
			}

			propertyBag.Title = propertyBag.Step.Uri.PathAndQuery;
			using (TempFile temp = new TempFile())
			{
				temp.FileName += "." + extension;
				using (FileStream fs = new FileStream(temp.FileName, FileMode.Create, FileAccess.Write, FileShare.Read, 0x1000))
				{
					await fs.WriteAsync(propertyBag.Response, 0, propertyBag.Response.Length);
				}

				ParserContext context = new ParserContext(temp.FileName);
				ITextParser parser = ParserFactory.CreateText(context);
				propertyBag.Text = parser.Parse();
			}

			return true;
		}
Exemple #3
0
 static object CreateObject(ParserContext context, Type itype)
 {
     string ext = GetFileExtention(context.Path);
     var types = parserMapping[ext];
     object obj = null;
     bool isFound = false;
     foreach (Type type in types)
     {
         obj = Activator.CreateInstance(type, context);
         if (itype.IsAssignableFrom(obj.GetType()))
         {
             isFound = true;
             break;
         }
     }
     if (!isFound)
         throw new InvalidDataException(ext + " is not supported");
     return obj;
 }
Exemple #4
0
        private void ShowDocument(string filepath, string encoding, string extension)
        {
            ParserContext context = new ParserContext(filepath);
            context.Encoding = Encoding.GetEncoding(encoding);

            if (Mode == ViewMode.Text)
            {
                AppendRichTextBox();
                var tparser = ParserFactory.CreateText(context);
                rtbPanel.Text = tparser.Parse();
                tbParserType.Text = tparser.GetType().Name;
            }
            else if (Mode == ViewMode.Structured)
            {
                switch (extension)
                {
                    case ".csv":
                        AppendSpreadsheetGrid();
                        context.Properties.Add("HasHeader", "1");
                        ISpreadsheetParser csvparser = ParserFactory.CreateSpreadsheet(context);
                        ss = csvparser.Parse();
                        tbParserType.Text = csvparser.GetType().Name;
                        var table1 = ss.Tables[0];
                        ShowToGrid(table1);
                        cbSheets.Items.Clear();
                        foreach (var table in ss.Tables)
                        {
                            cbSheets.Items.Add(table.Name);
                        }
                        cbSheets.SelectedIndex = 0;
                        panel1.Visible = true;
                        break;
                    case ".xlsx":
                    case ".xls":
                        AppendSpreadsheetGrid();
                        ISpreadsheetParser ssparser = ParserFactory.CreateSpreadsheet(context);
                        ss = ssparser.Parse();
                        tbParserType.Text = ssparser.GetType().Name;
                        var table0 = ss.Tables[0];
                        ShowToGrid(table0);
                        cbSheets.Items.Clear();
                        foreach (var table in ss.Tables)
                        {
                            cbSheets.Items.Add(table.Name);
                        }
                        cbSheets.SelectedIndex = 0;
                        panel1.Visible = true;
                        break;
                    case ".vcf":
                        AppendDataGridView();
                        var vparser = ParserFactory.CreateVCard(context);
                        ToxyBusinessCards vcards = vparser.Parse();
                        tbParserType.Text = vparser.GetType().Name;
                        gridPanel.GridView.DataSource = vcards.ToDataTable().DefaultView;
                        break;
                    case ".pptx":
                        //TODO: show slides
                        break;
                    case ".xml":
                    case ".htm":
                    case ".html":
                        AppendTreePanel();
                        var domparser = ParserFactory.CreateDom(context);
                        ToxyDom htmlDom = domparser.Parse();
                        TreeNode rootNode = treePanel.Tree.Nodes.Add(htmlDom.Root.NodeString);
                        treePanel.Tree.BeginUpdate();
                        AppendTree(rootNode, htmlDom.Root);
                        treePanel.Tree.EndUpdate();
                        //rootNode.ExpandAll();
                        break;
                }
            }
            else
            {
                AppendPropertyListPanel();
                var tparser = ParserFactory.CreateMetadata(context);
                ToxyMetadata metadatas = tparser.Parse();
                plPanel.Clear();
                foreach (var data in metadatas)
                {
                    plPanel.AddItem(data.Name, data.Value.ToString());
                }
                tbParserType.Text = tparser.GetType().Name;
            }
        }
Exemple #5
0
 public static ISpreadsheetParser CreateSpreadsheet(ParserContext context)
 {
     object obj = CreateObject(context, typeof(ISpreadsheetParser));
     ISpreadsheetParser parser = (ISpreadsheetParser)obj;
     return parser;
 }
Exemple #6
0
 public static ITextParser CreateText(ParserContext context)
 {
     object obj = CreateObject(context, typeof(ITextParser));
     ITextParser parser = (ITextParser)obj;
     return parser;
 }
Exemple #7
0
 public static VCardParser CreateVCard(ParserContext context)
 {
     object obj = CreateObject(context, typeof(VCardParser));
     VCardParser parser = (VCardParser)obj;
     return parser;
 }
Exemple #8
0
 public static IDomParser CreateDom(ParserContext context)
 {
     object obj = CreateObject(context, typeof(IDomParser));
     IDomParser parser = (IDomParser)obj;
     return parser;
 }
Exemple #9
0
        private void OpenFile(string filepath, string encoding)
        {

            if (string.IsNullOrWhiteSpace(filepath))
            {
                tbPath.Clear();
                return;
            }
            
            tbPath.Text = filepath;
            FileInfo fi = new FileInfo(filepath);
            ParserContext context = new ParserContext(filepath);
            context.Encoding = Encoding.GetEncoding(encoding);
            string extension = fi.Extension.ToLower();
            tbExtension.Text = extension;



            panel1.Visible = false;


            switch (extension)
            {
                case ".txt":
                case ".html":
                case ".htm":
                case ".pdf":
                    AppendRichTextBox();
                    var tparser = ParserFactory.CreateText(context);
                    richTextBox1.Text = tparser.Parse();
                    tbParserType.Text = tparser.GetType().Name;
                    break;
                case ".rtf":
                case ".docx":
                    AppendRichTextBox();
                    IDocumentParser docparser = ParserFactory.CreateDocument(context);
                    ToxyDocument doc = docparser.Parse();
                    tbParserType.Text = docparser.GetType().Name;
                    richTextBox1.Text = doc.ToString();
                    break;
                case ".csv":
                case ".xlsx":
                case ".xls":
                    AppendSpreadsheetGrid();
                    ISpreadsheetParser ssparser = ParserFactory.CreateSpreadsheet(context);
                    ss = ssparser.Parse();
                    tbParserType.Text = ssparser.GetType().Name;
                    //DataSet ds = ss.ToDataSet();
                    //dataGridView1.DataSource = ds.Tables[0].DefaultView;
                    var table0=ss.Tables[0];
                    ShowToGrid(table0);
                    cbSheets.Items.Clear();
                    foreach (var table in ss.Tables)
                    {
                        cbSheets.Items.Add(table.Name);
                    }
                    cbSheets.SelectedIndex = 0;
                    panel1.Visible = true;
                    break;
                case ".vcf":
                    AppendDataGridView();
                    var vparser = ParserFactory.CreateVCard(context);
                    ToxyBusinessCards vcards = vparser.Parse();
                    tbParserType.Text = vparser.GetType().Name;
                    dataGridView1.DataSource =vcards.ToDataTable().DefaultView;
                    break;
                default:
                    AppendRichTextBox();
                    richTextBox1.Text = "Unknown document";
                    tbParserType.Text = "";
                    break;
            }
            
        }
Exemple #10
0
        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.Multiselect = false;
            dialog.Filter = "Business Card Files (*.vcf)|*.vcf";

            if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                string path= dialog.FileName;
                ParserContext context=new ParserContext(path);
                VCardParser vparser=ParserFactory.CreateVCard(context);
                bcs = vparser.Parse();

                FillListView(bcs, checkBox1.Checked, checkBox2.Checked);
            }
            
        }
Exemple #11
0
 public static ISlideshowParser CreateSlideshow(ParserContext context)
 {
     object obj = CreateObject(context, typeof(ISlideshowParser), "CreateSlideshow");
     ISlideshowParser parser = (ISlideshowParser)obj;
     return parser;            
 }
Exemple #12
0
 public static IMetadataParser CreateMetadata(ParserContext context)
 {
     object obj = CreateObject(context, typeof(IMetadataParser), "CreateMetadata");
     IMetadataParser parser = (IMetadataParser)obj;
     return parser;
 }
Exemple #13
0
 public static IDocumentParser CreateDocument(ParserContext context)
 {
     object obj = CreateObject(context, typeof(IDocumentParser), "CreateDocument");
     IDocumentParser parser = (IDocumentParser)obj;
     return parser;
 }
Exemple #14
0
 public static IEmailParser CreateEmail(ParserContext context)
 {
     object obj = CreateObject(context, typeof(IEmailParser), "CreateEmail");
     IEmailParser parser = (IEmailParser)obj;
     return parser;
 }
        public ExcelTableTemplate Parse(string filepath)
        {
            var templateMetadata = new TemplateMetaData();
            if (filepath.StartsWith("/"))
                filepath = HostingEnvironment.MapPath(filepath);
            FilePath = filepath;
            ParserContext context = new ParserContext(filepath);
            ISpreadsheetParser parser = ParserFactory.CreateSpreadsheet(context);
            ToxySpreadsheet ss = parser.Parse();
            foreach (var table in ss.Tables)
            {
                _currTable = table;
                for (_currRowIndex = 0; _currRowIndex < table.Rows.Count; _currRowIndex++)
                {
                    var row = table.Rows[_currRowIndex];
                    for (_currColIndex = 0; _currColIndex < row.Cells.Count; _currColIndex++)
                    {
                        var cell = row.Cells[_currColIndex];

                        var tabletoken = ParseTable(cell);
                        if (tabletoken != null)
                        {
                            templateMetadata.Tables.Add(tabletoken);
                        }
                        else
                        {
                            var cellToken = ParseCell(cell);
                            if (cellToken != null)
                            {
                                templateMetadata.Cells.Add(cellToken);
                            }
                        }

                    }

                }

            }
            return null;
        }