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