Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
GrammarItemList.cs
Go to the documentation of this file.
1 #region License
2 /* **********************************************************************************
3  * Copyright (c) Roman Ivantsov
4  * This source code is subject to terms and conditions of the MIT License
5  * for Irony. A copy of the license can be found in the License.txt file
6  * at the root of this distribution.
7  * By using this source code in any fashion, you are agreeing to be bound by the terms of the
8  * MIT License.
9  * You must not remove this notice from this software.
10  * **********************************************************************************/
11 #endregion
12 
13 using System;
14 using System.Collections.Generic;
15 using System.Linq;
16 using System.Text;
17 using System.Xml;
18 using System.Windows.Forms;
19 using System.Reflection;
20 using System.IO;
21 using System.Threading;
22 
23 using Irony.Parsing;
24 
25 namespace Irony.GrammarExplorer {
26 
27  //Helper classes for supporting showing grammar list in top combo, saving list on exit and loading on start
28  public class GrammarItem {
29  public readonly string Caption;
30  public readonly string LongCaption;
31  public readonly string Location; //location of assembly containing the grammar
32  public readonly string TypeName; //full type name
33  internal bool _loading;
34  public GrammarItem(string caption, string location, string typeName) {
35  Caption = caption;
36  Location = location;
37  TypeName = typeName;
38  }
39  public GrammarItem(Type grammarClass, string assemblyLocation) {
40  _loading = true;
41  Location = assemblyLocation;
42  TypeName = grammarClass.FullName;
43  //Get language name from Language attribute
44  Caption = grammarClass.Name; //default caption
45  LongCaption = Caption;
46  var langAttr = LanguageAttribute.GetValue(grammarClass);
47  if (langAttr != null) {
48  Caption = langAttr.LanguageName;
49  if (!string.IsNullOrEmpty(langAttr.Version))
50  Caption += ", version " + langAttr.Version;
51  LongCaption = Caption;
52  if (!string.IsNullOrEmpty(langAttr.Description))
53  LongCaption += ": " + langAttr.Description;
54  }
55  }
56  public GrammarItem(XmlElement element) {
57  Caption = element.GetAttribute("Caption");
58  Location = element.GetAttribute("Location");
59  TypeName = element.GetAttribute("TypeName");
60  }
61  public void Save(XmlElement toElement) {
62  toElement.SetAttribute("Caption", Caption);
63  toElement.SetAttribute("Location", Location);
64  toElement.SetAttribute("TypeName", TypeName);
65  }
66  public override string ToString() {
67  return _loading ? LongCaption : Caption;
68  }
69 
70  }//class
71 
72  public class GrammarItemList : List<GrammarItem> {
73  public static GrammarItemList FromXml(string xml) {
74  GrammarItemList list = new GrammarItemList();
75  XmlDocument xdoc = new XmlDocument();
76  xdoc.LoadXml(xml);
77  XmlNodeList xlist = xdoc.SelectNodes("//Grammar");
78  foreach (XmlElement xitem in xlist) {
79  GrammarItem item = new GrammarItem(xitem);
80  list.Add(item);
81  }
82  return list;
83  }
84  public static GrammarItemList FromCombo(ComboBox combo) {
85  GrammarItemList list = new GrammarItemList();
86  foreach (GrammarItem item in combo.Items)
87  list.Add(item);
88  return list;
89  }
90 
91  public string ToXml() {
92  XmlDocument xdoc = new XmlDocument();
93  XmlElement xlist = xdoc.CreateElement("Grammars");
94  xdoc.AppendChild(xlist);
95  foreach (GrammarItem item in this) {
96  XmlElement xitem = xdoc.CreateElement("Grammar");
97  xlist.AppendChild(xitem);
98  item.Save(xitem);
99  } //foreach
100  return xdoc.OuterXml;
101  }//method
102 
103  public void ShowIn(ComboBox combo) {
104  combo.Items.Clear();
105  foreach (GrammarItem item in this)
106  combo.Items.Add(item);
107  }
108 
109  }//class
110 }
static GrammarItemList FromXml(string xml)
void Save(XmlElement toElement)
static GrammarItemList FromCombo(ComboBox combo)
GrammarItem(Type grammarClass, string assemblyLocation)
GrammarItem(string caption, string location, string typeName)