/// <summary>
        /// package
        /// </summary>
        /// <param name="packageDocument"></param>
        internal static List<String> findCoverHrefs(XElement packageDocument)
        {
            List<String> result = new List<String>();

            // try and find a meta tag with name = 'cover' and a non-blank id
            String coverResourceId = DOMUtil.getFindAttributeValue(packageDocument, NAMESPACE_OPF,
                                                OPFTags.meta, OPFAttributes.name, OPFValues.meta_cover,
                                                OPFAttributes.content);
            String coverHref = string.Empty;
            if (StringUtil.isNotBlank(coverResourceId))
            {
                coverHref = DOMUtil.getFindAttributeValue(packageDocument, NAMESPACE_OPF,
                        OPFTags.item, OPFAttributes.id, coverResourceId,
                        OPFAttributes.href);
                if (StringUtil.isNotBlank(coverHref))
                {
                    result.Add(coverHref);
                }
                else
                {
                    result.Add(coverResourceId); // maybe there was a cover href put in the cover id attribute
                }
            }
            // try and find a reference tag with type is 'cover' and reference is not blank
            coverHref = DOMUtil.getFindAttributeValue(packageDocument, NAMESPACE_OPF,
                                               OPFTags.reference, OPFAttributes.type, OPFValues.reference_cover,
                                               OPFAttributes.href);
            if (StringUtil.isNotBlank(coverHref))
            {
                result.Add(coverHref);
            }
            return result;
        }
        /// <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>
        /// Reads the manifest containing the resource ids, hrefs and mediatypes.
        /// </summary>
        /// <param>book</param>
        /// <param>resourcesByHref</param>
        /// <param>a Map with resources, with their id's as key.</param>
        /// <param name="packageDocument"></param>
        /// <param name="packageHref"></param>
        /// <param name="epubReader"></param>
        /// <param name="resources"></param>
        /// <param name="idMapping"></param>
        private static Resources readManifest(XElement packageDocument, String packageHref, EpubReader epubReader, Resources resources, Dictionary<String, String> idMapping)
        {
            XElement manifestElement = DOMUtil.getFirstElementByTagNameNS(packageDocument, NAMESPACE_OPF, OPFTags.manifest);
            Resources result = new Resources();
            if (manifestElement == null)
            {
                return result;
            }
            var itemElements = packageDocument.Elements(NAMESPACE_OPF + OPFTags.item).Elements<XElement>();

            foreach (XElement itemElement in (from e in itemElements where e.Value.Trim() != string.Empty select e))
            {
                String id = DOMUtil.getAttribute(itemElement, OPFAttributes.id);
                String href = DOMUtil.getAttribute(itemElement, OPFAttributes.href);
                try
                {
                    href = System.Web.HttpUtility.UrlDecode(href, System.Text.Encoding.GetEncoding(Constants.ENCODING));
                }
                catch (Exception e)
                {
                    //log.error(e.getMessage());
                }
                String mediaTypeName = DOMUtil.getAttribute(itemElement, OPFAttributes.media_type);
                Resource resource = resources.remove(href);
                if (resource == null)
                {
                    //log.error("resource with href '" + href + "' not found");
                    continue;
                }
                resource.setId(id);
                MediaType mediaType = MediatypeService.getMediaTypeByName(mediaTypeName);
                if (mediaType != null)
                {
                    resource.setMediaType(mediaType);
                }
                result.add(resource);
                idMapping.Add(id, resource.getId());
            }
            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;
        }
 /// <summary>
 /// Reads the book's guide. Here some more attempts are made at finding the cover
 /// page.
 /// </summary>
 /// <param name="packageDocument"></param>
 /// <param name="epubReader"></param>
 /// <param name="book"></param>
 /// <param name="resources">resources</param>
 private static void readGuide(XElement packageDocument, EpubReader epubReader, Book book, Resources resources)
 {
     XElement guideElement = DOMUtil.getFirstElementByTagNameNS(packageDocument, NAMESPACE_OPF, OPFTags.guide);
     if (guideElement == null)
     {
         return;
     }
     Guide guide = book.getGuide();
     var guideReferences = packageDocument.Elements(NAMESPACE_OPF + OPFTags.reference).Elements<XElement>();
     foreach (XElement referenceElement in (from e in guideReferences where e.Value.Trim() != string.Empty select e))
     {
         String resourceHref = DOMUtil.getAttribute(referenceElement, OPFAttributes.href);
         if (StringUtil.isBlank(resourceHref))
         {
             continue;
         }
         Resource resource = resources.getByHref(StringUtil.substringBefore(resourceHref, Constants.FRAGMENT_SEPARATOR_CHAR));
         if (resource == null)
         {
             //log.error("Guide is referencing resource with href " + resourceHref + " which could not be found");
             continue;
         }
         String type = DOMUtil.getAttribute(referenceElement, OPFAttributes.type);
         if (StringUtil.isBlank(type))
         {
             //log.error("Guide is referencing resource with href " + resourceHref + " which is missing the 'type' attribute");
             continue;
         }
         String title = DOMUtil.getAttribute(referenceElement, OPFAttributes.title);
         if (GuideReference.COVER.Equals(type))
         {
             continue; // cover is handled elsewhere
         }
         GuideReference reference = new GuideReference(resource, type, title, StringUtil.substringAfter(resourceHref, Constants.FRAGMENT_SEPARATOR_CHAR));
         guide.addReference(reference);
     }
 }