1
2
3
4
5
6 package org.apache.maven.buildcache.xml.diff.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.diff.Diff;
16 import org.apache.maven.buildcache.xml.diff.Mismatch;
17 import org.apache.maven.buildcache.xml.diff.MismatchDetail;
18 import org.codehaus.plexus.util.xml.pull.MXSerializer;
19 import org.codehaus.plexus.util.xml.pull.XmlSerializer;
20
21
22
23
24
25
26 @SuppressWarnings( "all" )
27 public class BuildCacheDiffXpp3Writer
28 {
29
30
31
32
33
34
35
36
37 private static final String NAMESPACE = null;
38
39
40
41
42 private String fileComment = null;
43
44
45
46
47
48
49
50
51
52
53
54 public void setFileComment( String fileComment )
55 {
56 this.fileComment = fileComment;
57 }
58
59
60
61
62
63
64
65
66 public void write( Writer writer, Diff diff )
67 throws java.io.IOException
68 {
69 XmlSerializer serializer = new MXSerializer();
70 serializer.setProperty( "http://xmlpull.org/v1/doc/properties.html#serializer-indentation", " " );
71 serializer.setProperty( "http://xmlpull.org/v1/doc/properties.html#serializer-line-separator", "\n" );
72 serializer.setOutput( writer );
73 serializer.startDocument( diff.getModelEncoding(), null );
74 writeDiff( diff, "diff", serializer );
75 serializer.endDocument();
76 }
77
78
79
80
81
82
83
84
85 public void write( OutputStream stream, Diff diff )
86 throws java.io.IOException
87 {
88 XmlSerializer serializer = new MXSerializer();
89 serializer.setProperty( "http://xmlpull.org/v1/doc/properties.html#serializer-indentation", " " );
90 serializer.setProperty( "http://xmlpull.org/v1/doc/properties.html#serializer-line-separator", "\n" );
91 serializer.setOutput( stream, diff.getModelEncoding() );
92 serializer.startDocument( diff.getModelEncoding(), null );
93 writeDiff( diff, "diff", serializer );
94 serializer.endDocument();
95 }
96
97
98
99
100
101
102
103
104
105 private void writeDiff( Diff diff, String tagName, XmlSerializer serializer )
106 throws java.io.IOException
107 {
108 if ( this.fileComment != null )
109 {
110 serializer.comment(this.fileComment);
111 }
112 serializer.setPrefix( "", "http://maven.apache.org/BUILD-CACHE-DIFF/1.0.0" );
113 serializer.setPrefix( "xsi", "http://www.w3.org/2001/XMLSchema-instance" );
114 serializer.startTag( NAMESPACE, tagName );
115 serializer.attribute( "", "xsi:schemaLocation", "http://maven.apache.org/BUILD-CACHE-DIFF/1.0.0 https://maven.apache.org/xsd/build-cache-diff-1.0.0.xsd" );
116 if ( ( diff.getMismatches() != null ) && ( diff.getMismatches().size() > 0 ) )
117 {
118 serializer.startTag( NAMESPACE, "mismatches" );
119 for ( Iterator iter = diff.getMismatches().iterator(); iter.hasNext(); )
120 {
121 Mismatch o = (Mismatch) iter.next();
122 writeMismatch( o, "mismatch", serializer );
123 }
124 serializer.endTag( NAMESPACE, "mismatches" );
125 }
126 serializer.endTag( NAMESPACE, tagName );
127 }
128
129
130
131
132
133
134
135
136
137 private void writeMismatch( Mismatch mismatch, String tagName, XmlSerializer serializer )
138 throws java.io.IOException
139 {
140 serializer.startTag( NAMESPACE, tagName );
141 if ( mismatch.getItem() != null )
142 {
143 serializer.attribute( NAMESPACE, "item", mismatch.getItem() );
144 }
145 if ( mismatch.getCurrent() != null )
146 {
147 serializer.attribute( NAMESPACE, "current", mismatch.getCurrent() );
148 }
149 if ( mismatch.getBaseline() != null )
150 {
151 serializer.attribute( NAMESPACE, "baseline", mismatch.getBaseline() );
152 }
153 if ( mismatch.getReason() != null )
154 {
155 serializer.attribute( NAMESPACE, "reason", mismatch.getReason() );
156 }
157 if ( mismatch.getResolution() != null )
158 {
159 serializer.attribute( NAMESPACE, "resolution", mismatch.getResolution() );
160 }
161 if ( mismatch.getContext() != null )
162 {
163 serializer.attribute( NAMESPACE, "context", mismatch.getContext() );
164 }
165 if ( ( mismatch.getDetail() != null ) && ( mismatch.getDetail().size() > 0 ) )
166 {
167 serializer.startTag( NAMESPACE, "detail" );
168 for ( Iterator iter = mismatch.getDetail().iterator(); iter.hasNext(); )
169 {
170 MismatchDetail o = (MismatchDetail) iter.next();
171 writeMismatchDetail( o, "detail", serializer );
172 }
173 serializer.endTag( NAMESPACE, "detail" );
174 }
175 serializer.endTag( NAMESPACE, tagName );
176 }
177
178
179
180
181
182
183
184
185
186 private void writeMismatchDetail( MismatchDetail mismatchDetail, String tagName, XmlSerializer serializer )
187 throws java.io.IOException
188 {
189 serializer.startTag( NAMESPACE, tagName );
190 serializer.endTag( NAMESPACE, tagName );
191 }
192
193 }