Coverage Report - org.apache.maven.plugin.eclipse.writers.myeclipse.MyEclipseStrutsDataWriter
 
Classes in this File Line Coverage Branch Coverage Complexity
MyEclipseStrutsDataWriter
0%
0/49
0%
0/24
2.833
 
 1  
 package org.apache.maven.plugin.eclipse.writers.myeclipse;
 2  
 
 3  
 import java.io.File;
 4  
 import java.io.FileWriter;
 5  
 import java.io.IOException;
 6  
 import java.util.HashMap;
 7  
 import java.util.Map;
 8  
 
 9  
 import org.apache.maven.plugin.MojoExecutionException;
 10  
 import org.apache.maven.plugin.eclipse.Constants;
 11  
 import org.apache.maven.plugin.eclipse.Messages;
 12  
 import org.apache.maven.plugin.eclipse.writers.AbstractEclipseWriter;
 13  
 import org.apache.maven.plugin.ide.IdeUtils;
 14  
 import org.codehaus.plexus.util.IOUtil;
 15  
 import org.codehaus.plexus.util.xml.PrettyPrintXMLWriter;
 16  
 import org.codehaus.plexus.util.xml.XMLWriter;
 17  
 
 18  
 /**
 19  
  * MyEclipse .mystrutsdata configuration file writer
 20  
  * 
 21  
  * @author Olivier Jacob
 22  
  */
 23  
 public class MyEclipseStrutsDataWriter
 24  
     extends AbstractEclipseWriter
 25  
 {
 26  
     private static final String MYECLIPSE_MYSTRUTSDATA_FILENAME = ".mystrutsdata";
 27  
 
 28  
     private static final String MYECLIPSE_STRUTS_PROPERTIES = "MyEclipseStrutsProperties";
 29  
 
 30  
     private static final String MYECLIPSE_STRUTS_VERSION = "strutsVersion";
 31  
 
 32  
     private static final String MYECLIPSE_STRUTS_BASE_PACKAGE = "basePackage";
 33  
 
 34  
     private static final String MYECLIPSE_STRUTS_PATTERN = "strutsPattern";
 35  
 
 36  
     private static final String MYECLIPSE_STRUTS_SERVLET_NAME = "servletName";
 37  
 
 38  
     private static final String MYECLIPSE_STRUTS_DEFAULT_PATTERN = "*.do";
 39  
 
 40  
     private static final String MYECLIPSE_STRUTS_SERVLET_DEFAULT_NAME = "action";
 41  
 
 42  
     private static Map strutsPatterns;
 43  
 
 44  
     private Map strutsProps;
 45  
 
 46  
     /**
 47  
      * Receive struts properties map from plugin
 48  
      * 
 49  
      * @param strutsProps
 50  
      * @see org.apache.maven.plugin.eclipse.MyEclipsePlugin#struts
 51  
      */
 52  
     public MyEclipseStrutsDataWriter( Map strutsProps )
 53  0
     {
 54  0
         this.strutsProps = strutsProps;
 55  
 
 56  0
         strutsPatterns = new HashMap();
 57  0
         strutsPatterns.put( "*.do", "0" );
 58  0
         strutsPatterns.put( "/do/*", "1" );
 59  0
     }
 60  
 
 61  
     /**
 62  
      * Write MyEclipse .mystrutsdata configuration file
 63  
      * 
 64  
      * @throws MojoExecutionException
 65  
      */
 66  
     public void write()
 67  
         throws MojoExecutionException
 68  
     {
 69  0
         String packaging = config.getProject().getPackaging();
 70  
 
 71  0
         if ( Constants.PROJECT_PACKAGING_WAR.equals( packaging ) )
 72  
         {
 73  
             FileWriter w;
 74  
             try
 75  
             {
 76  0
                 w = new FileWriter( new File( config.getEclipseProjectDirectory(), MYECLIPSE_MYSTRUTSDATA_FILENAME ) );
 77  
             }
 78  0
             catch ( IOException ex )
 79  
             {
 80  0
                 throw new MojoExecutionException( Messages.getString( "EclipsePlugin.erroropeningfile" ), ex ); //$NON-NLS-1$
 81  0
             }
 82  
 
 83  0
             XMLWriter writer = new PrettyPrintXMLWriter( w, "UTF-8", null );
 84  
 
 85  0
             writer.startElement( MYECLIPSE_STRUTS_PROPERTIES );
 86  
 
 87  0
             writer.startElement( MYECLIPSE_STRUTS_VERSION );
 88  0
             writer.writeText( getStrutsVersion() );
 89  0
             writer.endElement();
 90  
 
 91  0
             writer.startElement( MYECLIPSE_STRUTS_BASE_PACKAGE );
 92  0
             writer.writeText( getBasePackage() );
 93  0
             writer.endElement();
 94  
 
 95  0
             writer.startElement( MYECLIPSE_STRUTS_PATTERN );
 96  0
             writer.writeText( getStrutsPattern() );
 97  0
             writer.endElement();
 98  
 
 99  0
             writer.startElement( MYECLIPSE_STRUTS_SERVLET_NAME );
 100  0
             writer.writeText( getStrutsServletName() );
 101  0
             writer.endElement();
 102  
 
 103  
             // Close <MyEclipseStrutsProperties>
 104  0
             writer.endElement();
 105  
 
 106  0
             IOUtil.close( w );
 107  
         }
 108  0
     }
 109  
 
 110  
     /**
 111  
      * Retrieve Struts version from plugin configuration or if not specified from project dependencies. If none is
 112  
      * specified, use 1.2.9 as default
 113  
      * 
 114  
      * @return my eclipse struts version code
 115  
      */
 116  
     private String getStrutsVersion()
 117  
     {
 118  
         String version;
 119  
 
 120  0
         if ( strutsProps != null && strutsProps.get( "version" ) != null )
 121  
         {
 122  0
             version = (String) strutsProps.get( "version" );
 123  
         }
 124  
         else
 125  
         {
 126  0
             version =
 127  
                 IdeUtils.getArtifactVersion( new String[] { "struts", "struts-core" },
 128  
                                              config.getProject().getDependencies(), 5 );
 129  
 
 130  
             // Newest version supported by MyEclipse is Struts 1.2.x
 131  0
             if ( version == null )
 132  
             {
 133  0
                 version = "1.2.9";
 134  
             }
 135  
         }
 136  
 
 137  0
         int firstDotIndex = version.indexOf( '.' );
 138  0
         int secondDotIndex = version.indexOf( '.', firstDotIndex + 1 );
 139  0
         String majorVersion = version.substring( firstDotIndex + 1, secondDotIndex );
 140  
 
 141  0
         int v = Integer.parseInt( majorVersion );
 142  
 
 143  0
         return v > 2 ? "2" : majorVersion;
 144  
     }
 145  
 
 146  
     /**
 147  
      * Retrieve struts actions base package name from plugin configuration or use project groupId if not set
 148  
      * 
 149  
      * @return String
 150  
      */
 151  
     private String getBasePackage()
 152  
     {
 153  0
         if ( strutsProps != null && strutsProps.get( "base-package" ) != null )
 154  
         {
 155  0
             return (String) strutsProps.get( "base-package" );
 156  
         }
 157  0
         return config.getProject().getGroupId();
 158  
     }
 159  
 
 160  
     /**
 161  
      * Retrieve Struts servlet url-pattern from plugin configuration and convert it to the code MyEclipse uses. If not
 162  
      * set, use "*.do" as default
 163  
      * 
 164  
      * @return String
 165  
      */
 166  
     private String getStrutsPattern()
 167  
     {
 168  0
         if ( strutsProps != null && strutsProps.get( "pattern" ) != null )
 169  
         {
 170  0
             String pattern = (String) strutsPatterns.get( strutsProps.get( "pattern" ) );
 171  0
             return pattern != null ? pattern : (String) strutsPatterns.get( MYECLIPSE_STRUTS_DEFAULT_PATTERN );
 172  
         }
 173  0
         return (String) strutsPatterns.get( MYECLIPSE_STRUTS_DEFAULT_PATTERN );
 174  
     }
 175  
 
 176  
     /**
 177  
      * Retrieve Struts servlet name from plugin configuration. Use "action" as default
 178  
      * 
 179  
      * @return
 180  
      */
 181  
     private String getStrutsServletName()
 182  
     {
 183  0
         if ( strutsProps != null && strutsProps.get( "servlet-name" ) != null )
 184  
         {
 185  0
             return (String) strutsProps.get( "servlet-name" );
 186  
         }
 187  0
         return MYECLIPSE_STRUTS_SERVLET_DEFAULT_NAME;
 188  
     }
 189  
 }