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