1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.codehaus.mojo.castor;
17
18 import java.io.File;
19 import java.io.FileInputStream;
20 import java.io.FileNotFoundException;
21 import java.io.IOException;
22 import java.util.Properties;
23
24 import org.apache.maven.plugin.MojoExecutionException;
25 import org.apache.maven.plugin.logging.Log;
26 import org.exolab.castor.builder.SourceGenerator;
27 import org.exolab.castor.builder.binding.ExtendedBinding;
28 import org.exolab.castor.builder.factory.FieldInfoFactory;
29 import org.exolab.castor.builder.info.CollectionInfo;
30
31
32
33
34
35 class CastorSourceGenerator
36 extends SourceGenerator
37 {
38
39
40
41 private Log log;
42
43
44
45
46
47
48 private FieldInfoFactory fieldInfoFactory;
49
50
51
52
53
54 private boolean verbose;
55
56
57
58
59 public CastorSourceGenerator()
60 {
61 this( new FieldInfoFactory() );
62 }
63
64
65
66
67
68
69 public CastorSourceGenerator( FieldInfoFactory fieldInfoFactory )
70 {
71 super( fieldInfoFactory );
72 this.fieldInfoFactory = fieldInfoFactory;
73 }
74
75
76
77
78
79
80
81 public CastorSourceGenerator( FieldInfoFactory fieldInfoFactory, ExtendedBinding extendedBinding )
82 {
83 super( fieldInfoFactory, extendedBinding );
84 this.fieldInfoFactory = fieldInfoFactory;
85 }
86
87
88
89
90
91
92
93
94 public static CastorSourceGenerator createSourceGenerator( String types )
95 throws MojoExecutionException
96 {
97
98 CastorSourceGenerator sgen;
99 if ( types != null )
100 {
101 try
102 {
103 String typ = "j2".equals( types ) ? "arraylist" : types;
104 FieldInfoFactory factory = new FieldInfoFactory( typ );
105 sgen = new CastorSourceGenerator( factory );
106 }
107 catch ( Exception e )
108 {
109 try
110 {
111 sgen = new CastorSourceGenerator( (FieldInfoFactory) Class.forName( types ).newInstance() );
112 }
113 catch ( Exception e2 )
114 {
115 throw new MojoExecutionException( "Invalid types \"" + types + "\": " + e.getMessage() );
116 }
117 }
118 }
119 else
120 {
121 sgen = new CastorSourceGenerator();
122 }
123 return sgen;
124 }
125
126
127
128
129
130
131 public void setLog( Log log )
132 {
133 this.log = log;
134 }
135
136
137
138
139
140
141 public Log getLog()
142 {
143 return log;
144 }
145
146
147
148
149
150
151 public void log( String msg )
152 {
153 getLog().info( msg );
154 }
155
156
157
158
159
160
161 public void verbose( String msg )
162 {
163 if ( verbose )
164 {
165 getLog().info( msg );
166 }
167 }
168
169
170
171
172
173
174
175 public void setLineSeparatorStyle( String lineSeparator )
176 throws MojoExecutionException
177 {
178
179 String lineSep = System.getProperty( "line.separator" );
180 if ( lineSeparator != null )
181 {
182 if ( "win".equals( lineSeparator ) )
183 {
184 log( "Using Windows style line separation." );
185 lineSep = "\r\n";
186 }
187 else if ( "unix".equals( lineSeparator ) )
188 {
189 log( "Using UNIX style line separation." );
190 lineSep = "\n";
191 }
192 else if ( "mac".equals( lineSeparator ) )
193 {
194 log( "Using Macintosh style line separation." );
195 lineSep = "\r";
196 }
197 else
198 {
199 throw new MojoExecutionException( "Invalid value for lineseparator, must be win, unix, or mac." );
200 }
201 }
202 setLineSeparator( lineSep );
203 }
204
205
206
207
208
209
210
211 public void setVerbose( final boolean verbose )
212 {
213 this.verbose = verbose;
214 super.setVerbose( verbose );
215 }
216
217
218
219
220
221
222 public void setBindingFile( final File bindingFile )
223 {
224 setBinding( bindingFile.getAbsolutePath() );
225 }
226
227
228
229
230
231
232
233 public void setBuilderProperties( File properties )
234 throws MojoExecutionException
235 {
236
237 if ( properties != null )
238 {
239 String filePath = properties.getAbsolutePath();
240 Properties customProperties = new Properties();
241 try
242 {
243 customProperties.load( new FileInputStream( filePath ) );
244 }
245 catch ( FileNotFoundException e )
246 {
247 throw new MojoExecutionException( "Properties file \"" + filePath + "\" not found" );
248 }
249 catch ( IOException e )
250 {
251 throw new MojoExecutionException( "Can't read properties file \"" + filePath + "\": " + e );
252 }
253 setDefaultProperties( customProperties );
254
255
256
257 if ( generateExtraCollectionMethods() )
258 {
259 verbose( "Overriding default castorbuilder.properties and setting createExtraMethods to true" );
260 fieldInfoFactory.setCreateExtraMethods( true );
261 }
262
263 String suffix = getProperty( CollectionInfo.REFERENCE_SUFFIX_PROPERTY, null );
264 if ( suffix != null )
265 {
266 verbose( "Overriding default castorbuilder.properties and " + "setting referenceSuffixProperty to "
267 + suffix );
268 }
269 fieldInfoFactory.setReferenceMethodSuffix( suffix );
270
271 if ( boundPropertiesEnabled() )
272 {
273 verbose( "Overriding default castorbuilder.properties and setting boundProperties to true" );
274 fieldInfoFactory.setBoundProperties( true );
275 }
276
277 }
278 }
279
280 }