-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathExportManager.cs
More file actions
209 lines (183 loc) · 7.31 KB
/
ExportManager.cs
File metadata and controls
209 lines (183 loc) · 7.31 KB
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Xsl;
namespace Tomboy
{
interface INoteExport
{
}
public class ExportManager
{
private readonly XslCompiledTransform _xsl;
public ExportManager(string xslLocation)
{
_xsl = new XslCompiledTransform();
if (!LoadXSL(xslLocation))
{
throw new Exception("Could not load XSL:" + xslLocation + ". ");
}
}
public bool LoadXSL(string xslLocation)
{
if (File.Exists(xslLocation))
{
Logger.Debug("[Evernote] Using user-custom {0} file.", xslLocation);
_xsl.Load(xslLocation);
}
else
{
//TODO - could do fancy stuff with embedded assembly. Probably not worth it
Assembly asm = Assembly.GetExecutingAssembly();
string[] names = asm.GetManifestResourceNames();
string asmDir = System.IO.Path.GetDirectoryName(asm.Location);
string xslLocation2 = Path.Combine(asmDir, xslLocation);
if (File.Exists(xslLocation2))
{
Logger.Debug("[Evernote] Using user-custom {0} file.", xslLocation);
_xsl.Load(xslLocation);
}
else
{
Stream resource = asm.GetManifestResourceStream(xslLocation);
if (resource != null)
{
XmlTextReader reader = new XmlTextReader(resource);
Logger.Debug("[Evernote] Using user-custom {0} file.", xslLocation);
_xsl.Load(reader, null, null);
resource.Close();
return true;
}
Logger.Error("[Evernote] Unable to find XSL export template '{0}'.", xslLocation);
return false;
}
}
return true;
}
public string ApplyXSL(string content, string title, XmlResolver resolver, ValidationType validate)
{
StringReader reader = new StringReader(content);
XmlReaderSettings rsettings = new XmlReaderSettings();
if (validate != ValidationType.None)
{
rsettings.ValidationType = validate;
if (validate == ValidationType.DTD)
{
rsettings.ProhibitDtd = false;
}
rsettings.ValidationEventHandler += MyValidationEventHandler;
}
XmlReader doc = XmlReader.Create(reader, rsettings);
XsltArgumentList args = new XsltArgumentList();
args.AddParam("root-note", "", title);
args.AddExtensionObject("http://beatniksoftware.com/tomboy",
new TransformExtension());
StringWriter outWriter = new StringWriter();
XmlWriterSettings settings = new XmlWriterSettings();
settings.ConformanceLevel = ConformanceLevel.Auto;
settings.OmitXmlDeclaration = true;
XmlWriter writer = XmlWriter.Create(outWriter, settings);
if (writer == null)
{
Logger.Error("XmlWriter was null");
throw new NullReferenceException("xmlWriter was null");
}
_xsl.Transform(doc, args, writer, resolver);
doc.Close();
outWriter.Close();
return outWriter.ToString();
}
public string ApplyXSL(Note note, ValidationType validationType = ValidationType.None)
{
StringWriter sWriter = new StringWriter();
NoteArchiver.Write(sWriter, note.Data);
sWriter.Close();
NoteNameResolver resolver = new NoteNameResolver(note.Manager, note);
return ApplyXSL(sWriter.ToString(), note.Title, resolver, validationType);
}
// this gets called if we are validating our XML, which is a good idea in general.
public static void MyValidationEventHandler(object sender,
ValidationEventArgs args)
{
Logger.Error("XML validation failed: [" +args.Severity + "] " + args.Message +"\n" + args.Exception);
throw new XmlSchemaValidationException(args.Message, args.Exception);
}
}
public class NoteNameResolver : XmlResolver
{
readonly NoteManager _manager;
// Use this list to keep track of notes that have already been
// resolved.
readonly List<string> _resolvedNotes;
public NoteNameResolver(NoteManager manager, Note originNote)
{
_manager = manager;
_resolvedNotes = new List<string>();
// Add the original note to the list of resolved notes
// so it won't be included again.
_resolvedNotes.Add(originNote.Title.ToLower());
}
public override System.Net.ICredentials Credentials
{
set { }
}
public override object GetEntity(Uri absolute_uri, string role, Type of_object_to_return)
{
Note note = _manager.FindByUri(absolute_uri.ToString());
if (note == null)
return null;
StringWriter writer = new StringWriter();
NoteArchiver.Write(writer, note.Data);
Stream stream = WriterToStream(writer);
writer.Close();
return stream;
}
// Using UTF-16 does not work - the document is not processed.
// Also, the byte order marker (BOM in short, locate at U+FEFF,
// 0xef 0xbb 0xbf in UTF-8) must be included, otherwise parsing fails
// as well. This way the buffer contains an exact representation of
// the on-disk representation of notes.
//
// See http://en.wikipedia.org/wiki/Byte_Order_Mark for more
// information about the BOM.
static MemoryStream WriterToStream(TextWriter writer)
{
UTF8Encoding encoding = new UTF8Encoding();
string s = writer.ToString();
int bytesRequired = 3 + encoding.GetByteCount(s);
byte[] buffer = new byte[bytesRequired];
buffer[0] = 0xef;
buffer[1] = 0xbb;
buffer[2] = 0xbf;
encoding.GetBytes(s, 0, s.Length, buffer, 3);
return new MemoryStream(buffer);
}
public override Uri ResolveUri(Uri baseUri, string relativeUri)
{
string noteTitleLowered = relativeUri.ToLower();
if (_resolvedNotes.Contains(noteTitleLowered))
{
return new Uri("");
}
Note note = _manager.Find(relativeUri);
if (note != null)
{
_resolvedNotes.Add(noteTitleLowered);
return new Uri(note.Uri);
}
return new Uri("");
}
}
public class TransformExtension
{
public String ToNMToken(string s)
{
return Regex.Replace(s, @"\W", "-").ToLowerInvariant();
}
}
}