-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFB2File.cs
444 lines (392 loc) · 16.8 KB
/
FB2File.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
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Xml.Linq;
using FB2Library.Elements;
using FB2Library.HeaderItems;
namespace FB2Library
{
public class FB2File
{
private XNamespace _fileNameSpace = XNamespace.None;
private const string TitleInfoElementName = "title-info";
private const string SrcTitleInfoElementName = "src-title-info";
private const string DocumentInfoElementName = "document-info";
private const string Fb2TextDescriptionElementName = "description";
private const string Fb2TextBodyElementName = "body";
private const string Fb2BinaryElementName = "binary";
private readonly ItemTitleInfo _titleInfo = new ItemTitleInfo();
private readonly ItemTitleInfo _srcTitleInfo = new ItemTitleInfo();
private readonly ItemDocumentInfo _documentInfo = new ItemDocumentInfo();
private readonly ItemPublishInfo _publishInfo = new ItemPublishInfo();
private readonly List<ItemCustomInfo> _customInfo = new List<ItemCustomInfo>();
private BodyItem _mainBody;
private readonly List<BodyItem> _bodiesList = new List<BodyItem>();
private readonly Dictionary<string, BinaryItem> _binaryObjects = new Dictionary<string, BinaryItem>();
private readonly List<StyleElement> _styles = new List<StyleElement>();
private readonly List<ShareInstructionType> _output = new List<ShareInstructionType>();
/// <summary>
/// Get list of images (the image data itself) stored as binary objects
/// </summary>
public Dictionary<string, BinaryItem> Images { get { return _binaryObjects;} }
/// <summary>
/// Get list of bodies in document (including main)
/// </summary>
public List<BodyItem> Bodies { get { return _bodiesList;}}
/// <summary>
/// Get Document TitleInfo structure
/// </summary>
public ItemTitleInfo TitleInfo { get { return _titleInfo; } }
/// <summary>
/// Get Document DocumentInfo structure
/// </summary>
public ItemDocumentInfo DocumentInfo { get { return _documentInfo; } }
/// <summary>
/// Get Document SourceTitleInfo structure
/// </summary>
public ItemTitleInfo SourceTitleInfo { get { return _srcTitleInfo; } }
/// <summary>
/// Get custom added information (if available)
/// </summary>
public List<ItemCustomInfo> CustomInfo { get { return _customInfo; } }
/// <summary>
/// Get Document PublishInfo structure
/// </summary>
public ItemPublishInfo PublishInfo { get { return _publishInfo; } }
/// <summary>
/// Get main body structure (book itself)
/// </summary>
public BodyItem MainBody { get { return _mainBody; } }
/// <summary>
/// Get styles applied to the book
/// </summary>
public List<StyleElement> StyleSheets { get { return _styles; } }
/// <summary>
/// Loads the file as data from XML data
/// </summary>
/// <param name="fileDocument">XML document containing the file</param>
/// <param name="loadHeaderOnly">if true loads only header information</param>
public void Load(XDocument fileDocument,bool loadHeaderOnly)
{
if (fileDocument == null)
{
throw new ArgumentNullException("fileDocument");
}
if (fileDocument.Root == null)
{
throw new ArgumentException("Document's root is NULL (empty document passed)");
}
// theoretically the namespace should be "http://www.gribuser.ru/xml/fictionbook/2.0" but just to be sure with invalid files
_fileNameSpace = fileDocument.Root.GetDefaultNamespace();
_styles.Clear();
IEnumerable<XElement> xStyles = fileDocument.Elements(_fileNameSpace + StyleElement.StyleElementName).ToArray();
// attempt to load some bad FB2 with wrong namespace
if (!xStyles.Any())
{
xStyles = fileDocument.Elements(StyleElement.StyleElementName);
}
foreach (var style in xStyles)
{
var element = new StyleElement();
try
{
element.Load(style);
_styles.Add(element);
}
catch
{
// ignored
}
}
LoadDescriptionSection(fileDocument);
if (!loadHeaderOnly)
{
XNamespace namespaceUsed = _fileNameSpace;
// Load body elements (first is main text)
if (fileDocument.Root != null)
{
IEnumerable<XElement> xBodys = fileDocument.Root.Elements(_fileNameSpace + Fb2TextBodyElementName).ToArray();
// try to read some badly formatted FB2 files
if (!xBodys.Any())
{
namespaceUsed = "";
xBodys = fileDocument.Root.Elements(namespaceUsed + Fb2TextBodyElementName);
}
foreach (var body in xBodys)
{
var bodyItem = new BodyItem() { NameSpace = namespaceUsed };
try
{
bodyItem.Load(body);
}
catch (Exception)
{
continue;
}
_bodiesList.Add(bodyItem);
}
}
if (_bodiesList.Count > 0)
{
_mainBody = _bodiesList[0];
}
// Load binaries sections (currently images only)
if (fileDocument.Root != null)
{
IEnumerable<XElement> xBinaryes = fileDocument.Root.Elements(namespaceUsed + Fb2BinaryElementName).ToArray();
if (!xBinaryes.Any())
{
xBinaryes = fileDocument.Root.Elements(Fb2BinaryElementName);
}
foreach (var binarye in xBinaryes)
{
var item = new BinaryItem();
try
{
item.Load(binarye);
}
catch
{
continue;
}
// add just unique IDs to fix some invalid FB2s
if (!_binaryObjects.ContainsKey(item.Id))
{
_binaryObjects.Add(item.Id, item);
}
}
}
}
}
private void LoadDescriptionSection(XDocument fileDocument, bool loadBinaryItems = true)
{
if (fileDocument == null)
{
throw new ArgumentNullException("fileDocument");
}
if (fileDocument.Root == null)
{
throw new NullReferenceException("LoadDescriptionSection: Root is null");
}
XElement xTextDescription = fileDocument.Root.Element(_fileNameSpace + Fb2TextDescriptionElementName);
// attempt to load some bad FB2 with wrong namespace
XNamespace namespaceUsed = _fileNameSpace;
if (xTextDescription == null)
{
namespaceUsed = "";
xTextDescription = fileDocument.Root.Element(Fb2TextDescriptionElementName);
}
if (xTextDescription != null)
{
// Load Title info
XElement xTitleInfo = xTextDescription.Element(namespaceUsed + TitleInfoElementName);
if (xTitleInfo != null)
{
_titleInfo.Namespace = namespaceUsed;
try
{
_titleInfo.Load(xTitleInfo);
}
catch (Exception ex)
{
Debug.Print("Error reading title info : {0}", ex.Message);
}
}
// Load Src Title info
XElement xSrcTitleInfo = xTextDescription.Element(namespaceUsed + SrcTitleInfoElementName);
if (xSrcTitleInfo != null)
{
_srcTitleInfo.Namespace = _fileNameSpace;
try
{
_srcTitleInfo.Load(xSrcTitleInfo);
}
catch (Exception ex)
{
Debug.Print("Error reading source title info : {0}", ex.Message);
}
}
// Load document info
XElement xDocumentInfo = xTextDescription.Element(namespaceUsed + DocumentInfoElementName);
if (xDocumentInfo != null)
{
_documentInfo.Namespace = _fileNameSpace;
try
{
_documentInfo.Load(xDocumentInfo);
}
catch (Exception ex)
{
Debug.Print("Error reading document info : {0}", ex.Message);
}
}
// Load publish info
XElement xPublishInfo = xTextDescription.Element(namespaceUsed + ItemPublishInfo.PublishInfoElementName);
if (xPublishInfo != null)
{
_publishInfo.Namespace = _fileNameSpace;
try
{
_publishInfo.Load(xPublishInfo);
}
catch (Exception ex)
{
Debug.Print("Error reading publishing info : {0}", ex.Message);
}
}
XElement xCustomInfo = xTextDescription.Element(namespaceUsed + ItemCustomInfo.CustomInfoElementName);
if (xCustomInfo != null)
{
var custElement = new ItemCustomInfo {Namespace = _fileNameSpace};
try
{
custElement.Load(xCustomInfo);
_customInfo.Add(custElement);
}
catch (Exception ex)
{
Debug.Print("Error reading custom info : {0}", ex.Message);
}
}
IEnumerable<XElement> xInstructions = xTextDescription.Elements(xTextDescription.Name.Namespace + "output");
int outputCount = 0;
_output.Clear();
foreach (var xInstruction in xInstructions)
{
// only two elements allowed by scheme
if (outputCount > 1)
{
break;
}
var outp = new ShareInstructionType { Namespace = namespaceUsed };
try
{
outp.Load(xInstruction);
_output.Add(outp);
}
catch (Exception ex)
{
Debug.Print("Error reading output instructions : {0}", ex);
}
finally
{
outputCount++;
}
}
if (loadBinaryItems && _titleInfo.Cover != null)
{
foreach (InlineImageItem coverImag in _titleInfo.Cover.CoverpageImages)
{
if (string.IsNullOrEmpty(coverImag.HRef))
{
continue;
}
string coverref = coverImag.HRef.Substring(0, 1) == "#" ? coverImag.HRef.Substring(1) : coverImag.HRef;
IEnumerable<XElement> xBinaryes =
fileDocument.Root.Elements(_fileNameSpace + Fb2BinaryElementName).Where(
cov => ((cov.Attribute("id") != null) && (cov.Attribute("id").Value == coverref)));
foreach (var binarye in xBinaryes)
{
var item = new BinaryItem();
try
{
item.Load(binarye);
}
catch (Exception)
{
continue;
}
// add just unique IDs to fix some invalid FB2s
if (!_binaryObjects.ContainsKey(item.Id))
{
_binaryObjects.Add(item.Id, item);
}
}
}
}
}
}
public XDocument ToXML(bool bExportHeaderOnly)
{
var fileDocument = new XDocument(new XDeclaration("1.0","UTF-8","no"));
var xFictionBook = new XElement(Fb2Const.fb2DefaultNamespace+"FictionBook",
new XAttribute(XNamespace.Xmlns + "l", @"http://www.w3.org/1999/xlink"),
new XAttribute("xmlns", Fb2Const.fb2DefaultNamespace));
fileDocument.Add(xFictionBook);
foreach (StyleElement style in _styles)
{
try
{
xFictionBook.Add(style.ToXML());
}
catch (Exception ex)
{
Debug.Print("Error converting style data to XML: {0}", ex);
}
}
var xDescription = new XElement(Fb2Const.fb2DefaultNamespace+ Fb2TextDescriptionElementName);
xFictionBook.Add(xDescription);
try
{
xDescription.Add(_titleInfo.ToXML(TitleInfoElementName));
}
catch (Exception ex)
{
Debug.Print("Error converting TitleInfo data to XML: {0}", ex.Message);
}
if (_srcTitleInfo.BookTitle!=null)
{
try
{
xDescription.Add(_srcTitleInfo.ToXML(SrcTitleInfoElementName));
}
catch (Exception ex)
{
Debug.Print("Error converting SrcTitleInfo data to XML: {0}", ex.Message);
}
}
try
{
xDescription.Add(_documentInfo.ToXML());
}
catch (Exception ex)
{
Debug.Print("Error converting DocumentInfo data to XML: {0}", ex.Message);
}
xDescription.Add(_publishInfo.ToXML());
foreach (ItemCustomInfo custinfo in _customInfo)
{
xDescription.Add(custinfo.ToXML());
}
foreach (ShareInstructionType outp in _output)
{
xDescription.Add(outp.ToXML());
}
if (!bExportHeaderOnly)
{
foreach (BodyItem bodyItem in _bodiesList)
{
xFictionBook.Add(bodyItem.ToXML());
}
foreach (KeyValuePair<string, BinaryItem> bItem in _binaryObjects)
{
xFictionBook.Add(bItem.Value.ToXML());
}
}
else
{
foreach (InlineImageItem coverImag in _titleInfo.Cover.CoverpageImages)
{
if (string.IsNullOrEmpty(coverImag.HRef))
{
continue;
}
string coverref = coverImag.HRef.Substring(0, 1) == "#" ? coverImag.HRef.Substring(1) : coverImag.HRef;
xFictionBook.Add(_binaryObjects[coverref].ToXML());
}
}
return fileDocument;
}
}
}