You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by br...@apache.org on 2006/04/03 08:21:13 UTC
svn commit: r390962 - in /maven/archetype/trunk/maven-archetype-core/src:
main/java/org/apache/maven/archetype/
main/java/org/apache/maven/archetype/descriptor/
test/java/org/apache/maven/archetype/descriptor/
Author: brett
Date: Sun Apr 2 23:21:10 2006
New Revision: 390962
URL: http://svn.apache.org/viewcvs?rev=390962&view=rev
Log:
[ARCHETYPE-19] facilitate disabling filtering and changing encoding for some resources in the archetype
Submitted by: Adolfo Garcia
Added:
maven/archetype/trunk/maven-archetype-core/src/main/java/org/apache/maven/archetype/descriptor/TemplateDescriptor.java (with props)
Modified:
maven/archetype/trunk/maven-archetype-core/src/main/java/org/apache/maven/archetype/DefaultArchetype.java
maven/archetype/trunk/maven-archetype-core/src/main/java/org/apache/maven/archetype/descriptor/ArchetypeDescriptor.java
maven/archetype/trunk/maven-archetype-core/src/main/java/org/apache/maven/archetype/descriptor/ArchetypeDescriptorBuilder.java
maven/archetype/trunk/maven-archetype-core/src/test/java/org/apache/maven/archetype/descriptor/ArchetypeDescriptorBuilderTest.java
Modified: maven/archetype/trunk/maven-archetype-core/src/main/java/org/apache/maven/archetype/DefaultArchetype.java
URL: http://svn.apache.org/viewcvs/maven/archetype/trunk/maven-archetype-core/src/main/java/org/apache/maven/archetype/DefaultArchetype.java?rev=390962&r1=390961&r2=390962&view=diff
==============================================================================
--- maven/archetype/trunk/maven-archetype-core/src/main/java/org/apache/maven/archetype/DefaultArchetype.java (original)
+++ maven/archetype/trunk/maven-archetype-core/src/main/java/org/apache/maven/archetype/DefaultArchetype.java Sun Apr 2 23:21:10 2006
@@ -16,8 +16,12 @@
* limitations under the License.
*/
+import java.io.FileOutputStream;
+import java.io.OutputStream;
+import java.io.OutputStreamWriter;
import org.apache.maven.archetype.descriptor.ArchetypeDescriptor;
import org.apache.maven.archetype.descriptor.ArchetypeDescriptorBuilder;
+import org.apache.maven.archetype.descriptor.TemplateDescriptor;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.factory.ArtifactFactory;
import org.apache.maven.artifact.repository.ArtifactRepository;
@@ -332,7 +336,7 @@
{
if ( !pomFile.exists() )
{
- processTemplate( outputDirectory, context, ARCHETYPE_POM, false, null );
+ processTemplate( outputDirectory, context, ARCHETYPE_POM, new TemplateDescriptor(), false, null );
}
// ---------------------------------------------------------------------
@@ -523,7 +527,7 @@
{
FileUtils.mkdir( outputDirectory + DEFAULT_SOURCE_DIR );
}
- processSources( outputDirectory, context, descriptor.getSources(), packageName );
+ processSources( outputDirectory, context, descriptor, packageName );
}
if ( descriptor.getResources().size() > 0 )
@@ -532,7 +536,7 @@
{
FileUtils.mkdir( outputDirectory + DEFAULT_RESOURCE_DIR );
}
- processResources( outputDirectory, context, descriptor.getResources(), packageName );
+ processResources( outputDirectory, context, descriptor, packageName );
}
// ----------------------------------------------------------------------
@@ -546,7 +550,7 @@
FileUtils.mkdir( outputDirectory + DEFAULT_TEST_SOURCE_DIR );
}
- processSources( outputDirectory, context, descriptor.getTestSources(), packageName );
+ processTestSources( outputDirectory, context, descriptor, packageName );
}
if ( descriptor.getTestResources().size() > 0 )
@@ -555,7 +559,7 @@
{
FileUtils.mkdir( outputDirectory + DEFAULT_TEST_RESOURCE_DIR );
}
- processResources( outputDirectory, context, descriptor.getTestResources(), packageName );
+ processTestResources( outputDirectory, context, descriptor, packageName );
}
// ----------------------------------------------------------------------
@@ -564,7 +568,7 @@
if ( descriptor.getSiteResources().size() > 0 )
{
- processResources( outputDirectory, context, descriptor.getSiteResources(), packageName );
+ processSiteResources( outputDirectory, context, descriptor, packageName );
}
}
@@ -578,30 +582,63 @@
//
// ----------------------------------------------------------------------
- protected void processSources( String outputDirectory, Context context, List sources, String packageName )
+ protected void processSources( String outputDirectory, Context context, ArchetypeDescriptor descriptor, String packageName )
throws ArchetypeTemplateProcessingException
{
- for ( Iterator i = sources.iterator(); i.hasNext(); )
+ for ( Iterator i = descriptor.getSources().iterator(); i.hasNext(); )
{
String template = (String) i.next();
-
- processTemplate( outputDirectory, context, template, true, packageName );
+
+ processTemplate( outputDirectory, context, template, descriptor.getSourceDescriptor(template), true, packageName );
}
}
-
- protected void processResources( String outputDirectory, Context context, List resources, String packageName )
+
+ protected void processTestSources( String outputDirectory, Context context, ArchetypeDescriptor descriptor, String packageName )
throws ArchetypeTemplateProcessingException
{
- for ( Iterator i = resources.iterator(); i.hasNext(); )
+ for ( Iterator i = descriptor.getTestSources().iterator(); i.hasNext(); )
{
String template = (String) i.next();
+
+ processTemplate( outputDirectory, context, template, descriptor.getTestSourceDescriptor(template), true, packageName );
+ }
+ }
- processTemplate( outputDirectory, context, template, false, packageName );
+ protected void processResources( String outputDirectory, Context context, ArchetypeDescriptor descriptor, String packageName )
+ throws ArchetypeTemplateProcessingException
+ {
+ for ( Iterator i = descriptor.getResources().iterator(); i.hasNext(); )
+ {
+ String template = (String) i.next();
+
+ processTemplate( outputDirectory, context, template, descriptor.getResourceDescriptor(template), false, packageName );
+ }
+ }
+
+ protected void processTestResources( String outputDirectory, Context context, ArchetypeDescriptor descriptor, String packageName )
+ throws ArchetypeTemplateProcessingException
+ {
+ for ( Iterator i = descriptor.getTestResources().iterator(); i.hasNext(); )
+ {
+ String template = (String) i.next();
+
+ processTemplate( outputDirectory, context, template, descriptor.getTestResourceDescriptor(template), false, packageName );
+ }
+ }
+
+ protected void processSiteResources( String outputDirectory, Context context, ArchetypeDescriptor descriptor, String packageName )
+ throws ArchetypeTemplateProcessingException
+ {
+ for ( Iterator i = descriptor.getSiteResources().iterator(); i.hasNext(); )
+ {
+ String template = (String) i.next();
+
+ processTemplate( outputDirectory, context, template, descriptor.getSiteResourceDescriptor(template), false, packageName );
}
}
- protected void processTemplate( String outputDirectory, Context context, String template, boolean packageInFileName,
- String packageName )
+ protected void processTemplate( String outputDirectory, Context context, String template, TemplateDescriptor descriptor,
+ boolean packageInFileName, String packageName )
throws ArchetypeTemplateProcessingException
{
File f;
@@ -630,24 +667,50 @@
f.getParentFile().mkdirs();
}
- Writer writer = null;
- try
+ if ( descriptor.isFiltered() )
{
- writer = new FileWriter( f );
+ Writer writer = null;
+ try
+ {
+ writer = new OutputStreamWriter(new FileOutputStream(f), descriptor.getEncoding());
- template = ARCHETYPE_RESOURCES + "/" + template;
+ template = ARCHETYPE_RESOURCES + "/" + template;
- velocity.getEngine().mergeTemplate( template, context, writer );
+ velocity.getEngine().mergeTemplate( template, descriptor.getEncoding(), context, writer );
- writer.flush();
- }
- catch ( Exception e )
- {
- throw new ArchetypeTemplateProcessingException( "Error merging velocity templates", e );
+ writer.flush();
+ }
+ catch ( Exception e )
+ {
+ throw new ArchetypeTemplateProcessingException( "Error merging velocity templates", e );
+ }
+ finally
+ {
+ IOUtil.close( writer );
+ }
}
- finally
+ else
{
- IOUtil.close( writer );
+ InputStream is = getStream( ARCHETYPE_RESOURCES + "/" + template, null );
+
+ OutputStream fos = null;
+
+ try
+ {
+ fos = new FileOutputStream(f);
+
+ IOUtil.copy( is, fos );
+ }
+ catch ( Exception e )
+ {
+ throw new ArchetypeTemplateProcessingException( "Error copying file", e );
+ }
+ finally
+ {
+ IOUtil.close( fos );
+
+ IOUtil.close( is );
+ }
}
}
@@ -661,7 +724,6 @@
{
return Thread.currentThread().getContextClassLoader().getResourceAsStream( name );
}
-
return loader.getResourceAsStream( name );
}
}
Modified: maven/archetype/trunk/maven-archetype-core/src/main/java/org/apache/maven/archetype/descriptor/ArchetypeDescriptor.java
URL: http://svn.apache.org/viewcvs/maven/archetype/trunk/maven-archetype-core/src/main/java/org/apache/maven/archetype/descriptor/ArchetypeDescriptor.java?rev=390962&r1=390961&r2=390962&view=diff
==============================================================================
--- maven/archetype/trunk/maven-archetype-core/src/main/java/org/apache/maven/archetype/descriptor/ArchetypeDescriptor.java (original)
+++ maven/archetype/trunk/maven-archetype-core/src/main/java/org/apache/maven/archetype/descriptor/ArchetypeDescriptor.java Sun Apr 2 23:21:10 2006
@@ -17,7 +17,9 @@
*/
import java.util.ArrayList;
+import java.util.HashMap;
import java.util.List;
+import java.util.Map;
public class ArchetypeDescriptor
{
@@ -32,6 +34,41 @@
private List testResources;
private List siteResources;
+
+ /**
+ * <code>Map</code> that associates the items in the <code>List</code>
+ * <code>sources</code> with their attributes (instances of
+ * <code>TemplateDescriptor</code>.
+ */
+ private Map sourcesDescriptors;
+
+ /**
+ * <code>Map</code> that associates the items in the <code>List</code>
+ * <code>testSources</code> with their attributes (instances of
+ * <code>TemplateDescriptor</code>.
+ */
+ private Map testSourcesDescriptors;
+
+ /**
+ * <code>Map</code> that associates the items in the <code>List</code>
+ * <code>resources</code> with their attributes (instances of
+ * <code>TemplateDescriptor</code>.
+ */
+ private Map resourcesDescriptors;
+
+ /**
+ * <code>Map</code> that associates the items in the <code>List</code>
+ * <code>testResources</code> with their attributes (instances of
+ * <code>TemplateDescriptor</code>.
+ */
+ private Map testResourcesDescriptors;
+
+ /**
+ * <code>Map</code> that associates the items in the <code>List</code>
+ * <code>siteResources</code> with their attributes (instances of
+ * <code>TemplateDescriptor</code>.
+ */
+ private Map siteResourcesDescriptors;
/**
* This indicates the archetype can be a whole project or can be part
@@ -44,14 +81,24 @@
public ArchetypeDescriptor()
{
sources = new ArrayList();
-
+
resources = new ArrayList();
-
+
testSources = new ArrayList();
-
+
testResources = new ArrayList();
-
+
siteResources = new ArrayList();
+
+ sourcesDescriptors = new HashMap();
+
+ testSourcesDescriptors = new HashMap();
+
+ resourcesDescriptors = new HashMap();
+
+ testResourcesDescriptors = new HashMap();
+
+ siteResourcesDescriptors = new HashMap();
}
// ----------------------------------------------------------------------
@@ -71,6 +118,8 @@
public void addSource( String source )
{
sources.add( source );
+
+ putSourceDescriptor( source, new TemplateDescriptor() );
}
public List getSources()
@@ -78,9 +127,26 @@
return sources;
}
+ public void putSourceDescriptor( String source, TemplateDescriptor descriptor )
+ {
+ sourcesDescriptors.put( source, descriptor );
+ }
+
+ public TemplateDescriptor getSourceDescriptor( String source )
+ {
+ return (TemplateDescriptor)sourcesDescriptors.get(source);
+ }
+
+ public Map getSourcesDescriptors()
+ {
+ return sourcesDescriptors;
+ }
+
public void addTestSource( String testSource )
{
testSources.add( testSource );
+
+ putTestSourceDescriptor( testSource, new TemplateDescriptor() );
}
public List getTestSources()
@@ -88,9 +154,26 @@
return testSources;
}
+ public void putTestSourceDescriptor( String testSource, TemplateDescriptor descriptor )
+ {
+ testSourcesDescriptors.put(testSource, descriptor);
+ }
+
+ public TemplateDescriptor getTestSourceDescriptor(String testSource)
+ {
+ return (TemplateDescriptor)testSourcesDescriptors.get(testSource);
+ }
+
+ public Map getTestSourcesDescriptors()
+ {
+ return testSourcesDescriptors;
+ }
+
public void addResource( String resource )
{
resources.add( resource );
+
+ putResourceDescriptor( resource, new TemplateDescriptor() );
}
public List getResources()
@@ -98,9 +181,24 @@
return resources;
}
+ public void putResourceDescriptor( String resource, TemplateDescriptor descriptor )
+ {
+ resourcesDescriptors.put( resource, descriptor );
+ }
+
+ public TemplateDescriptor getResourceDescriptor( String resource )
+ {
+ return ( TemplateDescriptor ) resourcesDescriptors.get( resource );
+ }
+
+ public Map getReourcesDescriptors() {
+ return resourcesDescriptors;
+ }
+
public void addTestResource( String testResource )
{
testResources.add( testResource );
+ putTestResourceDescriptor( testResource, new TemplateDescriptor() );
}
public List getTestResources()
@@ -108,9 +206,26 @@
return testResources;
}
+ public void putTestResourceDescriptor( String testResource, TemplateDescriptor descriptor )
+ {
+ testResourcesDescriptors.put( testResource, descriptor );
+ }
+
+ public TemplateDescriptor getTestResourceDescriptor( String testResource )
+ {
+ return ( TemplateDescriptor ) testResourcesDescriptors.get(testResource);
+ }
+
+ public Map getTestReourcesDescriptors()
+ {
+ return testResourcesDescriptors;
+ }
+
public void addSiteResource( String siteResource )
{
siteResources.add( siteResource );
+
+ putSiteResourceDescriptor(siteResource, new TemplateDescriptor());
}
public List getSiteResources()
@@ -118,6 +233,21 @@
return siteResources;
}
+ public void putSiteResourceDescriptor(String siteResource, TemplateDescriptor descriptor)
+ {
+ siteResourcesDescriptors.put( siteResource, descriptor );
+ }
+
+ public TemplateDescriptor getSiteResourceDescriptor( String siteResource )
+ {
+ return ( TemplateDescriptor ) siteResourcesDescriptors.get(siteResource);
+ }
+
+ public Map getSiteReourcesDescriptors()
+ {
+ return siteResourcesDescriptors;
+ }
+
public boolean isAllowPartial()
{
return allowPartial;
@@ -128,4 +258,3 @@
this.allowPartial = allowPartial;
}
}
-
Modified: maven/archetype/trunk/maven-archetype-core/src/main/java/org/apache/maven/archetype/descriptor/ArchetypeDescriptorBuilder.java
URL: http://svn.apache.org/viewcvs/maven/archetype/trunk/maven-archetype-core/src/main/java/org/apache/maven/archetype/descriptor/ArchetypeDescriptorBuilder.java?rev=390962&r1=390961&r2=390962&view=diff
==============================================================================
--- maven/archetype/trunk/maven-archetype-core/src/main/java/org/apache/maven/archetype/descriptor/ArchetypeDescriptorBuilder.java (original)
+++ maven/archetype/trunk/maven-archetype-core/src/main/java/org/apache/maven/archetype/descriptor/ArchetypeDescriptorBuilder.java Sun Apr 2 23:21:10 2006
@@ -16,6 +16,8 @@
* limitations under the License.
*/
+import java.nio.charset.IllegalCharsetNameException;
+import java.nio.charset.UnsupportedCharsetException;
import org.codehaus.plexus.util.xml.Xpp3Dom;
import org.codehaus.plexus.util.xml.Xpp3DomBuilder;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
@@ -62,7 +64,7 @@
for ( int i = 0; i < sourceList.length; i++ )
{
- descriptor.addSource( sourceList[i].getValue() );
+ addSourceToDescriptor(sourceList[i], descriptor);
}
}
@@ -74,7 +76,7 @@
for ( int i = 0; i < resourceList.length; i++ )
{
- descriptor.addResource( resourceList[i].getValue() );
+ addResourceToDescriptor(resourceList[i], descriptor);
}
}
@@ -90,7 +92,7 @@
for ( int i = 0; i < testSourceList.length; i++ )
{
- descriptor.addTestSource( testSourceList[i].getValue() );
+ addTestSourceToDescriptor(testSourceList[i], descriptor);
}
}
@@ -102,7 +104,7 @@
for ( int i = 0; i < testResourceList.length; i++ )
{
- descriptor.addTestResource( testResourceList[i].getValue() );
+ addTestResourceToDescriptor(testResourceList[i], descriptor);
}
}
@@ -118,10 +120,282 @@
for ( int i = 0; i < siteResourceList.length; i++ )
{
- descriptor.addSiteResource( siteResourceList[i].getValue() );
+ addSiteResourceToDescriptor(siteResourceList[i], descriptor);
}
}
return descriptor;
+ }
+
+ /**
+ * Adds the source element <code>source</code> to the list of sources in the
+ * <code>descriptor</code> and sets its <code>TemplateDescriptor</code> to
+ * <i>filtered</i> and with the encoding specified in the <code>encoding</code>
+ * attribute or the Java virtual machine's default if it is not defined.
+ *
+ * @param source a <code><source></code> element from the <code><sources></code>
+ * @param descriptor the <code>ArchetypeDescriptor</code> to add the source template to.
+ * @throws XmlPullParserException if the encoding specified is not valid or supported.
+ */
+ private static void addSourceToDescriptor( Xpp3Dom source, ArchetypeDescriptor descriptor )
+ throws XmlPullParserException
+ {
+ descriptor.addSource( source.getValue() );
+
+ TemplateDescriptor sourceDesc = descriptor.getSourceDescriptor( source.getValue() );
+
+ sourceDesc.setFiltered( true );
+
+ if ( source.getAttribute( "encoding" ) != null)
+ {
+ try
+ {
+ sourceDesc.setEncoding( source.getAttribute( "encoding" ) );
+ }
+ catch ( IllegalCharsetNameException icne )
+ {
+ throw new XmlPullParserException( source.getAttribute( "encoding" )
+ + " is not a valid encoding." );
+ }
+ catch ( UnsupportedCharsetException uce )
+ {
+ throw new XmlPullParserException(source.getAttribute( "encoding" )
+ + " is not a supported encoding." );
+ }
+ }
+ }
+
+ /**
+ * Adds the resource element <code>resource</code> to the list of resources in the
+ * <code>descriptor</code> and sets its <code>TemplateDescriptor</code> to
+ * <i>filtered</i> if the attribute <code>filtered</code> was not
+ * specified or its value is <code>"true"</code>, or <code>false</code>
+ * if its value is <code>"false"</code>, and the encoding specified
+ * in the <code>encoding</code> attribute or the Java virtual machine's default if
+ * it is not defined. If the <code>resource</code> is a property file (ends in
+ * <code>.properties</code>) its encoding will be set to <code>iso-8859-1</code>
+ * even if some other encoding is specified in the attribute.
+ *
+ * @param resource a <code><resource></code> element from the <code><resources></code>
+ * @param descriptor the <code>ArchetypeDescriptor</code> to add the resource template to.
+ * @throws XmlPullParserException if the encoding specified is not valid or supported or if the
+ * value of the attribute <code>filtered</code> is no valid.
+ */
+ private static void addResourceToDescriptor( Xpp3Dom resource, ArchetypeDescriptor descriptor )
+ throws XmlPullParserException
+ {
+ descriptor.addResource( resource.getValue() );
+
+ if ( resource.getAttribute("filtered") != null)
+ {
+ TemplateDescriptor resourceDesc = descriptor.getResourceDescriptor( resource.getValue() );
+
+ try
+ {
+ resourceDesc.setFiltered( getValueFilteredAttribute( resource.getAttribute( "filtered" ) ) );
+ }
+ catch ( IllegalArgumentException iae ) {
+ throw new XmlPullParserException( iae.getMessage() );
+ }
+ }
+
+ if ( resource.getAttribute( "encoding" ) != null)
+ {
+ TemplateDescriptor resourceDesc = descriptor.getResourceDescriptor( resource.getValue() );
+
+ try
+ {
+ resourceDesc.setEncoding( resource.getAttribute( "encoding" ) );
+ }
+ catch ( IllegalCharsetNameException icne )
+ {
+ throw new XmlPullParserException( resource.getAttribute( "encoding" )
+ + " is not a valid encoding." );
+ }
+ catch ( UnsupportedCharsetException uce )
+ {
+ throw new XmlPullParserException( resource.getAttribute( "encoding" )
+ + " is not a supported encoding." );
+ }
+ }
+
+ if ( resource.getValue().endsWith(".properties") )
+ {
+ TemplateDescriptor resourceDesc = descriptor.getResourceDescriptor( resource.getValue() );
+
+ resourceDesc.setEncoding( "iso-8859-1" );
+ }
+ }
+
+ /**
+ * Adds the test-source element <code>source</code> to the list of sources in the
+ * <code>descriptor</code> and sets its <code>TemplateDescriptor</code> to
+ * <i>filtered</i> and with the encoding specified in the <code>encoding</code>
+ * attribute or the Java virtual machine's default if it is not defined.
+ *
+ * @param testSource a <code><source></code> element from the <code><testSources></code>
+ * @param descriptor the <code>ArchetypeDescriptor</code> to add the test-source template to.
+ * @throws XmlPullParserException if the encoding specified is not valid or supported.
+ */
+ private static void addTestSourceToDescriptor( Xpp3Dom testSource, ArchetypeDescriptor descriptor ) throws XmlPullParserException {
+ descriptor.addTestSource( testSource.getValue() );
+ TemplateDescriptor testSourceDesc = descriptor.getTestSourceDescriptor(testSource.getValue());
+ testSourceDesc.setFiltered(true);
+ if ( testSource.getAttribute("encoding") != null) {
+ try {
+ testSourceDesc.setEncoding(testSource.getAttribute("encoding"));
+ } catch(IllegalCharsetNameException icne) {
+ throw new XmlPullParserException(testSource.getAttribute("encoding")
+ + " is not a valid encoding.");
+ } catch(UnsupportedCharsetException uce) {
+ throw new XmlPullParserException(testSource.getAttribute("encoding")
+ + " is not a supported encoding.");
+ }
+ }
+ }
+
+ /**
+ * Adds the test-resource element <code>resource</code> to the list of test-resources in the
+ * <code>descriptor</code> and sets its <code>TemplateDescriptor</code> to
+ * <i>filtered</i> if the attribute <code>filtered</code> was not
+ * specified or its value is <code>"true"</code>, or <code>false</code>
+ * if its value is <code>"false"</code>, and the encoding specified
+ * in the <code>encoding</code> attribute or the Java virtual machine's default if
+ * it is not defined. If the <code>resource</code> is a property file (ends in
+ * <code>.properties</code>) its encoding will be set to <code>iso-8859-1</code>
+ * even if some other encoding is specified in the attribute.
+ *
+ * @param testResource a <code><resource></code> element from the <code><testResources></code>
+ * @param descriptor the <code>ArchetypeDescriptor</code> to add the test-resource template to.
+ * @throws XmlPullParserException if the encoding specified is not valid or supported or if the
+ * value of the attribute <code>filtered</code> is no valid.
+ */
+ private static void addTestResourceToDescriptor( Xpp3Dom testResource, ArchetypeDescriptor descriptor )
+ throws XmlPullParserException
+ {
+ descriptor.addTestResource( testResource.getValue() );
+
+ if ( testResource.getAttribute("filtered") != null)
+ {
+ TemplateDescriptor testResourceDesc = descriptor.getTestResourceDescriptor(testResource.getValue());
+
+ try
+ {
+ testResourceDesc.setFiltered(getValueFilteredAttribute(testResource.getAttribute("filtered")));
+ }
+ catch (IllegalArgumentException iae)
+ {
+ throw new XmlPullParserException(iae.getMessage());
+ }
+ }
+
+ if ( testResource.getAttribute("encoding") != null)
+ {
+ TemplateDescriptor testResourceDesc = descriptor.getTestResourceDescriptor(testResource.getValue());
+
+ try
+ {
+ testResourceDesc.setEncoding(testResource.getAttribute("encoding"));
+
+ }
+ catch(IllegalCharsetNameException icne)
+ {
+ throw new XmlPullParserException(testResource.getAttribute("encoding")
+ + " is not a valid encoding.");
+ }
+ catch(UnsupportedCharsetException uce)
+ {
+ throw new XmlPullParserException(testResource.getAttribute("encoding")
+ + " is not a supported encoding.");
+ }
+ }
+
+ if ( testResource.getValue().endsWith(".properties") )
+ {
+ TemplateDescriptor testResourceDesc = descriptor.getTestResourceDescriptor(testResource.getValue());
+
+ testResourceDesc.setEncoding("iso-8859-1");
+ }
+ }
+
+ /**
+ * Adds the site-resource element <code>resource</code> to the list of site-resources in the
+ * <code>descriptor</code> and sets its <code>TemplateDescriptor</code> to
+ * <i>filtered</i> if the attribute <code>filtered</code> was not
+ * specified or its value is <code>"true"</code>, or <code>false</code>
+ * if its value is <code>"false"</code>, and the encoding specified
+ * in the <code>encoding</code> attribute or the Java virtual machine's default if
+ * it is not defined. If the <code>resource</code> is a property file (ends in
+ * <code>.properties</code>) its encoding will be set to <code>iso-8859-1</code>
+ * even if some other encoding is specified in the attribute.
+ *
+ * @param siteResource a <code><resource></code> element from the <code><siteResources></code>
+ * @param descriptor the <code>ArchetypeDescriptor</code> to add the site-resource template to.
+ * @throws XmlPullParserException if the encoding specified is not valid or supported or if the
+ * value of the attribute <code>filtered</code> is no valid.
+ */
+ private static void addSiteResourceToDescriptor( Xpp3Dom siteResource, ArchetypeDescriptor descriptor )
+ throws XmlPullParserException
+ {
+ descriptor.addSiteResource( siteResource.getValue() );
+
+ if ( siteResource.getAttribute("filtered") != null )
+ {
+ TemplateDescriptor siteResourceDesc = descriptor.getSiteResourceDescriptor(siteResource.getValue());
+
+ try
+ {
+ siteResourceDesc.setFiltered(getValueFilteredAttribute(siteResource.getAttribute("filtered")));
+ }
+ catch (IllegalArgumentException iae)
+ {
+ throw new XmlPullParserException(iae.getMessage());
+ }
+ }
+ if ( siteResource.getAttribute("encoding") != null )
+ {
+ TemplateDescriptor siteResourceDesc = descriptor.getSiteResourceDescriptor(siteResource.getValue());
+
+ try
+ {
+ siteResourceDesc.setEncoding(siteResource.getAttribute("encoding"));
+ }
+ catch(IllegalCharsetNameException icne)
+ {
+ throw new XmlPullParserException(siteResource.getAttribute("encoding")
+ + " is not a valid encoding.");
+ }
+ catch(UnsupportedCharsetException uce)
+ {
+ throw new XmlPullParserException(siteResource.getAttribute("encoding")
+ + " is not a supported encoding.");
+ }
+ }
+ if ( siteResource.getValue().endsWith(".properties") )
+ {
+ TemplateDescriptor siteResourceDesc = descriptor.getSiteResourceDescriptor(siteResource.getValue());
+
+ siteResourceDesc.setEncoding("iso-8859-1");
+ }
+ }
+
+ private static boolean getValueFilteredAttribute( String str )
+ throws IllegalArgumentException
+ {
+ boolean ret = false;
+
+ if ( str.equals( "true" ) )
+ {
+ ret = true;
+ }
+ else if (str.equals("false"))
+ {
+ ret = false;
+ }
+ else
+ {
+ throw new IllegalArgumentException(str + " is not an accepted value for the attribute 'filtered'");
+ }
+ return ret;
}
}
Added: maven/archetype/trunk/maven-archetype-core/src/main/java/org/apache/maven/archetype/descriptor/TemplateDescriptor.java
URL: http://svn.apache.org/viewcvs/maven/archetype/trunk/maven-archetype-core/src/main/java/org/apache/maven/archetype/descriptor/TemplateDescriptor.java?rev=390962&view=auto
==============================================================================
--- maven/archetype/trunk/maven-archetype-core/src/main/java/org/apache/maven/archetype/descriptor/TemplateDescriptor.java (added)
+++ maven/archetype/trunk/maven-archetype-core/src/main/java/org/apache/maven/archetype/descriptor/TemplateDescriptor.java Sun Apr 2 23:21:10 2006
@@ -0,0 +1,126 @@
+/*
+ * TemplateDescriptor.java
+ *
+ * Created on March 30, 2006, 1:40 PM
+ *
+ * Copyright 2006 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.maven.archetype.descriptor;
+
+import java.io.OutputStreamWriter;
+import java.nio.charset.Charset;
+import java.nio.charset.IllegalCharsetNameException;
+import java.nio.charset.UnsupportedCharsetException;
+
+/**
+ * Contains the attributes of an archetype's template (either a source or resource file).
+ * The attributes indicate if the template should be filtered and it's encoding.
+ */
+public class TemplateDescriptor
+{
+
+ /**
+ * Determines if the template should be filtered or not.*/
+ private boolean filtered = true;
+
+ /**
+ * Determines the template's encoding. */
+ private String encoding;
+
+ /**
+ * Creates a new instance of <code>TemplateDescriptor<code> that should be filtered
+ * and has the default encoding. */
+ public TemplateDescriptor()
+ {
+ setFiltered( true );
+
+ setEncoding( getDefaultEncoding() );
+ }
+
+ /**
+ * Returns the canonical name of the default character encoding of this Java
+ * virtual machine.
+ *
+ * @return the name of the default character encoding.
+ */
+ private static String getDefaultEncoding()
+ {
+ String name = System.getProperty( "file.encoding" );
+
+ if ( name == null )
+ {
+ OutputStreamWriter out = new OutputStreamWriter( System.out );
+
+ name = out.getEncoding();
+ }
+
+ name = Charset.forName(name).name();
+
+ return name;
+ }
+
+ /**
+ * Returns <code>true</code> if the template should be filtered and
+ * <code>false</code> otherwise.
+ *
+ * @return <code>true</code> if the template should be filtered and
+ * <code>false</code> otherwise.
+ */
+ public boolean isFiltered()
+ {
+ return this.filtered;
+ }
+
+ /**
+ * Defines whether the template should be filtered (processed by Velocity)
+ * or not.
+ *
+ * @param filtered <code>true</code> if it should be processed by Velocity and
+ * <code>fales</code> otherwise.
+ */
+ public void setFiltered( boolean filtered )
+ {
+ this.filtered = filtered;
+ }
+
+ /**
+ * Returns the name of the encoding of the template file (e.g.
+ * <code>us-ascci</code>, <code>utf-8</code>, <code>iso-8859-1</code>).
+ *
+ * @return the name of the encoding of the template file.
+ */
+ public String getEncoding()
+ {
+ return this.encoding;
+ }
+
+ /**
+ * Sets the name of the encoding of the template file.
+ *
+ * @param encoding New value of property encoding.
+ * @throws IllegalCharsetNameException if the given charset name is illegal
+ * @throws UnsupportedCharsetException if no support for the named encoding
+ * is available in this instance of the Java virtual machine
+ */
+ public void setEncoding( String encoding )
+ throws IllegalCharsetNameException, UnsupportedCharsetException
+ {
+ Charset.forName( encoding );
+
+ this.encoding = encoding;
+ }
+
+}
Propchange: maven/archetype/trunk/maven-archetype-core/src/main/java/org/apache/maven/archetype/descriptor/TemplateDescriptor.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: maven/archetype/trunk/maven-archetype-core/src/main/java/org/apache/maven/archetype/descriptor/TemplateDescriptor.java
------------------------------------------------------------------------------
svn:keywords = "Author Date Id Revision"
Modified: maven/archetype/trunk/maven-archetype-core/src/test/java/org/apache/maven/archetype/descriptor/ArchetypeDescriptorBuilderTest.java
URL: http://svn.apache.org/viewcvs/maven/archetype/trunk/maven-archetype-core/src/test/java/org/apache/maven/archetype/descriptor/ArchetypeDescriptorBuilderTest.java?rev=390962&r1=390961&r2=390962&view=diff
==============================================================================
--- maven/archetype/trunk/maven-archetype-core/src/test/java/org/apache/maven/archetype/descriptor/ArchetypeDescriptorBuilderTest.java (original)
+++ maven/archetype/trunk/maven-archetype-core/src/test/java/org/apache/maven/archetype/descriptor/ArchetypeDescriptorBuilderTest.java Sun Apr 2 23:21:10 2006
@@ -16,9 +16,13 @@
* limitations under the License.
*/
+import java.io.IOException;
+import junit.framework.Test;
import junit.framework.TestCase;
import java.io.StringReader;
+import junit.framework.TestSuite;
+import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
/**
* @author <a href="mailto:jason@maven.org">Jason van Zyl</a>
@@ -27,6 +31,17 @@
public class ArchetypeDescriptorBuilderTest
extends TestCase
{
+ public ArchetypeDescriptorBuilderTest(String str) {
+ super(str);
+ }
+
+ public static Test suite() {
+ TestSuite suite= new TestSuite();
+ suite.addTest(new ArchetypeDescriptorBuilderTest("testBuilder"));
+ suite.addTest(new ArchetypeDescriptorBuilderTest("testBuild"));
+ return suite;
+ }
+
public void testBuilder()
throws Exception
{
@@ -46,25 +61,196 @@
assertEquals( 2, descriptor.getSources().size() );
assertEquals( "source0", descriptor.getSources().get( 0 ) );
+
+ assertNotNull( descriptor.getSourceDescriptor("source0") );
+
+ assertEquals( true, descriptor.getSourceDescriptor("source0").isFiltered() );
+
+ assertNotNull( descriptor.getSourceDescriptor("source0").getEncoding() );
assertEquals( "source1", descriptor.getSources().get( 1 ) );
+
+ assertNotNull( descriptor.getSourceDescriptor("source1") );
+
+ assertEquals( true, descriptor.getSourceDescriptor("source1").isFiltered() );
+
+ assertNotNull( descriptor.getSourceDescriptor("source1").getEncoding() );
assertEquals( 2, descriptor.getResources().size() );
assertEquals( "resource0", descriptor.getResources().get( 0 ) );
+
+ assertNotNull( descriptor.getResourceDescriptor("resource0") );
+
+ assertEquals( true, descriptor.getResourceDescriptor("resource0").isFiltered() );
+
+ assertNotNull( descriptor.getResourceDescriptor("resource0").getEncoding() );
assertEquals( "resource1", descriptor.getResources().get( 1 ) );
+
+ assertNotNull( descriptor.getResourceDescriptor("resource1") );
+
+ assertEquals( true, descriptor.getResourceDescriptor("resource1").isFiltered() );
+
+ assertNotNull( descriptor.getResourceDescriptor("resource1").getEncoding() );
assertEquals( 2, descriptor.getTestSources().size() );
assertEquals( "testSource0", descriptor.getTestSources().get( 0 ) );
+
+ assertNotNull( descriptor.getTestSourceDescriptor("testSource0") );
+
+ assertEquals( true, descriptor.getTestSourceDescriptor("testSource0").isFiltered() );
+
+ assertNotNull( descriptor.getTestSourceDescriptor("testSource0").getEncoding() );
assertEquals( "testSource1", descriptor.getTestSources().get( 1 ) );
+
+ assertNotNull( descriptor.getTestSourceDescriptor("testSource1") );
+
+ assertEquals( true, descriptor.getTestSourceDescriptor("testSource1").isFiltered() );
+
+ assertNotNull( descriptor.getTestSourceDescriptor("testSource1").getEncoding() );
assertEquals( 2, descriptor.getTestResources().size() );
assertEquals( "testResource0", descriptor.getTestResources().get( 0 ) );
+
+ assertNotNull( descriptor.getTestResourceDescriptor("testResource0") );
+
+ assertEquals( true, descriptor.getTestResourceDescriptor("testResource0").isFiltered() );
+
+ assertNotNull( descriptor.getTestResourceDescriptor("testResource0").getEncoding() );
assertEquals( "testResource1", descriptor.getTestResources().get( 1 ) );
+
+ assertNotNull( descriptor.getTestResourceDescriptor("testResource1") );
+
+ assertEquals( true, descriptor.getTestResourceDescriptor("testResource1").isFiltered() );
+
+ assertNotNull( descriptor.getTestResourceDescriptor("testResource1").getEncoding() );
+ }
+
+ public void testBuild() throws IOException, XmlPullParserException
+ {
+ String xml = "<archetype>"
+ + " <id>standard</id>"
+ + " <sources>"
+ + " <source encoding=\"utf-8\">source0</source>"
+ + " <source encoding=\"utf-8\">source1</source>"
+ + " </sources>"
+ + " <resources>"
+ + " <resource filtered=\"false\">resource0</resource>"
+ + " <resource encoding=\"iso-8859-1\">resource1</resource>"
+ + " </resources>"
+ + " <testSources>"
+ + " <source encoding=\"utf-8\">testSource0</source>"
+ + " <source encoding=\"utf-8\">testSource1</source>"
+ + " </testSources>"
+ + " <testResources>"
+ + " <resource encoding=\"us-ascii\">testResource0</resource>"
+ + " <resource filtered=\"false\">testResource1</resource>"
+ + " </testResources>"
+ + " <siteResources>"
+ + " <resource filtered=\"false\">siteResource0</resource>"
+ + " <resource encoding=\"utf-16\">siteResource1</resource>"
+ + " </siteResources>"
+ + "</archetype>";
+
+ ArchetypeDescriptorBuilder builder = new ArchetypeDescriptorBuilder();
+
+ ArchetypeDescriptor descriptor = builder.build( new StringReader( xml ) );
+
+ assertEquals( "standard", descriptor.getId() );
+
+ assertEquals( 2, descriptor.getSources().size() );
+
+ assertEquals( "source0", descriptor.getSources().get( 0 ) );
+
+ assertNotNull( descriptor.getSourceDescriptor("source0") );
+
+ assertEquals( true, descriptor.getSourceDescriptor("source0").isFiltered() );
+
+ assertEquals( "utf-8", descriptor.getSourceDescriptor("source0").getEncoding() );
+
+ assertEquals( "source1", descriptor.getSources().get( 1 ) );
+
+ assertNotNull( descriptor.getSourceDescriptor("source1") );
+
+ assertEquals( true, descriptor.getSourceDescriptor("source1").isFiltered() );
+
+ assertEquals( "utf-8", descriptor.getSourceDescriptor("source1").getEncoding() );
+
+ assertEquals( 2, descriptor.getResources().size() );
+
+ assertEquals( "resource0", descriptor.getResources().get( 0 ) );
+
+ assertNotNull( descriptor.getResourceDescriptor("resource0") );
+
+ assertEquals( false, descriptor.getResourceDescriptor("resource0").isFiltered() );
+
+ assertNotNull( descriptor.getResourceDescriptor("resource0").getEncoding() );
+
+ assertEquals( "resource1", descriptor.getResources().get( 1 ) );
+
+ assertNotNull( descriptor.getResourceDescriptor("resource1") );
+
+ assertEquals( true, descriptor.getResourceDescriptor("resource1").isFiltered() );
+
+ assertEquals( "iso-8859-1", descriptor.getResourceDescriptor("resource1").getEncoding() );
+
+ assertEquals( 2, descriptor.getTestSources().size() );
+
+ assertEquals( "testSource0", descriptor.getTestSources().get( 0 ) );
+
+ assertNotNull( descriptor.getTestSourceDescriptor("testSource0") );
+
+ assertEquals( true, descriptor.getTestSourceDescriptor("testSource0").isFiltered() );
+
+ assertEquals( "utf-8", descriptor.getTestSourceDescriptor("testSource0").getEncoding() );
+
+ assertEquals( "testSource1", descriptor.getTestSources().get( 1 ) );
+
+ assertNotNull( descriptor.getTestSourceDescriptor("testSource1") );
+
+ assertEquals( true, descriptor.getTestSourceDescriptor("testSource1").isFiltered() );
+
+ assertEquals( "utf-8", descriptor.getTestSourceDescriptor("testSource1").getEncoding() );
+
+ assertEquals( 2, descriptor.getTestResources().size() );
+
+ assertEquals( "testResource0", descriptor.getTestResources().get( 0 ) );
+
+ assertNotNull(descriptor.getTestResourceDescriptor("testResource0"));
+
+ assertEquals( true, descriptor.getTestResourceDescriptor("testResource0").isFiltered() );
+
+ assertEquals( "us-ascii", descriptor.getTestResourceDescriptor("testResource0").getEncoding() );
+
+ assertEquals( "testResource1", descriptor.getTestResources().get( 1 ) );
+
+ assertNotNull(descriptor.getTestResourceDescriptor("testResource1"));
+
+ assertEquals( false, descriptor.getTestResourceDescriptor("testResource1").isFiltered() );
+
+ assertNotNull(descriptor.getTestResourceDescriptor("testResource1").getEncoding() );
+
+ assertEquals( 2, descriptor.getSiteResources().size() );
+
+ assertEquals( "siteResource0", descriptor.getSiteResources().get( 0 ) );
+
+ assertNotNull(descriptor.getSiteResourceDescriptor("siteResource0"));
+
+ assertEquals( false, descriptor.getSiteResourceDescriptor("siteResource0").isFiltered() );
+
+ assertNotNull( descriptor.getSiteResourceDescriptor("siteResource0").getEncoding() );
+
+ assertEquals( "siteResource1", descriptor.getSiteResources().get( 1 ) );
+
+ assertNotNull(descriptor.getSiteResourceDescriptor("siteResource1"));
+
+ assertEquals( true, descriptor.getSiteResourceDescriptor("siteResource1").isFiltered() );
+
+ assertEquals( "utf-16", descriptor.getSiteResourceDescriptor("siteResource1").getEncoding());
}
}