4
4
using System . Xml ;
5
5
using System . IO ;
6
6
using System . Text ;
7
+ using System . Linq ;
7
8
8
9
namespace UEPub {
9
10
public class UEPubReader {
10
11
11
12
public string epubFolderLocation { get ; private set ; }
12
13
14
+ public List < string > chapters ;
15
+
13
16
private Dictionary < string , string > bookItems ;
14
17
private List < string > spine ;
15
18
@@ -32,6 +35,9 @@ public UEPubReader(string file){
32
35
epubFolderLocation = Application . temporaryCachePath + "/" + folderName ;
33
36
34
37
ZipUtil . Unzip ( file , epubFolderLocation ) ;
38
+
39
+ ParseContainer ( ) ;
40
+ ParseOPF ( ) ;
35
41
}
36
42
37
43
private void ParseContainer ( ) {
@@ -40,9 +46,11 @@ private void ParseContainer(){
40
46
XmlDocument doc = new XmlDocument ( ) ;
41
47
doc . Load ( containerFile ) ;
42
48
43
- XmlNode rootFile =
44
- doc . SelectSingleNode ( "/container/rootfiles/rootfile" ) ;
45
- opfFileString = rootFile . Attributes [ "full-path" ] . InnerText ;
49
+ var xmlnsManager = new System . Xml . XmlNamespaceManager ( doc . NameTable ) ;
50
+ xmlnsManager . AddNamespace ( "ns" , "urn:oasis:names:tc:opendocument:xmlns:container" ) ;
51
+
52
+ XmlNodeList rootFiles = doc . SelectNodes ( "/ns:container/ns:rootfiles/ns:rootfile" , xmlnsManager ) ;
53
+ opfFileString = rootFiles [ 0 ] . Attributes [ "full-path" ] . InnerText ;
46
54
}
47
55
48
56
private void ParseOPF ( ) {
@@ -51,17 +59,33 @@ private void ParseOPF(){
51
59
XmlDocument doc = new XmlDocument ( ) ;
52
60
doc . Load ( opfFile ) ;
53
61
54
- var nodes = doc . SelectNodes ( "/package/manifest" ) ;
62
+ var xmlnsManager = new System . Xml . XmlNamespaceManager ( doc . NameTable ) ;
63
+ xmlnsManager . AddNamespace ( "ns" , "http://www.idpf.org/2007/opf" ) ;
64
+ xmlnsManager . AddNamespace ( "dc" , "http://purl.org/dc/elements/1.1/" ) ;
65
+ xmlnsManager . AddNamespace ( "dcterms" , "http://purl.org/dc/terms/" ) ;
66
+
67
+ var nodes = doc . SelectNodes ( "/ns:package/ns:manifest/ns:item" , xmlnsManager ) ;
55
68
56
69
foreach ( XmlNode node in nodes ) {
57
70
bookItems [ node . Attributes [ "id" ] . InnerText ] = node . Attributes [ "href" ] . InnerText ;
58
71
}
59
72
60
- nodes = doc . SelectNodes ( "/package/spine" ) ;
73
+ nodes = doc . SelectNodes ( "/ns: package/ns: spine/ns:itemref" , xmlnsManager ) ;
61
74
62
75
foreach ( XmlNode node in nodes ) {
63
76
spine . Add ( node . Attributes [ "idref" ] . InnerText ) ;
64
77
}
78
+
79
+ var toc = spine . Select ( x => bookItems [ x ] )
80
+ . ToList ( ) ;
81
+
82
+ chapters = new List < string > ( ) ;
83
+
84
+ foreach ( string s in toc ) {
85
+ StreamReader sr = new StreamReader ( Path . GetDirectoryName ( opfFile ) + "/" + s ) ;
86
+ chapters . Add ( sr . ReadToEnd ( ) ) ;
87
+ sr . Close ( ) ;
88
+ }
65
89
}
66
90
}
67
91
}
0 commit comments