File size: 9,267 Bytes
fab29d7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using System.Xml.Linq;
using VersOne.Epub.Environment;
using VersOne.Epub.Options;
using VersOne.Epub.Schema;
using VersOne.Epub.Utils;

namespace VersOne.Epub.Internal
{
    internal class Epub3NavDocumentReader
    {
        private readonly EpubReaderOptions epubReaderOptions;

        public Epub3NavDocumentReader(EpubReaderOptions? epubReaderOptions = null)
        {
            this.epubReaderOptions = epubReaderOptions ?? new EpubReaderOptions();
        }

        public async Task<Epub3NavDocument?> ReadEpub3NavDocumentAsync(IZipFile epubFile, string contentDirectoryPath, EpubPackage package)
        {
            EpubManifestItem navManifestItem = package.Manifest.Items.Find(item => item.Properties != null && item.Properties.Contains(EpubManifestProperty.NAV));
            if (navManifestItem == null)
            {
                if (package.EpubVersion == EpubVersion.EPUB_2)
                {
                    return null;
                }
                else
                {
                    throw new Epub3NavException("EPUB parsing error: NAV item not found in EPUB manifest.");
                }
            }
            string navFileEntryPath = ContentPathUtils.Combine(contentDirectoryPath, navManifestItem.Href);
            IZipFileEntry navFileEntry = epubFile.GetEntry(navFileEntryPath) ??
                throw new Epub3NavException($"EPUB parsing error: navigation file {navFileEntryPath} not found in the EPUB file.");
            if (navFileEntry.Length > Int32.MaxValue)
            {
                throw new Epub3NavException($"EPUB parsing error: navigation file {navFileEntryPath} is larger than 2 GB.");
            }
            XDocument navDocument;
            using (Stream containerStream = navFileEntry.Open())
            {
                navDocument = await XmlUtils.LoadDocumentAsync(containerStream, epubReaderOptions.XmlReaderOptions).ConfigureAwait(false);
            }
            XNamespace xhtmlNamespace = navDocument.Root.Name.Namespace;
            XElement htmlNode = navDocument.Element(xhtmlNamespace + "html") ?? throw new Epub3NavException("EPUB parsing error: navigation file does not contain html element.");
            XElement bodyNode = htmlNode.Element(xhtmlNamespace + "body") ?? throw new Epub3NavException("EPUB parsing error: navigation file does not contain body element.");
            List<Epub3Nav> navs = new();
            ReadEpub3NavsWithinContainerElement(bodyNode, navs);
            return new(navFileEntryPath, navs);
        }

        private static void ReadEpub3NavsWithinContainerElement(XElement containerElement, List<Epub3Nav> resultNavs)
        {
            foreach (XElement childElement in containerElement.Elements())
            {
                if (childElement.GetLowerCaseLocalName() == "nav")
                {
                    Epub3Nav? epub3Nav = ReadEpub3Nav(childElement);
                    if (epub3Nav != null)
                    {
                        resultNavs.Add(epub3Nav);
                    }
                }
                else
                {
                    ReadEpub3NavsWithinContainerElement(childElement, resultNavs);
                }
            }
        }

        private static Epub3Nav? ReadEpub3Nav(XElement navNode)
        {
            Epub3StructuralSemanticsProperty? type = null;
            bool isHidden = false;
            string? head = null;
            Epub3NavOl? ol = null;
            foreach (XAttribute navNodeAttribute in navNode.Attributes())
            {
                string attributeValue = navNodeAttribute.Value;
                switch (navNodeAttribute.GetLowerCaseLocalName())
                {
                    case "type":
                        type = Epub3StructuralSemanticsPropertyParser.ParseProperty(attributeValue);
                        break;
                    case "hidden":
                        isHidden = true;
                        break;
                }
            }
            if (type == null)
            {
                return null;
            }
            foreach (XElement navChildNode in navNode.Elements())
            {
                switch (navChildNode.GetLowerCaseLocalName())
                {
                    case "h1":
                    case "h2":
                    case "h3":
                    case "h4":
                    case "h5":
                    case "h6":
                        head = navChildNode.Value.Trim();
                        break;
                    case "ol":
                        ol = ReadEpub3NavOl(navChildNode);
                        break;
                }
            }
            if (ol == null)
            {
                throw new Epub3NavException("EPUB parsing error: 'nav' element in the navigation file does not contain a required 'ol' element.");
            }
            return new(type, isHidden, head, ol);
        }

        private static Epub3NavOl ReadEpub3NavOl(XElement epub3NavOlNode)
        {
            bool isHidden = false;
            List<Epub3NavLi> lis = new();
            foreach (XAttribute navOlNodeAttribute in epub3NavOlNode.Attributes())
            {
                switch (navOlNodeAttribute.GetLowerCaseLocalName())
                {
                    case "hidden":
                        isHidden = true;
                        break;
                }
            }
            foreach (XElement navOlChildNode in epub3NavOlNode.Elements())
            {
                switch (navOlChildNode.GetLowerCaseLocalName())
                {
                    case "li":
                        Epub3NavLi epub3NavLi = ReadEpub3NavLi(navOlChildNode);
                        lis.Add(epub3NavLi);
                        break;
                }
            }
            return new(isHidden, lis);
        }

        private static Epub3NavLi ReadEpub3NavLi(XElement epub3NavLiNode)
        {
            Epub3NavAnchor? anchor = null;
            Epub3NavSpan? span = null;
            Epub3NavOl? childOl = null;
            foreach (XElement navLiChildNode in epub3NavLiNode.Elements())
            {
                switch (navLiChildNode.GetLowerCaseLocalName())
                {
                    case "a":
                        Epub3NavAnchor epub3NavAnchor = ReadEpub3NavAnchor(navLiChildNode);
                        anchor = epub3NavAnchor;
                        break;
                    case "span":
                        Epub3NavSpan epub3NavSpan = ReadEpub3NavSpan(navLiChildNode);
                        span = epub3NavSpan;
                        break;
                    case "ol":
                        Epub3NavOl epub3NavOl = ReadEpub3NavOl(navLiChildNode);
                        childOl = epub3NavOl;
                        break;
                }
            }
            if (anchor == null && span == null)
            {
                throw new Epub3NavException("EPUB parsing error: 'li' element in the navigation file must contain either an 'a' element or a 'span' element.");
            }
            return new(anchor, span, childOl);
        }

        private static Epub3NavAnchor ReadEpub3NavAnchor(XElement epub3NavAnchorNode)
        {
            string? href = null;
            string text;
            string? title = null;
            string? alt = null;
            Epub3StructuralSemanticsProperty? type = null;
            foreach (XAttribute navAnchorNodeAttribute in epub3NavAnchorNode.Attributes())
            {
                string attributeValue = navAnchorNodeAttribute.Value;
                switch (navAnchorNodeAttribute.GetLowerCaseLocalName())
                {
                    case "href":
                        href = Uri.UnescapeDataString(attributeValue);
                        break;
                    case "title":
                        title = attributeValue;
                        break;
                    case "alt":
                        alt = attributeValue;
                        break;
                    case "type":
                        type = Epub3StructuralSemanticsPropertyParser.ParseProperty(attributeValue);
                        break;
                }
            }
            text = epub3NavAnchorNode.Value.Trim();
            return new(href, text, title, alt, type);
        }

        private static Epub3NavSpan ReadEpub3NavSpan(XElement epub3NavSpanNode)
        {
            string text;
            string? title = null;
            string? alt = null;
            foreach (XAttribute navSpanNodeAttribute in epub3NavSpanNode.Attributes())
            {
                string attributeValue = navSpanNodeAttribute.Value;
                switch (navSpanNodeAttribute.GetLowerCaseLocalName())
                {
                    case "title":
                        title = attributeValue;
                        break;
                    case "alt":
                        alt = attributeValue;
                        break;
                }
            }
            text = epub3NavSpanNode.Value.Trim();
            return new(text, title, alt);
        }
    }
}