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