Coverage Report - org.apache.maven.plugin.eclipse.writers.myeclipse.MyEclipseSpringBeansWriter
 
Classes in this File Line Coverage Branch Coverage Complexity
MyEclipseSpringBeansWriter
0%
0/40
0%
0/4
2.25
MyEclipseSpringBeansWriter$1
0%
0/2
N/A
2.25
 
 1  
 package org.apache.maven.plugin.eclipse.writers.myeclipse;
 2  
 
 3  
 import java.io.File;
 4  
 import java.io.FileFilter;
 5  
 import java.io.FileWriter;
 6  
 import java.io.IOException;
 7  
 import java.util.ArrayList;
 8  
 import java.util.Collection;
 9  
 import java.util.Collections;
 10  
 import java.util.Iterator;
 11  
 import java.util.Map;
 12  
 
 13  
 import org.apache.maven.plugin.MojoExecutionException;
 14  
 import org.apache.maven.plugin.eclipse.Messages;
 15  
 import org.apache.maven.plugin.eclipse.writers.AbstractEclipseWriter;
 16  
 import org.codehaus.plexus.util.FileUtils;
 17  
 import org.codehaus.plexus.util.IOUtil;
 18  
 import org.codehaus.plexus.util.StringUtils;
 19  
 import org.codehaus.plexus.util.xml.PrettyPrintXMLWriter;
 20  
 import org.codehaus.plexus.util.xml.XMLWriter;
 21  
 
 22  
 /**
 23  
  * MyEclipse .springBeans configuration file writer
 24  
  * 
 25  
  * @author Olivier Jacob
 26  
  */
 27  
 public class MyEclipseSpringBeansWriter
 28  
     extends AbstractEclipseWriter
 29  
 {
 30  
     private static final String MYECLIPSE_SPRING_CONFIGURATION_FILENAME = ".springBeans";
 31  
 
 32  
     private static final String MYECLIPSE_SPRING_BEANS_PROJECT_DESCRIPTION = "beansProjectDescription";
 33  
 
 34  
     private static final String MYECLIPSE_SPRING_CONFIG_EXTENSIONS = "configExtensions";
 35  
 
 36  
     private static final String MYECLIPSE_SPRING_CONFIG_EXTENSION = "configExtension";
 37  
 
 38  
     private static final String MYECLIPSE_SPRING_CONFIGS = "configs";
 39  
 
 40  
     private static final String MYECLIPSE_SPRING_CONFIG = "config";
 41  
 
 42  
     private static final String MYECLIPSE_SPRING_CONFIGSETS = "configSets";
 43  
 
 44  
     private static final String MYECLIPSE_SPRING_VERSION = "springVersion";
 45  
 
 46  
     /**
 47  
      * Spring configuration filenames (injected by the plugin)
 48  
      */
 49  
     private Map springConfig;
 50  
 
 51  
     /**
 52  
      * Allow injection of Spring configuration filenames through constructor
 53  
      * 
 54  
      * @param springConfig a map holding Spring configuration properties
 55  
      */
 56  
     public MyEclipseSpringBeansWriter( Map springConfig )
 57  0
     {
 58  0
         this.springConfig = springConfig;
 59  0
     }
 60  
 
 61  
     /**
 62  
      * Write MyEclipse .springBeans configuration file
 63  
      * 
 64  
      * @throws MojoExecutionException
 65  
      */
 66  
     public void write()
 67  
         throws MojoExecutionException
 68  
     {
 69  
         FileWriter w;
 70  
         try
 71  
         {
 72  0
             w =
 73  
                 new FileWriter( new File( config.getEclipseProjectDirectory(), MYECLIPSE_SPRING_CONFIGURATION_FILENAME ) );
 74  
         }
 75  0
         catch ( IOException ex )
 76  
         {
 77  0
             throw new MojoExecutionException( Messages.getString( "EclipsePlugin.erroropeningfile" ), ex ); //$NON-NLS-1$
 78  0
         }
 79  
 
 80  0
         XMLWriter writer = new PrettyPrintXMLWriter( w, "UTF-8", null );
 81  
 
 82  0
         writer.startElement( MYECLIPSE_SPRING_BEANS_PROJECT_DESCRIPTION );
 83  
         // Configuration extension
 84  0
         writer.startElement( MYECLIPSE_SPRING_CONFIG_EXTENSIONS );
 85  0
         writer.startElement( MYECLIPSE_SPRING_CONFIG_EXTENSION );
 86  0
         writer.writeText( "xml" );
 87  0
         writer.endElement();
 88  0
         writer.endElement();
 89  
 
 90  
         // Configuration files
 91  0
         writer.startElement( MYECLIPSE_SPRING_CONFIGS );
 92  
 
 93  0
         Iterator onConfigFiles =
 94  
             getConfigurationFilesList( (String) springConfig.get( "basedir" ),
 95  
                                        (String) springConfig.get( "file-pattern" ) ).iterator();
 96  0
         while ( onConfigFiles.hasNext() )
 97  
         {
 98  0
             writer.startElement( MYECLIPSE_SPRING_CONFIG );
 99  0
             writer.writeText( StringUtils.replace( (String) onConfigFiles.next(), "\\", "/" ) );
 100  0
             writer.endElement();
 101  
         }
 102  0
         writer.endElement();
 103  
 
 104  
         // Configuration sets
 105  0
         writer.startElement( MYECLIPSE_SPRING_CONFIGSETS );
 106  0
         writer.endElement();
 107  
 
 108  
         // Spring version
 109  0
         writer.startElement( MYECLIPSE_SPRING_VERSION );
 110  0
         writer.writeText( (String) springConfig.get( "version" ) );
 111  0
         writer.endElement();
 112  
 
 113  0
         writer.endElement();
 114  
 
 115  0
         IOUtil.close( w );
 116  0
     }
 117  
 
 118  
     /**
 119  
      * Retrieve the list of Spring configuration files recursively from the <code>basedir</code> directory,
 120  
      * considering only filenames matching the <code>pattern</code> given
 121  
      * 
 122  
      * @param basedir the path to the base directory to search in
 123  
      * @param pattern file include pattern
 124  
      * @return the list of filenames matching the given pattern
 125  
      */
 126  
     private Collection getConfigurationFilesList( String basedir, String pattern )
 127  
     {
 128  0
         ArrayList configFiles = new ArrayList();
 129  
 
 130  
         try
 131  
         {
 132  0
             File directory = new File( basedir );
 133  
 
 134  0
             File[] subdirs = directory.listFiles( new FileFilter()
 135  
             {
 136  0
                 public boolean accept( File pathname )
 137  
                 {
 138  0
                     return pathname.isDirectory();
 139  
                 }
 140  
             } );
 141  
 
 142  0
             for ( int i = 0; i < subdirs.length; i++ )
 143  
             {
 144  0
                 configFiles.addAll( getConfigurationFilesList( subdirs[i].getPath(), pattern ) );
 145  
             }
 146  
 
 147  0
             configFiles.addAll( FileUtils.getFileNames( directory, pattern, null, true ) );
 148  
         }
 149  0
         catch ( IOException ioe )
 150  
         {
 151  0
             log.error( "Error while retrieving Spring configuration files. Returning list in current state" );
 152  0
         }
 153  
         // Sort them to have something constant
 154  0
         Collections.sort( configFiles );
 155  0
         return configFiles;
 156  
     }
 157  
 }