View Javadoc
1   package org.apache.maven.plugin.eclipse.writers.myeclipse;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   * 
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   * 
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  
23  import java.io.File;
24  import java.io.FileWriter;
25  import java.io.IOException;
26  import java.util.HashMap;
27  import java.util.Map;
28  
29  import org.apache.maven.plugin.MojoExecutionException;
30  import org.apache.maven.plugin.eclipse.Constants;
31  import org.apache.maven.plugin.eclipse.Messages;
32  import org.apache.maven.plugin.eclipse.writers.AbstractEclipseWriter;
33  import org.apache.maven.plugin.ide.IdeUtils;
34  import org.codehaus.plexus.util.IOUtil;
35  import org.codehaus.plexus.util.xml.PrettyPrintXMLWriter;
36  import org.codehaus.plexus.util.xml.XMLWriter;
37  
38  /**
39   * MyEclipse .mystrutsdata configuration file writer
40   * 
41   * @author Olivier Jacob
42   */
43  public class MyEclipseStrutsDataWriter
44      extends AbstractEclipseWriter
45  {
46      private static final String MYECLIPSE_MYSTRUTSDATA_FILENAME = ".mystrutsdata";
47  
48      private static final String MYECLIPSE_STRUTS_PROPERTIES = "MyEclipseStrutsProperties";
49  
50      private static final String MYECLIPSE_STRUTS_VERSION = "strutsVersion";
51  
52      private static final String MYECLIPSE_STRUTS_BASE_PACKAGE = "basePackage";
53  
54      private static final String MYECLIPSE_STRUTS_PATTERN = "strutsPattern";
55  
56      private static final String MYECLIPSE_STRUTS_SERVLET_NAME = "servletName";
57  
58      private static final String MYECLIPSE_STRUTS_DEFAULT_PATTERN = "*.do";
59  
60      private static final String MYECLIPSE_STRUTS_SERVLET_DEFAULT_NAME = "action";
61  
62      private static Map strutsPatterns;
63  
64      private Map strutsProps;
65  
66      /**
67       * Receive struts properties map from plugin
68       * 
69       * @param strutsProps
70       * @see org.apache.maven.plugin.eclipse.MyEclipsePlugin#struts
71       */
72      public MyEclipseStrutsDataWriter( Map strutsProps )
73      {
74          this.strutsProps = strutsProps;
75  
76          strutsPatterns = new HashMap();
77          strutsPatterns.put( "*.do", "0" );
78          strutsPatterns.put( "/do/*", "1" );
79      }
80  
81      /**
82       * Write MyEclipse .mystrutsdata configuration file
83       * 
84       * @throws MojoExecutionException
85       */
86      public void write()
87          throws MojoExecutionException
88      {
89          String packaging = config.getProject().getPackaging();
90  
91          if ( Constants.PROJECT_PACKAGING_WAR.equals( packaging ) )
92          {
93              FileWriter w;
94              try
95              {
96                  w = new FileWriter( new File( config.getEclipseProjectDirectory(), MYECLIPSE_MYSTRUTSDATA_FILENAME ) );
97              }
98              catch ( IOException ex )
99              {
100                 throw new MojoExecutionException( Messages.getString( "EclipsePlugin.erroropeningfile" ), ex ); //$NON-NLS-1$
101             }
102 
103             XMLWriter writer = new PrettyPrintXMLWriter( w, "UTF-8", null );
104 
105             writer.startElement( MYECLIPSE_STRUTS_PROPERTIES );
106 
107             writer.startElement( MYECLIPSE_STRUTS_VERSION );
108             writer.writeText( getStrutsVersion() );
109             writer.endElement();
110 
111             writer.startElement( MYECLIPSE_STRUTS_BASE_PACKAGE );
112             writer.writeText( getBasePackage() );
113             writer.endElement();
114 
115             writer.startElement( MYECLIPSE_STRUTS_PATTERN );
116             writer.writeText( getStrutsPattern() );
117             writer.endElement();
118 
119             writer.startElement( MYECLIPSE_STRUTS_SERVLET_NAME );
120             writer.writeText( getStrutsServletName() );
121             writer.endElement();
122 
123             // Close <MyEclipseStrutsProperties>
124             writer.endElement();
125 
126             IOUtil.close( w );
127         }
128     }
129 
130     /**
131      * Retrieve Struts version from plugin configuration or if not specified from project dependencies. If none is
132      * specified, use 1.2.9 as default
133      * 
134      * @return my eclipse struts version code
135      */
136     private String getStrutsVersion()
137     {
138         String version;
139 
140         if ( strutsProps != null && strutsProps.get( "version" ) != null )
141         {
142             version = (String) strutsProps.get( "version" );
143         }
144         else
145         {
146             version =
147                 IdeUtils.getArtifactVersion( new String[] { "struts", "struts-core" },
148                                              config.getProject().getDependencies(), 5 );
149 
150             // Newest version supported by MyEclipse is Struts 1.2.x
151             if ( version == null )
152             {
153                 version = "1.2.9";
154             }
155         }
156 
157         int firstDotIndex = version.indexOf( '.' );
158         int secondDotIndex = version.indexOf( '.', firstDotIndex + 1 );
159         String majorVersion = version.substring( firstDotIndex + 1, secondDotIndex );
160 
161         int v = Integer.parseInt( majorVersion );
162 
163         return v > 2 ? "2" : majorVersion;
164     }
165 
166     /**
167      * Retrieve struts actions base package name from plugin configuration or use project groupId if not set
168      * 
169      * @return String
170      */
171     private String getBasePackage()
172     {
173         if ( strutsProps != null && strutsProps.get( "base-package" ) != null )
174         {
175             return (String) strutsProps.get( "base-package" );
176         }
177         return config.getProject().getGroupId();
178     }
179 
180     /**
181      * Retrieve Struts servlet url-pattern from plugin configuration and convert it to the code MyEclipse uses. If not
182      * set, use "*.do" as default
183      * 
184      * @return String
185      */
186     private String getStrutsPattern()
187     {
188         if ( strutsProps != null && strutsProps.get( "pattern" ) != null )
189         {
190             String pattern = (String) strutsPatterns.get( strutsProps.get( "pattern" ) );
191             return pattern != null ? pattern : (String) strutsPatterns.get( MYECLIPSE_STRUTS_DEFAULT_PATTERN );
192         }
193         return (String) strutsPatterns.get( MYECLIPSE_STRUTS_DEFAULT_PATTERN );
194     }
195 
196     /**
197      * Retrieve Struts servlet name from plugin configuration. Use "action" as default
198      * 
199      * @return
200      */
201     private String getStrutsServletName()
202     {
203         if ( strutsProps != null && strutsProps.get( "servlet-name" ) != null )
204         {
205             return (String) strutsProps.get( "servlet-name" );
206         }
207         return MYECLIPSE_STRUTS_SERVLET_DEFAULT_NAME;
208     }
209 }