1 package org.apache.maven.archetype.old.descriptor;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.codehaus.plexus.util.xml.Xpp3Dom;
23 import org.codehaus.plexus.util.xml.Xpp3DomBuilder;
24 import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
25
26 import java.io.IOException;
27 import java.io.Reader;
28 import java.nio.charset.IllegalCharsetNameException;
29 import java.nio.charset.UnsupportedCharsetException;
30
31
32
33
34
35 public class ArchetypeDescriptorBuilder
36 {
37 public ArchetypeDescriptor build( Reader reader )
38 throws IOException, XmlPullParserException
39 {
40 ArchetypeDescriptor descriptor = new ArchetypeDescriptor();
41
42 Xpp3Dom dom = Xpp3DomBuilder.build( reader );
43
44 descriptor.setId( dom.getChild( "id" ).getValue() );
45
46 Xpp3Dom allowPartialDom = dom.getChild( "allowPartial" );
47
48 if ( allowPartialDom != null )
49 {
50 String allowPartial = allowPartialDom.getValue();
51
52 if ( "true".equals( allowPartial ) || "1".equals( allowPartial ) || "on".equals( allowPartial ) )
53 {
54 descriptor.setAllowPartial( true );
55 }
56 }
57
58
59
60
61
62 Xpp3Dom sources = dom.getChild( "sources" );
63
64 if ( sources != null )
65 {
66 Xpp3Dom[] sourceList = sources.getChildren( "source" );
67
68 for ( int i = 0; i < sourceList.length; i++ )
69 {
70 addSourceToDescriptor( sourceList[i], descriptor );
71 }
72 }
73
74 Xpp3Dom resources = dom.getChild( "resources" );
75
76 if ( resources != null )
77 {
78 Xpp3Dom[] resourceList = resources.getChildren( "resource" );
79
80 for ( int i = 0; i < resourceList.length; i++ )
81 {
82 addResourceToDescriptor( resourceList[i], descriptor );
83 }
84 }
85
86
87
88
89
90 Xpp3Dom testSources = dom.getChild( "testSources" );
91
92 if ( testSources != null )
93 {
94 Xpp3Dom[] testSourceList = testSources.getChildren( "source" );
95
96 for ( int i = 0; i < testSourceList.length; i++ )
97 {
98 addTestSourceToDescriptor( testSourceList[i], descriptor );
99 }
100 }
101
102 Xpp3Dom testResources = dom.getChild( "testResources" );
103
104 if ( testResources != null )
105 {
106 Xpp3Dom[] testResourceList = testResources.getChildren( "resource" );
107
108 for ( int i = 0; i < testResourceList.length; i++ )
109 {
110 addTestResourceToDescriptor( testResourceList[i], descriptor );
111 }
112 }
113
114
115
116
117
118 Xpp3Dom siteResources = dom.getChild( "siteResources" );
119
120 if ( siteResources != null )
121 {
122 Xpp3Dom[] siteResourceList = siteResources.getChildren( "resource" );
123
124 for ( int i = 0; i < siteResourceList.length; i++ )
125 {
126 addSiteResourceToDescriptor( siteResourceList[i], descriptor );
127 }
128 }
129
130 return descriptor;
131 }
132
133
134
135
136
137
138
139
140
141
142
143 private static void addSourceToDescriptor( Xpp3Dom source, ArchetypeDescriptor descriptor )
144 throws XmlPullParserException
145 {
146 descriptor.addSource( source.getValue() );
147
148 TemplateDescriptor sourceDesc = descriptor.getSourceDescriptor( source.getValue() );
149
150 sourceDesc.setFiltered( true );
151
152 if ( source.getAttribute( "encoding" ) != null )
153 {
154 try
155 {
156 sourceDesc.setEncoding( source.getAttribute( "encoding" ) );
157 }
158 catch ( IllegalCharsetNameException icne )
159 {
160 throw new XmlPullParserException( source.getAttribute( "encoding" ) + " is not a valid encoding." );
161 }
162 catch ( UnsupportedCharsetException uce )
163 {
164 throw new XmlPullParserException( source.getAttribute( "encoding" ) + " is not a supported encoding." );
165 }
166 }
167 }
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185 private static void addResourceToDescriptor( Xpp3Dom resource, ArchetypeDescriptor descriptor )
186 throws XmlPullParserException
187 {
188 descriptor.addResource( resource.getValue() );
189
190 if ( resource.getAttribute( "filtered" ) != null )
191 {
192 TemplateDescriptor resourceDesc = descriptor.getResourceDescriptor( resource.getValue() );
193
194 try
195 {
196 resourceDesc.setFiltered( getValueFilteredAttribute( resource.getAttribute( "filtered" ) ) );
197 }
198 catch ( IllegalArgumentException iae )
199 {
200 throw new XmlPullParserException( iae.getMessage() );
201 }
202 }
203
204 if ( resource.getAttribute( "encoding" ) != null )
205 {
206 TemplateDescriptor resourceDesc = descriptor.getResourceDescriptor( resource.getValue() );
207
208 try
209 {
210 resourceDesc.setEncoding( resource.getAttribute( "encoding" ) );
211 }
212 catch ( IllegalCharsetNameException icne )
213 {
214 throw new XmlPullParserException( resource.getAttribute( "encoding" ) + " is not a valid encoding." );
215 }
216 catch ( UnsupportedCharsetException uce )
217 {
218 throw new XmlPullParserException(
219 resource.getAttribute( "encoding" ) + " is not a supported encoding." );
220 }
221 }
222
223 if ( resource.getValue().endsWith( ".properties" ) )
224 {
225 TemplateDescriptor resourceDesc = descriptor.getResourceDescriptor( resource.getValue() );
226
227 resourceDesc.setEncoding( "iso-8859-1" );
228 }
229 }
230
231
232
233
234
235
236
237
238
239
240
241 private static void addTestSourceToDescriptor( Xpp3Dom testSource, ArchetypeDescriptor descriptor )
242 throws XmlPullParserException
243 {
244 descriptor.addTestSource( testSource.getValue() );
245 TemplateDescriptor testSourceDesc = descriptor.getTestSourceDescriptor( testSource.getValue() );
246 testSourceDesc.setFiltered( true );
247 if ( testSource.getAttribute( "encoding" ) != null )
248 {
249 try
250 {
251 testSourceDesc.setEncoding( testSource.getAttribute( "encoding" ) );
252 }
253 catch ( IllegalCharsetNameException icne )
254 {
255 throw new XmlPullParserException( testSource.getAttribute( "encoding" ) + " is not a valid encoding." );
256 }
257 catch ( UnsupportedCharsetException uce )
258 {
259 throw new XmlPullParserException(
260 testSource.getAttribute( "encoding" ) + " is not a supported encoding." );
261 }
262 }
263 }
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281 private static void addTestResourceToDescriptor( Xpp3Dom testResource, ArchetypeDescriptor descriptor )
282 throws XmlPullParserException
283 {
284 descriptor.addTestResource( testResource.getValue() );
285
286 if ( testResource.getAttribute( "filtered" ) != null )
287 {
288 TemplateDescriptor testResourceDesc = descriptor.getTestResourceDescriptor( testResource.getValue() );
289
290 try
291 {
292 testResourceDesc.setFiltered( getValueFilteredAttribute( testResource.getAttribute( "filtered" ) ) );
293 }
294 catch ( IllegalArgumentException iae )
295 {
296 throw new XmlPullParserException( iae.getMessage() );
297 }
298 }
299
300 if ( testResource.getAttribute( "encoding" ) != null )
301 {
302 TemplateDescriptor testResourceDesc = descriptor.getTestResourceDescriptor( testResource.getValue() );
303
304 try
305 {
306 testResourceDesc.setEncoding( testResource.getAttribute( "encoding" ) );
307
308 }
309 catch ( IllegalCharsetNameException icne )
310 {
311 throw new XmlPullParserException(
312 testResource.getAttribute( "encoding" ) + " is not a valid encoding." );
313 }
314 catch ( UnsupportedCharsetException uce )
315 {
316 throw new XmlPullParserException(
317 testResource.getAttribute( "encoding" ) + " is not a supported encoding." );
318 }
319 }
320
321 if ( testResource.getValue().endsWith( ".properties" ) )
322 {
323 TemplateDescriptor testResourceDesc = descriptor.getTestResourceDescriptor( testResource.getValue() );
324
325 testResourceDesc.setEncoding( "iso-8859-1" );
326 }
327 }
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345 private static void addSiteResourceToDescriptor( Xpp3Dom siteResource, ArchetypeDescriptor descriptor )
346 throws XmlPullParserException
347 {
348 descriptor.addSiteResource( siteResource.getValue() );
349
350 if ( siteResource.getAttribute( "filtered" ) != null )
351 {
352 TemplateDescriptor siteResourceDesc = descriptor.getSiteResourceDescriptor( siteResource.getValue() );
353
354 try
355 {
356 siteResourceDesc.setFiltered( getValueFilteredAttribute( siteResource.getAttribute( "filtered" ) ) );
357 }
358 catch ( IllegalArgumentException iae )
359 {
360 throw new XmlPullParserException( iae.getMessage() );
361 }
362 }
363 if ( siteResource.getAttribute( "encoding" ) != null )
364 {
365 TemplateDescriptor siteResourceDesc = descriptor.getSiteResourceDescriptor( siteResource.getValue() );
366
367 try
368 {
369 siteResourceDesc.setEncoding( siteResource.getAttribute( "encoding" ) );
370 }
371 catch ( IllegalCharsetNameException icne )
372 {
373 throw new XmlPullParserException(
374 siteResource.getAttribute( "encoding" ) + " is not a valid encoding." );
375 }
376 catch ( UnsupportedCharsetException uce )
377 {
378 throw new XmlPullParserException(
379 siteResource.getAttribute( "encoding" ) + " is not a supported encoding." );
380 }
381 }
382 if ( siteResource.getValue().endsWith( ".properties" ) )
383 {
384 TemplateDescriptor siteResourceDesc = descriptor.getSiteResourceDescriptor( siteResource.getValue() );
385
386 siteResourceDesc.setEncoding( "iso-8859-1" );
387 }
388 }
389
390 private static boolean getValueFilteredAttribute( String str )
391 throws IllegalArgumentException
392 {
393 boolean ret = false;
394
395 if ( str.equals( "true" ) )
396 {
397 ret = true;
398 }
399 else if ( str.equals( "false" ) )
400 {
401 ret = false;
402 }
403 else
404 {
405 throw new IllegalArgumentException( str + " is not an accepted value for the attribute 'filtered'" );
406 }
407 return ret;
408 }
409 }