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
20
21
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
48
49
50
51
52 public MyEclipseStrutsDataWriter( Map strutsProps )
53 {
54 this.strutsProps = strutsProps;
55
56 strutsPatterns = new HashMap();
57 strutsPatterns.put( "*.do", "0" );
58 strutsPatterns.put( "/do/*", "1" );
59 }
60
61
62
63
64
65
66 public void write()
67 throws MojoExecutionException
68 {
69 String packaging = config.getProject().getPackaging();
70
71 if ( Constants.PROJECT_PACKAGING_WAR.equals( packaging ) )
72 {
73 FileWriter w;
74 try
75 {
76 w = new FileWriter( new File( config.getEclipseProjectDirectory(), MYECLIPSE_MYSTRUTSDATA_FILENAME ) );
77 }
78 catch ( IOException ex )
79 {
80 throw new MojoExecutionException( Messages.getString( "EclipsePlugin.erroropeningfile" ), ex );
81 }
82
83 XMLWriter writer = new PrettyPrintXMLWriter( w, "UTF-8", null );
84
85 writer.startElement( MYECLIPSE_STRUTS_PROPERTIES );
86
87 writer.startElement( MYECLIPSE_STRUTS_VERSION );
88 writer.writeText( getStrutsVersion() );
89 writer.endElement();
90
91 writer.startElement( MYECLIPSE_STRUTS_BASE_PACKAGE );
92 writer.writeText( getBasePackage() );
93 writer.endElement();
94
95 writer.startElement( MYECLIPSE_STRUTS_PATTERN );
96 writer.writeText( getStrutsPattern() );
97 writer.endElement();
98
99 writer.startElement( MYECLIPSE_STRUTS_SERVLET_NAME );
100 writer.writeText( getStrutsServletName() );
101 writer.endElement();
102
103
104 writer.endElement();
105
106 IOUtil.close( w );
107 }
108 }
109
110
111
112
113
114
115
116 private String getStrutsVersion()
117 {
118 String version;
119
120 if ( strutsProps != null && strutsProps.get( "version" ) != null )
121 {
122 version = (String) strutsProps.get( "version" );
123 }
124 else
125 {
126 version =
127 IdeUtils.getArtifactVersion( new String[] { "struts", "struts-core" },
128 config.getProject().getDependencies(), 5 );
129
130
131 if ( version == null )
132 {
133 version = "1.2.9";
134 }
135 }
136
137 int firstDotIndex = version.indexOf( '.' );
138 int secondDotIndex = version.indexOf( '.', firstDotIndex + 1 );
139 String majorVersion = version.substring( firstDotIndex + 1, secondDotIndex );
140
141 int v = Integer.parseInt( majorVersion );
142
143 return v > 2 ? "2" : majorVersion;
144 }
145
146
147
148
149
150
151 private String getBasePackage()
152 {
153 if ( strutsProps != null && strutsProps.get( "base-package" ) != null )
154 {
155 return (String) strutsProps.get( "base-package" );
156 }
157 return config.getProject().getGroupId();
158 }
159
160
161
162
163
164
165
166 private String getStrutsPattern()
167 {
168 if ( strutsProps != null && strutsProps.get( "pattern" ) != null )
169 {
170 String pattern = (String) strutsPatterns.get( strutsProps.get( "pattern" ) );
171 return pattern != null ? pattern : (String) strutsPatterns.get( MYECLIPSE_STRUTS_DEFAULT_PATTERN );
172 }
173 return (String) strutsPatterns.get( MYECLIPSE_STRUTS_DEFAULT_PATTERN );
174 }
175
176
177
178
179
180
181 private String getStrutsServletName()
182 {
183 if ( strutsProps != null && strutsProps.get( "servlet-name" ) != null )
184 {
185 return (String) strutsProps.get( "servlet-name" );
186 }
187 return MYECLIPSE_STRUTS_SERVLET_DEFAULT_NAME;
188 }
189 }