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 w; |
70 | |
try |
71 | |
{ |
72 | 0 | w = |
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( w, "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 | 0 | Iterator onConfigFiles = |
94 | |
getConfigurationFilesList( (String) springConfig.get( "basedir" ), |
95 | |
(String) springConfig.get( "file-pattern" ) ).iterator(); |
96 | 0 | while ( onConfigFiles.hasNext() ) |
97 | |
{ |
98 | 0 | writer.startElement( MYECLIPSE_SPRING_CONFIG ); |
99 | 0 | writer.writeText( StringUtils.replace( (String) onConfigFiles.next(), "\\", "/" ) ); |
100 | 0 | writer.endElement(); |
101 | |
} |
102 | 0 | writer.endElement(); |
103 | |
|
104 | |
|
105 | 0 | writer.startElement( MYECLIPSE_SPRING_CONFIGSETS ); |
106 | 0 | writer.endElement(); |
107 | |
|
108 | |
|
109 | 0 | writer.startElement( MYECLIPSE_SPRING_VERSION ); |
110 | 0 | writer.writeText( (String) springConfig.get( "version" ) ); |
111 | 0 | writer.endElement(); |
112 | |
|
113 | 0 | writer.endElement(); |
114 | |
|
115 | 0 | IOUtil.close( w ); |
116 | 0 | } |
117 | |
|
118 | |
|
119 | |
|
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | |
private Collection getConfigurationFilesList( String basedir, String pattern ) |
127 | |
{ |
128 | 0 | ArrayList configFiles = new ArrayList(); |
129 | |
|
130 | |
try |
131 | |
{ |
132 | 0 | File directory = new File( basedir ); |
133 | |
|
134 | 0 | File[] subdirs = directory.listFiles( new FileFilter() |
135 | |
{ |
136 | 0 | public boolean accept( File pathname ) |
137 | |
{ |
138 | 0 | return pathname.isDirectory(); |
139 | |
} |
140 | |
} ); |
141 | |
|
142 | 0 | for ( int i = 0; i < subdirs.length; i++ ) |
143 | |
{ |
144 | 0 | configFiles.addAll( getConfigurationFilesList( subdirs[i].getPath(), pattern ) ); |
145 | |
} |
146 | |
|
147 | 0 | configFiles.addAll( FileUtils.getFileNames( directory, pattern, null, true ) ); |
148 | |
} |
149 | 0 | catch ( IOException ioe ) |
150 | |
{ |
151 | 0 | log.error( "Error while retrieving Spring configuration files. Returning list in current state" ); |
152 | 0 | } |
153 | |
|
154 | 0 | Collections.sort( configFiles ); |
155 | 0 | return configFiles; |
156 | |
} |
157 | |
} |