PageRenderTime 63ms CodeModel.GetById 2ms app.highlight 54ms RepoModel.GetById 1ms app.codeStats 1ms

/WpfApp4/ExtractStyles.cs

https://bitbucket.org/csereat/visualconfigui
C# | 739 lines | 581 code | 128 blank | 30 comment | 145 complexity | d7a86080188b3ec7ddc960a5a28e83be MD5 | raw file
  1using DocumentFormat.OpenXml.Packaging;
  2using Newtonsoft.Json;
  3using Newtonsoft.Json.Linq;
  4using System;
  5using System.Collections.Generic;
  6using System.Dynamic;
  7using System.IO;
  8using System.Linq;
  9using System.Reflection;
 10using System.Security.Cryptography;
 11using System.Text;
 12using System.Threading.Tasks;
 13using System.Windows.Controls;
 14using System.Xml;
 15using System.Xml.Linq;
 16using WpfApp4.Properties;
 17
 18
 19namespace WpfApp4
 20{
 21    
 22    class ExtractStyles
 23    {
 24        
 25        
 26        static List<string> ExcludedElements = new List<string> { "rsid", "docId" };  
 27        static List<string> ExcludedAttributes = new List<string> { "settings", "sectPr"}; 
 28        dynamic JsonrootElement;
 29        StyleDicList _StyleDicList;
 30
 31        public void extractStyles(string filenamewpath,TextBox resultBox, List<string> stlList, List<string> tblList, List<List<string>> Lists, string Config, StyleDicList _StyleDicList)
 32        {
 33            this._StyleDicList = _StyleDicList;
 34            JsonrootElement = new ExpandoObject();
 35            string path = Path.Combine(Settings.Default.ConfigPath, Config);
 36
 37            var assembly = Assembly.GetExecutingAssembly();
 38            string result = "";
 39            
 40
 41            List<ExpandoObject> JsonList = new List<ExpandoObject>();
 42
 43            var resourceName = "WpfApp4.dictionary.ooxml_interop.txt";
 44            using (Stream stream = assembly.GetManifestResourceStream(resourceName))
 45            using (StreamReader reader = new StreamReader(stream))
 46            {
 47                result = reader.ReadToEnd();
 48            }
 49            dynamic O2IDictionary = JObject.Parse(result);
 50
 51            #region FileDefinition
 52            resultBox.AppendText("Extracting file settings..." + Environment.NewLine);
 53            JsonrootElement.FileDefinitionPart = new ExpandoObject();
 54
 55            #region Document Part - sections
 56            XDocument ExtractedDocument = ExtractPart(filenamewpath, "document", false);
 57            XNamespace ns = ExtractedDocument.Root.GetNamespaceOfPrefix("w");
 58            JsonList = new List<ExpandoObject>();
 59
 60            foreach (XElement section in ExtractedDocument.Root.Descendants(ns + "sectPr"))
 61            {
 62                ProcessElements(section, JsonrootElement.FileDefinitionPart, O2IDictionary.document.sectPr, null, JsonList);
 63            }
 64
 65            #endregion
 66
 67            #region Document Part - TOC and fields
 68            
 69            var FDPart = (IDictionary<string, Object>)JsonrootElement.FileDefinitionPart;
 70           
 71            IList<string> FieldFormats = new List<string>();
 72
 73            XElement prevParent = ExtractedDocument.Root;
 74            string prevNodeValue ="";
 75
 76            foreach (XElement itext in ExtractedDocument.Root.Descendants(ns + "instrText"))
 77            {
 78                if (itext.Value.Length>=8 && itext.Value.Substring(0, 8) != " PAGEREF")
 79                {
 80                    if (itext.Parent == prevParent)
 81                    {
 82                        FieldFormats[FieldFormats.Count - 1] = FieldFormats.Last().Replace(FieldFormats.Last(), FieldFormats.Last() + "|" + itext.Value);
 83                    }
 84                    else
 85                    {
 86                        if (!FieldFormats.Contains(itext.Value))
 87                        {
 88                            FieldFormats.Add(itext.Value);
 89                        }
 90                    }
 91                    prevParent = itext.Parent;                   
 92                 }
 93            }
 94
 95            prevParent = ExtractedDocument.Root;
 96            string NodeValue;
 97            foreach (XElement itext in ExtractedDocument.Root.Descendants(ns + "fldSimple"))
 98            {
 99
100                if (itext.Parent == prevParent)
101                {
102                    FieldFormats[FieldFormats.Count-1] = FieldFormats.Last().Replace(FieldFormats.Last(), FieldFormats.Last() + "|" + itext.Attribute(ns + "instr").Value);
103                }
104                else
105                {
106                    if (!FieldFormats.Contains(itext.Attribute(ns + "instr").Value))
107                    { 
108                         FieldFormats.Add(itext.Attribute(ns + "instr").Value);
109                    }
110                }
111                
112                prevParent = itext.Parent;
113            }
114
115            if (FieldFormats.Count != 0)           
116            FDPart.Add("FieldFormats", FieldFormats.Distinct());
117
118            List<CaptionConfig> captionConfigList = new List<CaptionConfig>();
119
120            foreach (string field in FieldFormats)
121            {
122                if (field.Contains("TOC")) // table of contents - tempTOC
123                {
124
125                }
126                else if (field.Contains("SEQ"))  //captionconfig 
127                {
128                    CaptionConfig tempcc = new CaptionConfig();
129
130                    if (field.Contains("STYLEREF"))
131                    {
132                        tempcc.IncludeChapterNumber = true;
133                        tempcc.ChapterStyleLevel = Convert.ToInt32(field.Substring(10,1)); 
134                    }
135
136                    captionConfigList.Add(tempcc);
137                }
138            }
139
140            #endregion
141
142            #region Table List
143
144            Dictionary<string, List<TableView>> TableViewList = new Dictionary<string, List<TableView>>();
145            int viewInd=0;
146            foreach (XElement tablePrNode in ExtractedDocument.Root.Descendants(ns + "tblPr"))
147            {
148                
149                string ActTableStyleName = "";
150                foreach (XElement childNode in tablePrNode.Nodes())
151                {
152                    if (childNode.Name == ns + "tblStyle")
153                    {
154                        ActTableStyleName = childNode.Attribute(ns + "val").Value;
155                        if (!tblList.Any(s => ActTableStyleName.Contains(s)))
156                        {
157                            tblList.Add(ActTableStyleName); //xmlstyleID
158                            if (!TableViewList.Keys.Contains(ActTableStyleName))
159                            { 
160                                TableViewList.Add(ActTableStyleName, new List<TableView>());
161                            }
162                        }
163                    }
164
165                    if ((childNode.Name == ns + "tblLook"))
166                    {
167                        viewInd++;
168                        TableView tempView = new TableView();
169                        tempView.ViewName = "TableView" + viewInd;
170                        foreach (XAttribute xat in childNode.Attributes())
171                        {
172                            if (xat.Name == ns + "firstRow") { tempView.HeaderRow = xat.Value; }
173                            if (xat.Name == ns + "lastRow") { tempView.TotalRow = xat.Value; }
174                            if (xat.Name == ns + "firstColumn") { tempView.FirstColumn = xat.Value; }
175                            if (xat.Name == ns + "lastColumn") { tempView.LastColumn = xat.Value; }
176                            if (xat.Name == ns + "noHBand") { tempView.BandedRows = xat.Value; }
177                            if (xat.Name == ns + "noVBand") { tempView.BandedColumns = xat.Value; }
178                            
179                        }
180                        TableViewList[ActTableStyleName].Add(tempView);
181                    }
182
183                }
184
185            }
186
187            #endregion
188
189            #region Settings Part
190            XDocument ExtractedSettings = ExtractPart(filenamewpath, "settings", false);
191            ns = ExtractedSettings.Root.GetNamespaceOfPrefix("w");
192
193            if (ExtractedSettings != null)
194                ProcessElements(ExtractedSettings.Root, JsonrootElement.FileDefinitionPart, O2IDictionary.settings, null, new List<ExpandoObject>());
195
196            #endregion
197
198            #region Numbering Extract
199            resultBox.AppendText("Extracting numberings..." + Environment.NewLine);
200            XDocument ExtractedNumbering = ExtractPart(filenamewpath, "numbering", false);
201            JsonList = new List<ExpandoObject>();
202
203
204            bool LinkedListTemplateFound = false;
205
206            if (ExtractedNumbering != null)
207            {
208                ns = ExtractedNumbering.Root.GetNamespaceOfPrefix("w");
209
210                foreach (XElement rootElement in ExtractedNumbering.Root.Elements(ns + "abstractNum"))
211                {
212
213                    foreach (XElement listlevel in rootElement.Elements(ns + "lvl").Elements(ns + "pStyle"))
214                    {
215                        LinkedListTemplateFound = true;
216                    }
217                    if (LinkedListTemplateFound)
218                    {
219                        ProcessElements(rootElement, JsonrootElement.FileDefinitionPart, O2IDictionary.numbering, null, JsonList);
220                        LinkedListTemplateFound = false;
221                    }
222
223                }
224            }
225           
226            int i = 0;
227            foreach (dynamic _ListTemplate in JsonList)
228            {
229                
230               // dynamic _ListTemplate = _ListTemplate1;
231                
232                if (_ListTemplate.multiLevelType == "OutlineNumbered" || _ListTemplate.multiLevelType == "hybridmulti")
233                {
234
235                    i = 0;
236                    List<string> NumberedStyles = new List<string>(); 
237                    List<string> BulletedStyles = new List<string>();
238
239                    IDictionary<string, Object> _listLevel = _ListTemplate.ListLevel[i];
240                    
241                    while (_listLevel.ContainsKey("LinkedStyle") &&
242                                    (_ListTemplate.ListLevel[i].NumberStyle == "wdListNumberStyleBullet"))
243                    {
244
245                        if (i == 0)
246                        {
247                            BulletedStyles.Add("BulletedList");
248                        }
249                        BulletedStyles.Add(_ListTemplate.ListLevel[i].LinkedStyle);
250                        i++;
251
252                        if (i > 4)
253                            break;
254                        _listLevel = _ListTemplate.ListLevel[i];
255                    }
256                    
257                    if (i > 1)
258                        Lists.Add(BulletedStyles);
259
260
261                    i = 0;
262                    while (_listLevel.ContainsKey("LinkedStyle") &&
263                        (_ListTemplate.ListLevel[i].NumberStyle != "wdListNumberStyleBullet"))
264                    {
265                        
266                        if (i == 0)
267                        {
268                            NumberedStyles.Add("NumberedList");
269                        }
270                        NumberedStyles.Add(_ListTemplate.ListLevel[i].LinkedStyle);
271                        i++;
272                        if (i > 4)
273                            break;
274                        _listLevel = _ListTemplate.ListLevel[i];
275                        
276                    }
277
278                    if (i > 1)
279                        Lists.Add(NumberedStyles);
280                }
281
282            }
283            #endregion
284
285            #endregion
286
287            #region Theme Extract
288
289            XDocument ExtractedThemes = ExtractPart(filenamewpath, "theme", false);
290            ns = ExtractedThemes.Root.GetNamespaceOfPrefix("a");
291            resultBox.AppendText("Extracting color scheme..." + Environment.NewLine);
292            XElement ColorScheme = ExtractedThemes.Root.Element(ns + "themeElements").Element(ns + "clrScheme");
293            resultBox.AppendText("Extracting font scheme..." + Environment.NewLine);
294            XElement FontScheme = ExtractedThemes.Root.Element(ns + "themeElements").Element(ns + "fontScheme");
295            resultBox.AppendText("Extracting effects scheme..." + Environment.NewLine);
296            XElement EffectScheme = ExtractedThemes.Root.Element(ns + "themeElements").Element(ns + "fmtScheme");
297            string XMLDeclaration = ExtractedThemes.Declaration.ToString();
298
299            if (Directory.Exists(path)) Directory.Delete(path, true);
300            Directory.CreateDirectory(path);
301
302
303            File.WriteAllText(path + @"\color.theme", XMLDeclaration + Environment.NewLine + ColorScheme.ToString());
304            File.WriteAllText(path + @"\font.theme", XMLDeclaration + Environment.NewLine + FontScheme.ToString());
305            File.WriteAllText(path + @"\effect.theme", XMLDeclaration + Environment.NewLine + EffectScheme.ToString());
306
307            #endregion
308
309            #region StyleExtract
310
311            resultBox.AppendText("Extracting styles..." + Environment.NewLine);
312
313            XDocument ExtractedStyles = ExtractPart(filenamewpath, "styles", false);
314            ns = ExtractedStyles.Root.GetNamespaceOfPrefix("w");
315
316            JsonList = new List<ExpandoObject>();
317
318      
319            if (ExtractedStyles != null)
320            {
321                JsonrootElement.StyleDefinitionPart = new ExpandoObject();
322                int ind = 1;
323
324                #region Font and Paragraph Defaults
325                if (ExtractedStyles.Root.Element(ns + "docDefaults") != null)
326                {
327                    ProcessElements(ExtractedStyles.Root.Element(ns + "docDefaults"), JsonrootElement.FileDefinitionPart, O2IDictionary.docDefaults, null, new List<ExpandoObject>());
328                }
329                #endregion
330
331                foreach (XElement rootElement in ExtractedStyles.Root.Elements(ns + "style"))
332                {
333
334                    if (EnableStyleToExtract(rootElement.Element(ns + "name").Attribute(ns + "val").Value, rootElement, ns))
335                    {
336                        //LogToFile("***************Processing style: " + rootElement.Attribute(ns + "styleId").Value + "******************");
337                        ProcessElements(rootElement, JsonrootElement.StyleDefinitionPart, O2IDictionary.style, null, JsonList);
338
339                        StyleNameInfo _StyleNameInfo = new StyleNameInfo();
340
341                        _StyleNameInfo.StyleIndex = ind.ToString();
342                        _StyleNameInfo.XmlName = rootElement.Element(ns + "name").Attribute(ns + "val").Value;
343                        _StyleNameInfo.XmlStyleID = rootElement.Attribute(ns + "styleId").Value;
344                        _StyleNameInfo.BuiltinStyleName = JsonList[ind - 1].FirstOrDefault(x => x.Key == "BuiltinStyleName").Value.ToString();
345                        if (_StyleNameInfo.BuiltinStyleName.Contains("Accent") == true)
346                        {
347                            _StyleNameInfo.BuiltinStyleName = _StyleNameInfo.BuiltinStyleName.Replace(" Accent", " - Accent");
348                        }
349                        //_StyleNameInfo.XmlStyleID_BaseStyle = rootElement.Element(ns + "name")
350                        _StyleNameInfo.PreferredStyle = "Not Applicable";
351                        _StyleNameInfo.StyleDescription = "Not Applicable";
352
353                        // if (_StyleNameInfo.BuiltinStyleName.Substring(0, 2) != "wd") _StyleNameInfo.BuiltinStyleName = JsonList; 
354
355                        _StyleDicList.StyleNameList.Add(_StyleNameInfo);
356
357                        //Dropdown list
358                        if (rootElement.Attribute(ns+"type").Value == "paragraph" || rootElement.Attribute(ns + "type").Value == "character") stlList.Add(_StyleNameInfo.XmlName);
359
360                        ind++;
361                    }
362                }
363            }
364            #endregion
365
366
367
368            //JsonrootElement.StyleDefinitions;
369            // 
370            //File.WriteAllText(path + @"\StyleListDic.txt", JsonConvert.SerializeObject(_StyleDicList, Newtonsoft.Json.Formatting.Indented));
371
372           // WriteConfig(path);    
373            resultBox.AppendText("Completed." + Environment.NewLine);
374            resultBox.ScrollToEnd();
375        }
376
377        public List<string> InheritanceList(List<string> SelectedStyles)
378        {
379            string BaseStyle;
380            List<string> StylesToExport = new List<string>();
381        
382            foreach (string StyleName in SelectedStyles)
383            {
384                BaseStyle = StyleName;                
385
386                while (BaseStyle != "")
387                {
388                    IDictionary<string, Object> _Style = JsonrootElement.StyleDefinitionPart.StyleSettings[Convert.ToInt32(_StyleDicList.StyleNameList.FirstOrDefault(x => x.XmlStyleID == BaseStyle).StyleIndex)-1];
389
390                    if (_Style.ContainsKey("BaseStyle"))
391                    {    
392                        if (!StylesToExport.Contains(BaseStyle)) StylesToExport.Add(BaseStyle);
393                        BaseStyle = _Style.FirstOrDefault(x => x.Key == "BaseStyle").Value.ToString();
394                    }
395                    else
396                    {
397                        if (!StylesToExport.Contains(BaseStyle)) StylesToExport.Add(BaseStyle);
398                        BaseStyle = "";
399                    }
400                }
401            }
402
403            return StylesToExport;
404        }
405
406        public void RemoveStylesFromJSON(List<string> SelectedStyles)
407        {
408            List<string> StylesToInclude = InheritanceList(SelectedStyles);
409
410            int i = 0;
411            while (i < JsonrootElement.StyleDefinitionPart.StyleSettings.Count)
412            {
413                if (JsonrootElement.StyleDefinitionPart.StyleSettings[i].StyleType.ToString() == "wdStyleTypeTable" &&
414                       !StylesToInclude.Contains(JsonrootElement.StyleDefinitionPart.StyleSettings[i].StyleID))
415                {
416                    List<ExpandoObject> _styles = JsonrootElement.StyleDefinitionPart.StyleSettings;
417                    _StyleDicList.StyleNameList.RemoveAt(i);
418                    _styles.RemoveAt(i);
419
420                }
421                else { i++; }                    
422            }
423            int index = 1;
424            foreach (dynamic tmpStl in _StyleDicList.StyleNameList)
425            {
426                tmpStl.StyleIndex = index.ToString();
427                index++;
428            }
429
430        }
431        public void WriteConfig(string path)
432        {
433            string DicJson = JsonConvert.SerializeObject(JsonrootElement, Newtonsoft.Json.Formatting.Indented);
434            File.WriteAllText(path + @"\config.txt",  EncryptFile(DicJson));
435            //File.WriteAllText(path + @"\config_notEncrypted.txt", DicJson);
436
437        }
438        public static XDocument ExtractPart(string fileName, string part2extract, bool getStylesWithEffectsPart = true)
439        {
440            XDocument part = null;
441
442            using (var document = WordprocessingDocument.Open(fileName, false))
443            {
444                var docPart = document.MainDocumentPart;
445                if (part2extract == "styles")
446                {
447                    StylesPart stylesPart = null;
448                    if (getStylesWithEffectsPart)
449                        stylesPart = docPart.StylesWithEffectsPart;
450                    else
451                        stylesPart = docPart.StyleDefinitionsPart;
452
453                    if (stylesPart != null)
454                    {
455                        using (var reader = XmlNodeReader.Create(stylesPart.GetStream(FileMode.Open, FileAccess.Read)))
456                        {
457                            part = XDocument.Load(reader);
458                        }
459                    }
460                }
461                if (part2extract == "numbering")
462                {
463                    NumberingDefinitionsPart numberingPart = null;
464                    numberingPart = docPart.NumberingDefinitionsPart;
465                    if (numberingPart != null)
466                    {
467                        using (var reader = XmlNodeReader.Create(numberingPart.GetStream(FileMode.Open, FileAccess.Read)))
468                        {
469                            part = XDocument.Load(reader);
470                        }
471                    }
472                }
473                if (part2extract == "settings")
474                {
475                    DocumentSettingsPart settingsPart = null;
476                    settingsPart = docPart.DocumentSettingsPart;
477                    if (settingsPart != null)
478                    {
479                        using (var reader = XmlNodeReader.Create(settingsPart.GetStream(FileMode.Open, FileAccess.Read)))
480                        {
481                            part = XDocument.Load(reader);
482                        }
483                    }
484                }
485                if (part2extract == "theme")
486                {
487                    ThemePart themePart = null;
488                    themePart = docPart.ThemePart;
489                    if (themePart != null)
490                    {
491                        using (var reader = XmlNodeReader.Create(themePart.GetStream(FileMode.Open, FileAccess.Read)))
492                        {
493                            part = XDocument.Load(reader);
494                        }
495                    }
496                }
497
498                if (part2extract == "document")
499                {
500                    if (docPart != null)
501                    {
502                        using (var reader = XmlNodeReader.Create(docPart.GetStream(FileMode.Open, FileAccess.Read)))
503                        {
504                            part = XDocument.Load(reader);
505                        }
506                    }
507                }
508            }
509            return part;
510        }
511
512        public static bool EnableStyleToExtract(string StyleXmlName, XElement rootElement, XNamespace ns)
513        {
514            if (StyleXmlName.Length > 4)
515            if (StyleXmlName.Substring(StyleXmlName.Length - 4) == "Char" && rootElement.Attribute(ns + "type").Value == "character" && rootElement.Elements(ns + "link").Any())
516            {
517                return false;
518            }
519            return true;
520        }
521
522        public static void ProcessElements(XElement ActualElement, ExpandoObject JsonElement, dynamic O2IDictionaryPart, string OldSequenceValue, List<ExpandoObject> JsonList)
523        {
524
525            var xJsonElement = (IDictionary<string, Object>)JsonElement;
526             //LogToFile("Processing ActualElement: " + ActualElement.Name.LocalName);
527            bool SequenceElement;
528            string SequenceValue = null;
529            string ActualElementSequenceValue = null;
530            string ElementInteropValue = O2IDictionaryPart.interop;
531            //LogToFile("Interop name of the Actual XElement(ElementInteropValue): " + ElementInteropValue);
532            bool Elementskipped = false;
533            dynamic NextJsonElement = new ExpandoObject();
534
535            var xNextJsonElement = (IDictionary<string, Object>)NextJsonElement;
536
537            if (O2IDictionaryPart.GetValue("sequence") != null)
538            {
539                SequenceElement = true;
540                SequenceValue = O2IDictionaryPart.GetValue("sequence");
541                ActualElementSequenceValue = SequenceValue;
542            }
543            else
544            {
545                SequenceElement = false;
546            }
547
548            //LogToFile("List(SequenceElement): " + SequenceElement.ToString());
549
550            if (SequenceElement)
551            {
552                JsonList.Add(NextJsonElement);
553                ////LogToFile("Adding next JSON node to List");
554            }
555            else
556            {
557                if (ElementInteropValue != "")
558                {
559                    xJsonElement.Add(ElementInteropValue, xNextJsonElement);
560                    ////LogToFile("Adding next JSON node to Object");
561                }
562                else
563                {
564                    Elementskipped = true;
565                    // //LogToFile("Skipping main element");
566                }
567            }
568
569            ////LogToFile("Checking attributes...");
570            if (ActualElement.HasAttributes)
571
572                if (!ExcludedAttributes.Any(s => ActualElement.Name.LocalName.Contains(s)))  
573                   // ActualElement.Name.LocalName.ToString() != "settings" && ActualElement.Name.LocalName.ToString() != "sectPr")
574                {
575                    ////LogToFile("Actual Element has attributes");
576                    foreach (XAttribute Xat in ActualElement.Attributes())
577                    {
578
579                        //LogToFile("Processing attribute: " + ActualElement.Name.LocalName + @"\@" + Xat.Name.LocalName);
580                        dynamic AttributeInterop = O2IDictionaryPart.GetValue("@" + Xat.Name.LocalName);
581                        dynamic AttributeInteropValue;
582                        if (AttributeInterop.ToString() == "" || AttributeInterop.interop == "")
583                        {
584                            AttributeInterop = O2IDictionaryPart;
585                            AttributeInteropValue = Xat.Value;
586                            xJsonElement[AttributeInterop.interop.ToString()] = AttributeInteropValue.ToString();
587                        }
588                        else
589                        {
590                            AttributeInteropValue = AttributeInterop.GetValue(Xat.Value);
591                            if (AttributeInteropValue == null) AttributeInteropValue = Xat.Value;
592                            if (Elementskipped)
593                            {
594                                xJsonElement.Add(AttributeInterop.interop.ToString(), AttributeInteropValue.ToString());
595                            }
596                            else
597                            {
598                                xNextJsonElement.Add(AttributeInterop.interop.ToString(), AttributeInteropValue.ToString());
599                            }
600                        }
601                    }
602                    //LogToFile("Building JSON object: \r\n Attribute:" + AttributeInterop.interop.ToString() + "\r\n Value: " + AttributeInteropValue.ToString());
603                
604                }
605                else
606                {
607                    ////LogToFile("Actual Element has NO attributes");
608                }
609
610            ////LogToFile("Checking child elements...");
611            if (ActualElement.HasElements)
612            {
613                List<ExpandoObject> NextJsonList = new List<ExpandoObject>();
614                ////LogToFile("Actual Element has child element(s)");
615                foreach (XElement ChildElement in ActualElement.Elements())
616                {
617                    //LogToFile("Processing child element(s): " + ActualElement.Name.LocalName + @"\" + ChildElement.Name.LocalName);
618                    dynamic NextO2IDictionaryPart = O2IDictionaryPart.GetValue(ChildElement.Name.LocalName);
619                    if (NextO2IDictionaryPart != null && !ExcludedElements.Any(s => ChildElement.Name.LocalName.Contains(s)))
620                    //if (!ExcludedElements.Any(s => ChildElement.Name.LocalName.Contains(s)))
621                    {
622                        //dynamic NextO2IDictionaryPart = O2IDictionaryPart.GetValue(ChildElement.Name.LocalName);
623                        string nextSequenceValue = NextO2IDictionaryPart.GetValue("sequence");
624
625                        if (ActualElementSequenceValue != null && nextSequenceValue != null)
626                        {
627                            ProcessElements(ChildElement, NextJsonElement, NextO2IDictionaryPart, SequenceValue, NextJsonList);
628                        }
629                        else if ((SequenceValue == nextSequenceValue && nextSequenceValue != null) || (SequenceValue == null && nextSequenceValue != null))
630                        {
631                            ProcessElements(ChildElement, NextJsonElement, NextO2IDictionaryPart, SequenceValue, JsonList);
632                        }
633                        else if (nextSequenceValue != SequenceValue && nextSequenceValue != null)
634                        {
635                            JsonList = new List<ExpandoObject>();
636                            ProcessElements(ChildElement, NextJsonElement, NextO2IDictionaryPart, SequenceValue, JsonList);
637                        }
638                        else
639                        {
640                            ProcessElements(ChildElement, NextJsonElement, NextO2IDictionaryPart, SequenceValue, new List<ExpandoObject>());
641                        }
642                        SequenceValue = NextO2IDictionaryPart.GetValue("sequence");
643
644                    }
645                    else
646                    {
647                        // //LogToFile("Element skipped");
648                    }
649                };
650            }
651            else
652            {
653                ////LogToFile("ActualElement has NO child element.");
654            }
655            if (!ActualElement.HasAttributes && !ActualElement.HasElements)
656            {
657                xJsonElement[ElementInteropValue] = "1";
658            }
659            if (SequenceElement)
660            {
661                xJsonElement[ElementInteropValue] = JsonList;
662            }
663
664        }
665        public string EncryptFile(string plainContent)
666        {
667            
668            string key = Settings.Default.Config + Settings.Default.ApplicationName;
669            if (key.Length > 8)
670                key = key.Substring(0, 8);
671            else
672                key = (key + "!@#$%^&*").Substring(0, 8);
673            byte[] plainContentbytes = Encoding.UTF8.GetBytes(plainContent);
674            using (var DES = new DESCryptoServiceProvider())
675            {
676                DES.IV = Encoding.UTF8.GetBytes(key);
677                DES.Key = Encoding.UTF8.GetBytes(key);
678                DES.Mode = CipherMode.CBC;
679                DES.Padding = PaddingMode.PKCS7;
680
681
682                using (var memStream = new MemoryStream())
683                {
684                    CryptoStream cryptoStream = new CryptoStream(memStream, DES.CreateEncryptor(), CryptoStreamMode.Write);
685
686                    cryptoStream.Write(plainContentbytes, 0, plainContentbytes.Length);
687                    cryptoStream.FlushFinalBlock();
688                    memStream.Position = 0;
689
690                    return Convert.ToBase64String(memStream.ToArray());
691                }
692            }
693        }
694
695       
696        public class StyleDicList
697        {
698            public IList<StyleNameInfo> StyleNameList { get; set; }
699        }
700
701        public class StyleNameInfo
702        {
703            public string StyleIndex { get; set; }
704            public string XmlStyleID { get; set; }
705            public string XmlName { get; set; }
706            public string BuiltinStyleName { get; set; }
707            //public string XmlStyleID_BaseStyle { get; set; }
708            public bool IsImported { get; set; }
709            public string PreferredStyle { get; set; }
710            public string StyleDescription { get; set; }
711        }
712
713        public class TableView
714        {
715            public string ViewName { get; set; }
716            public string HeaderRow { get; set; }
717            public string FirstColumn { get; set; }
718            public string TotalRow { get; set; }
719            public string LastColumn { get; set; }
720            public string BandedRows { get; set; }
721            public string BandedColumns { get; set; }
722
723        }
724
725        public class CaptionConfig
726        {
727            public string CaptionType { get; set; }
728            public List<string> CaptionLabels { get; set; }
729            public string Position { get; set; }
730            public string NumberStyle { get; set; }
731            public bool IncludeChapterNumber { get; set; }
732            public string Separator { get; set; }
733            public int ChapterStyleLevel { get; set; }
734            public string CaptionStyle { get; set; }
735            public string TableContentStyle { get; set; }
736        }
737
738    }
739}