1 | |
package org.apache.maven.doxia.book.services.indexer; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
import java.io.FileNotFoundException; |
23 | |
import java.io.FileReader; |
24 | |
|
25 | |
import org.apache.maven.doxia.Doxia; |
26 | |
import org.apache.maven.doxia.book.BookDoxiaException; |
27 | |
import org.apache.maven.doxia.book.context.BookContext; |
28 | |
import org.apache.maven.doxia.book.context.BookIndex; |
29 | |
import org.apache.maven.doxia.book.model.BookModel; |
30 | |
import org.apache.maven.doxia.book.model.Chapter; |
31 | |
import org.apache.maven.doxia.book.model.Section; |
32 | |
import org.apache.maven.doxia.index.IndexEntry; |
33 | |
import org.apache.maven.doxia.index.IndexingSink; |
34 | |
import org.apache.maven.doxia.parser.ParseException; |
35 | |
import org.apache.maven.doxia.parser.manager.ParserNotFoundException; |
36 | |
import org.codehaus.plexus.logging.AbstractLogEnabled; |
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | 2 | public class DefaultBookIndexer |
47 | |
extends AbstractLogEnabled |
48 | |
implements BookIndexer |
49 | |
{ |
50 | |
|
51 | |
|
52 | |
|
53 | |
private Doxia doxia; |
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
public void indexBook( BookModel book, BookContext bookContext ) |
61 | |
throws BookDoxiaException |
62 | |
{ |
63 | 7 | BookIndex index = new BookIndex(); |
64 | |
|
65 | 7 | for ( Chapter chapter : book.getChapters() ) |
66 | |
{ |
67 | 14 | indexChapter( bookContext, index, chapter ); |
68 | |
} |
69 | |
|
70 | 7 | bookContext.setIndex( index ); |
71 | 7 | } |
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
private void indexChapter( BookContext context, IndexEntry bookEntry, Chapter chapter ) |
86 | |
throws BookDoxiaException |
87 | |
{ |
88 | 14 | IndexEntry chapterEntry = new IndexEntry( bookEntry, chapter.getId( ) ); |
89 | |
|
90 | 14 | chapterEntry.setTitle( chapter.getTitle() ); |
91 | |
|
92 | 14 | for ( Section section : chapter.getSections() ) |
93 | |
{ |
94 | 28 | indexSection( context, chapterEntry, section ); |
95 | |
} |
96 | 14 | } |
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
|
106 | |
private void indexSection( BookContext bookContext, IndexEntry chapterEntry, Section section ) |
107 | |
throws BookDoxiaException |
108 | |
{ |
109 | 28 | BookContext.BookFile bookFile = (BookContext.BookFile) bookContext.getFiles().get( section.getId() ); |
110 | |
|
111 | 28 | if ( bookFile == null ) |
112 | |
{ |
113 | 0 | throw new BookDoxiaException( "No document that matches section with id=" |
114 | |
+ section.getId() + "." ); |
115 | |
} |
116 | |
|
117 | |
|
118 | |
|
119 | |
|
120 | |
|
121 | 28 | IndexEntry sectionEntry = new IndexEntry( chapterEntry, section.getId() ); |
122 | |
|
123 | 28 | IndexingSink sink = new IndexingSink( sectionEntry ); |
124 | |
|
125 | |
try |
126 | |
{ |
127 | 28 | doxia.parse( new FileReader( bookFile.getFile() ), bookFile.getParserId(), sink ); |
128 | |
} |
129 | 0 | catch ( ParserNotFoundException e ) |
130 | |
{ |
131 | 0 | throw new BookDoxiaException( "Parser not found: " |
132 | |
+ bookFile.getParserId() + ".", e ); |
133 | |
} |
134 | 0 | catch ( ParseException e ) |
135 | |
{ |
136 | 0 | throw new BookDoxiaException( "Error while parsing document: " |
137 | |
+ bookFile.getFile().getAbsolutePath() + ".", e ); |
138 | |
} |
139 | 0 | catch ( FileNotFoundException e ) |
140 | |
{ |
141 | 0 | throw new BookDoxiaException( "Could not find document: " |
142 | |
+ bookFile.getFile().getAbsolutePath() + ".", e ); |
143 | 28 | } |
144 | |
|
145 | 28 | sectionEntry.setTitle( sink.getTitle() ); |
146 | 28 | } |
147 | |
} |