1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31 package org.jomc.mojo;
32
33 import java.io.File;
34 import java.util.Locale;
35 import java.util.logging.Level;
36 import javax.xml.bind.JAXBContext;
37 import javax.xml.bind.util.JAXBSource;
38 import javax.xml.transform.Source;
39 import org.apache.commons.io.FileUtils;
40 import org.apache.maven.model.Resource;
41 import org.apache.maven.plugin.MojoExecutionException;
42 import org.apache.maven.project.MavenProject;
43 import org.jomc.model.Module;
44 import org.jomc.modlet.ModelContext;
45 import org.jomc.modlet.ModelValidationReport;
46 import org.jomc.modlet.ObjectFactory;
47 import org.jomc.tools.ResourceFileProcessor;
48
49
50
51
52
53
54
55 public abstract class AbstractResourcesWriteMojo extends AbstractJomcMojo
56 {
57
58
59
60
61 private static final String TOOLNAME = "ResourceFileProcessor";
62
63
64
65
66
67
68 private String resourceBundleDefaultLanguage;
69
70
71
72
73 public AbstractResourcesWriteMojo()
74 {
75 super();
76 }
77
78 @Override
79 protected final void executeTool() throws Exception
80 {
81 this.logSeparator();
82
83 if ( this.isResourceProcessingEnabled() )
84 {
85 this.logProcessingModule( TOOLNAME, this.getResourcesModuleName() );
86
87 final ModelContext context = this.createModelContext( this.getResourcesClassLoader() );
88 final ResourceFileProcessor tool = this.createResourceFileProcessor( context );
89 final JAXBContext jaxbContext = context.createContext( this.getModel() );
90 final Source source = new JAXBSource( jaxbContext, new ObjectFactory().createModel( tool.getModel() ) );
91 final ModelValidationReport validationReport = context.validateModel( this.getModel(), source );
92
93 if ( this.resourceBundleDefaultLanguage != null )
94 {
95 tool.setResourceBundleDefaultLocale(
96 new Locale( this.resourceBundleDefaultLanguage.toLowerCase( Locale.ENGLISH ) ) );
97
98 }
99
100 this.log( context, validationReport.isModelValid() ? Level.INFO : Level.SEVERE, validationReport );
101
102 if ( validationReport.isModelValid() )
103 {
104 final Module module =
105 tool.getModules() != null ? tool.getModules().getModule( this.getResourcesModuleName() ) : null;
106
107 if ( module != null )
108 {
109 if ( !this.getResourcesDirectory().exists() && !this.getResourcesDirectory().mkdirs() )
110 {
111 throw new MojoExecutionException( Messages.getMessage(
112 "failedCreatingDirectory", this.getResourcesDirectory().getAbsolutePath() ) );
113
114 }
115
116 tool.writeResourceBundleResourceFiles( module, this.getResourcesDirectory() );
117
118 if ( !this.getResourcesDirectory().equals( this.getResourcesOutputDirectory() ) )
119 {
120 FileUtils.copyDirectory( this.getResourcesDirectory(), this.getResourcesOutputDirectory() );
121 }
122
123 final Resource resource = new Resource();
124 resource.setDirectory( this.getResourcesDirectory().getAbsolutePath() );
125 resource.setFiltering( false );
126
127 this.addMavenResource( this.getMavenProject(), resource );
128
129 this.logToolSuccess( TOOLNAME );
130 }
131 else
132 {
133 this.logMissingModule( this.getResourcesModuleName() );
134 }
135 }
136 else
137 {
138 throw new MojoExecutionException( Messages.getMessage( "resourceProcessingFailure" ) );
139 }
140 }
141 else if ( this.isLoggable( Level.INFO ) )
142 {
143 this.log( Level.INFO, Messages.getMessage( "resourceProcessingDisabled" ), null );
144 }
145 }
146
147
148
149
150
151
152
153
154 protected abstract String getResourcesModuleName() throws MojoExecutionException;
155
156
157
158
159
160
161
162
163 protected abstract ClassLoader getResourcesClassLoader() throws MojoExecutionException;
164
165
166
167
168
169
170
171
172 protected abstract File getResourcesDirectory() throws MojoExecutionException;
173
174
175
176
177
178
179
180
181
182
183 protected abstract File getResourcesOutputDirectory() throws MojoExecutionException;
184
185
186
187
188
189
190
191
192
193
194
195 protected abstract void addMavenResource( MavenProject mavenProject, Resource resource )
196 throws MojoExecutionException;
197
198 }