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