1 package org.codehaus.mojo.pde.descriptor;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 import java.io.File;
20 import java.io.FileReader;
21 import java.io.IOException;
22 import java.io.Reader;
23
24 import org.apache.maven.plugin.MojoExecutionException;
25 import org.codehaus.mojo.pde.descriptor.io.xpp3.FeatureXpp3Reader;
26 import org.codehaus.mojo.pde.descriptor.io.xpp3.FragmentXpp3Reader;
27 import org.codehaus.mojo.pde.descriptor.io.xpp3.PluginXpp3Reader;
28 import org.codehaus.mojo.pde.descriptor.io.xpp3.ProductXpp3Reader;
29 import org.codehaus.plexus.util.IOUtil;
30 import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
31
32 /**
33 *
34 *
35 */
36 public class DescriptorUtil
37 {
38
39 /**
40 * Load the descriptor file for the pde project. This will either load the product descriptor
41 * (based on the productFilename) or one of the eclipse artifacts in the following order:
42 * feature ("feature.xml"), fragment ("fragment.xml"), plugin ("plugin.xml").
43 *
44 * @param basedir the pde plugin base directory
45 * @param productFilename optional, if specified then the eclipse product file that will be
46 * returned.
47 * @return the descriptor for the pde project
48 * @throws MojoExecutionException build failures.
49 */
50 public static Descriptor getDescriptor( File basedir, String productFilename )
51 throws MojoExecutionException
52 {
53 Descriptor d = null;
54
55 if ( productFilename != null )
56 {
57 d = loadProductDescriptor( basedir, productFilename );
58 }
59 else
60 {
61 d = loadFeatureDescriptor( basedir );
62
63 if ( d == null )
64 {
65 d = loadFragmentDescriptor( basedir );
66 }
67
68 if ( d == null )
69 {
70 d = loadPluginDescriptor( basedir );
71 }
72 }
73
74 if ( d == null )
75 {
76 throw new MojoExecutionException( basedir.getPath() + " is not a PDE project." );
77 }
78
79 return d;
80 }
81
82 /**
83 * Load Feature from pde project directory.
84 *
85 * @param basedir the pde project directory.
86 * @return a Descriptor containing the Feature
87 * @throws MojoExecutionException build failures.
88 */
89 private static Descriptor loadFeatureDescriptor( File basedir )
90 throws MojoExecutionException
91 {
92 FeatureDescriptor descriptor = null;
93
94 File file = new File( basedir, "feature.xml" );
95
96 if ( file.exists() )
97 {
98 try
99 {
100 descriptor = readFeatureDescriptor( new FileReader( file ) );
101 }
102 catch ( IOException e )
103 {
104 throw new MojoExecutionException( "Error reading " + file );
105 }
106 }
107
108 return descriptor;
109
110 }
111
112 /**
113 * Load Fragment from pde project directory.
114 *
115 * @param basedir the pde project directory.
116 * @return a Descriptor containing the Fragment
117 * @throws MojoExecutionException build failures.
118 */
119 private static Descriptor loadFragmentDescriptor( File basedir )
120 throws MojoExecutionException
121 {
122 FeatureDescriptor descriptor = null;
123
124 File file = new File( basedir, "fragment.xml" );
125 if ( file.exists() )
126 {
127 try
128 {
129 descriptor = readFeatureDescriptor( new FileReader( file ) );
130 }
131 catch ( IOException e )
132 {
133 throw new MojoExecutionException( "Error reading " + file );
134 }
135 }
136
137 return descriptor;
138
139 }
140
141 /**
142 * Load Plugin from pde project directory.
143 *
144 * @param basedir the pde project directory.
145 * @return a Descriptor containing the Plugin
146 * @throws MojoExecutionException build failures.
147 */
148 private static Descriptor loadPluginDescriptor( File basedir )
149 throws MojoExecutionException
150 {
151 PluginDescriptor d = null;
152
153 File file = new File( basedir, "plugin.xml" );
154 if ( file.exists() )
155 {
156 try
157 {
158 d = readPluginDescriptor( new FileReader( file ) );
159 }
160 catch ( IOException e )
161 {
162 throw new MojoExecutionException( "Error reading " + file );
163 }
164 }
165 else
166 {
167 d = new PluginDescriptor();
168 }
169
170 if ( d.getId() == null || d.getVersion() == null )
171 {
172 ManifestBean bean = new ManifestBean( basedir );
173 if ( d.getId() == null )
174 {
175 d.setId( bean.getId() );
176 }
177
178 if ( d.getVersion() == null )
179 {
180 d.setVersion( bean.getVersion() );
181 }
182 }
183
184 return d;
185
186 }
187
188 /**
189 * Load Product from pde project directory.
190 *
191 * @param basedir the pde project directory.
192 * @param productFilename the file name for the *.product file for the project.
193 * @return a Descriptor containing the Product
194 * @throws MojoExecutionException build failures.
195 */
196 private static Descriptor loadProductDescriptor( File basedir, String productFilename )
197 throws MojoExecutionException
198 {
199 ProductDescriptor d = null;
200
201 File file = new File( basedir, productFilename );
202 if ( file.exists() )
203 {
204 try
205 {
206 d = readProductDescriptor( new FileReader( file ) );
207 }
208 catch ( IOException e )
209 {
210 throw new MojoExecutionException( "Error reading " + file );
211 }
212 }
213
214 return d;
215 }
216
217 /**
218 * Load the plugin.xml via a Reader
219 *
220 * @param reader plugin file
221 * @throws MojoExecutionException build failures.
222 * @return PluginDescriptor
223 */
224 public static PluginDescriptor readPluginDescriptor( Reader reader )
225 throws MojoExecutionException
226 {
227 PluginDescriptor descriptor;
228 try
229 {
230 PluginXpp3Reader r = new PluginXpp3Reader();
231
232 descriptor = r.read( reader, false );
233
234 }
235 catch ( IOException e )
236 {
237 throw new MojoExecutionException( "Error reading plugin.xml descriptor", e );
238 }
239 catch ( XmlPullParserException e )
240 {
241 throw new MojoExecutionException( "Error reading plugin.xml descriptor", e );
242 }
243 finally
244 {
245 IOUtil.close( reader );
246 }
247
248 return descriptor;
249 }
250
251 /**
252 * Load the feature.xml via a Reader
253 *
254 * @param reader the feature file
255 * @return FeatureDescriptor
256 * @throws MojoExecutionException build failures.
257 */
258 public static FeatureDescriptor readFeatureDescriptor( Reader reader )
259 throws MojoExecutionException
260 {
261 FeatureDescriptor descriptor;
262 try
263 {
264 FeatureXpp3Reader r = new FeatureXpp3Reader();
265 descriptor = r.read( reader, false );
266 }
267 catch ( IOException e )
268 {
269 throw new MojoExecutionException( "Error reading feature.xml descriptor", e );
270 }
271 catch ( XmlPullParserException e )
272 {
273 throw new MojoExecutionException( "Error reading feature.xml descriptor", e );
274 }
275 finally
276 {
277 IOUtil.close( reader );
278 }
279
280 return descriptor;
281 }
282
283 /**
284 * Load the fragment.xml via a Reader
285 *
286 * @param reader the fragment file
287 * @return FragmentDescriptor
288 * @throws MojoExecutionException build failures.
289 */
290 public static FragmentDescriptor readFragmentDescriptor( Reader reader )
291 throws MojoExecutionException
292 {
293 FragmentDescriptor descriptor;
294 try
295 {
296 FragmentXpp3Reader r = new FragmentXpp3Reader();
297 descriptor = r.read( reader, false );
298 }
299 catch ( IOException e )
300 {
301 throw new MojoExecutionException( "Error reading fragment.xml descriptor", e );
302 }
303 catch ( XmlPullParserException e )
304 {
305 throw new MojoExecutionException( "Error reading fragment.xml descriptor", e );
306 }
307 finally
308 {
309 IOUtil.close( reader );
310 }
311
312 return descriptor;
313 }
314
315 /**
316 * Load the *.product via a Reader
317 *
318 * @param reader the product file
319 * @return ProductDescriptor
320 * @throws MojoExecutionException build failures.
321 */
322 public static ProductDescriptor readProductDescriptor( Reader reader )
323 throws MojoExecutionException
324 {
325 ProductDescriptor descriptor;
326 try
327 {
328 ProductXpp3Reader r = new ProductXpp3Reader();
329 descriptor = r.read( reader, false );
330 }
331 catch ( IOException e )
332 {
333 throw new MojoExecutionException( "Error reading *.product descriptor", e );
334 }
335 catch ( XmlPullParserException e )
336 {
337 throw new MojoExecutionException( "Error reading *.product descriptor", e );
338 }
339 finally
340 {
341 IOUtil.close( reader );
342 }
343
344 return descriptor;
345 }
346
347 /**
348 * Return the String version of the pdetype based on the descriptor.
349 *
350 * @param descriptor the project descriptor
351 * @return product, plugin, feature, or fragment based upon the descriptor.
352 */
353 public static String getPDEType( Descriptor descriptor )
354 {
355 if ( descriptor instanceof ProductDescriptor )
356 {
357 return "product";
358 }
359 if ( descriptor instanceof PluginDescriptor )
360 {
361 return "plugin";
362 }
363 if ( descriptor instanceof FeatureDescriptor )
364 {
365 return "feature";
366 }
367 if ( descriptor instanceof FragmentDescriptor )
368 {
369 return "fragment";
370 }
371
372 return null;
373 }
374
375 }