Esempio n. 1
0
        private static string GetTextFromFileData(byte[] fileDate, Constants.FileFormats format)
        {
            string textFull = string.Empty;

            switch (format)
            {
            case Constants.FileFormats.doc:
            {
                textFull = GetTextFromXmlDocument(OldDocReader.GetXMLContent(fileDate));
                break;
            }

            case Constants.FileFormats.docx:
            {
                textFull = GetTextFromDocx(fileDate);
                break;
            }

            case Constants.FileFormats.txt:
            {
                textFull = GetContentFromTxt(fileDate);
                break;
            }
            }
            return(textFull);
        }
Esempio n. 2
0
        private static string[] GetTextFromStyledData(byte[] fileDate, Constants.FileFormats format)
        {
            switch (format)
            {
            case Constants.FileFormats.doc:
                return(GetTextFromStyledXmlDocument(OldDocReader.GetXMLContent(fileDate)));

            case Constants.FileFormats.docx:
                return(GetTextFromStyledDocx(fileDate));

            default:
                return(null);
            }
        }
Esempio n. 3
0
        private static bool IsStyledSource(byte[] fileDate, Constants.FileFormats format)
        {
            switch (format)
            {
            case Constants.FileFormats.doc:
            {
                XmlDocument         xdoc          = OldDocReader.GetXMLContent(fileDate);
                const string        wordNamespace = "http://schemas.openxmlformats.org/wordprocessingml/2006/main";
                NameTable           nt            = new NameTable();
                XmlNamespaceManager nsManager     = new XmlNamespaceManager(nt);
                nsManager.AddNamespace("w", wordNamespace);

                XmlNodeList parNodes = xdoc.SelectNodes("//w:p", nsManager);

                foreach (XmlNode parNode in parNodes)
                {
                    if (parNode.OuterXml.Contains("Heading3"))
                    {
                        return(true);
                    }
                }
                break;
            }

            case Constants.FileFormats.docx:
            {
                using (Stream stream = new MemoryStream(fileDate))
                {
                    try
                    {
                        using (WordprocessingDocument wDocument = WordprocessingDocument.Open(stream, false))
                        {
                            Body body = wDocument.MainDocumentPart.Document.Body;
                            return(body.Descendants <ParagraphStyleId>().Any(psId => psId.Val == "3"));
                        }
                    }
                    catch (Exception ex)
                    {
                        throw new Exception("Неверная структура docx документа - " + ex.Message);
                    }
                }
            }

            case Constants.FileFormats.txt:
                return(false);
            }
            return(false);
        }
Esempio n. 4
0
        /// <summary>Получение новостей
        /// </summary>
        /// <param name="fileDate">Исходный файл </param>
        /// <param name="format">Формат исходного файла </param>
        /// <returns>Список новостей</returns>
        public static string ParseNews(byte[] fileDate, Constants.FileFormats format, SPWeb web, ref List <News> result)
        {
            string[] separatedNews;

            string textFull = GetTextFromFileData(fileDate, format);

            Regex regex = new Regex("\r\n[ ]{2,}\r\n", RegexOptions.None);

            textFull = regex.Replace(textFull, "\r\n \r\n");

            string s  = '\u000D'.ToString() + '\u000A'.ToString() + '\u000D'.ToString() + '\u000A'.ToString();  //separators between different news, CR-LF style
            string s1 = '\u000D'.ToString() + '\u000A'.ToString() + " " + '\u000D'.ToString() + '\u000A'.ToString();

            string[] newsSeparators = new string[] { s, s1 };
            separatedNews = textFull.Trim().Split(newsSeparators, StringSplitOptions.RemoveEmptyEntries);

            DateTime globalNewsTime = new DateTime();                                       //extracts global news time from 2nd line of text

            if (separatedNews.Length == 0)
            {
                throw new Exception("Пустой документ");
            }

            string strGlobalNewsTime = "";

            if (separatedNews[0].Contains("ЛЕНТА НОВОСТЕЙ"))
            {
                strGlobalNewsTime = separatedNews[0].Substring(separatedNews[0].IndexOf(',') + 2);
                if (!ParseDateTime(strGlobalNewsTime, out globalNewsTime))
                {
                    throw new Exception("Неверный формат даты в шапке новостей. ");
                }
            }
            else
            {
                globalNewsTime = DateTime.Now;
            }

            int        numNews     = 0;
            string     errorsParse = string.Empty;
            MainHelper mainHelper  = new MainHelper(web);

            foreach (string n in separatedNews)
            {
                string text = n.Trim();
                if (IsValidNews(text))
                {
                    numNews++;
                    News nws = new News();
                    nws.Active   = true;
                    nws.Mistakes = string.Empty;

                    nws.Title = text.Substring(0, text.IndexOf('\n'));  //place is optional
                    if (string.IsNullOrEmpty(nws.Title))
                    {
                        errorsParse += string.Format("Неверный формат заголовка, заголовок не найден, новость в документе № {0}</br>", numNews.ToString());
                        continue;
                    }
                    text = text.Substring(text.IndexOf('\n') + 1);

                    nws.Content = text;

                    errorsParse += ParseNewsHeader.Parse(text, globalNewsTime, numNews, ref nws, mainHelper);
                    if (nws.Source == null)
                    {
                        nws.Source = string.Empty;
                    }
                    result.Add(nws);
                }
                else
                {
                    if (numNews != 0)
                    {
                        errorsParse += string.Format("Обнаружен блок, не являющийся новостью, после новости № {0}</br>", numNews.ToString());
                    }
                }
            }
            return(errorsParse);
        }
Esempio n. 5
0
        /// <summary>Получение Обзоров прессы
        /// </summary>
        /// <param name="fileDate">Исходный файл </param>
        /// <param name="format">Формат исходного файла </param>
        /// <returns>Список обзоров прессы</returns>
        public static string ParsePressReviews(byte[] fileDate, Constants.FileFormats format, ref List <PressReview> result)
        {
            string[] blocks;
            if (IsStyledSource(fileDate, format))
            {
                blocks = GetTextFromStyledData(fileDate, format);
            }
            else
            {
                string textFull = GetTextFromFileData(fileDate, format);

                string   s = '\u000D'.ToString() + '\u000A'.ToString() + '\u000D'.ToString() + '\u000A'.ToString(); //separators between different news, CR-LF style
                string[] newsSeparators = new string[] { s };                                                       //looks tricky
                blocks = textFull.Split(newsSeparators, StringSplitOptions.RemoveEmptyEntries);
            }

            int indexShift = 0, globalTitleIndexShift = 0;              //globalTitleIndexShift сдвигает счетчик блоков на один, если существует глобальный заголовок, содержащий "ТЭК СЕГОДНЯ"

            if (blocks.Length >= 2 && blocks[1].ToUpper().Contains("СОДЕРЖАНИЕ"))
            {
                indexShift = 2;
            }
            if (blocks[0].ToUpper().Contains("ТЭК СЕГОДНЯ"))
            {
                globalTitleIndexShift = 1;
            }

            int    numPrews    = 0;
            string errorsParse = string.Empty;

            for (int i = globalTitleIndexShift + indexShift; i < blocks.Length; i++)
            {
                string contentBlock = blocks[i].Trim();
                if (contentBlock.Length == 0 || contentBlock.IndexOf('\n') == -1)
                {
                    errorsParse += string.Format("Обнаружен блок, не являющийся обзором прессы, после документа № {0}</br>", numPrews.ToString());
                    continue;
                }

                numPrews++;
                PressReview review = new PressReview();
                review.Active = true;

                string caption = contentBlock.Substring(0, contentBlock.IndexOf('\n')).Trim();

                string content = string.Empty;
                try
                {
                    content = contentBlock.Substring(contentBlock.IndexOf('\n') + 1);
                }
                catch (Exception ex)
                {
                    errorsParse += string.Format("Не найдено тело обзора, обзор в документе № {0} - {1}</br>", numPrews.ToString(), ex.Message);
                    continue;
                }
                review.Content = content;

                try
                {
                    if (caption.IndexOf('(') != -1)
                    {
                        review.Source = caption.Substring(0, caption.IndexOf('(') - 1);
                        caption       = caption.Substring(caption.IndexOf('(') + 1);
                        review.Place  = caption.Substring(0, caption.IndexOf(')'));
                        caption       = caption.Substring(caption.IndexOf(',') + 2);
                    }
                    else
                    {
                        review.Source = caption.Substring(0, caption.IndexOf(',') - 1);
                        caption       = caption.Substring(caption.IndexOf(',') + 2);
                    }
                }
                catch (Exception ex)
                {
                    errorsParse += string.Format("Неверный формат в теле заголовка обзора в полях - источник/географическое место/номер издания. Обзор в документе № {0} - {1}</br>", review.ToString(), ex.Message);
                    continue;
                }

                string[] separators      = new string[] { "  ", "   " };
                string[] attributes      = caption.Split(separators, 3, StringSplitOptions.RemoveEmptyEntries);
                DateTime pressReviewDate = new DateTime();
                if (attributes.Length == 3)
                {
                    string[] first;
                    if (attributes[0].Contains(','))
                    {
                        first = attributes[0].Split(',');
                        review.SourceNumber = first[0];
                        ParseNewsHeader.GetDate(first[1], out pressReviewDate);
                    }
                    else
                    {
                        ParseNewsHeader.GetDate(attributes[0], out pressReviewDate);
                    }
                    review.Writer = attributes[1].Substring(1);
                    review.Title  = attributes[2].Trim();
                }
                else if (attributes.Length == 2)
                {
                    string[] first;
                    if (attributes[0].Contains(','))
                    {
                        first = attributes[0].Split(',');
                        review.SourceNumber = first[0];
                        ParseNewsHeader.GetDate(first[1], out pressReviewDate);
                    }
                    else
                    {
                        ParseNewsHeader.GetDate(attributes[0], out pressReviewDate);
                    }
                    review.Title = attributes[1].Trim();
                }

                if (string.IsNullOrEmpty(review.Source))
                {
                    errorsParse += string.Format("Отсутствует источник, обзор в документе № {0}</br>", review.ToString());
                    continue;
                }

                if (string.IsNullOrEmpty(review.Title))
                {
                    errorsParse += string.Format("Неверный формат заголовка, заголовок не найден, обзор в документе № {0}</br>", numPrews.ToString());
                    continue;
                }

                if (1 == pressReviewDate.Year)
                {
                    errorsParse += string.Format("Неверный формат даты, обзор в документе № {0}<br/>Тема новости: {1}</br>", numPrews.ToString(), review.Title);
                    continue;
                }
                review.PressReviewDate = pressReviewDate;

                result.Add(review);
            }
            return(errorsParse);
        }