/// <summary> /// Adds the given spineReference to the spine references and returns it. /// </summary> /// <param name="spineReference"></param> public SpineReference addSpineReference(SpineReference spineReference) { if (spineReferences == null) { this.spineReferences = new List<SpineReference>(); } spineReferences.Add(spineReference); return spineReference; }
/// <summary> /// Finds the first resource that has the given resourceId. Null if not found. /// </summary> /// <param name="resourceId"></param> public int findFirstResourceById(string resourceId) { if (StringUtil.isBlank(resourceId)) { return -1; } for (int i = 0; i < spineReferences.Count; i++) { SpineReference spineReference = spineReferences[i]; if (resourceId.Equals(spineReference.getResourceId())) { return i; } } return -1; }
/// <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; }