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