/// <summary> /// Reads the document's spine, containing all sections in reading order. /// </summary> /// <param>book</param> /// <param>resourcesById</param> /// <param name="packageDocument"></param> /// <param name="epubReader"></param> /// <param name="resources"></param> /// <param name="idMapping"></param> private static Spine readSpine(XElement packageDocument, EpubReader epubReader, Resources resources, Dictionary<String, String> idMapping) { XElement spineElement = DOMUtil.getFirstElementByTagNameNS(packageDocument, NAMESPACE_OPF, OPFTags.spine); if (spineElement == null) { //log.error("Element " + OPFTags.spine + " not found in package document, generating one automatically"); return generateSpineFromResources(resources); } Spine result = new Spine(); result.setTocResource(findTableOfContentsResource(spineElement, resources)); var spineNodes = packageDocument.Elements(NAMESPACE_OPF + OPFTags.itemref).Elements<XElement>(); IEnumerator spineNode = spineNodes.GetEnumerator(); List<SpineReference> spineReferences = new List<SpineReference>(); while (spineNode.MoveNext()) { XElement spineItem = (XElement)spineNode.Current; String itemref = DOMUtil.getAttribute(spineItem, OPFAttributes.idref); if (StringUtil.isBlank(itemref)) { //log.error("itemref with missing or empty idref"); // XXX continue; } String id = idMapping[itemref]; if (id == null) { id = itemref; } Resource resource = resources.getByIdOrHref(id); if (resource == null) { //log.error("resource with id \'" + id + "\' not found"); continue; } SpineReference spineReference = new SpineReference(resource); if (OPFValues.no.Equals(DOMUtil.getAttribute(spineItem, OPFAttributes.linear))) { spineReference.setLinear(false); } spineReferences.Add(spineReference); } result.setSpineReferences(spineReferences); return result; }
/// <summary> /// The spine tag should contain a 'toc' attribute with as value the resource id of /// the table of contents resource. Here we try several ways of finding this table /// of contents resource. We try the given attribute value, some often-used ones /// and finally look through all resources for the first resource with the table of /// contents mimetype. /// </summary> /// <param>resourcesById</param> /// <param name="spineElement"></param> /// <param name="resources"></param> private static Resource findTableOfContentsResource(XElement spineElement, Resources resources) { String tocResourceId = DOMUtil.getAttribute(spineElement, OPFAttributes.toc); Resource tocResource = null; if (StringUtil.isNotBlank(tocResourceId)) { tocResource = resources.getByIdOrHref(tocResourceId); } if (tocResource != null) { return tocResource; } for (int i = 0; i < POSSIBLE_NCX_ITEM_IDS.Length; i++) { tocResource = resources.getByIdOrHref(POSSIBLE_NCX_ITEM_IDS[i]); if (tocResource != null) { return tocResource; } tocResource = resources.getByIdOrHref(POSSIBLE_NCX_ITEM_IDS[i].ToUpper()); if (tocResource != null) { return tocResource; } } // get the first resource with the NCX mediatype tocResource = resources.findFirstResourceByMediaType(MediatypeService.NCX); if (tocResource == null) { // log.error("Could not find table of contents resource. Tried resource with id '" + tocResourceId + "', " + Constants.DEFAULT_TOC_ID + ", " + Constants.DEFAULT_TOC_ID.toUpperCase() + " and any NCX resource."); } return tocResource; }