View Javadoc
1   package org.apache.maven.archetype.old.descriptor;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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   * @author <a href="mailto:jason@maven.org">Jason van Zyl</a>
33   * @version $Id$
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          // Main
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          // Test
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         // Site
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      * Adds the source element <code>source</code> to the list of sources in the
135      * <code>descriptor</code> and sets its <code>TemplateDescriptor</code> to
136      * <i>filtered</i> and with the encoding specified in the <code>encoding</code>
137      * attribute or the Java virtual machine's default if it is not defined.
138      *
139      * @param source     a <code>&lt;source&gt;</code> element from the <code>&lt;sources&gt;</code>
140      * @param descriptor the <code>ArchetypeDescriptor</code> to add the source template to.
141      * @throws XmlPullParserException if the encoding specified is not valid or supported.
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      * Adds the resource element <code>resource</code> to the list of resources in the
171      * <code>descriptor</code> and sets its <code>TemplateDescriptor</code> to
172      * <i>filtered</i> if the attribute <code>filtered</code> was not
173      * specified or its value is <code>&quot;true&quot;</code>, or <code>false</code>
174      * if its value is <code>&quot;false&quot;</code>, and the encoding specified
175      * in the <code>encoding</code> attribute or the Java virtual machine's default if
176      * it is not defined. If the <code>resource</code> is a property file (ends in
177      * <code>.properties</code>) its encoding will be set to <code>iso-8859-1</code>
178      * even if some other encoding is specified in the attribute.
179      *
180      * @param resource   a <code>&lt;resource&gt;</code> element from the <code>&lt;resources&gt;</code>
181      * @param descriptor the <code>ArchetypeDescriptor</code> to add the resource template to.
182      * @throws XmlPullParserException if the encoding specified is not valid or supported or if the
183      *                                value of the attribute <code>filtered</code> is no valid.
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      * Adds the test-source element <code>source</code> to the list of sources in the
233      * <code>descriptor</code> and sets its <code>TemplateDescriptor</code> to
234      * <i>filtered</i> and with the encoding specified in the <code>encoding</code>
235      * attribute or the Java virtual machine's default if it is not defined.
236      *
237      * @param testSource a <code>&lt;source&gt;</code> element from the <code>&lt;testSources&gt;</code>
238      * @param descriptor the <code>ArchetypeDescriptor</code> to add the test-source template to.
239      * @throws XmlPullParserException if the encoding specified is not valid or supported.
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      * Adds the test-resource element <code>resource</code> to the list of test-resources in the
267      * <code>descriptor</code> and sets its <code>TemplateDescriptor</code> to
268      * <i>filtered</i> if the attribute <code>filtered</code> was not
269      * specified or its value is <code>&quot;true&quot;</code>, or <code>false</code>
270      * if its value is <code>&quot;false&quot;</code>, and the encoding specified
271      * in the <code>encoding</code> attribute or the Java virtual machine's default if
272      * it is not defined. If the <code>resource</code> is a property file (ends in
273      * <code>.properties</code>) its encoding will be set to <code>iso-8859-1</code>
274      * even if some other encoding is specified in the attribute.
275      *
276      * @param testResource a <code>&lt;resource&gt;</code> element from the <code>&lt;testResources&gt;</code>
277      * @param descriptor   the <code>ArchetypeDescriptor</code> to add the test-resource template to.
278      * @throws XmlPullParserException if the encoding specified is not valid or supported or if the
279      *                                value of the attribute <code>filtered</code> is no valid.
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      * Adds the site-resource element <code>resource</code> to the list of site-resources in the
331      * <code>descriptor</code> and sets its <code>TemplateDescriptor</code> to
332      * <i>filtered</i> if the attribute <code>filtered</code> was not
333      * specified or its value is <code>&quot;true&quot;</code>, or <code>false</code>
334      * if its value is <code>&quot;false&quot;</code>, and the encoding specified
335      * in the <code>encoding</code> attribute or the Java virtual machine's default if
336      * it is not defined. If the <code>resource</code> is a property file (ends in
337      * <code>.properties</code>) its encoding will be set to <code>iso-8859-1</code>
338      * even if some other encoding is specified in the attribute.
339      *
340      * @param siteResource a <code>&lt;resource&gt;</code> element from the <code>&lt;siteResources&gt;</code>
341      * @param descriptor   the <code>ArchetypeDescriptor</code> to add the site-resource template to.
342      * @throws XmlPullParserException if the encoding specified is not valid or supported or if the
343      *                                value of the attribute <code>filtered</code> is no valid.
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 }