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