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.ant;
32
33 import java.io.File;
34 import java.io.IOException;
35 import java.util.logging.Level;
36 import javax.xml.bind.JAXBContext;
37 import javax.xml.bind.JAXBException;
38 import javax.xml.bind.util.JAXBSource;
39 import javax.xml.transform.Source;
40 import org.apache.tools.ant.BuildException;
41 import org.jomc.model.Implementation;
42 import org.jomc.model.Module;
43 import org.jomc.model.Specification;
44 import org.jomc.modlet.Model;
45 import org.jomc.modlet.ModelContext;
46 import org.jomc.modlet.ModelException;
47 import org.jomc.modlet.ModelValidationReport;
48 import org.jomc.modlet.ObjectFactory;
49 import org.jomc.tools.ResourceFileProcessor;
50
51
52
53
54
55
56
57 public final class GenerateResourcesTask extends ResourceFileProcessorTask
58 {
59
60
61 private File resourcesDirectory;
62
63
64 public GenerateResourcesTask()
65 {
66 super();
67 }
68
69
70
71
72
73
74
75
76 public File getResourcesDirectory()
77 {
78 return this.resourcesDirectory;
79 }
80
81
82
83
84
85
86
87
88 public void setResourcesDirectory( final File value )
89 {
90 this.resourcesDirectory = value;
91 }
92
93
94 @Override
95 public void preExecuteTask() throws BuildException
96 {
97 super.preExecuteTask();
98
99 this.assertNotNull( "resourcesDirectory", this.getResourcesDirectory() );
100 }
101
102
103
104
105
106
107 @Override
108 public void processResourceFiles() throws BuildException
109 {
110 ProjectClassLoader classLoader = null;
111 boolean suppressExceptionOnClose = true;
112
113 try
114 {
115 this.log( Messages.getMessage( "generatingResources", this.getModel() ) );
116
117 classLoader = this.newProjectClassLoader();
118 final ModelContext context = this.newModelContext( classLoader );
119 final ResourceFileProcessor tool = this.newResourceFileProcessor();
120 final JAXBContext jaxbContext = context.createContext( this.getModel() );
121 final Model model = this.getModel( context );
122 final Source source = new JAXBSource( jaxbContext, new ObjectFactory().createModel( model ) );
123 final ModelValidationReport validationReport = context.validateModel( this.getModel(), source );
124
125 this.logValidationReport( context, validationReport );
126 tool.setModel( model );
127
128 if ( validationReport.isModelValid() )
129 {
130 final Specification s = this.getSpecification( model );
131 final Implementation i = this.getImplementation( model );
132 final Module m = this.getModule( model );
133
134 if ( s != null )
135 {
136 tool.writeResourceBundleResourceFiles( s, this.getResourcesDirectory() );
137 }
138
139 if ( i != null )
140 {
141 tool.writeResourceBundleResourceFiles( i, this.getResourcesDirectory() );
142 }
143
144 if ( m != null )
145 {
146 tool.writeResourceBundleResourceFiles( m, this.getResourcesDirectory() );
147 }
148
149 if ( this.isModulesProcessingRequested() )
150 {
151 tool.writeResourceBundleResourceFiles( this.getResourcesDirectory() );
152 }
153
154 suppressExceptionOnClose = false;
155 }
156 else
157 {
158 throw new ModelException( Messages.getMessage( "invalidModel", this.getModel() ) );
159 }
160 }
161 catch ( final IOException e )
162 {
163 throw new ResourceProcessingException( Messages.getMessage( e ), e, this.getLocation() );
164 }
165 catch ( final JAXBException e )
166 {
167 throw new ResourceProcessingException( Messages.getMessage( e ), e, this.getLocation() );
168 }
169 catch ( final ModelException e )
170 {
171 throw new ResourceProcessingException( Messages.getMessage( e ), e, this.getLocation() );
172 }
173 finally
174 {
175 try
176 {
177 if ( classLoader != null )
178 {
179 classLoader.close();
180 }
181 }
182 catch ( final IOException e )
183 {
184 if ( suppressExceptionOnClose )
185 {
186 this.logMessage( Level.SEVERE, Messages.getMessage( e ), e );
187 }
188 else
189 {
190 throw new ResourceProcessingException( Messages.getMessage( e ), e, this.getLocation() );
191 }
192 }
193 }
194 }
195
196
197 @Override
198 public GenerateResourcesTask clone()
199 {
200 final GenerateResourcesTask clone = (GenerateResourcesTask) super.clone();
201 clone.resourcesDirectory =
202 this.resourcesDirectory != null ? new File( this.resourcesDirectory.getAbsolutePath() ) : null;
203
204 return clone;
205 }
206
207 }