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