-
Notifications
You must be signed in to change notification settings - Fork 0
/
SavedQueries.cs
169 lines (144 loc) · 5.23 KB
/
SavedQueries.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
using System;
using System.Diagnostics;
using System.Collections.Generic;
using System.IO;
using System.Xml;
using System.Net;
namespace OxfordV2
{
public static class SavedQueries
{
public static bool Instance { get; set; } = false;
public static bool DeleteOnExport { get; set; } = true;
public static string WordID { get; set; }
public static List<Quote> Quotes { get; set; }
public static List<Sense> Senses { get; set; }
public static string ExportFileName { get; set; } = "OED-export.xml";
static SavedQueries()
{
Instance=true;
Quotes = new();
Senses = new();
}
public static void AddMember(Sense sense) {
Senses.Add(sense);
if (Senses.Count == 1) {
Console.WriteLine($"{Senses.Count} sense saved for export.");
}
else {
Console.WriteLine($"{Senses.Count} senses saved for export.");
}
}
public static void AddMember(Quote quote) {
Quotes.Add(quote);
if (Quotes.Count == 1) {
Console.WriteLine($"{Quotes.Count} quote saved for export.");
}
else {
Console.WriteLine($"{Quotes.Count} quotes saved for export.");
}
}
public static void RenderXML() {
string xmlFile = Path.Combine(Environment.CurrentDirectory, ExportFileName);
File.Delete(xmlFile);
FileStream xmlFileStream = File.Create(xmlFile);
XmlWriter xml = XmlWriter.Create(xmlFileStream,
new XmlWriterSettings { Indent = true });
xml.WriteStartDocument();
xml.WriteStartElement("SuperMemoCollection");
int count = Quotes.Count + Senses.Count;
xml.WriteElementString("Count", $"{count}");
// @TODO Add count number and ID number
if (Quotes.Count > 0) {
Console.WriteLine("Exporting Quotes...");
for (int i = 0; i < Quotes.Count; i++) {
try {
xml.WriteStartElement("SuperMemoElement");
int ID = i + 1;
xml.WriteElementString("ID", $"{ID}");
xml.WriteElementString("Title", $"{Quotes[i].Author} - {Quotes[i].Year}");
xml.WriteElementString("Type", "Topic");
xml.WriteStartElement("Content");
xml.WriteElementString("Question", $"\"{Quotes[i].Text}\" --{Quotes[i].Author}, {Quotes[i].Year}");
string encoded = WebUtility.HtmlEncode("<H5 dir=ltr align=left><Font size=\"1\" style=\"color: transparent\"> SuperMemo Reference:</font><br><FONT class=reference>Title:\"My Test Quote\" <br>Source: Oxford English Dictionary");
xml.WriteElementString("SuperMemoReference", encoded);
xml.WriteEndElement();
xml.WriteEndElement();
}
catch (AggregateException ae)
{
var ex = ae.Flatten().InnerExceptions;
Console.WriteLine("Error writing XML document:");
foreach (var exception in ex)
{
Console.WriteLine($"{ex.ToString()}");
}
}
}
}
if (Senses.Count > 0) {
Console.WriteLine("Exporting Senses...");
string obsoleteText = "";
string mainUsageText = "";
for (int i = 0; i < Senses.Count; i++) {
if (Senses[i].IsObsolete) {
obsoleteText = "This usage is obsolete.";
}
else {
obsoleteText = "This usage is NOT obsolete.";
}
if (Senses[i].IsMainUsage) {
mainUsageText = "This sense is the main sense for this word.";
}
else {
mainUsageText = "This sense is NOT the main sense for this word.";
}
try {
xml.WriteStartElement("SuperMemoElement");
int ID = i + 1;
xml.WriteElementString("ID", $"{ID}");
xml.WriteElementString("Title", $"{Senses[i].Definition}");
xml.WriteElementString("Type", "Topic");
xml.WriteStartElement("Content");
xml.WriteElementString("Question", $"Sense: \"{Senses[i].Definition}\" --This sense was first used in the year {Senses[i].Start}, {obsoleteText}, {mainUsageText}, {Senses[i].OedReference}");
string encoded = WebUtility.HtmlEncode("<H5 dir=ltr align=left><Font size=\"1\" style=\"color: transparent\"> SuperMemo Reference:</font><br><FONT class=reference>Title:\"My Test Quote\" <br>Source: Oxford English Dictionary");
xml.WriteElementString("SuperMemoReference", encoded);
xml.WriteEndElement();
xml.WriteEndElement();
}
catch (AggregateException ae)
{
var ex = ae.Flatten().InnerExceptions;
Console.WriteLine("Error writing XML document:");
foreach (var exception in ex)
{
Console.WriteLine($"{ex.ToString()}");
}
}
}
}
try {
xml.WriteEndElement();
xml.WriteEndDocument();
xml.Flush();
xml.Close();
xmlFileStream.Close();
Console.WriteLine("The XML file is saved here:");
Console.WriteLine(xmlFile);
Trace.WriteLine("The contents of the exported XML file are:");
Trace.WriteLine(File.ReadAllText(xmlFile));
Console.WriteLine("Please press enter:..........");
Console.ReadLine();
}
catch (AggregateException ae)
{
var ex = ae.Flatten().InnerExceptions;
Console.WriteLine("Error exporting XML document:");
foreach (var exception in ex)
{
Console.WriteLine($"{ex.ToString()}");
}
}
}
}
}