Search in sources :

Example 1 with NavPoint

use of org.eclipse.mylyn.docs.epub.ncx.NavPoint in project mylyn.docs by eclipse.

the class TestOPSPublication method testReadTableOfContents.

/**
 * Test method for {@link org.eclipse.mylyn.docs.epub.core.OPSPublication#readTableOfContents(java.io.File)} .
 *
 * @throws Exception
 */
@Test
public final void testReadTableOfContents() throws Exception {
    epub.add(oebps);
    oebps.addItem(new File("testdata/plain-page.xhtml"));
    epub.pack(epubFile);
    EPUB epub_in = new EPUB();
    epub_in.unpack(epubFile, epubFolder);
    Publication oebps_in = epub_in.getOPSPublications().get(0);
    assertTrue(oebps_in.getTableOfContents() != null);
    assertTrue(oebps_in.getTableOfContents() instanceof Ncx);
    Ncx ncx = (Ncx) oebps_in.getTableOfContents();
    NavPoint h1_1 = ncx.getNavMap().getNavPoints().get(0);
    NavPoint h1_2 = ncx.getNavMap().getNavPoints().get(1);
    assertEquals("First item", getText(h1_1));
    assertEquals("Second item", getText(h1_2));
}
Also used : Ncx(org.eclipse.mylyn.docs.epub.ncx.Ncx) NavPoint(org.eclipse.mylyn.docs.epub.ncx.NavPoint) EPUB(org.eclipse.mylyn.docs.epub.core.EPUB) Publication(org.eclipse.mylyn.docs.epub.core.Publication) OPSPublication(org.eclipse.mylyn.docs.epub.core.OPSPublication) File(java.io.File) Test(org.junit.Test)

Example 2 with NavPoint

use of org.eclipse.mylyn.docs.epub.ncx.NavPoint in project mylyn.docs by eclipse.

the class TestOPSPublication method testGenerateTableOfContents.

/**
 * Test method for {@link org.eclipse.mylyn.docs.epub.core.OPSPublication#generateTableOfContents()} .
 * <ul>
 * <li>Table of contents shall be generated from content per default.</li>
 * </ul>
 *
 * @throws Exception
 */
@Test
public final void testGenerateTableOfContents() throws Exception {
    epub.add(oebps);
    oebps.addItem(new File("testdata/plain-page.xhtml"));
    epub.pack(epubFile);
    assertTrue(oebps.getTableOfContents() != null);
    assertTrue(oebps.getTableOfContents() instanceof Ncx);
    Ncx ncx = (Ncx) oebps.getTableOfContents();
    NavPoint h1_1 = ncx.getNavMap().getNavPoints().get(0);
    NavPoint h1_2 = ncx.getNavMap().getNavPoints().get(1);
    assertEquals("First item", getText(h1_1));
    assertEquals("Second item", getText(h1_2));
    epubFile.delete();
}
Also used : Ncx(org.eclipse.mylyn.docs.epub.ncx.Ncx) NavPoint(org.eclipse.mylyn.docs.epub.ncx.NavPoint) File(java.io.File) Test(org.junit.Test)

Example 3 with NavPoint

use of org.eclipse.mylyn.docs.epub.ncx.NavPoint in project mylyn.docs by eclipse.

the class TestTOCGenerator method testNormal.

@Test
public void testNormal() throws ParserConfigurationException, SAXException, IOException {
    String html = "<body><h1 id='h1-1'>test</h1><h2 id='h2-1'>test</h2><h2 id='h2-2'>test</h2>" + "<h3 id='h3-1'>test</h3><h1 id='h1-2'>test</h1></body>";
    Ncx ncx = createNcx();
    TOCGenerator.parse(new InputSource(new StringReader(html)), "test.html", ncx, 0);
    EList<NavPoint> points = ncx.getNavMap().getNavPoints();
    // h1
    assertEquals(2, points.size());
    // h2
    assertEquals(2, points.get(0).getNavPoints().size());
    // h3
    assertEquals(1, points.get(0).getNavPoints().get(1).getNavPoints().size());
}
Also used : Ncx(org.eclipse.mylyn.docs.epub.ncx.Ncx) InputSource(org.xml.sax.InputSource) NavPoint(org.eclipse.mylyn.docs.epub.ncx.NavPoint) StringReader(java.io.StringReader) Test(org.junit.Test)

Example 4 with NavPoint

use of org.eclipse.mylyn.docs.epub.ncx.NavPoint in project mylyn.docs by eclipse.

the class TestTOCGenerator method testH1throughH6.

/**
 * Verifies that the table of contents generator is capable of handling all levels from 1 through 6.
 */
@Test
public void testH1throughH6() throws ParserConfigurationException, SAXException, IOException {
    StringBuilder sb = new StringBuilder();
    sb.append("<body>");
    for (int i = 1; i < 7; i++) {
        sb.append(String.format("<h%1$d id='h%1$d'>test</h%1$d>%n", i));
    }
    sb.append("</body>");
    Ncx ncx = createNcx();
    TOCGenerator.parse(new InputSource(new StringReader(sb.toString())), "test.html", ncx, 0);
    EList<NavPoint> points = ncx.getNavMap().getNavPoints();
    for (int i = 1; i < 6; i++) {
        assertEquals(1, points.size());
        points = points.get(0).getNavPoints();
    }
}
Also used : Ncx(org.eclipse.mylyn.docs.epub.ncx.Ncx) InputSource(org.xml.sax.InputSource) NavPoint(org.eclipse.mylyn.docs.epub.ncx.NavPoint) StringReader(java.io.StringReader) NavPoint(org.eclipse.mylyn.docs.epub.ncx.NavPoint) Test(org.junit.Test)

Example 5 with NavPoint

use of org.eclipse.mylyn.docs.epub.ncx.NavPoint in project mylyn.docs by eclipse.

the class TOCGenerator method endElement.

@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
    int currentLevel = isHeader(qName);
    int parentLevel = currentLevel - 1;
    // only handle header level specifications H1 through H6
    if (currentLevel > 0 && currentLevel < 7) {
        recording = false;
        NavPoint navPoint = createNavPoint(buffer.toString());
        // level immediately above, as one would expect.
        if (currentLevel > 1) {
            NavPoint parent = headers[parentLevel - 1];
            while (parent == null && parentLevel > 1) {
                parentLevel = parentLevel - 1;
                parent = headers[parentLevel - 1];
            }
            if (parent == null) {
                ncx.getNavMap().getNavPoints().add(navPoint);
                headers[0] = navPoint;
            } else {
                parent.getNavPoints().add(navPoint);
                headers[parentLevel] = navPoint;
            }
        } else {
            ncx.getNavMap().getNavPoints().add(navPoint);
            headers[0] = navPoint;
        }
        buffer.setLength(0);
    }
}
Also used : NavPoint(org.eclipse.mylyn.docs.epub.ncx.NavPoint) NavPoint(org.eclipse.mylyn.docs.epub.ncx.NavPoint)

Aggregations

NavPoint (org.eclipse.mylyn.docs.epub.ncx.NavPoint)11 Ncx (org.eclipse.mylyn.docs.epub.ncx.Ncx)8 Test (org.junit.Test)8 StringReader (java.io.StringReader)5 InputSource (org.xml.sax.InputSource)5 File (java.io.File)3 EPUB (org.eclipse.mylyn.docs.epub.core.EPUB)2 OPSPublication (org.eclipse.mylyn.docs.epub.core.OPSPublication)2 Publication (org.eclipse.mylyn.docs.epub.core.Publication)2 Meta (org.eclipse.mylyn.docs.epub.ncx.Meta)2 FeatureMap (org.eclipse.emf.ecore.util.FeatureMap)1 FeatureEList (org.eclipse.emf.ecore.util.FeatureMapUtil.FeatureEList)1 Content (org.eclipse.mylyn.docs.epub.ncx.Content)1 NavLabel (org.eclipse.mylyn.docs.epub.ncx.NavLabel)1 Text (org.eclipse.mylyn.docs.epub.ncx.Text)1