1
2
3
4
5 package org.apache.maven.settings.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.settings.Activation;
16 import org.apache.maven.settings.ActivationFile;
17 import org.apache.maven.settings.ActivationOS;
18 import org.apache.maven.settings.ActivationProperty;
19 import org.apache.maven.settings.IdentifiableBase;
20 import org.apache.maven.settings.Mirror;
21 import org.apache.maven.settings.Profile;
22 import org.apache.maven.settings.Proxy;
23 import org.apache.maven.settings.Repository;
24 import org.apache.maven.settings.RepositoryBase;
25 import org.apache.maven.settings.RepositoryPolicy;
26 import org.apache.maven.settings.Server;
27 import org.apache.maven.settings.Settings;
28 import org.apache.maven.settings.TrackableBase;
29 import org.codehaus.plexus.util.xml.Xpp3Dom;
30 import org.codehaus.plexus.util.xml.pull.MXSerializer;
31 import org.codehaus.plexus.util.xml.pull.XmlSerializer;
32
33
34
35
36
37
38 public class SettingsXpp3Writer {
39
40
41
42
43
44
45
46
47
48 private String NAMESPACE;
49
50
51
52
53
54
55
56
57
58
59
60
61
62 public void write( Writer writer, Settings settings )
63 throws java.io.IOException
64 {
65 XmlSerializer serializer = new MXSerializer();
66 serializer.setProperty( "http://xmlpull.org/v1/doc/properties.html#serializer-indentation", " " );
67 serializer.setProperty( "http://xmlpull.org/v1/doc/properties.html#serializer-line-separator", "\n" );
68 serializer.setOutput( writer );
69 serializer.startDocument( settings.getModelEncoding(), null );
70 writeSettings( settings, "settings", serializer );
71 serializer.endDocument();
72 }
73
74
75
76
77
78
79
80
81
82 private void writeActivation( Activation activation, String tagName, XmlSerializer serializer )
83 throws java.io.IOException
84 {
85 if ( activation != null )
86 {
87 serializer.startTag( NAMESPACE, tagName );
88 if ( activation.isActiveByDefault() != false )
89 {
90 serializer.startTag( NAMESPACE, "activeByDefault" ).text( String.valueOf( activation.isActiveByDefault() ) ).endTag( NAMESPACE, "activeByDefault" );
91 }
92 if ( activation.getJdk() != null )
93 {
94 serializer.startTag( NAMESPACE, "jdk" ).text( activation.getJdk() ).endTag( NAMESPACE, "jdk" );
95 }
96 if ( activation.getOs() != null )
97 {
98 writeActivationOS( (ActivationOS) activation.getOs(), "os", serializer );
99 }
100 if ( activation.getProperty() != null )
101 {
102 writeActivationProperty( (ActivationProperty) activation.getProperty(), "property", serializer );
103 }
104 if ( activation.getFile() != null )
105 {
106 writeActivationFile( (ActivationFile) activation.getFile(), "file", serializer );
107 }
108 serializer.endTag( NAMESPACE, tagName );
109 }
110 }
111
112
113
114
115
116
117
118
119
120 private void writeActivationFile( ActivationFile activationFile, String tagName, XmlSerializer serializer )
121 throws java.io.IOException
122 {
123 if ( activationFile != null )
124 {
125 serializer.startTag( NAMESPACE, tagName );
126 if ( activationFile.getMissing() != null )
127 {
128 serializer.startTag( NAMESPACE, "missing" ).text( activationFile.getMissing() ).endTag( NAMESPACE, "missing" );
129 }
130 if ( activationFile.getExists() != null )
131 {
132 serializer.startTag( NAMESPACE, "exists" ).text( activationFile.getExists() ).endTag( NAMESPACE, "exists" );
133 }
134 serializer.endTag( NAMESPACE, tagName );
135 }
136 }
137
138
139
140
141
142
143
144
145
146 private void writeActivationOS( ActivationOS activationOS, String tagName, XmlSerializer serializer )
147 throws java.io.IOException
148 {
149 if ( activationOS != null )
150 {
151 serializer.startTag( NAMESPACE, tagName );
152 if ( activationOS.getName() != null )
153 {
154 serializer.startTag( NAMESPACE, "name" ).text( activationOS.getName() ).endTag( NAMESPACE, "name" );
155 }
156 if ( activationOS.getFamily() != null )
157 {
158 serializer.startTag( NAMESPACE, "family" ).text( activationOS.getFamily() ).endTag( NAMESPACE, "family" );
159 }
160 if ( activationOS.getArch() != null )
161 {
162 serializer.startTag( NAMESPACE, "arch" ).text( activationOS.getArch() ).endTag( NAMESPACE, "arch" );
163 }
164 if ( activationOS.getVersion() != null )
165 {
166 serializer.startTag( NAMESPACE, "version" ).text( activationOS.getVersion() ).endTag( NAMESPACE, "version" );
167 }
168 serializer.endTag( NAMESPACE, tagName );
169 }
170 }
171
172
173
174
175
176
177
178
179
180 private void writeActivationProperty( ActivationProperty activationProperty, String tagName, XmlSerializer serializer )
181 throws java.io.IOException
182 {
183 if ( activationProperty != null )
184 {
185 serializer.startTag( NAMESPACE, tagName );
186 if ( activationProperty.getName() != null )
187 {
188 serializer.startTag( NAMESPACE, "name" ).text( activationProperty.getName() ).endTag( NAMESPACE, "name" );
189 }
190 if ( activationProperty.getValue() != null )
191 {
192 serializer.startTag( NAMESPACE, "value" ).text( activationProperty.getValue() ).endTag( NAMESPACE, "value" );
193 }
194 serializer.endTag( NAMESPACE, tagName );
195 }
196 }
197
198
199
200
201
202
203
204
205
206 private void writeIdentifiableBase( IdentifiableBase identifiableBase, String tagName, XmlSerializer serializer )
207 throws java.io.IOException
208 {
209 if ( identifiableBase != null )
210 {
211 serializer.startTag( NAMESPACE, tagName );
212 if ( identifiableBase.getId() != null && !identifiableBase.getId().equals( "default" ) )
213 {
214 serializer.startTag( NAMESPACE, "id" ).text( identifiableBase.getId() ).endTag( NAMESPACE, "id" );
215 }
216 serializer.endTag( NAMESPACE, tagName );
217 }
218 }
219
220
221
222
223
224
225
226
227
228 private void writeMirror( Mirror mirror, String tagName, XmlSerializer serializer )
229 throws java.io.IOException
230 {
231 if ( mirror != null )
232 {
233 serializer.startTag( NAMESPACE, tagName );
234 if ( mirror.getMirrorOf() != null )
235 {
236 serializer.startTag( NAMESPACE, "mirrorOf" ).text( mirror.getMirrorOf() ).endTag( NAMESPACE, "mirrorOf" );
237 }
238 if ( mirror.getName() != null )
239 {
240 serializer.startTag( NAMESPACE, "name" ).text( mirror.getName() ).endTag( NAMESPACE, "name" );
241 }
242 if ( mirror.getUrl() != null )
243 {
244 serializer.startTag( NAMESPACE, "url" ).text( mirror.getUrl() ).endTag( NAMESPACE, "url" );
245 }
246 if ( mirror.getId() != null && !mirror.getId().equals( "default" ) )
247 {
248 serializer.startTag( NAMESPACE, "id" ).text( mirror.getId() ).endTag( NAMESPACE, "id" );
249 }
250 serializer.endTag( NAMESPACE, tagName );
251 }
252 }
253
254
255
256
257
258
259
260
261
262 private void writeProfile( Profile profile, String tagName, XmlSerializer serializer )
263 throws java.io.IOException
264 {
265 if ( profile != null )
266 {
267 serializer.startTag( NAMESPACE, tagName );
268 if ( profile.getActivation() != null )
269 {
270 writeActivation( (Activation) profile.getActivation(), "activation", serializer );
271 }
272 if ( profile.getProperties() != null && profile.getProperties().size() > 0 )
273 {
274 serializer.startTag( NAMESPACE, "properties" );
275 for ( Iterator iter = profile.getProperties().keySet().iterator(); iter.hasNext(); )
276 {
277 String key = (String) iter.next();
278 String value = (String) profile.getProperties().get( key );
279 serializer.startTag( NAMESPACE, "" + key + "" ).text( value ).endTag( NAMESPACE, "" + key + "" );
280 }
281 serializer.endTag( NAMESPACE, "properties" );
282 }
283 if ( profile.getRepositories() != null && profile.getRepositories().size() > 0 )
284 {
285 serializer.startTag( NAMESPACE, "repositories" );
286 for ( Iterator iter = profile.getRepositories().iterator(); iter.hasNext(); )
287 {
288 Repository o = (Repository) iter.next();
289 writeRepository( o, "repository", serializer );
290 }
291 serializer.endTag( NAMESPACE, "repositories" );
292 }
293 if ( profile.getPluginRepositories() != null && profile.getPluginRepositories().size() > 0 )
294 {
295 serializer.startTag( NAMESPACE, "pluginRepositories" );
296 for ( Iterator iter = profile.getPluginRepositories().iterator(); iter.hasNext(); )
297 {
298 Repository o = (Repository) iter.next();
299 writeRepository( o, "pluginRepository", serializer );
300 }
301 serializer.endTag( NAMESPACE, "pluginRepositories" );
302 }
303 if ( profile.getId() != null && !profile.getId().equals( "default" ) )
304 {
305 serializer.startTag( NAMESPACE, "id" ).text( profile.getId() ).endTag( NAMESPACE, "id" );
306 }
307 serializer.endTag( NAMESPACE, tagName );
308 }
309 }
310
311
312
313
314
315
316
317
318
319 private void writeProxy( Proxy proxy, String tagName, XmlSerializer serializer )
320 throws java.io.IOException
321 {
322 if ( proxy != null )
323 {
324 serializer.startTag( NAMESPACE, tagName );
325 if ( proxy.isActive() != true )
326 {
327 serializer.startTag( NAMESPACE, "active" ).text( String.valueOf( proxy.isActive() ) ).endTag( NAMESPACE, "active" );
328 }
329 if ( proxy.getProtocol() != null && !proxy.getProtocol().equals( "http" ) )
330 {
331 serializer.startTag( NAMESPACE, "protocol" ).text( proxy.getProtocol() ).endTag( NAMESPACE, "protocol" );
332 }
333 if ( proxy.getUsername() != null )
334 {
335 serializer.startTag( NAMESPACE, "username" ).text( proxy.getUsername() ).endTag( NAMESPACE, "username" );
336 }
337 if ( proxy.getPassword() != null )
338 {
339 serializer.startTag( NAMESPACE, "password" ).text( proxy.getPassword() ).endTag( NAMESPACE, "password" );
340 }
341 if ( proxy.getPort() != 8080 )
342 {
343 serializer.startTag( NAMESPACE, "port" ).text( String.valueOf( proxy.getPort() ) ).endTag( NAMESPACE, "port" );
344 }
345 if ( proxy.getHost() != null )
346 {
347 serializer.startTag( NAMESPACE, "host" ).text( proxy.getHost() ).endTag( NAMESPACE, "host" );
348 }
349 if ( proxy.getNonProxyHosts() != null )
350 {
351 serializer.startTag( NAMESPACE, "nonProxyHosts" ).text( proxy.getNonProxyHosts() ).endTag( NAMESPACE, "nonProxyHosts" );
352 }
353 if ( proxy.getId() != null && !proxy.getId().equals( "default" ) )
354 {
355 serializer.startTag( NAMESPACE, "id" ).text( proxy.getId() ).endTag( NAMESPACE, "id" );
356 }
357 serializer.endTag( NAMESPACE, tagName );
358 }
359 }
360
361
362
363
364
365
366
367
368
369 private void writeRepository( Repository repository, String tagName, XmlSerializer serializer )
370 throws java.io.IOException
371 {
372 if ( repository != null )
373 {
374 serializer.startTag( NAMESPACE, tagName );
375 if ( repository.getReleases() != null )
376 {
377 writeRepositoryPolicy( (RepositoryPolicy) repository.getReleases(), "releases", serializer );
378 }
379 if ( repository.getSnapshots() != null )
380 {
381 writeRepositoryPolicy( (RepositoryPolicy) repository.getSnapshots(), "snapshots", serializer );
382 }
383 if ( repository.getId() != null )
384 {
385 serializer.startTag( NAMESPACE, "id" ).text( repository.getId() ).endTag( NAMESPACE, "id" );
386 }
387 if ( repository.getName() != null )
388 {
389 serializer.startTag( NAMESPACE, "name" ).text( repository.getName() ).endTag( NAMESPACE, "name" );
390 }
391 if ( repository.getUrl() != null )
392 {
393 serializer.startTag( NAMESPACE, "url" ).text( repository.getUrl() ).endTag( NAMESPACE, "url" );
394 }
395 if ( repository.getLayout() != null && !repository.getLayout().equals( "default" ) )
396 {
397 serializer.startTag( NAMESPACE, "layout" ).text( repository.getLayout() ).endTag( NAMESPACE, "layout" );
398 }
399 serializer.endTag( NAMESPACE, tagName );
400 }
401 }
402
403
404
405
406
407
408
409
410
411 private void writeRepositoryBase( RepositoryBase repositoryBase, String tagName, XmlSerializer serializer )
412 throws java.io.IOException
413 {
414 if ( repositoryBase != null )
415 {
416 serializer.startTag( NAMESPACE, tagName );
417 if ( repositoryBase.getId() != null )
418 {
419 serializer.startTag( NAMESPACE, "id" ).text( repositoryBase.getId() ).endTag( NAMESPACE, "id" );
420 }
421 if ( repositoryBase.getName() != null )
422 {
423 serializer.startTag( NAMESPACE, "name" ).text( repositoryBase.getName() ).endTag( NAMESPACE, "name" );
424 }
425 if ( repositoryBase.getUrl() != null )
426 {
427 serializer.startTag( NAMESPACE, "url" ).text( repositoryBase.getUrl() ).endTag( NAMESPACE, "url" );
428 }
429 if ( repositoryBase.getLayout() != null && !repositoryBase.getLayout().equals( "default" ) )
430 {
431 serializer.startTag( NAMESPACE, "layout" ).text( repositoryBase.getLayout() ).endTag( NAMESPACE, "layout" );
432 }
433 serializer.endTag( NAMESPACE, tagName );
434 }
435 }
436
437
438
439
440
441
442
443
444
445 private void writeRepositoryPolicy( RepositoryPolicy repositoryPolicy, String tagName, XmlSerializer serializer )
446 throws java.io.IOException
447 {
448 if ( repositoryPolicy != null )
449 {
450 serializer.startTag( NAMESPACE, tagName );
451 if ( repositoryPolicy.isEnabled() != true )
452 {
453 serializer.startTag( NAMESPACE, "enabled" ).text( String.valueOf( repositoryPolicy.isEnabled() ) ).endTag( NAMESPACE, "enabled" );
454 }
455 if ( repositoryPolicy.getUpdatePolicy() != null )
456 {
457 serializer.startTag( NAMESPACE, "updatePolicy" ).text( repositoryPolicy.getUpdatePolicy() ).endTag( NAMESPACE, "updatePolicy" );
458 }
459 if ( repositoryPolicy.getChecksumPolicy() != null )
460 {
461 serializer.startTag( NAMESPACE, "checksumPolicy" ).text( repositoryPolicy.getChecksumPolicy() ).endTag( NAMESPACE, "checksumPolicy" );
462 }
463 serializer.endTag( NAMESPACE, tagName );
464 }
465 }
466
467
468
469
470
471
472
473
474
475 private void writeServer( Server server, String tagName, XmlSerializer serializer )
476 throws java.io.IOException
477 {
478 if ( server != null )
479 {
480 serializer.startTag( NAMESPACE, tagName );
481 if ( server.getUsername() != null )
482 {
483 serializer.startTag( NAMESPACE, "username" ).text( server.getUsername() ).endTag( NAMESPACE, "username" );
484 }
485 if ( server.getPassword() != null )
486 {
487 serializer.startTag( NAMESPACE, "password" ).text( server.getPassword() ).endTag( NAMESPACE, "password" );
488 }
489 if ( server.getPrivateKey() != null )
490 {
491 serializer.startTag( NAMESPACE, "privateKey" ).text( server.getPrivateKey() ).endTag( NAMESPACE, "privateKey" );
492 }
493 if ( server.getPassphrase() != null )
494 {
495 serializer.startTag( NAMESPACE, "passphrase" ).text( server.getPassphrase() ).endTag( NAMESPACE, "passphrase" );
496 }
497 if ( server.getFilePermissions() != null )
498 {
499 serializer.startTag( NAMESPACE, "filePermissions" ).text( server.getFilePermissions() ).endTag( NAMESPACE, "filePermissions" );
500 }
501 if ( server.getDirectoryPermissions() != null )
502 {
503 serializer.startTag( NAMESPACE, "directoryPermissions" ).text( server.getDirectoryPermissions() ).endTag( NAMESPACE, "directoryPermissions" );
504 }
505 if ( server.getConfiguration() != null )
506 {
507 ((Xpp3Dom) server.getConfiguration()).writeToSerializer( NAMESPACE, serializer );
508 }
509 if ( server.getId() != null && !server.getId().equals( "default" ) )
510 {
511 serializer.startTag( NAMESPACE, "id" ).text( server.getId() ).endTag( NAMESPACE, "id" );
512 }
513 serializer.endTag( NAMESPACE, tagName );
514 }
515 }
516
517
518
519
520
521
522
523
524
525 private void writeSettings( Settings settings, String tagName, XmlSerializer serializer )
526 throws java.io.IOException
527 {
528 if ( settings != null )
529 {
530 serializer.startTag( NAMESPACE, tagName );
531 if ( settings.getLocalRepository() != null )
532 {
533 serializer.startTag( NAMESPACE, "localRepository" ).text( settings.getLocalRepository() ).endTag( NAMESPACE, "localRepository" );
534 }
535 if ( settings.isInteractiveMode() != true )
536 {
537 serializer.startTag( NAMESPACE, "interactiveMode" ).text( String.valueOf( settings.isInteractiveMode() ) ).endTag( NAMESPACE, "interactiveMode" );
538 }
539 if ( settings.isUsePluginRegistry() != false )
540 {
541 serializer.startTag( NAMESPACE, "usePluginRegistry" ).text( String.valueOf( settings.isUsePluginRegistry() ) ).endTag( NAMESPACE, "usePluginRegistry" );
542 }
543 if ( settings.isOffline() != false )
544 {
545 serializer.startTag( NAMESPACE, "offline" ).text( String.valueOf( settings.isOffline() ) ).endTag( NAMESPACE, "offline" );
546 }
547 if ( settings.getProxies() != null && settings.getProxies().size() > 0 )
548 {
549 serializer.startTag( NAMESPACE, "proxies" );
550 for ( Iterator iter = settings.getProxies().iterator(); iter.hasNext(); )
551 {
552 Proxy o = (Proxy) iter.next();
553 writeProxy( o, "proxy", serializer );
554 }
555 serializer.endTag( NAMESPACE, "proxies" );
556 }
557 if ( settings.getServers() != null && settings.getServers().size() > 0 )
558 {
559 serializer.startTag( NAMESPACE, "servers" );
560 for ( Iterator iter = settings.getServers().iterator(); iter.hasNext(); )
561 {
562 Server o = (Server) iter.next();
563 writeServer( o, "server", serializer );
564 }
565 serializer.endTag( NAMESPACE, "servers" );
566 }
567 if ( settings.getMirrors() != null && settings.getMirrors().size() > 0 )
568 {
569 serializer.startTag( NAMESPACE, "mirrors" );
570 for ( Iterator iter = settings.getMirrors().iterator(); iter.hasNext(); )
571 {
572 Mirror o = (Mirror) iter.next();
573 writeMirror( o, "mirror", serializer );
574 }
575 serializer.endTag( NAMESPACE, "mirrors" );
576 }
577 if ( settings.getProfiles() != null && settings.getProfiles().size() > 0 )
578 {
579 serializer.startTag( NAMESPACE, "profiles" );
580 for ( Iterator iter = settings.getProfiles().iterator(); iter.hasNext(); )
581 {
582 Profile o = (Profile) iter.next();
583 writeProfile( o, "profile", serializer );
584 }
585 serializer.endTag( NAMESPACE, "profiles" );
586 }
587 if ( settings.getActiveProfiles() != null && settings.getActiveProfiles().size() > 0 )
588 {
589 serializer.startTag( NAMESPACE, "activeProfiles" );
590 for ( Iterator iter = settings.getActiveProfiles().iterator(); iter.hasNext(); )
591 {
592 String activeProfile = (String) iter.next();
593 serializer.startTag( NAMESPACE, "activeProfile" ).text( activeProfile ).endTag( NAMESPACE, "activeProfile" );
594 }
595 serializer.endTag( NAMESPACE, "activeProfiles" );
596 }
597 if ( settings.getPluginGroups() != null && settings.getPluginGroups().size() > 0 )
598 {
599 serializer.startTag( NAMESPACE, "pluginGroups" );
600 for ( Iterator iter = settings.getPluginGroups().iterator(); iter.hasNext(); )
601 {
602 String pluginGroup = (String) iter.next();
603 serializer.startTag( NAMESPACE, "pluginGroup" ).text( pluginGroup ).endTag( NAMESPACE, "pluginGroup" );
604 }
605 serializer.endTag( NAMESPACE, "pluginGroups" );
606 }
607 serializer.endTag( NAMESPACE, tagName );
608 }
609 }
610
611
612
613
614
615
616
617
618
619 private void writeTrackableBase( TrackableBase trackableBase, String tagName, XmlSerializer serializer )
620 throws java.io.IOException
621 {
622 if ( trackableBase != null )
623 {
624 serializer.startTag( NAMESPACE, tagName );
625 serializer.endTag( NAMESPACE, tagName );
626 }
627 }
628
629
630 }