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