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