1 package org.apache.maven.plugin.eclipse.writers.myeclipse;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
40
41
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
68
69
70
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
83
84
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 );
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
124 writer.endElement();
125
126 IOUtil.close( w );
127 }
128 }
129
130
131
132
133
134
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
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
168
169
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
182
183
184
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
198
199
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 }