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 | 5 | public class ArchetypeDescriptorBuilder |
36 | |
{ |
37 | |
public ArchetypeDescriptor build( Reader reader ) |
38 | |
throws IOException, XmlPullParserException |
39 | |
{ |
40 | 5 | ArchetypeDescriptor descriptor = new ArchetypeDescriptor(); |
41 | |
|
42 | 5 | Xpp3Dom dom = Xpp3DomBuilder.build( reader ); |
43 | |
|
44 | 5 | descriptor.setId( dom.getChild( "id" ).getValue() ); |
45 | |
|
46 | 5 | Xpp3Dom allowPartialDom = dom.getChild( "allowPartial" ); |
47 | |
|
48 | 5 | if ( allowPartialDom != null ) |
49 | |
{ |
50 | 0 | String allowPartial = allowPartialDom.getValue(); |
51 | |
|
52 | 0 | if ( "true".equals( allowPartial ) || "1".equals( allowPartial ) || "on".equals( allowPartial ) ) |
53 | |
{ |
54 | 0 | descriptor.setAllowPartial( true ); |
55 | |
} |
56 | |
} |
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | 5 | Xpp3Dom sources = dom.getChild( "sources" ); |
63 | |
|
64 | 5 | if ( sources != null ) |
65 | |
{ |
66 | 5 | Xpp3Dom[] sourceList = sources.getChildren( "source" ); |
67 | |
|
68 | 12 | for ( int i = 0; i < sourceList.length; i++ ) |
69 | |
{ |
70 | 7 | addSourceToDescriptor( sourceList[i], descriptor ); |
71 | |
} |
72 | |
} |
73 | |
|
74 | 5 | Xpp3Dom resources = dom.getChild( "resources" ); |
75 | |
|
76 | 5 | if ( resources != null ) |
77 | |
{ |
78 | 4 | Xpp3Dom[] resourceList = resources.getChildren( "resource" ); |
79 | |
|
80 | 10 | for ( int i = 0; i < resourceList.length; i++ ) |
81 | |
{ |
82 | 6 | addResourceToDescriptor( resourceList[i], descriptor ); |
83 | |
} |
84 | |
} |
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | 5 | Xpp3Dom testSources = dom.getChild( "testSources" ); |
91 | |
|
92 | 5 | if ( testSources != null ) |
93 | |
{ |
94 | 3 | Xpp3Dom[] testSourceList = testSources.getChildren( "source" ); |
95 | |
|
96 | 9 | for ( int i = 0; i < testSourceList.length; i++ ) |
97 | |
{ |
98 | 6 | addTestSourceToDescriptor( testSourceList[i], descriptor ); |
99 | |
} |
100 | |
} |
101 | |
|
102 | 5 | Xpp3Dom testResources = dom.getChild( "testResources" ); |
103 | |
|
104 | 5 | if ( testResources != null ) |
105 | |
{ |
106 | 2 | Xpp3Dom[] testResourceList = testResources.getChildren( "resource" ); |
107 | |
|
108 | 6 | for ( int i = 0; i < testResourceList.length; i++ ) |
109 | |
{ |
110 | 4 | addTestResourceToDescriptor( testResourceList[i], descriptor ); |
111 | |
} |
112 | |
} |
113 | |
|
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | 5 | Xpp3Dom siteResources = dom.getChild( "siteResources" ); |
119 | |
|
120 | 5 | if ( siteResources != null ) |
121 | |
{ |
122 | 3 | Xpp3Dom[] siteResourceList = siteResources.getChildren( "resource" ); |
123 | |
|
124 | 7 | for ( int i = 0; i < siteResourceList.length; i++ ) |
125 | |
{ |
126 | 4 | addSiteResourceToDescriptor( siteResourceList[i], descriptor ); |
127 | |
} |
128 | |
} |
129 | |
|
130 | 5 | 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 | 7 | descriptor.addSource( source.getValue() ); |
147 | |
|
148 | 7 | TemplateDescriptor sourceDesc = descriptor.getSourceDescriptor( source.getValue() ); |
149 | |
|
150 | 7 | sourceDesc.setFiltered( true ); |
151 | |
|
152 | 7 | if ( source.getAttribute( "encoding" ) != null ) |
153 | |
{ |
154 | |
try |
155 | |
{ |
156 | 2 | sourceDesc.setEncoding( source.getAttribute( "encoding" ) ); |
157 | |
} |
158 | 0 | catch ( IllegalCharsetNameException icne ) |
159 | |
{ |
160 | 0 | throw new XmlPullParserException( source.getAttribute( "encoding" ) + " is not a valid encoding." ); |
161 | |
} |
162 | 0 | catch ( UnsupportedCharsetException uce ) |
163 | |
{ |
164 | 0 | throw new XmlPullParserException( source.getAttribute( "encoding" ) + " is not a supported encoding." ); |
165 | 2 | } |
166 | |
} |
167 | 7 | } |
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 | 6 | descriptor.addResource( resource.getValue() ); |
189 | |
|
190 | 6 | if ( resource.getAttribute( "filtered" ) != null ) |
191 | |
{ |
192 | 1 | TemplateDescriptor resourceDesc = descriptor.getResourceDescriptor( resource.getValue() ); |
193 | |
|
194 | |
try |
195 | |
{ |
196 | 1 | resourceDesc.setFiltered( getValueFilteredAttribute( resource.getAttribute( "filtered" ) ) ); |
197 | |
} |
198 | 0 | catch ( IllegalArgumentException iae ) |
199 | |
{ |
200 | 0 | throw new XmlPullParserException( iae.getMessage() ); |
201 | 1 | } |
202 | |
} |
203 | |
|
204 | 6 | if ( resource.getAttribute( "encoding" ) != null ) |
205 | |
{ |
206 | 1 | TemplateDescriptor resourceDesc = descriptor.getResourceDescriptor( resource.getValue() ); |
207 | |
|
208 | |
try |
209 | |
{ |
210 | 1 | resourceDesc.setEncoding( resource.getAttribute( "encoding" ) ); |
211 | |
} |
212 | 0 | catch ( IllegalCharsetNameException icne ) |
213 | |
{ |
214 | 0 | throw new XmlPullParserException( resource.getAttribute( "encoding" ) + " is not a valid encoding." ); |
215 | |
} |
216 | 0 | catch ( UnsupportedCharsetException uce ) |
217 | |
{ |
218 | 0 | throw new XmlPullParserException( |
219 | |
resource.getAttribute( "encoding" ) + " is not a supported encoding." ); |
220 | 1 | } |
221 | |
} |
222 | |
|
223 | 6 | if ( resource.getValue().endsWith( ".properties" ) ) |
224 | |
{ |
225 | 2 | TemplateDescriptor resourceDesc = descriptor.getResourceDescriptor( resource.getValue() ); |
226 | |
|
227 | 2 | resourceDesc.setEncoding( "iso-8859-1" ); |
228 | |
} |
229 | 6 | } |
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 | 6 | descriptor.addTestSource( testSource.getValue() ); |
245 | 6 | TemplateDescriptor testSourceDesc = descriptor.getTestSourceDescriptor( testSource.getValue() ); |
246 | 6 | testSourceDesc.setFiltered( true ); |
247 | 6 | if ( testSource.getAttribute( "encoding" ) != null ) |
248 | |
{ |
249 | |
try |
250 | |
{ |
251 | 2 | testSourceDesc.setEncoding( testSource.getAttribute( "encoding" ) ); |
252 | |
} |
253 | 0 | catch ( IllegalCharsetNameException icne ) |
254 | |
{ |
255 | 0 | throw new XmlPullParserException( testSource.getAttribute( "encoding" ) + " is not a valid encoding." ); |
256 | |
} |
257 | 0 | catch ( UnsupportedCharsetException uce ) |
258 | |
{ |
259 | 0 | throw new XmlPullParserException( |
260 | |
testSource.getAttribute( "encoding" ) + " is not a supported encoding." ); |
261 | 2 | } |
262 | |
} |
263 | 6 | } |
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 | 4 | descriptor.addTestResource( testResource.getValue() ); |
285 | |
|
286 | 4 | if ( testResource.getAttribute( "filtered" ) != null ) |
287 | |
{ |
288 | 1 | TemplateDescriptor testResourceDesc = descriptor.getTestResourceDescriptor( testResource.getValue() ); |
289 | |
|
290 | |
try |
291 | |
{ |
292 | 1 | testResourceDesc.setFiltered( getValueFilteredAttribute( testResource.getAttribute( "filtered" ) ) ); |
293 | |
} |
294 | 0 | catch ( IllegalArgumentException iae ) |
295 | |
{ |
296 | 0 | throw new XmlPullParserException( iae.getMessage() ); |
297 | 1 | } |
298 | |
} |
299 | |
|
300 | 4 | if ( testResource.getAttribute( "encoding" ) != null ) |
301 | |
{ |
302 | 1 | TemplateDescriptor testResourceDesc = descriptor.getTestResourceDescriptor( testResource.getValue() ); |
303 | |
|
304 | |
try |
305 | |
{ |
306 | 1 | testResourceDesc.setEncoding( testResource.getAttribute( "encoding" ) ); |
307 | |
|
308 | |
} |
309 | 0 | catch ( IllegalCharsetNameException icne ) |
310 | |
{ |
311 | 0 | throw new XmlPullParserException( |
312 | |
testResource.getAttribute( "encoding" ) + " is not a valid encoding." ); |
313 | |
} |
314 | 0 | catch ( UnsupportedCharsetException uce ) |
315 | |
{ |
316 | 0 | throw new XmlPullParserException( |
317 | |
testResource.getAttribute( "encoding" ) + " is not a supported encoding." ); |
318 | 1 | } |
319 | |
} |
320 | |
|
321 | 4 | if ( testResource.getValue().endsWith( ".properties" ) ) |
322 | |
{ |
323 | 0 | TemplateDescriptor testResourceDesc = descriptor.getTestResourceDescriptor( testResource.getValue() ); |
324 | |
|
325 | 0 | testResourceDesc.setEncoding( "iso-8859-1" ); |
326 | |
} |
327 | 4 | } |
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 | 4 | descriptor.addSiteResource( siteResource.getValue() ); |
349 | |
|
350 | 4 | if ( siteResource.getAttribute( "filtered" ) != null ) |
351 | |
{ |
352 | 1 | TemplateDescriptor siteResourceDesc = descriptor.getSiteResourceDescriptor( siteResource.getValue() ); |
353 | |
|
354 | |
try |
355 | |
{ |
356 | 1 | siteResourceDesc.setFiltered( getValueFilteredAttribute( siteResource.getAttribute( "filtered" ) ) ); |
357 | |
} |
358 | 0 | catch ( IllegalArgumentException iae ) |
359 | |
{ |
360 | 0 | throw new XmlPullParserException( iae.getMessage() ); |
361 | 1 | } |
362 | |
} |
363 | 4 | if ( siteResource.getAttribute( "encoding" ) != null ) |
364 | |
{ |
365 | 1 | TemplateDescriptor siteResourceDesc = descriptor.getSiteResourceDescriptor( siteResource.getValue() ); |
366 | |
|
367 | |
try |
368 | |
{ |
369 | 1 | siteResourceDesc.setEncoding( siteResource.getAttribute( "encoding" ) ); |
370 | |
} |
371 | 0 | catch ( IllegalCharsetNameException icne ) |
372 | |
{ |
373 | 0 | throw new XmlPullParserException( |
374 | |
siteResource.getAttribute( "encoding" ) + " is not a valid encoding." ); |
375 | |
} |
376 | 0 | catch ( UnsupportedCharsetException uce ) |
377 | |
{ |
378 | 0 | throw new XmlPullParserException( |
379 | |
siteResource.getAttribute( "encoding" ) + " is not a supported encoding." ); |
380 | 1 | } |
381 | |
} |
382 | 4 | if ( siteResource.getValue().endsWith( ".properties" ) ) |
383 | |
{ |
384 | 0 | TemplateDescriptor siteResourceDesc = descriptor.getSiteResourceDescriptor( siteResource.getValue() ); |
385 | |
|
386 | 0 | siteResourceDesc.setEncoding( "iso-8859-1" ); |
387 | |
} |
388 | 4 | } |
389 | |
|
390 | |
private static boolean getValueFilteredAttribute( String str ) |
391 | |
throws IllegalArgumentException |
392 | |
{ |
393 | 3 | boolean ret = false; |
394 | |
|
395 | 3 | if ( str.equals( "true" ) ) |
396 | |
{ |
397 | 0 | ret = true; |
398 | |
} |
399 | 3 | else if ( str.equals( "false" ) ) |
400 | |
{ |
401 | 3 | ret = false; |
402 | |
} |
403 | |
else |
404 | |
{ |
405 | 0 | throw new IllegalArgumentException( str + " is not an accepted value for the attribute 'filtered'" ); |
406 | |
} |
407 | 3 | return ret; |
408 | |
} |
409 | |
} |