View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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   * @author <a href="mailto:jason@maven.org">Jason van Zyl</a>
32   * @version $Id$
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          // Main
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          // Test
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         // Site
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      * Adds the source element <code>source</code> to the list of sources in the
119      * <code>descriptor</code> and sets its <code>TemplateDescriptor</code> to
120      * <i>filtered</i> and with the encoding specified in the <code>encoding</code>
121      * attribute or the Java virtual machine's default if it is not defined.
122      *
123      * @param source     a <code>&lt;source&gt;</code> element from the <code>&lt;sources&gt;</code>
124      * @param descriptor the <code>ArchetypeDescriptor</code> to add the source template to.
125      * @throws XmlPullParserException if the encoding specified is not valid or supported.
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      * Adds the resource element <code>resource</code> to the list of resources in the
148      * <code>descriptor</code> and sets its <code>TemplateDescriptor</code> to
149      * <i>filtered</i> if the attribute <code>filtered</code> was not
150      * specified or its value is <code>&quot;true&quot;</code>, or <code>false</code>
151      * if its value is <code>&quot;false&quot;</code>, and the encoding specified
152      * in the <code>encoding</code> attribute or the Java virtual machine's default if
153      * it is not defined. If the <code>resource</code> is a property file (ends in
154      * <code>.properties</code>) its encoding will be set to <code>iso-8859-1</code>
155      * even if some other encoding is specified in the attribute.
156      *
157      * @param resource   a <code>&lt;resource&gt;</code> element from the <code>&lt;resources&gt;</code>
158      * @param descriptor the <code>ArchetypeDescriptor</code> to add the resource template to.
159      * @throws XmlPullParserException if the encoding specified is not valid or supported or if the
160      *                                value of the attribute <code>filtered</code> is no valid.
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      * Adds the test-source element <code>source</code> to the list of sources in the
197      * <code>descriptor</code> and sets its <code>TemplateDescriptor</code> to
198      * <i>filtered</i> and with the encoding specified in the <code>encoding</code>
199      * attribute or the Java virtual machine's default if it is not defined.
200      *
201      * @param testSource a <code>&lt;source&gt;</code> element from the <code>&lt;testSources&gt;</code>
202      * @param descriptor the <code>ArchetypeDescriptor</code> to add the test-source template to.
203      * @throws XmlPullParserException if the encoding specified is not valid or supported.
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      * Adds the test-resource element <code>resource</code> to the list of test-resources in the
223      * <code>descriptor</code> and sets its <code>TemplateDescriptor</code> to
224      * <i>filtered</i> if the attribute <code>filtered</code> was not
225      * specified or its value is <code>&quot;true&quot;</code>, or <code>false</code>
226      * if its value is <code>&quot;false&quot;</code>, and the encoding specified
227      * in the <code>encoding</code> attribute or the Java virtual machine's default if
228      * it is not defined. If the <code>resource</code> is a property file (ends in
229      * <code>.properties</code>) its encoding will be set to <code>iso-8859-1</code>
230      * even if some other encoding is specified in the attribute.
231      *
232      * @param testResource a <code>&lt;resource&gt;</code> element from the <code>&lt;testResources&gt;</code>
233      * @param descriptor   the <code>ArchetypeDescriptor</code> to add the test-resource template to.
234      * @throws XmlPullParserException if the encoding specified is not valid or supported or if the
235      *                                value of the attribute <code>filtered</code> is no valid.
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      * Adds the site-resource element <code>resource</code> to the list of site-resources in the
274      * <code>descriptor</code> and sets its <code>TemplateDescriptor</code> to
275      * <i>filtered</i> if the attribute <code>filtered</code> was not
276      * specified or its value is <code>&quot;true&quot;</code>, or <code>false</code>
277      * if its value is <code>&quot;false&quot;</code>, and the encoding specified
278      * in the <code>encoding</code> attribute or the Java virtual machine's default if
279      * it is not defined. If the <code>resource</code> is a property file (ends in
280      * <code>.properties</code>) its encoding will be set to <code>iso-8859-1</code>
281      * even if some other encoding is specified in the attribute.
282      *
283      * @param siteResource a <code>&lt;resource&gt;</code> element from the <code>&lt;siteResources&gt;</code>
284      * @param descriptor   the <code>ArchetypeDescriptor</code> to add the site-resource template to.
285      * @throws XmlPullParserException if the encoding specified is not valid or supported or if the
286      *                                value of the attribute <code>filtered</code> is no valid.
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 }