1 | |
package org.apache.maven.doxia.book.services.io; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
import org.apache.maven.doxia.book.model.io.xpp3.BookModelXpp3Reader; |
23 | |
import org.apache.maven.doxia.book.model.BookModel; |
24 | |
import org.apache.maven.doxia.book.BookDoxiaException; |
25 | |
import org.apache.maven.doxia.book.context.BookContext; |
26 | |
import org.apache.maven.doxia.module.site.SiteModule; |
27 | |
import org.apache.maven.doxia.module.site.manager.SiteModuleManager; |
28 | |
import org.codehaus.plexus.util.IOUtil; |
29 | |
import org.codehaus.plexus.util.ReaderFactory; |
30 | |
import org.codehaus.plexus.util.xml.pull.XmlPullParserException; |
31 | |
import org.codehaus.plexus.logging.AbstractLogEnabled; |
32 | |
|
33 | |
import java.io.IOException; |
34 | |
import java.io.File; |
35 | |
import java.io.Reader; |
36 | |
import java.util.Collection; |
37 | |
import java.util.Map; |
38 | |
import java.util.TreeMap; |
39 | |
import java.util.List; |
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | 2 | public class DefaultBookIo |
49 | |
extends AbstractLogEnabled |
50 | |
implements BookIo |
51 | |
{ |
52 | |
|
53 | |
|
54 | |
|
55 | |
private SiteModuleManager siteModuleManager; |
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
public BookModel readBook( File bookDescriptor ) |
63 | |
throws BookDoxiaException |
64 | |
{ |
65 | 2 | Reader reader = null; |
66 | |
try |
67 | |
{ |
68 | 2 | reader = ReaderFactory.newXmlReader( bookDescriptor ); |
69 | 2 | return new BookModelXpp3Reader().read( reader, true ); |
70 | |
} |
71 | 0 | catch ( IOException e ) |
72 | |
{ |
73 | 0 | throw new BookDoxiaException( "Error while reading book descriptor.", e ); |
74 | |
} |
75 | 0 | catch ( XmlPullParserException e ) |
76 | |
{ |
77 | 0 | throw new BookDoxiaException( "Error while reading book descriptor.", e ); |
78 | |
} |
79 | |
finally |
80 | |
{ |
81 | 2 | IOUtil.close( reader ); |
82 | |
} |
83 | |
} |
84 | |
|
85 | |
|
86 | |
public void loadFiles( BookContext context, List<File> files ) |
87 | |
{ |
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | 7 | Collection<SiteModule> siteModules = siteModuleManager.getSiteModules(); |
93 | |
|
94 | 7 | for ( SiteModule siteModule : siteModules ) |
95 | |
{ |
96 | 28 | String extension = siteModule.getExtension(); |
97 | |
|
98 | 28 | String sourceDirectory = File.separator + siteModule.getSourceDirectory() + File.separator; |
99 | |
|
100 | 28 | String parserId = siteModule.getParserId(); |
101 | |
|
102 | 28 | for ( File file : files ) |
103 | |
{ |
104 | 112 | String name = file.getName(); |
105 | |
|
106 | 112 | String path = file.getAbsolutePath(); |
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | 112 | if ( path.indexOf( sourceDirectory ) != -1 ) |
112 | |
{ |
113 | 0 | name = name.substring( 0, name.length() - extension.length() - 1 ); |
114 | |
|
115 | 0 | context.getFiles().put( name, new BookContext.BookFile( file, parserId ) ); |
116 | |
} |
117 | 112 | else if ( name.endsWith( extension ) ) |
118 | |
{ |
119 | 28 | name = name.substring( 0, name.length() - extension.length() - 1 ); |
120 | |
|
121 | |
|
122 | 28 | if ( !context.getFiles().containsKey( name ) ) |
123 | |
{ |
124 | 28 | context.getFiles().put( name, new BookContext.BookFile( file, parserId ) ); |
125 | |
} |
126 | |
} |
127 | 112 | } |
128 | 28 | } |
129 | |
|
130 | 7 | if ( getLogger().isDebugEnabled() ) |
131 | |
{ |
132 | 0 | getLogger().debug( "Dumping document <-> id mapping:" ); |
133 | |
|
134 | 0 | Map<String, BookContext.BookFile> map = new TreeMap<String, BookContext.BookFile>( context.getFiles() ); |
135 | |
|
136 | 0 | for ( Map.Entry<String, BookContext.BookFile> entry : map.entrySet() ) |
137 | |
{ |
138 | 0 | BookContext.BookFile file = entry.getValue(); |
139 | |
|
140 | 0 | getLogger().debug( " " + entry.getKey() + "=" + file.getFile() + ", parser: " + file.getParserId() ); |
141 | 0 | } |
142 | |
} |
143 | 7 | } |
144 | |
} |