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.buildcache.xml.build.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.buildcache.xml.build.Artifact;
34 import org.apache.maven.buildcache.xml.build.Build;
35 import org.apache.maven.buildcache.xml.build.CompletedExecution;
36 import org.apache.maven.buildcache.xml.build.DigestItem;
37 import org.apache.maven.buildcache.xml.build.ProjectsInputInfo;
38 import org.apache.maven.buildcache.xml.build.PropertyValue;
39 import org.apache.maven.buildcache.xml.build.Scm;
40 import org.codehaus.plexus.util.xml.pull.MXSerializer;
41 import org.codehaus.plexus.util.xml.pull.XmlSerializer;
42
43
44
45
46
47
48 @SuppressWarnings( "all" )
49 public class BuildCacheBuildXpp3Writer
50 {
51
52
53
54
55
56
57
58
59 private static final String NAMESPACE = null;
60
61
62
63
64 private String fileComment = null;
65
66
67
68
69
70
71
72
73
74
75
76 public void setFileComment( String fileComment )
77 {
78 this.fileComment = fileComment;
79 }
80
81
82
83
84
85
86
87
88 public void write( Writer writer, Build build )
89 throws java.io.IOException
90 {
91 XmlSerializer serializer = new MXSerializer();
92 serializer.setProperty( "http://xmlpull.org/v1/doc/properties.html#serializer-indentation", " " );
93 serializer.setProperty( "http://xmlpull.org/v1/doc/properties.html#serializer-line-separator", "\n" );
94 serializer.setOutput( writer );
95 serializer.startDocument( build.getModelEncoding(), null );
96 writeBuild( build, "build", serializer );
97 serializer.endDocument();
98 }
99
100
101
102
103
104
105
106
107 public void write( OutputStream stream, Build build )
108 throws java.io.IOException
109 {
110 XmlSerializer serializer = new MXSerializer();
111 serializer.setProperty( "http://xmlpull.org/v1/doc/properties.html#serializer-indentation", " " );
112 serializer.setProperty( "http://xmlpull.org/v1/doc/properties.html#serializer-line-separator", "\n" );
113 serializer.setOutput( stream, build.getModelEncoding() );
114 serializer.startDocument( build.getModelEncoding(), null );
115 writeBuild( build, "build", serializer );
116 serializer.endDocument();
117 }
118
119
120
121
122
123
124
125
126
127 private void writeArtifact( Artifact artifact, String tagName, XmlSerializer serializer )
128 throws java.io.IOException
129 {
130 serializer.startTag( NAMESPACE, tagName );
131 if ( artifact.getGroupId() != null )
132 {
133 serializer.startTag( NAMESPACE, "groupId" ).text( artifact.getGroupId() ).endTag( NAMESPACE, "groupId" );
134 }
135 if ( artifact.getArtifactId() != null )
136 {
137 serializer.startTag( NAMESPACE, "artifactId" ).text( artifact.getArtifactId() ).endTag( NAMESPACE, "artifactId" );
138 }
139 if ( artifact.getVersion() != null )
140 {
141 serializer.startTag( NAMESPACE, "version" ).text( artifact.getVersion() ).endTag( NAMESPACE, "version" );
142 }
143 if ( artifact.getClassifier() != null )
144 {
145 serializer.startTag( NAMESPACE, "classifier" ).text( artifact.getClassifier() ).endTag( NAMESPACE, "classifier" );
146 }
147 if ( artifact.getType() != null )
148 {
149 serializer.startTag( NAMESPACE, "type" ).text( artifact.getType() ).endTag( NAMESPACE, "type" );
150 }
151 if ( artifact.getScope() != null )
152 {
153 serializer.startTag( NAMESPACE, "scope" ).text( artifact.getScope() ).endTag( NAMESPACE, "scope" );
154 }
155 if ( artifact.getFileName() != null )
156 {
157 serializer.startTag( NAMESPACE, "fileName" ).text( artifact.getFileName() ).endTag( NAMESPACE, "fileName" );
158 }
159 if ( artifact.getFileHash() != null )
160 {
161 serializer.startTag( NAMESPACE, "fileHash" ).text( artifact.getFileHash() ).endTag( NAMESPACE, "fileHash" );
162 }
163 if ( artifact.getFileSize() != 0L )
164 {
165 serializer.startTag( NAMESPACE, "fileSize" ).text( String.valueOf( artifact.getFileSize() ) ).endTag( NAMESPACE, "fileSize" );
166 }
167 if ( artifact.getFilePath() != null )
168 {
169 serializer.startTag( NAMESPACE, "filePath" ).text( artifact.getFilePath() ).endTag( NAMESPACE, "filePath" );
170 }
171 serializer.endTag( NAMESPACE, tagName );
172 }
173
174
175
176
177
178
179
180
181
182 private void writeBuild( Build build, String tagName, XmlSerializer serializer )
183 throws java.io.IOException
184 {
185 if ( this.fileComment != null )
186 {
187 serializer.comment(this.fileComment);
188 }
189 serializer.setPrefix( "", "http://maven.apache.org/BUILD-CACHE-BUILD/1.2.0" );
190 serializer.setPrefix( "xsi", "http://www.w3.org/2001/XMLSchema-instance" );
191 serializer.startTag( NAMESPACE, tagName );
192 serializer.attribute( "", "xsi:schemaLocation", "http://maven.apache.org/BUILD-CACHE-BUILD/1.2.0 https://maven.apache.org/xsd/build-cache-build-1.2.0.xsd" );
193 if ( build.getCacheImplementationVersion() != null )
194 {
195 serializer.startTag( NAMESPACE, "cacheImplementationVersion" ).text( build.getCacheImplementationVersion() ).endTag( NAMESPACE, "cacheImplementationVersion" );
196 }
197 if ( build.is_final() != null )
198 {
199 serializer.startTag( NAMESPACE, "final" ).text( String.valueOf( build.is_final() ) ).endTag( NAMESPACE, "final" );
200 }
201 if ( build.getHashFunction() != null )
202 {
203 serializer.startTag( NAMESPACE, "hashFunction" ).text( build.getHashFunction() ).endTag( NAMESPACE, "hashFunction" );
204 }
205 if ( build.getBuildTime() != null )
206 {
207 serializer.startTag( NAMESPACE, "buildTime" ).text( new java.text.SimpleDateFormat( "yyyy-MM-dd'T'HH:mm:ss.SSS", java.util.Locale.US ).format( build.getBuildTime() ) ).endTag( NAMESPACE, "buildTime" );
208 }
209 if ( build.getBuildServer() != null )
210 {
211 serializer.startTag( NAMESPACE, "buildServer" ).text( build.getBuildServer() ).endTag( NAMESPACE, "buildServer" );
212 }
213 if ( build.getScm() != null )
214 {
215 writeScm( (Scm) build.getScm(), "scm", serializer );
216 }
217 if ( ( build.getGoals() != null ) && ( build.getGoals().size() > 0 ) )
218 {
219 serializer.startTag( NAMESPACE, "goals" );
220 for ( Iterator iter = build.getGoals().iterator(); iter.hasNext(); )
221 {
222 String goal = (String) iter.next();
223 serializer.startTag( NAMESPACE, "goal" ).text( goal ).endTag( NAMESPACE, "goal" );
224 }
225 serializer.endTag( NAMESPACE, "goals" );
226 }
227 if ( build.getArtifact() != null )
228 {
229 writeArtifact( (Artifact) build.getArtifact(), "artifact", serializer );
230 }
231 if ( ( build.getAttachedArtifacts() != null ) && ( build.getAttachedArtifacts().size() > 0 ) )
232 {
233 serializer.startTag( NAMESPACE, "attachedArtifacts" );
234 for ( Iterator iter = build.getAttachedArtifacts().iterator(); iter.hasNext(); )
235 {
236 Artifact o = (Artifact) iter.next();
237 writeArtifact( o, "attachedArtifact", serializer );
238 }
239 serializer.endTag( NAMESPACE, "attachedArtifacts" );
240 }
241 if ( ( build.getExecutions() != null ) && ( build.getExecutions().size() > 0 ) )
242 {
243 serializer.startTag( NAMESPACE, "executions" );
244 for ( Iterator iter = build.getExecutions().iterator(); iter.hasNext(); )
245 {
246 CompletedExecution o = (CompletedExecution) iter.next();
247 writeCompletedExecution( o, "execution", serializer );
248 }
249 serializer.endTag( NAMESPACE, "executions" );
250 }
251 if ( build.getProjectsInputInfo() != null )
252 {
253 writeProjectsInputInfo( (ProjectsInputInfo) build.getProjectsInputInfo(), "projectsInputInfo", serializer );
254 }
255 serializer.endTag( NAMESPACE, tagName );
256 }
257
258
259
260
261
262
263
264
265
266 private void writeCompletedExecution( CompletedExecution completedExecution, String tagName, XmlSerializer serializer )
267 throws java.io.IOException
268 {
269 serializer.startTag( NAMESPACE, tagName );
270 if ( completedExecution.getExecutionKey() != null )
271 {
272 serializer.startTag( NAMESPACE, "executionKey" ).text( completedExecution.getExecutionKey() ).endTag( NAMESPACE, "executionKey" );
273 }
274 if ( completedExecution.getMojoClassName() != null )
275 {
276 serializer.startTag( NAMESPACE, "mojoClassName" ).text( completedExecution.getMojoClassName() ).endTag( NAMESPACE, "mojoClassName" );
277 }
278 if ( ( completedExecution.getProperties() != null ) && ( completedExecution.getProperties().size() > 0 ) )
279 {
280 serializer.startTag( NAMESPACE, "properties" );
281 for ( Iterator iter = completedExecution.getProperties().iterator(); iter.hasNext(); )
282 {
283 PropertyValue o = (PropertyValue) iter.next();
284 writePropertyValue( o, "property", serializer );
285 }
286 serializer.endTag( NAMESPACE, "properties" );
287 }
288 serializer.endTag( NAMESPACE, tagName );
289 }
290
291
292
293
294
295
296
297
298
299 private void writeDigestItem( DigestItem digestItem, String tagName, XmlSerializer serializer )
300 throws java.io.IOException
301 {
302 serializer.startTag( NAMESPACE, tagName );
303 if ( digestItem.getType() != null )
304 {
305 serializer.attribute( NAMESPACE, "type", digestItem.getType() );
306 }
307 if ( digestItem.getHash() != null )
308 {
309 serializer.attribute( NAMESPACE, "hash", digestItem.getHash() );
310 }
311 if ( digestItem.getFileChecksum() != null )
312 {
313 serializer.attribute( NAMESPACE, "fileChecksum", digestItem.getFileChecksum() );
314 }
315 if ( digestItem.getContent() != null )
316 {
317 serializer.attribute( NAMESPACE, "content", digestItem.getContent() );
318 }
319 if ( digestItem.getIsText() != null )
320 {
321 serializer.attribute( NAMESPACE, "isText", digestItem.getIsText() );
322 }
323 if ( digestItem.getCharset() != null )
324 {
325 serializer.attribute( NAMESPACE, "charset", digestItem.getCharset() );
326 }
327 if ( digestItem.getEol() != null )
328 {
329 serializer.attribute( NAMESPACE, "eol", digestItem.getEol() );
330 }
331 serializer.text( digestItem.getValue() );
332 serializer.endTag( NAMESPACE, tagName );
333 }
334
335
336
337
338
339
340
341
342
343 private void writeProjectsInputInfo( ProjectsInputInfo projectsInputInfo, String tagName, XmlSerializer serializer )
344 throws java.io.IOException
345 {
346 serializer.startTag( NAMESPACE, tagName );
347 if ( projectsInputInfo.getChecksum() != null )
348 {
349 serializer.startTag( NAMESPACE, "checksum" ).text( projectsInputInfo.getChecksum() ).endTag( NAMESPACE, "checksum" );
350 }
351 if ( ( projectsInputInfo.getItems() != null ) && ( projectsInputInfo.getItems().size() > 0 ) )
352 {
353 serializer.startTag( NAMESPACE, "items" );
354 for ( Iterator iter = projectsInputInfo.getItems().iterator(); iter.hasNext(); )
355 {
356 DigestItem o = (DigestItem) iter.next();
357 writeDigestItem( o, "item", serializer );
358 }
359 serializer.endTag( NAMESPACE, "items" );
360 }
361 serializer.endTag( NAMESPACE, tagName );
362 }
363
364
365
366
367
368
369
370
371
372 private void writePropertyValue( PropertyValue propertyValue, String tagName, XmlSerializer serializer )
373 throws java.io.IOException
374 {
375 serializer.startTag( NAMESPACE, tagName );
376 if ( propertyValue.getName() != null )
377 {
378 serializer.attribute( NAMESPACE, "name", propertyValue.getName() );
379 }
380 if ( propertyValue.isTracked() != null )
381 {
382 serializer.attribute( NAMESPACE, "tracked", String.valueOf( propertyValue.isTracked() ) );
383 }
384 serializer.text( propertyValue.getValue() );
385 serializer.endTag( NAMESPACE, tagName );
386 }
387
388
389
390
391
392
393
394
395
396 private void writeScm( Scm scm, String tagName, XmlSerializer serializer )
397 throws java.io.IOException
398 {
399 serializer.startTag( NAMESPACE, tagName );
400 if ( scm.getSourceBranch() != null )
401 {
402 serializer.startTag( NAMESPACE, "sourceBranch" ).text( scm.getSourceBranch() ).endTag( NAMESPACE, "sourceBranch" );
403 }
404 if ( scm.getRevision() != null )
405 {
406 serializer.startTag( NAMESPACE, "revision" ).text( scm.getRevision() ).endTag( NAMESPACE, "revision" );
407 }
408 serializer.endTag( NAMESPACE, tagName );
409 }
410
411 }