/// <summary> /// Adds a resource to the resources. Fixes the resources id and href if necessary. /// /// </summary> /// <param name="resource"></param> public Resource add(Resource resource) { fixResourceHref(resource); fixResourceId(resource); resources.Add(resource.getHref(), resource); return resource; }
public void testGetContents1() { Book book = new Book(); Resource resource1 = new Resource("id1", System.Text.Encoding.UTF8.GetBytes("Hello, world !"), "chapter1.html", MediatypeService.XHTML); book.getSpine().addResource(resource1); book.getTableOfContents().addSection(resource1, "My first chapter"); Assert.AreEqual(1, book.getContents().Count); }
/// <summary> /// Adds the resource to the table of contents of the book as a child section of /// the given parentSection /// </summary> /// <param name="parentSection"></param> /// <param name="sectionTitle"></param> /// <param name="resource"></param> public TOCReference addSection(TOCReference parentSection, string sectionTitle, Resource resource) { getResources().add(resource); if (spine.findFirstResourceById(resource.getId()) < 0) { spine.addSpineReference(new SpineReference(resource)); } return parentSection.addChildSection(new TOCReference(sectionTitle, resource)); }
public void testGetContents3() { Book book = new Book(); Resource resource1 = new Resource("id1", System.Text.Encoding.UTF8.GetBytes("Hello, world !"), "chapter1.html", MediatypeService.XHTML); book.getSpine().addResource(resource1); Resource resource2 = new Resource("id1", System.Text.Encoding.UTF8.GetBytes("Hello, world !"), "chapter2.html", MediatypeService.XHTML); book.getTableOfContents().addSection(resource2, "My first chapter"); book.getGuide().addReference(new GuideReference(resource2, GuideReference.FOREWORD, "The Foreword")); Assert.AreEqual(2, book.getContents().Count); }
/// <summary> /// Adds a resource to the book's set of resources, table of contents and if there /// is no resource with the id in the spine also adds it to the spine. /// </summary> /// <param name="title"></param> /// <param name="resource"></param> public TOCReference addSection(string title, Resource resource) { getResources().add(resource); TOCReference tocReference = tableOfContents.addTOCReference(new TOCReference(title, resource)); if (spine.findFirstResourceById(resource.getId()) < 0) { spine.addSpineReference(new SpineReference(resource)); } return tocReference; }
public void testAddResource1() { Resource resource = new Resource("foo"); TableOfContents toc = new TableOfContents(); TOCReference tocReference = toc.addSection(resource, "apple/pear", "/"); Assert.IsNotNull(tocReference); Assert.IsNotNull(tocReference.getResource()); Assert.AreEqual(2, toc.size()); Assert.AreEqual("pear", tocReference.getTitle()); }
public void testAddResourceWithIndexes() { Resource resource = new Resource("foo"); TableOfContents toc = new TableOfContents(); TOCReference tocReference = toc.addSection(resource, new int[] { 0, 0 }, "Section ", "."); // check newly created TOCReference Assert.IsNotNull(tocReference); Assert.IsNotNull(tocReference.getResource()); Assert.AreEqual("Section 1.1", tocReference.getTitle()); // check table of contents Assert.AreEqual(1, toc.getTocReferences().Count); Assert.AreEqual(1, toc.getTocReferences()[0].getChildren().Count); Assert.AreEqual(2, toc.size()); Assert.AreEqual("Section 1", toc.getTocReferences()[0].getTitle()); Assert.AreEqual("Section 1.1", toc.getTocReferences()[0].getChildren()[0].getTitle()); Assert.AreEqual(1, toc.getTocReferences()[0].getChildren().Count); }
/// /// <param name="packageResource"></param> /// <param name="epubReader"></param> /// <param name="book"></param> /// <param name="resources"></param> public static void read(Resource packageResource, EpubReader epubReader, Book book, Resources resources) { XElement packageDocument = XElement.Load(packageResource.getInputStream()); String packageHref = packageResource.getHref(); resources = fixHrefs(packageHref, resources); readGuide(packageDocument, epubReader, book, resources); System.Collections.Generic.Dictionary<string, string> idMapping = new Dictionary<string, string>(); resources = readManifest(packageDocument, packageHref, epubReader, resources, idMapping); book.setResources(resources); readCover(packageDocument, book); // book.setMetadata(PackageDocumentMetadataReader.readMetadata(packageDocument, book.getResources())); book.setSpine(readSpine(packageDocument, epubReader, book.getResources(), idMapping)); // if we did not find a cover page then we make the first page of the book the cover page if (book.getCoverPage() == null && book.getSpine().size() > 0) { book.setCoverPage(book.getSpine().getResource(0)); } }
/// <summary> /// Calls addTOCReferenceAtLocation after splitting the path using the /// DEFAULT_PATH_SEPARATOR. /// </summary> /// <param name="resource"></param> /// <param name="path"></param> public TOCReference addSection(Resource resource, string path) { return addSection(resource, path, DEFAULT_PATH_SEPARATOR); }
/// <summary> /// Adds the given Resources to the TableOfContents at the location specified by /// the pathElements. Example: Calling this method with a Resource and new /// String[] {"chapter1", "paragraph1"} will result in the following: /// <ul> /// <li>a TOCReference with the title "chapter1" at the root level.<br/> /// If this TOCReference did not yet exist it will have been created and does not /// point to any resource</li> /// <li>A TOCReference that has the title "paragraph1". This /// TOCReference will be the child of TOCReference "chapter1" and will point to the /// given Resource</li> /// </ul> /// </summary> /// <param name="resource"></param> /// <param name="pathElements"></param> public TOCReference addSection(Resource resource, String[] pathElements) { if (pathElements == null || pathElements.Length == 0) { return null; } TOCReference result = null; List<TOCReference> currentTocReferences = this.tocReferences; for (int i = 0; i < pathElements.Length; i++) { String currentTitle = pathElements[i]; result = findTocReferenceByTitle(currentTitle, currentTocReferences); if (result == null) { result = new TOCReference(currentTitle, null); currentTocReferences.Add(result); } currentTocReferences = result.getChildren(); } result.setResource(resource); return result; }
/// <summary> /// Adds the given Resources to the TableOfContents at the location specified by /// the pathElements. Example: Calling this method with a Resource and new int[] /// {0, 0} will result in the following: /// <ul> /// <li>a TOCReference at the root level.<br/> If this TOCReference did /// not yet exist it will have been created with a title of "" and does not point /// to any resource</li> /// <li>A TOCReference that points to the given resource and is a child /// of the previously created TOCReference.<br/> If this TOCReference didn't exist /// yet it will be created and have a title of ""</li> /// </ul> /// </summary> /// <param name="resource"></param> /// <param name="pathElements"></param> /// <param name="sectionTitlePrefix"></param> /// <param name="sectionNumberSeparator"></param> public TOCReference addSection(Resource resource, int[] pathElements, string sectionTitlePrefix, string sectionNumberSeparator) { if (pathElements == null || pathElements.Length == 0) { return null; } TOCReference result = null; List<TOCReference> currentTocReferences = this.tocReferences; for (int i = 0; i < pathElements.Length; i++) { int currentIndex = pathElements[i]; if (currentIndex > 0 && currentIndex < (currentTocReferences.Count - 1)) { result = currentTocReferences[currentIndex]; } else { result = null; } if (result == null) { paddTOCReferences(currentTocReferences, pathElements, i, sectionTitlePrefix, sectionNumberSeparator); result = currentTocReferences[currentIndex]; } currentTocReferences = result.getChildren(); } result.setResource(resource); return result; }
/// <summary> /// Sets the resource to the given resource and sets the fragmentId to null. /// </summary> /// <param name="resource"></param> public void setResource(Resource resource, string fragmentId) { base.setResource(resource); this.fragmentId = fragmentId; }
/// /// <param name="packageResource"></param> /// <param name="book"></param> private Resource processNcxResource(Resource packageResource, Book book) { return null; }
/// <summary> /// Check if the id is a valid identifier. if not: prepend with valid identifier /// </summary> /// <param name="resourceId"></param> /// <param name="resource"></param> private string makeValidId(string resourceId, Resource resource) { if (StringUtil.isNotBlank(resourceId)) { resourceId = getResourceItemPrefix(resource) + resourceId; } return resourceId; }
public TitledResourceReference(Resource resource, string title, string fragmentId) : base(resource) { this.title = title; this.fragmentId = fragmentId; }
/// /// <param name="resource"></param> private void fixResourceHref(Resource resource) { if (StringUtil.isNotBlank(resource.getHref()) && !resources.ContainsKey(resource.getHref())) { return; } if (StringUtil.isBlank(resource.getHref())) { if (resource.getMediaType() == null) { throw new ArgumentException("Resource must have either a MediaType or a href"); } int i = 1; String href = createHref(resource.getMediaType(), i); while (resources.ContainsKey(href)) { href = createHref(resource.getMediaType(), (++i)); } resource.setHref(href); } }
public TitledResourceReference(Resource resource) : this(resource, null) { }
/// /// <param name="resource"></param> public ResourceReference(Resource resource) { this.resource = resource; }
/// /// <param name="resource"></param> public SpineReference(Resource resource) : this(resource, true) { }
/// <summary> /// As per the epub file format the spine officially maintains a reference to the /// Table of Contents. The epubwriter will look for it here first, followed by some /// clever tricks to find it elsewhere if not found. Put it here to be sure of the /// expected behaviours. /// </summary> /// <param name="tocResource">tocResource</param> public void setTocResource(Resource tocResource) { this.tocResource = tocResource; }
/// <summary> /// The position within the spine of the given resource. /// </summary> /// something < 0 if not found. /// <param name="currentResource"></param> public int getResourceIndex(Resource currentResource) { if (currentResource == null) { return -1; } return getResourceIndex(currentResource.getHref()); }
/// /// <param name="resource"></param> private string getResourceItemPrefix(Resource resource) { String result; if (MediatypeService.isBitmapImage(resource.getMediaType())) { result = IMAGE_PREFIX; } else { result = ITEM_PREFIX; } return result; }
/// <summary> /// Calls addTOCReferenceAtLocation after splitting the path using the given /// pathSeparator. /// </summary> /// <param name="resource"></param> /// <param name="path"></param> /// <param name="pathSeparator"></param> public TOCReference addSection(Resource resource, string path, string pathSeparator) { String[] pathElements = path.Split(Convert.ToChar(pathSeparator)); return addSection(resource, pathElements); }
public TitledResourceReference(Resource resource, string title) : this(resource, title, null) { }
/// <summary> /// Adds the given resource to the spine references and returns it. /// </summary> /// <param>spineReference</param> /// <param name="resource"></param> public SpineReference addResource(Resource resource) { return addSpineReference(new SpineReference(resource)); }
/// <summary> /// Writes the resource to the resultStream. /// </summary> /// <param name="resource"></param> /// <param name="resultStream"></param> private void writeResource(Resource resource, ZipOutputStream resultStream) { }
/// <summary> /// Besides setting the resource it also sets the fragmentId to null. /// </summary> /// <param name="resource">resource</param> public void setResource(Resource resource) { this.resource = resource; }
/// /// <param name="coverPage"></param> public void setCoverPage(Resource coverPage) { GuideReference coverpageGuideReference = new GuideReference(coverPage, GuideReference.COVER, DEFAULT_COVER_TITLE); setCoverReference(coverpageGuideReference); }
/// /// <param name="resource"></param> /// <param name="linear"></param> public SpineReference(Resource resource, bool linear) : base(resource) { this.linear = linear; }
/// <summary> /// Sets the resource to the given resource and sets the fragmentId to null. /// </summary> /// <param name="resource"></param> public void setResource(Resource resource) { setResource(resource, null); }