private static EpubNavigationPageTarget ReadNavigationPageTarget(XElement navigationPageTargetNode) { EpubNavigationPageTarget result = new EpubNavigationPageTarget(); foreach (XAttribute navigationPageTargetNodeAttribute in navigationPageTargetNode.Attributes()) { string attributeValue = navigationPageTargetNodeAttribute.Value; switch (navigationPageTargetNodeAttribute.Name.LocalName.ToLowerInvariant()) { case "id": result.Id = attributeValue; break; case "value": result.Value = attributeValue; break; case "type": EpubNavigationPageTargetType type; if (!Enum.TryParse(attributeValue, out type)) { throw new Exception(String.Format("Incorrect EPUB navigation page target: {0} is incorrect value for page target type.", attributeValue)); } result.Type = type; break; case "class": result.Class = attributeValue; break; case "playOrder": result.PlayOrder = attributeValue; break; } } if (result.Type == default(EpubNavigationPageTargetType)) { throw new Exception("Incorrect EPUB navigation page target: page target type is missing."); } foreach (XElement navigationPageTargetChildNode in navigationPageTargetNode.Elements()) { switch (navigationPageTargetChildNode.Name.LocalName.ToLowerInvariant()) { case "navlabel": EpubNavigationLabel navigationLabel = ReadNavigationLabel(navigationPageTargetChildNode); result.NavigationLabels.Add(navigationLabel); break; case "content": EpubNavigationContent content = ReadNavigationContent(navigationPageTargetChildNode); result.Content = content; break; } } if (!result.NavigationLabels.Any()) { throw new Exception("Incorrect EPUB navigation page target: at least one navLabel element is required."); } return(result); }
private static EpubNavigationPoint ReadNavigationPoint(XElement navigationPointNode) { EpubNavigationPoint result = new EpubNavigationPoint(); foreach (XAttribute navigationPointNodeAttribute in navigationPointNode.Attributes()) { string attributeValue = navigationPointNodeAttribute.Value; switch (navigationPointNodeAttribute.Name.LocalName.ToLowerInvariant()) { case "id": result.Id = attributeValue; break; case "class": result.Class = attributeValue; break; case "playOrder": result.PlayOrder = attributeValue; break; } } if (String.IsNullOrWhiteSpace(result.Id)) { throw new Exception("Incorrect EPUB navigation point: point ID is missing."); } result.NavigationLabels = new List <EpubNavigationLabel>(); result.ChildNavigationPoints = new List <EpubNavigationPoint>(); foreach (XElement navigationPointChildNode in navigationPointNode.Elements()) { switch (navigationPointChildNode.Name.LocalName.ToLowerInvariant()) { case "navlabel": EpubNavigationLabel navigationLabel = ReadNavigationLabel(navigationPointChildNode); result.NavigationLabels.Add(navigationLabel); break; case "content": EpubNavigationContent content = ReadNavigationContent(navigationPointChildNode); result.Content = content; break; case "navpoint": EpubNavigationPoint childNavigationPoint = ReadNavigationPoint(navigationPointChildNode); result.ChildNavigationPoints.Add(childNavigationPoint); break; } } if (!result.NavigationLabels.Any()) { throw new Exception(String.Format("EPUB parsing error: navigation point {0} should contain at least one navigation label.", result.Id)); } if (result.Content == null) { throw new Exception(String.Format("EPUB parsing error: navigation point {0} should contain content.", result.Id)); } return(result); }
private static EpubNavigationLabel ReadNavigationLabel(XElement navigationLabelNode) { EpubNavigationLabel result = new EpubNavigationLabel(); XElement navigationLabelTextNode = navigationLabelNode.Element(navigationLabelNode.Name.Namespace + "text"); if (navigationLabelTextNode == null) { throw new Exception("Incorrect EPUB navigation label: label text element is missing."); } result.Text = navigationLabelTextNode.Value; return(result); }
private static EpubNavigationLabel ReadNavigationLabel(XmlNode navigationLabelNode) { EpubNavigationLabel result = new EpubNavigationLabel(); XmlNode navigationLabelTextNode = navigationLabelNode.ChildNodes.OfType <XmlNode>(). Where(node => String.Compare(node.LocalName, "text", StringComparison.OrdinalIgnoreCase) == 0).FirstOrDefault(); if (navigationLabelTextNode == null) { throw new Exception("Incorrect EPUB navigation label: label text element is missing"); } result.Text = navigationLabelTextNode.InnerText; return(result); }
private static EpubNavigationTarget ReadNavigationTarget(XElement navigationTargetNode) { EpubNavigationTarget result = new EpubNavigationTarget(); foreach (XAttribute navigationPageTargetNodeAttribute in navigationTargetNode.Attributes()) { string attributeValue = navigationPageTargetNodeAttribute.Value; switch (navigationPageTargetNodeAttribute.Name.LocalName.ToLowerInvariant()) { case "id": result.Id = attributeValue; break; case "value": result.Value = attributeValue; break; case "class": result.Class = attributeValue; break; case "playOrder": result.PlayOrder = attributeValue; break; } } if (String.IsNullOrWhiteSpace(result.Id)) { throw new Exception("Incorrect EPUB navigation target: navigation target ID is missing."); } foreach (XElement navigationTargetChildNode in navigationTargetNode.Elements()) { switch (navigationTargetChildNode.Name.LocalName.ToLowerInvariant()) { case "navlabel": EpubNavigationLabel navigationLabel = ReadNavigationLabel(navigationTargetChildNode); result.NavigationLabels.Add(navigationLabel); break; case "content": EpubNavigationContent content = ReadNavigationContent(navigationTargetChildNode); result.Content = content; break; } } if (!result.NavigationLabels.Any()) { throw new Exception("Incorrect EPUB navigation target: at least one navLabel element is required."); } return(result); }
private static async Task <EpubNavigationLabel> ReadNavigationLabelAsync(XmlReader reader) { EpubNavigationLabel result = new EpubNavigationLabel(); var navigationLabelText = string.Empty; //We have to read to <text> subnode of the navLabel node do { if ((reader.LocalName.ToLowerInvariant() == "text") && (reader.NodeType == XmlNodeType.Element)) { navigationLabelText = reader.ReadElementContentAsString(); } } while (await reader.ReadAsync() && !(reader.NodeType == XmlNodeType.EndElement && reader.LocalName.ToLowerInvariant() == "navlabel")); if (string.IsNullOrEmpty(navigationLabelText)) { throw new Exception("Incorrect EPUB navigation label: label text element is missing"); } result.Text = navigationLabelText; return(result); }
private static EpubNavigationList ReadNavigationList(XElement navigationListNode) { EpubNavigationList result = new EpubNavigationList(); foreach (XAttribute navigationListNodeAttribute in navigationListNode.Attributes()) { string attributeValue = navigationListNodeAttribute.Value; switch (navigationListNodeAttribute.Name.LocalName.ToLowerInvariant()) { case "id": result.Id = attributeValue; break; case "class": result.Class = attributeValue; break; } } foreach (XElement navigationListChildNode in navigationListNode.Elements()) { switch (navigationListChildNode.Name.LocalName.ToLowerInvariant()) { case "navlabel": EpubNavigationLabel navigationLabel = ReadNavigationLabel(navigationListChildNode); result.NavigationLabels.Add(navigationLabel); break; case "navTarget": EpubNavigationTarget navigationTarget = ReadNavigationTarget(navigationListChildNode); result.NavigationTargets.Add(navigationTarget); break; } } if (!result.NavigationLabels.Any()) { throw new Exception("Incorrect EPUB navigation page target: at least one navLabel element is required."); } return(result); }
private static async Task <EpubNavigationPoint> ReadNavigationPointAsync(XmlReader reader) { EpubNavigationPoint result = new EpubNavigationPoint(); //we have to skip first entry as it could be empty after new sub-reader created if (reader.NodeType == XmlNodeType.None) { await reader.ReadAsync(); } //Now the pointer should point to the <navPoint> itself while (reader.MoveToNextAttribute()) //Doing this we just passing through <navPoint> tag { switch (reader.LocalName.ToLowerInvariant()) //We have to collect all possible attributes from the <navPoint> { case "id": result.Id = reader.Value; break; case "class": result.Class = reader.Value; break; case "playorder": result.PlayOrder = reader.Value; break; } } if (String.IsNullOrWhiteSpace(result.Id)) { throw new Exception("Incorrect EPUB navigation point: point ID is missing"); } result.NavigationLabels = new List <EpubNavigationLabel>(); result.ChildNavigationPoints = new List <EpubNavigationPoint>(); // We need to make sure that we will return pointer back to <navPoint> entry after reading all attributes reader.MoveToElement(); //Now we are looking for subnodes - navLabel, content and sub-navPoints while (await reader.ReadAsync()) { if (reader.NodeType != XmlNodeType.EndElement) { switch (reader.LocalName.ToLowerInvariant()) { case "navlabel": EpubNavigationLabel navigationLabel = await ReadNavigationLabelAsync(reader); // Adding label to collection result.NavigationLabels.Add(navigationLabel); break; case "content": EpubNavigationContent content = await ReadNavigationContentAsync(reader); //Adding content to collection result.Content = content; break; case "navpoint": //Yeep. Looks like we found a <navPoint> sub-node XmlReader subTree = reader.ReadSubtree(); //Cooking a separate sub-reader scope for this node to separate it from the others siblings EpubNavigationPoint childNavigationPoint = await ReadNavigationPointAsync(subTree); // I hate recursion... //Adding a child to a collection result.ChildNavigationPoints.Add(childNavigationPoint); subTree.Dispose(); break; } } } ; return(result); }