You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by jv...@apache.org on 2007/07/06 18:35:16 UTC
svn commit: r553946 [7/19] - in /maven/archetype/branches/maven-archetypeng:
./ maven-archetypeng-bundles/
maven-archetypeng-bundles/maven-archetypeng-archetype/
maven-archetypeng-bundles/maven-archetypeng-archetype/src/
maven-archetypeng-bundles/maven...
Added: maven/archetype/branches/maven-archetypeng/maven-archetypeng-core/maven-archetypeng-common/src/main/java/org/codehaus/mojo/archetypeng/DefaultArchetypeFilesResolver.java
URL: http://svn.apache.org/viewvc/maven/archetype/branches/maven-archetypeng/maven-archetypeng-core/maven-archetypeng-common/src/main/java/org/codehaus/mojo/archetypeng/DefaultArchetypeFilesResolver.java?view=auto&rev=553946
==============================================================================
--- maven/archetype/branches/maven-archetypeng/maven-archetypeng-core/maven-archetypeng-common/src/main/java/org/codehaus/mojo/archetypeng/DefaultArchetypeFilesResolver.java (added)
+++ maven/archetype/branches/maven-archetypeng/maven-archetypeng-core/maven-archetypeng-common/src/main/java/org/codehaus/mojo/archetypeng/DefaultArchetypeFilesResolver.java Fri Jul 6 09:34:35 2007
@@ -0,0 +1,471 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.codehaus.mojo.archetypeng;
+
+import java.util.Arrays;
+import org.codehaus.mojo.archetypeng.archetype.filesets.FileSet;
+
+import org.codehaus.plexus.logging.AbstractLogEnabled;
+import org.codehaus.plexus.util.FileUtils;
+import org.codehaus.plexus.util.StringUtils;
+
+import java.io.File;
+import java.io.IOException;
+
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Set;
+
+/**
+ * @plexus.component
+ */
+public class DefaultArchetypeFilesResolver
+extends AbstractLogEnabled
+implements ArchetypeFilesResolver
+{
+ public List getFilesWithExtension ( List files, String extension )
+ {
+ ListScanner scanner = new ListScanner ();
+ scanner.setBasedir ( "" );
+
+ scanner.setIncludes ( "**/*." + extension );
+ scanner.setExcludes ( "" );
+
+ return scanner.scan ( files );
+ }
+
+ public List getFilteredFiles ( List files, String filtered )
+ {
+ ListScanner scanner = new ListScanner ();
+ scanner.setBasedir ( "" );
+
+ scanner.setIncludes ( filtered );
+ scanner.setExcludes ( "" );
+
+ List result = scanner.scan ( files );
+ getLogger ().debug (
+ "Scanned " + result.size () + " filtered files in " + files.size () + " files"
+ );
+
+ return result;
+ }
+
+ public List filterFiles ( FileSet fileSet, List archetypeResources )
+ {
+ ListScanner scanner = new ListScanner ();
+ scanner.setBasedir ( fileSet.getDirectory () );
+ scanner.setIncludes ( fileSet.getIncludes () );
+ scanner.setExcludes ( fileSet.getExcludes () );
+ scanner.setCaseSensitive ( true );
+
+ return scanner.scan ( archetypeResources );
+ }
+
+ public List filterFiles ( String moduleOffset, FileSet fileSet, List archetypeResources )
+ {
+ ListScanner scanner = new ListScanner ();
+ scanner.setBasedir (
+ ( StringUtils.isEmpty ( moduleOffset ) ? "" : ( moduleOffset + "/" ) )
+ + fileSet.getDirectory ()
+ );
+ scanner.setIncludes ( fileSet.getIncludes () );
+ scanner.setExcludes ( fileSet.getExcludes () );
+ scanner.setCaseSensitive ( true );
+
+ return scanner.scan ( archetypeResources );
+ }
+
+ public List findOtherResources ( int level, List files, String languages )
+ {
+ ListScanner scanner = new ListScanner ();
+
+ scanner.setBasedir ( "" );
+
+ String includes = "";
+ for ( int i = 0; i < level; i++ )
+ {
+ includes += "*/";
+ }
+
+ scanner.setIncludes ( includes + "**" );
+ scanner.setExcludes ( languages );
+
+ List result = scanner.scan ( files );
+ getLogger ().debug (
+ "Scanned " + result.size () + " other resources in " + files.size ()
+ + " files at level " + level
+ );
+
+ return result;
+ }
+
+ public List findOtherResources ( int level, List files, List sourcesFiles, String languages )
+ {
+ ListScanner scanner = new ListScanner ();
+
+ scanner.setBasedir ( "" );
+
+ Set selectedDirectories = new HashSet ();
+
+ List includes = new ArrayList ();
+
+ Iterator sourcesFilesIterator = sourcesFiles.iterator ();
+ while ( sourcesFilesIterator.hasNext () )
+ {
+ String sourcesFile = (String) sourcesFilesIterator.next ();
+
+ String directory = PathUtils.getDirectory ( sourcesFile, level - 1 );
+ if ( !selectedDirectories.contains ( directory ) )
+ {
+ includes.add ( directory + "/**" );
+ }
+
+ selectedDirectories.add ( directory );
+ }
+
+ scanner.setExcludes ( languages );
+
+ List result = scanner.scan ( files );
+ getLogger ().debug (
+ "Scanned " + result.size () + " other resources in " + files.size ()
+ + " files at level " + level
+ );
+
+ return result;
+ }
+
+ public List findOtherSources ( int level, List files, String languages )
+ {
+ ListScanner scanner = new ListScanner ();
+ scanner.setBasedir ( "" );
+
+ String levelDirectory = "";
+ for ( int i = 0; i < ( level - 1 ); i++ )
+ {
+ levelDirectory += "*/";
+ }
+
+ String includes = "";
+ String[] languagesAsArray = StringUtils.split ( languages );
+ for ( int i = 0; i < languagesAsArray.length; i++ )
+ {
+ includes += levelDirectory + languagesAsArray[i];
+ }
+
+ scanner.setIncludes ( includes );
+
+ List result = scanner.scan ( files );
+ getLogger ().debug (
+ "Scanned " + result.size () + " other sources in " + files.size () + " files at level "
+ + level
+ );
+
+ return result;
+ }
+
+ public List findResourcesMainFiles ( List files, String languages )
+ {
+ ListScanner scanner = new ListScanner ();
+ scanner.setBasedir ( "src/main" );
+
+ scanner.setIncludes ( "**" );
+ scanner.setExcludes ( languages );
+
+ List result = scanner.scan ( files );
+ getLogger ().debug (
+ "Scanned " + result.size () + " resources in " + files.size () + " files"
+ );
+
+ return result;
+ }
+
+ public List findResourcesTestFiles ( List files, String languages )
+ {
+ ListScanner scanner = new ListScanner ();
+ scanner.setBasedir ( "src/test" );
+
+ scanner.setIncludes ( "**" );
+ scanner.setExcludes ( languages );
+
+ List result = scanner.scan ( files );
+ getLogger ().debug (
+ "Scanned " + result.size () + " test resources in " + files.size () + " files"
+ );
+
+ return result;
+ }
+
+ public List findSiteFiles ( List files, String languages )
+ {
+ ListScanner scanner = new ListScanner ();
+ scanner.setBasedir ( "src/site" );
+
+ scanner.setIncludes ( "**" );
+ scanner.setExcludes ( languages );
+
+ List result = scanner.scan ( files );
+ getLogger ().debug (
+ "Scanned " + result.size () + " site resources in " + files.size () + " files"
+ );
+
+ return result;
+ }
+
+ public List findSourcesMainFiles ( List files, String languages )
+ {
+ ListScanner scanner = new ListScanner ();
+ scanner.setBasedir ( "src/main" );
+
+ scanner.setIncludes ( languages );
+
+ List result = scanner.scan ( files );
+ getLogger ().debug (
+ "Scanned " + result.size () + " sources in " + files.size () + " files"
+ );
+
+ return result;
+ }
+
+ public List findSourcesTestFiles ( List files, String languages )
+ {
+ ListScanner scanner = new ListScanner ();
+ scanner.setBasedir ( "src/test" );
+
+ scanner.setIncludes ( languages );
+
+ List result = scanner.scan ( files );
+ getLogger ().debug (
+ "Scanned " + result.size () + " test sources in " + files.size () + " files"
+ );
+
+ return result;
+ }
+
+ public List getPackagedFiles ( List files, String packageName )
+ {
+ List packagedFiles = new ArrayList ();
+ Iterator filesIterator = files.iterator ();
+ while ( filesIterator.hasNext () )
+ {
+ String file = (String) filesIterator.next ();
+
+ if ( file.startsWith ( packageName ) )
+ {
+ packagedFiles.add ( file.substring ( packageName.length () + 1 ) );
+ }
+ }
+ getLogger ().debug (
+ "Scanned " + packagedFiles.size () + " pachaged files in " + files.size () + " files"
+ );
+ return packagedFiles;
+ }
+
+ public String resolvePackage ( File basedir, List languages )
+ throws IOException
+ {
+ getLogger ().debug ( "Resolving package in "+basedir+" using languages "+languages );
+
+ List files = resolveFiles ( basedir, languages );
+
+ return resolvePackage ( files );
+ }
+
+ public List getUnfilteredFiles ( List files, String filtered )
+ {
+ ListScanner scanner = new ListScanner ();
+ scanner.setBasedir ( "" );
+
+ scanner.setIncludes ( "**" );
+ scanner.setExcludes ( filtered );
+
+ List result = scanner.scan ( files );
+ getLogger ().debug (
+ "Scanned " + result.size () + " unfiltered files in " + files.size () + " files"
+ );
+
+ return result;
+ }
+
+ public List getUnpackagedFiles ( List files, String packageName )
+ {
+ List unpackagedFiles = new ArrayList ();
+ Iterator filesIterator = files.iterator ();
+ while ( filesIterator.hasNext () )
+ {
+ String file = (String) filesIterator.next ();
+
+ if ( !file.startsWith ( packageName ) )
+ {
+ unpackagedFiles.add ( file );
+ }
+ }
+ getLogger ().debug (
+ "Scanned " + unpackagedFiles.size () + " unpachaged files in " + files.size ()
+ + " files"
+ );
+ return unpackagedFiles;
+ }
+
+ private String getCommonPackage ( String packageName, String templatePackage )
+ {
+ String common = "";
+
+ String difference = StringUtils.difference ( packageName, templatePackage );
+ if ( StringUtils.isNotEmpty ( difference ) )
+ {
+ String temporaryCommon =
+ StringUtils.substring (
+ templatePackage,
+ 0,
+ templatePackage.lastIndexOf ( difference )
+ );
+ if ( !difference.startsWith ( "." ) )
+ {
+ common =
+ StringUtils.substring (
+ temporaryCommon,
+ 0,
+ temporaryCommon.lastIndexOf ( "." )
+ );
+ }
+ else
+ {
+ common = temporaryCommon;
+ }
+ }
+ else
+ {
+ common = packageName;
+ }
+
+ return common;
+ }
+
+ private List resolveFiles ( File basedir, List languages )
+ throws IOException
+ {
+ String[] languagesArray = (String[]) languages.toArray ( new String[languages.size ()] );
+ String[] languagesPathesArray = new String[languagesArray.length];
+ for ( int i = 0; i < languagesArray.length; i++ )
+ {
+ languagesPathesArray[i] = "**/src/**/" + languagesArray[i] + "/**";
+ }
+
+ String excludes = "target";
+ Iterator defaultExcludes = Arrays.asList ( ListScanner.DEFAULTEXCLUDES ).iterator ();
+ while ( defaultExcludes.hasNext () )
+ {
+ excludes += "," + (String) defaultExcludes.next () + "/**";
+ }
+
+ List absoluteFiles =
+ FileUtils.getFiles (
+ basedir,
+ StringUtils.join ( languagesPathesArray, "," ),
+ excludes
+ );
+
+ getLogger ().debug ( "Found " + absoluteFiles.size () + " potential archetype files" );
+
+ List files = new ArrayList ( absoluteFiles.size () );
+
+ Iterator filesIterator = absoluteFiles.iterator ();
+ while ( filesIterator.hasNext () )
+ {
+ File file = (File) filesIterator.next ();
+ String filePath =
+ StringUtils.prechomp (
+ file.getAbsolutePath (),
+ basedir.getAbsolutePath () + File.separator
+ );
+
+ String minusSrc = StringUtils.prechomp ( filePath, "src" + File.separator );
+
+ for ( int i = 0; i < languagesArray.length; i++ )
+ {
+ String language = languagesArray[i];
+
+ if ( StringUtils.countMatches (
+ minusSrc,
+ File.separator + language + File.separator
+ )
+ > 0
+ )
+ {
+ String minusLanguage =
+ StringUtils.prechomp ( minusSrc, language + File.separator );
+
+ files.add ( toUnixPath ( minusLanguage ) );
+ }
+ }
+ }
+
+ getLogger ().debug (
+ "Found " + files.size () + " archetype files for package resolution "
+ );
+
+ return files;
+ }
+
+ private String resolvePackage ( List files )
+ {
+ String packageName = null;
+ Iterator minusLanguageIterator = files.iterator ();
+ while ( minusLanguageIterator.hasNext () )
+ {
+ String minusLanguage = (String) minusLanguageIterator.next ();
+
+ String filePackage;
+ if ( minusLanguage.indexOf( "/" ) >= 0 )
+ {
+ filePackage =
+ StringUtils.replace (
+ minusLanguage.substring ( 0, minusLanguage.lastIndexOf ( "/" ) ),
+ "/",
+ "."
+ );
+ }
+ else
+ {
+ filePackage = "";
+ }
+
+ if ( packageName == null )
+ {
+ packageName = filePackage;
+ }
+ else
+ {
+ packageName = getCommonPackage ( packageName, filePackage );
+ }
+ }
+
+ getLogger ().debug ( "Package resolved to " + packageName );
+
+ return packageName;
+ }
+
+ private String toUnixPath ( String path )
+ {
+ return path.replace ( File.separatorChar, '/' );
+ }
+}
Propchange: maven/archetype/branches/maven-archetypeng/maven-archetypeng-core/maven-archetypeng-common/src/main/java/org/codehaus/mojo/archetypeng/DefaultArchetypeFilesResolver.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: maven/archetype/branches/maven-archetypeng/maven-archetypeng-core/maven-archetypeng-common/src/main/java/org/codehaus/mojo/archetypeng/DefaultArchetypeFilesResolver.java
------------------------------------------------------------------------------
svn:keywords = "Author Date Id Revision"
Added: maven/archetype/branches/maven-archetypeng/maven-archetypeng-core/maven-archetypeng-common/src/main/java/org/codehaus/mojo/archetypeng/DefaultArchetypePropertiesManager.java
URL: http://svn.apache.org/viewvc/maven/archetype/branches/maven-archetypeng/maven-archetypeng-core/maven-archetypeng-common/src/main/java/org/codehaus/mojo/archetypeng/DefaultArchetypePropertiesManager.java?view=auto&rev=553946
==============================================================================
--- maven/archetype/branches/maven-archetypeng/maven-archetypeng-core/maven-archetypeng-common/src/main/java/org/codehaus/mojo/archetypeng/DefaultArchetypePropertiesManager.java (added)
+++ maven/archetype/branches/maven-archetypeng/maven-archetypeng-core/maven-archetypeng-common/src/main/java/org/codehaus/mojo/archetypeng/DefaultArchetypePropertiesManager.java Fri Jul 6 09:34:35 2007
@@ -0,0 +1,97 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.codehaus.mojo.archetypeng;
+
+import org.codehaus.plexus.logging.AbstractLogEnabled;
+import org.codehaus.plexus.util.IOUtil;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+
+import java.util.Iterator;
+import java.util.Properties;
+
+/**
+ * @plexus.component
+ */
+public class DefaultArchetypePropertiesManager
+extends AbstractLogEnabled
+implements ArchetypePropertiesManager
+{
+ public void readProperties ( Properties properties, File propertyFile )
+ throws FileNotFoundException, IOException
+ {
+ getLogger ().debug ( "Reading property file " + propertyFile );
+
+ InputStream is = new FileInputStream ( propertyFile );
+
+ try
+ {
+ properties.load ( is );
+
+ getLogger ().debug ( "Read " + properties.size() + " properties" );
+ }
+ finally
+ {
+ IOUtil.close ( is );
+ }
+ }
+
+ public void writeProperties ( Properties properties, File propertyFile )
+ throws IOException
+ {
+ Properties storedProperties = new Properties ();
+ try
+ {
+ readProperties ( storedProperties, propertyFile );
+ }
+ catch ( FileNotFoundException ex )
+ {
+ getLogger ().debug ( "Property file not found. Creating a new one" );
+ }
+
+ getLogger ().debug ( "Adding " + properties.size() + " properties" );
+
+ Iterator propertiesIterator = properties.keySet ().iterator ();
+ while ( propertiesIterator.hasNext () )
+ {
+ String propertyKey = (String) propertiesIterator.next ();
+ storedProperties.setProperty ( propertyKey, properties.getProperty ( propertyKey ) );
+ }
+
+ OutputStream os = new FileOutputStream ( propertyFile );
+
+ try
+ {
+ storedProperties.store ( os, "" );
+
+ getLogger ().debug ( "Stored " + storedProperties.size() + " properties" );
+ }
+ finally
+ {
+ IOUtil.close ( os );
+ }
+ }
+}
Propchange: maven/archetype/branches/maven-archetypeng/maven-archetypeng-core/maven-archetypeng-common/src/main/java/org/codehaus/mojo/archetypeng/DefaultArchetypePropertiesManager.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: maven/archetype/branches/maven-archetypeng/maven-archetypeng-core/maven-archetypeng-common/src/main/java/org/codehaus/mojo/archetypeng/DefaultArchetypePropertiesManager.java
------------------------------------------------------------------------------
svn:keywords = "Author Date Id Revision"
Added: maven/archetype/branches/maven-archetypeng/maven-archetypeng-core/maven-archetypeng-common/src/main/java/org/codehaus/mojo/archetypeng/DefaultArchetypeRegistryManager.java
URL: http://svn.apache.org/viewvc/maven/archetype/branches/maven-archetypeng/maven-archetypeng-core/maven-archetypeng-common/src/main/java/org/codehaus/mojo/archetypeng/DefaultArchetypeRegistryManager.java?view=auto&rev=553946
==============================================================================
--- maven/archetype/branches/maven-archetypeng/maven-archetypeng-core/maven-archetypeng-common/src/main/java/org/codehaus/mojo/archetypeng/DefaultArchetypeRegistryManager.java (added)
+++ maven/archetype/branches/maven-archetypeng/maven-archetypeng-core/maven-archetypeng-common/src/main/java/org/codehaus/mojo/archetypeng/DefaultArchetypeRegistryManager.java Fri Jul 6 09:34:35 2007
@@ -0,0 +1,269 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.codehaus.mojo.archetypeng;
+
+import org.apache.maven.artifact.repository.ArtifactRepository;
+import org.apache.maven.artifact.repository.ArtifactRepositoryFactory;
+import org.apache.maven.artifact.repository.ArtifactRepositoryPolicy;
+import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
+
+import org.codehaus.mojo.archetypeng.registry.ArchetypeRegistry;
+import org.codehaus.mojo.archetypeng.registry.ArchetypeRepository;
+import org.codehaus.mojo.archetypeng.registry.io.xpp3.ArchetypeRegistryXpp3Reader;
+import org.codehaus.mojo.archetypeng.registry.io.xpp3.ArchetypeRegistryXpp3Writer;
+
+import org.codehaus.plexus.logging.AbstractLogEnabled;
+import org.codehaus.plexus.util.IOUtil;
+import org.codehaus.plexus.util.StringUtils;
+import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
+
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.FileReader;
+import java.io.FileWriter;
+import java.io.IOException;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Iterator;
+import java.util.List;
+
+/**
+ * @plexus.component
+ */
+public class DefaultArchetypeRegistryManager
+extends AbstractLogEnabled
+implements ArchetypeRegistryManager
+{
+ /**
+ * Used to create ArtifactRepository objects given the urls of the remote repositories.
+ *
+ * @plexus.requirement
+ */
+ private ArtifactRepositoryFactory artifactRepositoryFactory;
+
+ /**
+ * Determines whether the layout is legacy or not.
+ *
+ * @plexus.requirement roleHint="default"
+ */
+ private ArtifactRepositoryLayout defaultArtifactRepositoryLayout;
+
+ public List getArchetypeGroups ( File archetypeRegistryFile )
+ {
+ try
+ {
+ ArchetypeRegistry registry = readArchetypeRegistry ( archetypeRegistryFile );
+ return registry.getArchetypeGroups ();
+ }
+ catch ( IOException e )
+ {
+ getLogger ().warn ( "Can not read ~/m2/archetype.xml" );
+ return
+ Arrays.asList (
+ new String[] { "org.apache.maven.archetypes", "org.codehaus.mojo.archetypes" }
+ );
+ }
+ catch ( XmlPullParserException e )
+ {
+ getLogger ().warn ( "Can not read ~/m2/archetype.xml" );
+ return
+ Arrays.asList (
+ new String[] { "org.apache.maven.archetypes", "org.codehaus.mojo.archetypes" }
+ );
+ }
+ }
+
+ public ArchetypeRegistry readArchetypeRegistry ( File archetypeRegistryFile )
+ throws IOException, FileNotFoundException, XmlPullParserException
+ {
+ ArchetypeRegistryXpp3Reader reader = new ArchetypeRegistryXpp3Reader ();
+ FileReader fileReader = new FileReader ( archetypeRegistryFile );
+
+ try
+ {
+ return reader.read ( fileReader );
+ }
+ finally
+ {
+ IOUtil.close ( fileReader );
+ }
+ }
+
+ public List getRepositories (
+ List pomRemoteRepositories,
+ String remoteRepositories,
+ File archetypeRegistryFile
+ )
+ throws IOException, XmlPullParserException
+ {
+ List archetypeRemoteRepositories = new ArrayList ( pomRemoteRepositories );
+
+ ArchetypeRegistry registry = readArchetypeRegistry ( archetypeRegistryFile );
+ if ( !registry.getArchetypeRepositories ().isEmpty () )
+ {
+ archetypeRemoteRepositories = new ArrayList ();
+
+ Iterator repositories = registry.getArchetypeRepositories ().iterator ();
+ while ( repositories.hasNext () )
+ {
+ ArchetypeRepository repository = (ArchetypeRepository) repositories.next ();
+
+ archetypeRemoteRepositories.add (
+ createRepository ( repository.getUrl (), repository.getId () )
+ );
+ }
+ }
+
+ if ( remoteRepositories != null )
+ {
+ archetypeRemoteRepositories = new ArrayList ();
+
+ String[] s = StringUtils.split ( remoteRepositories, "," );
+
+ for ( int i = 0; i < s.length; i++ )
+ {
+ archetypeRemoteRepositories.add ( createRepository ( s[i], "id" + i ) );
+ }
+ }
+
+ return archetypeRemoteRepositories;
+ }
+
+ public void writeArchetypeRegistry (
+ File archetypeRegistryFile,
+ ArchetypeRegistry archetypeRegistry
+ )
+ throws IOException
+ {
+ ArchetypeRegistryXpp3Writer writer = new ArchetypeRegistryXpp3Writer ();
+ FileWriter fileWriter = new FileWriter ( archetypeRegistryFile );
+
+ try
+ {
+ writer.write ( fileWriter, archetypeRegistry );
+ }
+ finally
+ {
+ IOUtil.close ( fileWriter );
+ }
+ }
+
+ /**
+ * Code stealed from MavenArchetypeMojo
+ * (org.apache.maven.plugins:maven-archetype-plugin:1.0-alpha4).
+ */
+ private ArtifactRepository createRepository ( String url, String repositoryId )
+ {
+ // snapshots vs releases
+ // offline = to turning the update policy off
+
+ // TODO: we'll need to allow finer grained creation of repositories but this will do for now
+
+ String updatePolicyFlag = ArtifactRepositoryPolicy.UPDATE_POLICY_ALWAYS;
+
+ String checksumPolicyFlag = ArtifactRepositoryPolicy.CHECKSUM_POLICY_WARN;
+
+ ArtifactRepositoryPolicy snapshotsPolicy =
+ new ArtifactRepositoryPolicy ( true, updatePolicyFlag, checksumPolicyFlag );
+
+ ArtifactRepositoryPolicy releasesPolicy =
+ new ArtifactRepositoryPolicy ( true, updatePolicyFlag, checksumPolicyFlag );
+
+ return
+ artifactRepositoryFactory.createArtifactRepository (
+ repositoryId,
+ url,
+ defaultArtifactRepositoryLayout,
+ snapshotsPolicy,
+ releasesPolicy
+ );
+ }
+
+ public List getLanguages ( String archetypeLanguages, File archetypeRegistryFile )
+ throws IOException
+ {
+ List languages = new ArrayList ();
+
+ if (StringUtils.isNotEmpty(archetypeLanguages))
+ {
+ languages.addAll ( Arrays.asList ( StringUtils.split ( archetypeLanguages, "," ) ) );
+ }
+
+ try
+ {
+ ArchetypeRegistry registry = readArchetypeRegistry ( archetypeRegistryFile );
+
+ languages.addAll ( registry.getLanguages () );
+ }
+ catch ( IOException e )
+ {
+ getLogger ().warn ( "Can not read ~/m2/archetype.xml" );
+ }
+ catch ( XmlPullParserException e )
+ {
+ getLogger ().warn ( "Can not read ~/m2/archetype.xml" );
+ }
+
+ if (languages.isEmpty())
+ {
+ languages.addAll (Constants.DEFAULT_LANGUAGES);
+ }
+
+ return languages;
+ }
+
+ public List getFilteredExtensions (
+ String archetypeFilteredExtentions,
+ File archetypeRegistryFile
+ )
+ throws IOException
+ {
+ List filteredExtensions = new ArrayList ();
+
+ if (StringUtils.isNotEmpty(archetypeFilteredExtentions))
+ {
+ filteredExtensions.addAll ( Arrays.asList ( StringUtils.split ( archetypeFilteredExtentions, "," ) ) );
+ }
+
+
+ try
+ {
+ ArchetypeRegistry registry = readArchetypeRegistry ( archetypeRegistryFile );
+
+ filteredExtensions.addAll ( registry.getFilteredExtensions () );
+ }
+ catch ( IOException e )
+ {
+ getLogger ().warn ( "Can not read ~/m2/archetype.xml" );
+ }
+ catch ( XmlPullParserException e )
+ {
+ getLogger ().warn ( "Can not read ~/m2/archetype.xml" );
+ }
+
+ if(filteredExtensions.isEmpty())
+ {
+ filteredExtensions.addAll(Constants.DEFAULT_FILTERED_EXTENSIONS);
+ }
+
+ return filteredExtensions;
+ }
+}
Propchange: maven/archetype/branches/maven-archetypeng/maven-archetypeng-core/maven-archetypeng-common/src/main/java/org/codehaus/mojo/archetypeng/DefaultArchetypeRegistryManager.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: maven/archetype/branches/maven-archetypeng/maven-archetypeng-core/maven-archetypeng-common/src/main/java/org/codehaus/mojo/archetypeng/DefaultArchetypeRegistryManager.java
------------------------------------------------------------------------------
svn:keywords = "Author Date Id Revision"
Added: maven/archetype/branches/maven-archetypeng/maven-archetypeng-core/maven-archetypeng-common/src/main/java/org/codehaus/mojo/archetypeng/DefaultPomManager.java
URL: http://svn.apache.org/viewvc/maven/archetype/branches/maven-archetypeng/maven-archetypeng-core/maven-archetypeng-common/src/main/java/org/codehaus/mojo/archetypeng/DefaultPomManager.java?view=auto&rev=553946
==============================================================================
--- maven/archetype/branches/maven-archetypeng/maven-archetypeng-core/maven-archetypeng-common/src/main/java/org/codehaus/mojo/archetypeng/DefaultPomManager.java (added)
+++ maven/archetype/branches/maven-archetypeng/maven-archetypeng-core/maven-archetypeng-common/src/main/java/org/codehaus/mojo/archetypeng/DefaultPomManager.java Fri Jul 6 09:34:35 2007
@@ -0,0 +1,453 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.codehaus.mojo.archetypeng;
+
+import org.apache.maven.model.Build;
+import org.apache.maven.model.Dependency;
+import org.apache.maven.model.Model;
+import org.apache.maven.model.Parent;
+import org.apache.maven.model.Plugin;
+import org.apache.maven.model.ReportPlugin;
+import org.apache.maven.model.Reporting;
+import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
+import org.apache.maven.model.io.xpp3.MavenXpp3Writer;
+
+import org.codehaus.mojo.archetypeng.exception.InvalidPackaging;
+
+import org.codehaus.plexus.logging.AbstractLogEnabled;
+import org.codehaus.plexus.util.FileUtils;
+import org.codehaus.plexus.util.IOUtil;
+import org.codehaus.plexus.util.StringUtils;
+import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
+
+import org.dom4j.Document;
+import org.dom4j.DocumentException;
+import org.dom4j.Element;
+import org.dom4j.Node;
+
+import org.dom4j.io.SAXReader;
+import org.dom4j.io.XMLWriter;
+
+import org.jdom.JDOMException;
+
+import org.jdom.input.SAXBuilder;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.FileReader;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.OutputStreamWriter;
+import java.io.Reader;
+import java.io.StringWriter;
+import java.io.Writer;
+
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * @plexus.component
+ */
+public class DefaultPomManager
+extends AbstractLogEnabled
+implements PomManager
+{
+ public void addModule ( File pom, String artifactId )
+ throws FileNotFoundException,
+ IOException,
+ XmlPullParserException,
+ DocumentException,
+ InvalidPackaging
+ {
+ boolean found = false;
+
+ StringWriter writer = new StringWriter ();
+ Reader fileReader = new FileReader ( pom );
+
+ try
+ {
+ fileReader = new FileReader ( pom );
+
+ SAXReader reader = new SAXReader ();
+ Document document = reader.read ( fileReader );
+ Element project = document.getRootElement ();
+
+ String packaging = null;
+ Element packagingElement = project.element ( "packaging" );
+ if ( packagingElement != null )
+ {
+ packaging = packagingElement.getStringValue ();
+ }
+ if ( !"pom".equals ( packaging ) )
+ {
+ throw new InvalidPackaging (
+ "Unable to add module to the current project as it is not of packaging type 'pom'"
+ );
+ }
+
+ Element modules = project.element ( "modules" );
+ if ( modules == null )
+ {
+ modules = project.addText ( " " ).addElement ( "modules" );
+ modules.setText ( "\n " );
+ project.addText ( "\n" );
+ }
+ // TODO: change to while loop
+ for ( Iterator i = modules.elementIterator ( "module" ); i.hasNext () && !found; )
+ {
+ Element module = (Element) i.next ();
+ if ( module.getText ().equals ( artifactId ) )
+ {
+ found = true;
+ }
+ }
+ if ( !found )
+ {
+ Node lastTextNode = null;
+ for ( Iterator i = modules.nodeIterator (); i.hasNext (); )
+ {
+ Node node = (Node) i.next ();
+ if ( node.getNodeType () == Node.ELEMENT_NODE )
+ {
+ lastTextNode = null;
+ }
+ else if ( node.getNodeType () == Node.TEXT_NODE )
+ {
+ lastTextNode = node;
+ }
+ }
+
+ if ( lastTextNode != null )
+ {
+ modules.remove ( lastTextNode );
+ }
+
+ modules.addText ( "\n " );
+ modules.addElement ( "module" ).setText ( artifactId );
+ modules.addText ( "\n " );
+
+ XMLWriter xmlWriter = new XMLWriter ( writer );
+ xmlWriter.write ( document );
+
+ FileUtils.fileWrite ( pom.getAbsolutePath (), writer.toString () );
+ } // end if
+ }
+ finally
+ {
+ IOUtil.close ( fileReader );
+ }
+ }
+
+ public void addParent ( File pom, File parentPom )
+ throws FileNotFoundException, IOException, XmlPullParserException
+ {
+ Model generatedModel = readPom ( pom );
+
+ Model parentModel = readPom ( parentPom );
+
+ Parent parent = new Parent ();
+ parent.setGroupId ( parentModel.getGroupId () );
+ if ( parent.getGroupId () == null )
+ {
+ parent.setGroupId ( parentModel.getParent ().getGroupId () );
+ }
+ parent.setArtifactId ( parentModel.getArtifactId () );
+ parent.setVersion ( parentModel.getVersion () );
+ if ( parent.getVersion () == null )
+ {
+ parent.setVersion ( parentModel.getParent ().getVersion () );
+ }
+ generatedModel.setParent ( parent );
+
+ writePom ( generatedModel, pom );
+ }
+
+ public void mergePoms ( File pom, File temporaryPom )
+ throws FileNotFoundException, IOException, XmlPullParserException
+ {
+ Model model = readPom ( pom );
+ Model generatedModel = readPom ( temporaryPom );
+ mergeDependencies ( model, generatedModel );
+ mergeBuildPlugins ( model, generatedModel );
+ mergeReportPlugins ( model, generatedModel );
+
+//
+// // Potential merging
+//
+// model.getModelEncoding ();
+// model.getModelVersion ();
+//
+// model.getGroupId ();
+// model.getArtifactId ();
+// model.getVersion ();
+// model.getParent ();
+//
+// model.getId ();
+// model.getName ();
+// model.getInceptionYear ();
+// model.getDescription ();
+// model.getUrl ();
+// model.getLicenses ();
+// model.getProperties ();
+//
+// model.getOrganization ();
+// model.getMailingLists ();
+// model.getContributors ();
+// model.getDevelopers ();
+//
+// model.getScm ();
+// model.getCiManagement ();
+// model.getDistributionManagement ();
+// model.getIssueManagement ();
+//
+// model.getPackaging ();
+//// model.getDependencies (); // done
+// model.getDependencyManagement ();
+// model.getPrerequisites ().getMaven ();
+// model.getPrerequisites ().getModelEncoding ();
+//
+// model.getProfiles ();
+// model.getModules ();
+// model.getRepositories ();
+// model.getPluginRepositories ();
+//
+// model.getBuild ().getDefaultGoal ();
+// model.getBuild ().getFinalName ();
+// model.getBuild ().getModelEncoding ();
+// model.getBuild ().getFilters ();
+// model.getBuild ().getDirectory ();
+// model.getBuild ().getOutputDirectory ();
+// model.getBuild ().getSourceDirectory ();
+// model.getBuild ().getResources ();
+// model.getBuild ().getScriptSourceDirectory ();
+// model.getBuild ().getTestOutputDirectory ();
+// model.getBuild ().getTestResources ();
+// model.getBuild ().getTestSourceDirectory ();
+// model.getBuild ().getPluginManagement ();
+// model.getBuild ().getExtensions ();
+//// model.getBuild ().getPluginsAsMap (); // done
+//
+// model.getReporting ().getModelEncoding ();
+// model.getReporting ().getOutputDirectory ();
+//// model.getReporting ().getReportPluginsAsMap (); // done
+//
+
+ writePom ( model, pom );
+ }
+
+ public Model readPom ( final File pomFile )
+ throws FileNotFoundException, IOException, XmlPullParserException
+ { // TODO ensure correct encoding by using default one from method argument !!!
+
+ Model model;
+ Reader pomReader = null;
+ try
+ {
+ FileCharsetDetector detector = new FileCharsetDetector ( pomFile );
+
+ String fileEncoding = detector.isFound () ? detector.getCharset () : "UTF-8";
+
+ pomReader = new InputStreamReader ( new FileInputStream ( pomFile ), fileEncoding );
+
+ MavenXpp3Reader reader = new MavenXpp3Reader ();
+
+ model = reader.read ( pomReader );
+
+ if ( StringUtils.isEmpty ( model.getModelEncoding () ) )
+ {
+ model.setModelEncoding ( fileEncoding );
+ }
+ }
+ finally
+ {
+ IOUtil.close ( pomReader );
+ }
+ return model;
+ }
+
+ public void writePom ( final Model model, final File pomFile )
+ throws IOException
+ {
+ InputStream inputStream = null;
+ Writer outputStreamWriter = null;
+// FileCharsetDetector detector = new FileCharsetDetector ( pomFile );
+
+ String fileEncoding =
+ StringUtils.isEmpty ( model.getModelEncoding () ) ? model.getModelEncoding () : "UTF-8";
+
+ try
+ {
+ inputStream = new FileInputStream ( pomFile );
+
+ SAXBuilder builder = new SAXBuilder ();
+ org.jdom.Document doc = builder.build ( inputStream );
+ inputStream.close ();
+ inputStream = null;
+
+ MavenJDOMWriter writer = new MavenJDOMWriter ();
+
+ outputStreamWriter =
+ new OutputStreamWriter ( new FileOutputStream ( pomFile ), fileEncoding );
+
+ org.jdom.output.Format form =
+ org.jdom.output.Format.getRawFormat ().setEncoding ( fileEncoding );
+ writer.write ( model, doc, outputStreamWriter, form );
+ outputStreamWriter.close ();
+ outputStreamWriter = null;
+ }
+ catch ( JDOMException exc )
+ {
+ throw (IOException) new IOException ( "Cannot parse the POM by JDOM." );
+ }
+ catch ( FileNotFoundException e )
+ {
+ getLogger ().debug ( "Creating pom file " + pomFile );
+
+ Writer pomWriter = null;
+
+ try
+ {
+// pomWriter = new FileWriter ( pomFile );
+ pomWriter =
+ new OutputStreamWriter ( new FileOutputStream ( pomFile ), fileEncoding );
+
+ MavenXpp3Writer writer = new MavenXpp3Writer ();
+ writer.write ( pomWriter, model );
+ }
+ finally
+ {
+ IOUtil.close ( pomWriter );
+ }
+ }
+ finally
+ {
+ IOUtil.close ( inputStream );
+ IOUtil.close ( outputStreamWriter );
+ }
+ }
+
+ private Map createDependencyMap ( List dependencies )
+ {
+ Map dependencyMap = new HashMap ();
+ Iterator dependenciesIterator = dependencies.iterator ();
+ while ( dependenciesIterator.hasNext () )
+ {
+ Dependency dependency = (Dependency) dependenciesIterator.next ();
+
+ dependencyMap.put (
+ dependency.getGroupId () + ":" + dependency.getArtifactId (),
+ dependency
+ );
+ }
+
+ return dependencyMap;
+ }
+
+ private void mergeBuildPlugins ( Model model, Model generatedModel )
+ {
+ if ( generatedModel.getBuild () != null )
+ {
+ if ( model.getBuild () == null )
+ {
+ model.setBuild ( new Build () );
+ }
+
+ Map pluginsByIds = model.getBuild ().getPluginsAsMap ();
+ Map generatedPluginsByIds = generatedModel.getBuild ().getPluginsAsMap ();
+
+ Iterator generatedPluginsIds = generatedPluginsByIds.keySet ().iterator ();
+ while ( generatedPluginsIds.hasNext () )
+ {
+ String generatedPluginsId = (String) generatedPluginsIds.next ();
+
+ if ( !pluginsByIds.containsKey ( generatedPluginsId ) )
+ {
+ model.getBuild ().addPlugin (
+ (Plugin) generatedPluginsByIds.get ( generatedPluginsId )
+ );
+ }
+ else
+ {
+ getLogger ().warn ( "Can not override plugin: " + generatedPluginsId );
+ }
+ }
+ }
+ }
+
+ private void mergeDependencies ( Model model, Model generatedModel )
+ {
+ Map dependenciesByIds = createDependencyMap ( model.getDependencies () );
+ Map generatedDependenciesByIds = createDependencyMap ( generatedModel.getDependencies () );
+
+ Iterator generatedDependencyIds = generatedDependenciesByIds.keySet ().iterator ();
+ while ( generatedDependencyIds.hasNext () )
+ {
+ String generatedDependencyId = (String) generatedDependencyIds.next ();
+
+ if ( !dependenciesByIds.containsKey ( generatedDependencyId ) )
+ {
+ model.addDependency (
+ (Dependency) generatedDependenciesByIds.get ( generatedDependencyId )
+ );
+ }
+ else
+ {
+ getLogger ().warn ( "Can not override property: " + generatedDependencyId );
+ }
+ }
+ }
+
+ private void mergeReportPlugins ( Model model, Model generatedModel )
+ {
+ if ( generatedModel.getReporting () != null )
+ {
+ if ( model.getReporting () == null )
+ {
+ model.setReporting ( new Reporting () );
+ }
+
+ Map reportPluginsByIds = model.getReporting ().getReportPluginsAsMap ();
+ Map generatedReportPluginsByIds =
+ generatedModel.getReporting ().getReportPluginsAsMap ();
+
+ Iterator generatedReportPluginsIds = generatedReportPluginsByIds.keySet ().iterator ();
+ while ( generatedReportPluginsIds.hasNext () )
+ {
+ String generatedReportPluginsId = (String) generatedReportPluginsIds.next ();
+
+ if ( !reportPluginsByIds.containsKey ( generatedReportPluginsId ) )
+ {
+ model.getReporting ().addPlugin (
+ (ReportPlugin) generatedReportPluginsByIds.get ( generatedReportPluginsId )
+ );
+ }
+ else
+ {
+ getLogger ().warn ( "Can not override report: " + generatedReportPluginsId );
+ }
+ }
+ }
+ }
+}
Propchange: maven/archetype/branches/maven-archetypeng/maven-archetypeng-core/maven-archetypeng-common/src/main/java/org/codehaus/mojo/archetypeng/DefaultPomManager.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: maven/archetype/branches/maven-archetypeng/maven-archetypeng-core/maven-archetypeng-common/src/main/java/org/codehaus/mojo/archetypeng/DefaultPomManager.java
------------------------------------------------------------------------------
svn:keywords = "Author Date Id Revision"
Added: maven/archetype/branches/maven-archetypeng/maven-archetypeng-core/maven-archetypeng-common/src/main/java/org/codehaus/mojo/archetypeng/FileCharsetDetector.java
URL: http://svn.apache.org/viewvc/maven/archetype/branches/maven-archetypeng/maven-archetypeng-core/maven-archetypeng-common/src/main/java/org/codehaus/mojo/archetypeng/FileCharsetDetector.java?view=auto&rev=553946
==============================================================================
--- maven/archetype/branches/maven-archetypeng/maven-archetypeng-core/maven-archetypeng-common/src/main/java/org/codehaus/mojo/archetypeng/FileCharsetDetector.java (added)
+++ maven/archetype/branches/maven-archetypeng/maven-archetypeng-core/maven-archetypeng-common/src/main/java/org/codehaus/mojo/archetypeng/FileCharsetDetector.java Fri Jul 6 09:34:35 2007
@@ -0,0 +1,116 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.codehaus.mojo.archetypeng;
+
+import org.codehaus.plexus.logging.AbstractLogEnabled;
+
+import org.mozilla.intl.chardet.nsDetector;
+import org.mozilla.intl.chardet.nsICharsetDetectionObserver;
+import org.mozilla.intl.chardet.nsPSMDetector;
+
+import java.io.BufferedInputStream;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+
+import java.util.Arrays;
+
+/**
+ * @author rafale
+ */
+public class FileCharsetDetector
+extends AbstractLogEnabled
+{
+ private String charset = null;
+
+ private boolean found = false;
+
+ public FileCharsetDetector ( File detectedFile )
+ throws FileNotFoundException, IOException
+ {
+ nsDetector det = new nsDetector ( nsPSMDetector.ALL );
+
+ det.Init (
+ new nsICharsetDetectionObserver ()
+ {
+ public void Notify ( String charset )
+ {
+ FileCharsetDetector.this.charset = charset;
+ FileCharsetDetector.this.found = true;
+ }
+ }
+ );
+
+ BufferedInputStream imp = new BufferedInputStream ( new FileInputStream ( detectedFile ) );
+
+ byte[] buf = new byte[1024];
+ int len;
+ boolean done = false;
+ boolean isAscii = true;
+
+ while ( ( len = imp.read ( buf, 0, buf.length ) ) != -1 )
+ {
+ // Check if the stream is only ascii.
+ if ( isAscii )
+ {
+ isAscii = det.isAscii ( buf, len );
+ }
+
+ // DoIt if non-ascii and not done yet.
+ if ( !isAscii && !done )
+ {
+ done = det.DoIt ( buf, len, false );
+ found = done;
+ }
+ }
+ det.DataEnd ();
+
+ if ( !isFound () )
+ {
+ String[] prob = det.getProbableCharsets ();
+
+// if ( Arrays.asList ( prob ).contains ( "windows-1252" ) )
+// {
+// charset = "ISO-8859-1";
+// }
+// else
+ if ( prob.length > 0 )
+ {
+ charset = prob[0];
+ }
+ }
+
+ if ( isAscii )
+ {
+ charset = "ASCII";
+ }
+ }
+
+ public String getCharset ()
+ {
+ return charset;
+ }
+
+ public boolean isFound ()
+ {
+ return found;
+ }
+}
Propchange: maven/archetype/branches/maven-archetypeng/maven-archetypeng-core/maven-archetypeng-common/src/main/java/org/codehaus/mojo/archetypeng/FileCharsetDetector.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: maven/archetype/branches/maven-archetypeng/maven-archetypeng-core/maven-archetypeng-common/src/main/java/org/codehaus/mojo/archetypeng/FileCharsetDetector.java
------------------------------------------------------------------------------
svn:keywords = "Author Date Id Revision"
Added: maven/archetype/branches/maven-archetypeng/maven-archetypeng-core/maven-archetypeng-common/src/main/java/org/codehaus/mojo/archetypeng/ListScanner.java
URL: http://svn.apache.org/viewvc/maven/archetype/branches/maven-archetypeng/maven-archetypeng-core/maven-archetypeng-common/src/main/java/org/codehaus/mojo/archetypeng/ListScanner.java?view=auto&rev=553946
==============================================================================
--- maven/archetype/branches/maven-archetypeng/maven-archetypeng-core/maven-archetypeng-common/src/main/java/org/codehaus/mojo/archetypeng/ListScanner.java (added)
+++ maven/archetype/branches/maven-archetypeng/maven-archetypeng-core/maven-archetypeng-common/src/main/java/org/codehaus/mojo/archetypeng/ListScanner.java Fri Jul 6 09:34:35 2007
@@ -0,0 +1,565 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.codehaus.mojo.archetypeng;
+
+import org.codehaus.plexus.util.SelectorUtils;
+import org.codehaus.plexus.util.StringUtils;
+
+import java.io.File;
+
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+
+/**
+ * Class for scanning a directory for files/directories which match certain criteria.
+ *
+ * <p>These criteria consist of selectors and patterns which have been specified. With the selectors
+ * you can select which files you want to have included. Files which are not selected are excluded.
+ * With patterns you can include or exclude files based on their filename.</p>
+ *
+ * <p>The idea is simple. A given directory is recursively scanned for all files and directories.
+ * Each file/directory is matched against a set of selectors, including special support for matching
+ * against filenames with include and and exclude patterns. Only files/directories which match at
+ * least one pattern of the include pattern list or other file selector, and don't match any pattern
+ * of the exclude pattern list or fail to match against a required selector will be placed in the
+ * list of files/directories found.</p>
+ *
+ * <p>When no list of include patterns is supplied, "**" will be used, which means that everything
+ * will be matched. When no list of exclude patterns is supplied, an empty list is used, such that
+ * nothing will be excluded. When no selectors are supplied, none are applied.</p>
+ *
+ * <p>The filename pattern matching is done as follows: The name to be matched is split up in path
+ * segments. A path segment is the name of a directory or file, which is bounded by <code>
+ * File.separator</code> ('/' under UNIX, '\' under Windows). For example, "abc/def/ghi/xyz.java" is
+ * split up in the segments "abc", "def","ghi" and "xyz.java". The same is done for the pattern
+ * against which should be matched.</p>
+ *
+ * <p>The segments of the name and the pattern are then matched against each other. When '**' is
+ * used for a path segment in the pattern, it matches zero or more path segments of the name.</p>
+ *
+ * <p>There is a special case regarding the use of <code>File.separator</code>s at the beginning of
+ * the pattern and the string to match:<br>
+ * When a pattern starts with a <code>File.separator</code>, the string to match must also start
+ * with a <code>File.separator</code>. When a pattern does not start with a <code>
+ * File.separator</code>, the string to match may not start with a <code>File.separator</code>. When
+ * one of these rules is not obeyed, the string will not match.</p>
+ *
+ * <p>When a name path segment is matched against a pattern path segment, the following special
+ * characters can be used:<br>
+ * '*' matches zero or more characters<br>
+ * '?' matches one character.</p>
+ *
+ * <p>Examples:</p>
+ *
+ * <p>"**\*.class" matches all .class files/dirs in a directory tree.</p>
+ *
+ * <p>"test\a??.java" matches all files/dirs which start with an 'a', then two more characters and
+ * then ".java", in a directory called test.</p>
+ *
+ * <p>"**" matches everything in a directory tree.</p>
+ *
+ * <p>"**\test\**\XYZ*" matches all files/dirs which start with "XYZ" and where there is a parent
+ * directory called test (e.g. "abc\test\def\ghi\XYZ123").</p>
+ *
+ * <p>Case sensitivity may be turned off if necessary. By default, it is turned on.</p>
+ *
+ * <p>Example of usage:</p>
+ *
+ * <pre>
+ String[] includes = {"**\\*.class"};
+ String[] excludes = {"modules\\*\\**"};
+ ds.setIncludes(includes);
+ ds.setExcludes(excludes);
+ ds.setBasedir(new File("test"));
+ ds.setCaseSensitive(true);
+ ds.scan();
+
+ System.out.println("FILES:");
+ String[] files = ds.getIncludedFiles();
+ for (int i = 0; i < files.length; i++) {
+ System.out.println(files[i]);
+ }
+ * </pre>
+ *
+ * <p>This will scan a directory called test for .class files, but excludes all files in all proper
+ * subdirectories of a directory called "modules"</p>
+ *
+ * <p>This class was stealed from rg.coudehaus.plexus.util.DirectoryScanner and adapted to search
+ * from a List<String></p>
+ *
+ * @author Arnout J. Kuiper <a href="mailto:ajkuiper@wxs.nl">ajkuiper@wxs.nl</a>
+ * @author Magesh Umasankar
+ * @author <a href="mailto:bruce@callenish.com">Bruce Atherton</a>
+ * @author <a href="mailto:levylambert@tiscali-dsl.de">Antoine Levy-Lambert</a>
+ */
+public class ListScanner
+{
+ /**
+ * Patterns which should be excluded by default.
+ *
+ * @see #addDefaultExcludes()
+ */
+ public static final String[] DEFAULTEXCLUDES =
+ { // Miscellaneous typical temporary files
+ "**/*~", "**/#*#", "**/.#*", "**/%*%", "**/._*",
+
+ // CVS
+ "**/CVS", "**/CVS/**", "**/.cvsignore",
+
+ // SCCS
+ "**/SCCS", "**/SCCS/**",
+
+ // Visual SourceSafe
+ "**/vssver.scc",
+
+ // Subversion
+ "**/.svn", "**/.svn/**",
+
+ // Arch
+ "**/.arch-ids", "**/.arch-ids/**",
+
+ // Bazaar
+ "**/.bzr", "**/.bzr/**",
+
+ // SurroundSCM
+ "**/.MySCMServerInfo",
+
+ // Mac
+ "**/.DS_Store"
+ };
+
+ /**
+ * The base directory to be scanned.
+ */
+ protected String basedir;
+
+ /**
+ * Whether or not everything tested so far has been included.
+ */
+ protected boolean everythingIncluded = true;
+
+ /**
+ * The patterns for the files to be excluded.
+ */
+ protected String[] excludes;
+
+ /**
+ * The patterns for the files to be included.
+ */
+ protected String[] includes;
+
+ /**
+ * Whether or not the file system should be treated as a case sensitive one.
+ */
+ protected boolean isCaseSensitive = true;
+
+ /**
+ * Sole constructor.
+ */
+ public ListScanner ( )
+ { }
+
+ public static String getDefaultExcludes ()
+ {
+ return StringUtils.join ( DEFAULTEXCLUDES, "," );
+ }
+
+ /**
+ * Tests whether or not a string matches against a pattern. The pattern may contain two special
+ * characters:<br>
+ * '*' means zero or more characters<br>
+ * '?' means one and only one character
+ *
+ * @param pattern The pattern to match against. Must not be <code>null</code>.
+ * @param str The string which must be matched against the pattern. Must not be <code>
+ * null</code>.
+ *
+ * @return <code>true</code> if the string matches against the pattern, or <code>false</code>
+ * otherwise.
+ */
+ public static boolean match ( String pattern, String str )
+ {
+ return SelectorUtils.match ( pattern, str );
+ }
+
+ /**
+ * Tests whether or not a string matches against a pattern. The pattern may contain two special
+ * characters:<br>
+ * '*' means zero or more characters<br>
+ * '?' means one and only one character
+ *
+ * @param pattern The pattern to match against. Must not be <code>null</code>.
+ * @param str The string which must be matched against the pattern. Must not be
+ * <code>null</code>.
+ * @param isCaseSensitive Whether or not matching should be performed case sensitively.
+ *
+ * @return <code>true</code> if the string matches against the pattern, or <code>false</code>
+ * otherwise.
+ */
+ protected static boolean match ( String pattern, String str, boolean isCaseSensitive )
+ {
+ return SelectorUtils.match ( pattern, str, isCaseSensitive );
+ }
+
+ /**
+ * Tests whether or not a given path matches a given pattern.
+ *
+ * @param pattern The pattern to match against. Must not be <code>null</code>.
+ * @param str The path to match, as a String. Must not be <code>null</code>.
+ *
+ * @return <code>true</code> if the pattern matches against the string, or <code>false</code>
+ * otherwise.
+ */
+ protected static boolean matchPath ( String pattern, String str )
+ {
+ return SelectorUtils.matchPath ( pattern, str );
+ }
+
+ /**
+ * Tests whether or not a given path matches a given pattern.
+ *
+ * @param pattern The pattern to match against. Must not be <code>null</code>.
+ * @param str The path to match, as a String. Must not be <code>null</code>.
+ * @param isCaseSensitive Whether or not matching should be performed case sensitively.
+ *
+ * @return <code>true</code> if the pattern matches against the string, or <code>false</code>
+ * otherwise.
+ */
+ protected static boolean matchPath ( String pattern, String str, boolean isCaseSensitive )
+ {
+ return SelectorUtils.matchPath ( pattern, str, isCaseSensitive );
+ }
+
+ /**
+ * Tests whether or not a given path matches the start of a given pattern up to the first "**".
+ *
+ * <p>This is not a general purpose test and should only be used if you can live with false
+ * positives. For example, <code>pattern=**\a</code> and <code>str=b</code> will yield <code>
+ * true</code>.</p>
+ *
+ * @param pattern The pattern to match against. Must not be <code>null</code>.
+ * @param str The path to match, as a String. Must not be <code>null</code>.
+ *
+ * @return whether or not a given path matches the start of a given pattern up to the first
+ * "**".
+ */
+ protected static boolean matchPatternStart ( String pattern, String str )
+ {
+ return SelectorUtils.matchPatternStart ( pattern, str );
+ }
+
+ /**
+ * Tests whether or not a given path matches the start of a given pattern up to the first "**".
+ *
+ * <p>This is not a general purpose test and should only be used if you can live with false
+ * positives. For example, <code>pattern=**\a</code> and <code>str=b</code> will yield <code>
+ * true</code>.</p>
+ *
+ * @param pattern The pattern to match against. Must not be <code>null</code>.
+ * @param str The path to match, as a String. Must not be <code>null</code>.
+ * @param isCaseSensitive Whether or not matching should be performed case sensitively.
+ *
+ * @return whether or not a given path matches the start of a given pattern up to the first
+ * "**".
+ */
+ protected static boolean matchPatternStart (
+ String pattern,
+ String str,
+ boolean isCaseSensitive
+ )
+ {
+ return SelectorUtils.matchPatternStart ( pattern, str, isCaseSensitive );
+ }
+
+ /**
+ * Adds default exclusions to the current exclusions set.
+ */
+ public void addDefaultExcludes ()
+ {
+ int excludesLength = ( excludes == null ) ? 0 : excludes.length;
+ String[] newExcludes;
+ newExcludes = new String[excludesLength + DEFAULTEXCLUDES.length];
+ if ( excludesLength > 0 )
+ {
+ System.arraycopy ( excludes, 0, newExcludes, 0, excludesLength );
+ }
+ for ( int i = 0; i < DEFAULTEXCLUDES.length; i++ )
+ {
+ newExcludes[i + excludesLength] =
+ DEFAULTEXCLUDES[i].replace ( '/', File.separatorChar ).replace (
+ '\\',
+ File.separatorChar
+ );
+ }
+ excludes = newExcludes;
+ }
+
+ /**
+ * Returns the base directory to be scanned. This is the directory which is scanned recursively.
+ *
+ * @return the base directory to be scanned
+ */
+ public String getBasedir ()
+ {
+ return basedir;
+ }
+
+ /**
+ * Sets the base directory to be scanned. This is the directory which is scanned recursively.
+ *
+ * @param basedir The base directory for scanning. Should not be <code>null</code>.
+ */
+ public void setBasedir ( String basedir )
+ {
+ this.basedir = basedir;
+ }
+
+ /**
+ * Sets whether or not the file system should be regarded as case sensitive.
+ *
+ * @param isCaseSensitive whether or not the file system should be regarded as a case
+ * sensitive one
+ */
+ public void setCaseSensitive ( boolean isCaseSensitive )
+ {
+ this.isCaseSensitive = isCaseSensitive;
+ }
+
+ /**
+ * Sets the list of exclude patterns to use. All '/' and '\' characters are replaced by <code>
+ * File.separatorChar</code>, so the separator used need not match <code>
+ * File.separatorChar</code>.
+ *
+ * <p>When a pattern ends with a '/' or '\', "**" is appended.</p>
+ *
+ * @param excludes A list of exclude patterns. May be <code>null</code>, indicating that no
+ * files should be excluded. If a non-<code>null</code> list is given, all
+ * elements must be non-<code>null</code>.
+ */
+ public void setExcludes ( List excludesList )
+ {
+ String[] excludes = (String[]) excludesList.toArray ( new String[excludesList.size ()] );
+ if ( excludes == null )
+ {
+ this.excludes = null;
+ }
+ else
+ {
+ setExcludes ( excludes );
+ }
+ }
+
+ public void setExcludes ( String excludes )
+ {
+ if ( excludes == null )
+ {
+ this.excludes = null;
+ }
+ else
+ {
+ setExcludes ( StringUtils.split ( excludes, "," ) );
+ }
+ }
+
+ /**
+ * Sets the list of include patterns to use. All '/' and '\' characters are replaced by <code>
+ * File.separatorChar</code>, so the separator used need not match <code>
+ * File.separatorChar</code>.
+ *
+ * <p>When a pattern ends with a '/' or '\', "**" is appended.</p>
+ *
+ * @param includes A list of include patterns. May be <code>null</code>, indicating that all
+ * files should be included. If a non-<code>null</code> list is given, all
+ * elements must be non-<code>null</code>.
+ */
+ public void setIncludes ( List includesList )
+ {
+ String[] includes = (String[]) includesList.toArray ( new String[includesList.size ()] );
+ if ( includes == null )
+ {
+ this.includes = null;
+ }
+ else
+ {
+ setIncludes ( includes );
+ }
+ }
+
+ public void setIncludes ( String includes )
+ {
+ if ( includes == null )
+ {
+ this.includes = null;
+ }
+ else
+ {
+ setIncludes ( StringUtils.split ( includes, "," ) );
+ }
+ }
+
+ /**
+ * Scans the base directory for files which match at least one include pattern and don't match
+ * any exclude patterns. If there are selectors then the files must pass muster there, as well.
+ *
+ * @exception IllegalStateException if the base directory was set incorrectly (i.e. if it is
+ * <code>null</code>, doesn't exist, or isn't a directory).
+ */
+ public List scan ( List files )
+ throws IllegalStateException
+ {
+ if ( basedir == null )
+ {
+ throw new IllegalStateException ( "No basedir set" );
+ }
+
+ if ( includes == null )
+ {
+ // No includes supplied, so set it to 'matches all'
+ includes = new String[1];
+ includes[0] = "**";
+ }
+ if ( excludes == null )
+ {
+ excludes = new String[0];
+ }
+
+ List result = new ArrayList ();
+
+ Iterator iterator = files.iterator ();
+ while ( iterator.hasNext () )
+ {
+ String fileName = (String) iterator.next ();
+
+ if ( fileName.startsWith ( getBasedir () )
+ && isIncluded ( fileName )
+ && !isExcluded ( fileName )
+ )
+ {
+ result.add ( fileName );
+ }
+ }
+ return result;
+ }
+
+ /**
+ * Tests whether or not a name matches against at least one exclude pattern.
+ *
+ * @param name The name to match. Must not be <code>null</code>.
+ *
+ * @return <code>true</code> when the name matches against at least one exclude pattern, or
+ * <code>false</code> otherwise.
+ */
+ protected boolean isExcluded ( String name )
+ {
+ for ( int i = 0; i < excludes.length; i++ )
+ {
+ if ( matchPath (
+ ( ( getBasedir ().length () != 0 ) ? ( getBasedir () + "/" ) : "" )
+ + excludes[i],
+ name,
+ isCaseSensitive
+ )
+ )
+ {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ /**
+ * Tests whether or not a name matches against at least one include pattern.
+ *
+ * @param name The name to match. Must not be <code>null</code>.
+ *
+ * @return <code>true</code> when the name matches against at least one include pattern, or
+ * <code>false</code> otherwise.
+ */
+ protected boolean isIncluded ( String name )
+ {
+ for ( int i = 0; i < includes.length; i++ )
+ {
+ if ( matchPath (
+ ( ( getBasedir ().length () != 0 ) ? ( getBasedir () + "/" ) : "" )
+ + includes[i],
+ name,
+ isCaseSensitive
+ )
+ )
+ {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ private void setExcludes ( String[] excludes )
+ {
+ if ( ( excludes == null ) || ( excludes.length == 0 ) )
+ {
+ this.excludes = null;
+ }
+ else
+ {
+ this.excludes = new String[excludes.length];
+ for ( int i = 0; i < excludes.length; i++ )
+ {
+ String pattern;
+ pattern =
+ excludes[i].trim ().replace ( '/', File.separatorChar ).replace (
+ '\\',
+ File.separatorChar
+ );
+ if ( pattern.endsWith ( File.separator ) )
+ {
+ pattern += "**";
+ }
+ this.excludes[i] = pattern;
+ }
+ }
+ }
+
+ private void setIncludes ( String[] includes )
+ {
+ if ( ( includes == null ) || ( includes.length == 0 ) )
+ {
+ this.includes = null;
+ }
+ else
+ {
+ this.includes = new String[includes.length];
+ for ( int i = 0; i < includes.length; i++ )
+ {
+ String pattern;
+ pattern =
+ includes[i].trim ().replace ( '/', File.separatorChar ).replace (
+ '\\',
+ File.separatorChar
+ );
+ if ( pattern.endsWith ( File.separator ) )
+ {
+ pattern += "**";
+ }
+ this.includes[i] = pattern;
+ }
+ }
+ }
+}
Propchange: maven/archetype/branches/maven-archetypeng/maven-archetypeng-core/maven-archetypeng-common/src/main/java/org/codehaus/mojo/archetypeng/ListScanner.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: maven/archetype/branches/maven-archetypeng/maven-archetypeng-core/maven-archetypeng-common/src/main/java/org/codehaus/mojo/archetypeng/ListScanner.java
------------------------------------------------------------------------------
svn:keywords = "Author Date Id Revision"