1
2
3
4
5 package org.apache.maven.profiles.io.xpp3;
6
7
8
9
10
11 import java.io.Writer;
12 import java.text.DateFormat;
13 import java.util.Iterator;
14 import java.util.Locale;
15 import org.apache.maven.profiles.Activation;
16 import org.apache.maven.profiles.ActivationFile;
17 import org.apache.maven.profiles.ActivationOS;
18 import org.apache.maven.profiles.ActivationProperty;
19 import org.apache.maven.profiles.Profile;
20 import org.apache.maven.profiles.ProfilesRoot;
21 import org.apache.maven.profiles.Repository;
22 import org.apache.maven.profiles.RepositoryBase;
23 import org.apache.maven.profiles.RepositoryPolicy;
24 import org.codehaus.plexus.util.xml.pull.MXSerializer;
25 import org.codehaus.plexus.util.xml.pull.XmlSerializer;
26
27
28
29
30
31
32 public class ProfilesXpp3Writer {
33
34
35
36
37
38
39
40
41
42 private String NAMESPACE;
43
44
45
46
47
48
49
50
51
52
53
54
55
56 public void write( Writer writer, ProfilesRoot profilesRoot )
57 throws java.io.IOException
58 {
59 XmlSerializer serializer = new MXSerializer();
60 serializer.setProperty( "http://xmlpull.org/v1/doc/properties.html#serializer-indentation", " " );
61 serializer.setProperty( "http://xmlpull.org/v1/doc/properties.html#serializer-line-separator", "\n" );
62 serializer.setOutput( writer );
63 serializer.startDocument( profilesRoot.getModelEncoding(), null );
64 writeProfilesRoot( profilesRoot, "profilesXml", serializer );
65 serializer.endDocument();
66 }
67
68
69
70
71
72
73
74
75
76 private void writeActivation( Activation activation, String tagName, XmlSerializer serializer )
77 throws java.io.IOException
78 {
79 if ( activation != null )
80 {
81 serializer.startTag( NAMESPACE, tagName );
82 if ( activation.isActiveByDefault() != false )
83 {
84 serializer.startTag( NAMESPACE, "activeByDefault" ).text( String.valueOf( activation.isActiveByDefault() ) ).endTag( NAMESPACE, "activeByDefault" );
85 }
86 if ( activation.getJdk() != null )
87 {
88 serializer.startTag( NAMESPACE, "jdk" ).text( activation.getJdk() ).endTag( NAMESPACE, "jdk" );
89 }
90 if ( activation.getOs() != null )
91 {
92 writeActivationOS( (ActivationOS) activation.getOs(), "os", serializer );
93 }
94 if ( activation.getProperty() != null )
95 {
96 writeActivationProperty( (ActivationProperty) activation.getProperty(), "property", serializer );
97 }
98 if ( activation.getFile() != null )
99 {
100 writeActivationFile( (ActivationFile) activation.getFile(), "file", serializer );
101 }
102 serializer.endTag( NAMESPACE, tagName );
103 }
104 }
105
106
107
108
109
110
111
112
113
114 private void writeActivationFile( ActivationFile activationFile, String tagName, XmlSerializer serializer )
115 throws java.io.IOException
116 {
117 if ( activationFile != null )
118 {
119 serializer.startTag( NAMESPACE, tagName );
120 if ( activationFile.getMissing() != null )
121 {
122 serializer.startTag( NAMESPACE, "missing" ).text( activationFile.getMissing() ).endTag( NAMESPACE, "missing" );
123 }
124 if ( activationFile.getExists() != null )
125 {
126 serializer.startTag( NAMESPACE, "exists" ).text( activationFile.getExists() ).endTag( NAMESPACE, "exists" );
127 }
128 serializer.endTag( NAMESPACE, tagName );
129 }
130 }
131
132
133
134
135
136
137
138
139
140 private void writeActivationOS( ActivationOS activationOS, String tagName, XmlSerializer serializer )
141 throws java.io.IOException
142 {
143 if ( activationOS != null )
144 {
145 serializer.startTag( NAMESPACE, tagName );
146 if ( activationOS.getName() != null )
147 {
148 serializer.startTag( NAMESPACE, "name" ).text( activationOS.getName() ).endTag( NAMESPACE, "name" );
149 }
150 if ( activationOS.getFamily() != null )
151 {
152 serializer.startTag( NAMESPACE, "family" ).text( activationOS.getFamily() ).endTag( NAMESPACE, "family" );
153 }
154 if ( activationOS.getArch() != null )
155 {
156 serializer.startTag( NAMESPACE, "arch" ).text( activationOS.getArch() ).endTag( NAMESPACE, "arch" );
157 }
158 if ( activationOS.getVersion() != null )
159 {
160 serializer.startTag( NAMESPACE, "version" ).text( activationOS.getVersion() ).endTag( NAMESPACE, "version" );
161 }
162 serializer.endTag( NAMESPACE, tagName );
163 }
164 }
165
166
167
168
169
170
171
172
173
174 private void writeActivationProperty( ActivationProperty activationProperty, String tagName, XmlSerializer serializer )
175 throws java.io.IOException
176 {
177 if ( activationProperty != null )
178 {
179 serializer.startTag( NAMESPACE, tagName );
180 if ( activationProperty.getName() != null )
181 {
182 serializer.startTag( NAMESPACE, "name" ).text( activationProperty.getName() ).endTag( NAMESPACE, "name" );
183 }
184 if ( activationProperty.getValue() != null )
185 {
186 serializer.startTag( NAMESPACE, "value" ).text( activationProperty.getValue() ).endTag( NAMESPACE, "value" );
187 }
188 serializer.endTag( NAMESPACE, tagName );
189 }
190 }
191
192
193
194
195
196
197
198
199
200 private void writeProfile( Profile profile, String tagName, XmlSerializer serializer )
201 throws java.io.IOException
202 {
203 if ( profile != null )
204 {
205 serializer.startTag( NAMESPACE, tagName );
206 if ( profile.getId() != null )
207 {
208 serializer.startTag( NAMESPACE, "id" ).text( profile.getId() ).endTag( NAMESPACE, "id" );
209 }
210 if ( profile.getActivation() != null )
211 {
212 writeActivation( (Activation) profile.getActivation(), "activation", serializer );
213 }
214 if ( profile.getProperties() != null && profile.getProperties().size() > 0 )
215 {
216 serializer.startTag( NAMESPACE, "properties" );
217 for ( Iterator iter = profile.getProperties().keySet().iterator(); iter.hasNext(); )
218 {
219 String key = (String) iter.next();
220 String value = (String) profile.getProperties().get( key );
221 serializer.startTag( NAMESPACE, "" + key + "" ).text( value ).endTag( NAMESPACE, "" + key + "" );
222 }
223 serializer.endTag( NAMESPACE, "properties" );
224 }
225 if ( profile.getRepositories() != null && profile.getRepositories().size() > 0 )
226 {
227 serializer.startTag( NAMESPACE, "repositories" );
228 for ( Iterator iter = profile.getRepositories().iterator(); iter.hasNext(); )
229 {
230 Repository o = (Repository) iter.next();
231 writeRepository( o, "repository", serializer );
232 }
233 serializer.endTag( NAMESPACE, "repositories" );
234 }
235 if ( profile.getPluginRepositories() != null && profile.getPluginRepositories().size() > 0 )
236 {
237 serializer.startTag( NAMESPACE, "pluginRepositories" );
238 for ( Iterator iter = profile.getPluginRepositories().iterator(); iter.hasNext(); )
239 {
240 Repository o = (Repository) iter.next();
241 writeRepository( o, "pluginRepository", serializer );
242 }
243 serializer.endTag( NAMESPACE, "pluginRepositories" );
244 }
245 serializer.endTag( NAMESPACE, tagName );
246 }
247 }
248
249
250
251
252
253
254
255
256
257 private void writeProfilesRoot( ProfilesRoot profilesRoot, String tagName, XmlSerializer serializer )
258 throws java.io.IOException
259 {
260 if ( profilesRoot != null )
261 {
262 serializer.startTag( NAMESPACE, tagName );
263 if ( profilesRoot.getProfiles() != null && profilesRoot.getProfiles().size() > 0 )
264 {
265 serializer.startTag( NAMESPACE, "profiles" );
266 for ( Iterator iter = profilesRoot.getProfiles().iterator(); iter.hasNext(); )
267 {
268 Profile o = (Profile) iter.next();
269 writeProfile( o, "profile", serializer );
270 }
271 serializer.endTag( NAMESPACE, "profiles" );
272 }
273 if ( profilesRoot.getActiveProfiles() != null && profilesRoot.getActiveProfiles().size() > 0 )
274 {
275 serializer.startTag( NAMESPACE, "activeProfiles" );
276 for ( Iterator iter = profilesRoot.getActiveProfiles().iterator(); iter.hasNext(); )
277 {
278 String activeProfile = (String) iter.next();
279 serializer.startTag( NAMESPACE, "activeProfile" ).text( activeProfile ).endTag( NAMESPACE, "activeProfile" );
280 }
281 serializer.endTag( NAMESPACE, "activeProfiles" );
282 }
283 serializer.endTag( NAMESPACE, tagName );
284 }
285 }
286
287
288
289
290
291
292
293
294
295 private void writeRepository( Repository repository, String tagName, XmlSerializer serializer )
296 throws java.io.IOException
297 {
298 if ( repository != null )
299 {
300 serializer.startTag( NAMESPACE, tagName );
301 if ( repository.getReleases() != null )
302 {
303 writeRepositoryPolicy( (RepositoryPolicy) repository.getReleases(), "releases", serializer );
304 }
305 if ( repository.getSnapshots() != null )
306 {
307 writeRepositoryPolicy( (RepositoryPolicy) repository.getSnapshots(), "snapshots", serializer );
308 }
309 if ( repository.getId() != null )
310 {
311 serializer.startTag( NAMESPACE, "id" ).text( repository.getId() ).endTag( NAMESPACE, "id" );
312 }
313 if ( repository.getName() != null )
314 {
315 serializer.startTag( NAMESPACE, "name" ).text( repository.getName() ).endTag( NAMESPACE, "name" );
316 }
317 if ( repository.getUrl() != null )
318 {
319 serializer.startTag( NAMESPACE, "url" ).text( repository.getUrl() ).endTag( NAMESPACE, "url" );
320 }
321 if ( repository.getLayout() != null && !repository.getLayout().equals( "default" ) )
322 {
323 serializer.startTag( NAMESPACE, "layout" ).text( repository.getLayout() ).endTag( NAMESPACE, "layout" );
324 }
325 serializer.endTag( NAMESPACE, tagName );
326 }
327 }
328
329
330
331
332
333
334
335
336
337 private void writeRepositoryBase( RepositoryBase repositoryBase, String tagName, XmlSerializer serializer )
338 throws java.io.IOException
339 {
340 if ( repositoryBase != null )
341 {
342 serializer.startTag( NAMESPACE, tagName );
343 if ( repositoryBase.getId() != null )
344 {
345 serializer.startTag( NAMESPACE, "id" ).text( repositoryBase.getId() ).endTag( NAMESPACE, "id" );
346 }
347 if ( repositoryBase.getName() != null )
348 {
349 serializer.startTag( NAMESPACE, "name" ).text( repositoryBase.getName() ).endTag( NAMESPACE, "name" );
350 }
351 if ( repositoryBase.getUrl() != null )
352 {
353 serializer.startTag( NAMESPACE, "url" ).text( repositoryBase.getUrl() ).endTag( NAMESPACE, "url" );
354 }
355 if ( repositoryBase.getLayout() != null && !repositoryBase.getLayout().equals( "default" ) )
356 {
357 serializer.startTag( NAMESPACE, "layout" ).text( repositoryBase.getLayout() ).endTag( NAMESPACE, "layout" );
358 }
359 serializer.endTag( NAMESPACE, tagName );
360 }
361 }
362
363
364
365
366
367
368
369
370
371 private void writeRepositoryPolicy( RepositoryPolicy repositoryPolicy, String tagName, XmlSerializer serializer )
372 throws java.io.IOException
373 {
374 if ( repositoryPolicy != null )
375 {
376 serializer.startTag( NAMESPACE, tagName );
377 if ( repositoryPolicy.isEnabled() != true )
378 {
379 serializer.startTag( NAMESPACE, "enabled" ).text( String.valueOf( repositoryPolicy.isEnabled() ) ).endTag( NAMESPACE, "enabled" );
380 }
381 if ( repositoryPolicy.getUpdatePolicy() != null )
382 {
383 serializer.startTag( NAMESPACE, "updatePolicy" ).text( repositoryPolicy.getUpdatePolicy() ).endTag( NAMESPACE, "updatePolicy" );
384 }
385 if ( repositoryPolicy.getChecksumPolicy() != null )
386 {
387 serializer.startTag( NAMESPACE, "checksumPolicy" ).text( repositoryPolicy.getChecksumPolicy() ).endTag( NAMESPACE, "checksumPolicy" );
388 }
389 serializer.endTag( NAMESPACE, tagName );
390 }
391 }
392
393
394 }