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.FileFilter;
25 import java.io.FileWriter;
26 import java.io.IOException;
27 import java.util.ArrayList;
28 import java.util.Collection;
29 import java.util.Collections;
30 import java.util.Map;
31
32 import org.apache.maven.plugin.MojoExecutionException;
33 import org.apache.maven.plugin.eclipse.Messages;
34 import org.apache.maven.plugin.eclipse.writers.AbstractEclipseWriter;
35 import org.apache.maven.plugin.ide.IdeUtils;
36 import org.codehaus.plexus.util.FileUtils;
37 import org.codehaus.plexus.util.IOUtil;
38 import org.codehaus.plexus.util.xml.PrettyPrintXMLWriter;
39 import org.codehaus.plexus.util.xml.XMLWriter;
40
41
42
43
44
45
46 public class MyEclipseSpringBeansWriter
47 extends AbstractEclipseWriter
48 {
49 private static final String MYECLIPSE_SPRING_CONFIGURATION_FILENAME = ".springBeans";
50
51 private static final String MYECLIPSE_SPRING_BEANS_PROJECT_DESCRIPTION = "beansProjectDescription";
52
53 private static final String MYECLIPSE_SPRING_CONFIG_EXTENSIONS = "configExtensions";
54
55 private static final String MYECLIPSE_SPRING_CONFIG_EXTENSION = "configExtension";
56
57 private static final String MYECLIPSE_SPRING_CONFIGS = "configs";
58
59 private static final String MYECLIPSE_SPRING_CONFIG = "config";
60
61 private static final String MYECLIPSE_SPRING_CONFIGSETS = "configSets";
62
63 private static final String MYECLIPSE_SPRING_VERSION = "springVersion";
64
65
66
67
68 private Map springConfig;
69
70
71
72
73
74
75 public MyEclipseSpringBeansWriter( Map springConfig )
76 {
77 this.springConfig = springConfig;
78 }
79
80
81
82
83
84
85 public void write()
86 throws MojoExecutionException
87 {
88 FileWriter springFileWriter;
89 try
90 {
91 springFileWriter =
92 new FileWriter( new File( config.getEclipseProjectDirectory(), MYECLIPSE_SPRING_CONFIGURATION_FILENAME ) );
93 }
94 catch ( IOException ex )
95 {
96 throw new MojoExecutionException( Messages.getString( "EclipsePlugin.erroropeningfile" ), ex );
97 }
98
99 XMLWriter writer = new PrettyPrintXMLWriter( springFileWriter, "UTF-8", null );
100
101 writer.startElement( MYECLIPSE_SPRING_BEANS_PROJECT_DESCRIPTION );
102
103 writer.startElement( MYECLIPSE_SPRING_CONFIG_EXTENSIONS );
104 writer.startElement( MYECLIPSE_SPRING_CONFIG_EXTENSION );
105 writer.writeText( "xml" );
106 writer.endElement();
107 writer.endElement();
108
109
110 writer.startElement( MYECLIPSE_SPRING_CONFIGS );
111
112
113
114 File basedir = config.getEclipseProjectDirectory();
115
116 for (Object o : getConfigurationFilesList(new File(basedir, (String) springConfig.get("basedir")),
117 (String) springConfig.get("file-pattern"))) {
118 String onConfigFileName = (String) o;
119 File onConfigFile = new File(onConfigFileName);
120 String relativeFileName = IdeUtils.toRelativeAndFixSeparator(basedir, onConfigFile, false);
121
122 writer.startElement(MYECLIPSE_SPRING_CONFIG);
123 writer.writeText(relativeFileName);
124 writer.endElement();
125 }
126 writer.endElement();
127
128
129 writer.startElement( MYECLIPSE_SPRING_CONFIGSETS );
130 writer.endElement();
131
132
133 writer.startElement( MYECLIPSE_SPRING_VERSION );
134 writer.writeText( (String) springConfig.get( "version" ) );
135 writer.endElement();
136
137 writer.endElement();
138
139 IOUtil.close( springFileWriter );
140 }
141
142
143
144
145
146
147
148
149
150 private Collection getConfigurationFilesList( File basedir, String pattern )
151 {
152 ArrayList configFiles = new ArrayList();
153
154 try
155 {
156 if ( basedir.exists() )
157 {
158 log.debug( "Scanning " + basedir + " for spring definition files" );
159 File[] subdirs = basedir.listFiles( new FileFilter()
160 {
161 public boolean accept( File pathname )
162 {
163 return pathname.isDirectory();
164 }
165 } );
166
167 if ( subdirs != null )
168 {
169 for (File subdir : subdirs) {
170 configFiles.addAll(getConfigurationFilesList(subdir, pattern));
171 }
172 }
173
174 configFiles.addAll( FileUtils.getFileNames( basedir, pattern, null, true ) );
175 }
176 else
177 {
178
179
180 log.warn( Messages.getString( "MyEclipseSpringBeansWriter.baseDirDoesNotExist",
181 new Object[] { basedir } ) );
182 }
183 }
184 catch ( IOException ioe )
185 {
186 log.error( "Error while retrieving Spring configuration files. Returning list in current state" );
187 }
188
189 Collections.sort( configFiles );
190 return configFiles;
191 }
192 }