1 package org.apache.maven.artifact.ant.util;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.File;
23 import java.io.FileOutputStream;
24 import java.io.IOException;
25 import java.io.OutputStreamWriter;
26
27 import org.apache.tools.ant.DirectoryScanner;
28 import org.apache.tools.ant.types.FileList;
29 import org.apache.tools.ant.types.FileSet;
30 import org.apache.tools.ant.types.Path;
31 import org.codehaus.plexus.util.IOUtil;
32 import org.codehaus.plexus.util.StringUtils;
33 import org.codehaus.plexus.util.xml.PrettyPrintXMLWriter;
34 import org.codehaus.plexus.util.xml.XMLWriter;
35 import org.codehaus.plexus.util.xml.XmlWriterUtil;
36
37
38
39
40 public class AntBuildWriter
41 {
42 public static final String DEFAULT_FILE_ENCODING = "UTF-8";
43
44
45
46
47 protected static final int DEFAULT_INDENTATION_SIZE = XmlWriterUtil.DEFAULT_INDENTATION_SIZE;
48
49 private XMLWriter writer;
50
51 private OutputStreamWriter outputStreamWriter;
52
53
54
55
56
57
58
59
60
61 public void openAntBuild( File dependenciesBuildFile, String name, String defaultTarget )
62 throws IOException
63 {
64 String encoding = DEFAULT_FILE_ENCODING;
65
66 if ( ! dependenciesBuildFile.getParentFile().exists() )
67 {
68 dependenciesBuildFile.getParentFile().mkdirs();
69 }
70 outputStreamWriter = new OutputStreamWriter( new FileOutputStream( dependenciesBuildFile ), encoding );
71
72 writer =
73 new PrettyPrintXMLWriter( outputStreamWriter, StringUtils.repeat( " ", DEFAULT_INDENTATION_SIZE ),
74 encoding, null );
75 writer.startElement( "project" );
76 writer.addAttribute( "name", name );
77 writer.addAttribute( "default", defaultTarget );
78
79 XmlWriterUtil.writeLineBreak( writer );
80 }
81
82
83
84
85
86
87 public void closeAntBuild()
88 throws IOException
89 {
90 writer.endElement();
91
92 XmlWriterUtil.writeLineBreak( writer );
93
94 IOUtil.close( outputStreamWriter );
95 }
96
97
98
99
100
101
102
103 public void openTarget( String targetName )
104 throws IOException
105 {
106 writer.startElement( "target" );
107 writer.addAttribute( "name", targetName );
108 }
109
110
111
112
113
114
115 public void closeTarget()
116 throws IOException
117 {
118 writer.endElement();
119 }
120
121
122
123
124
125
126
127 public void writeFileSet( FileSet fileSet, String id )
128 {
129 writer.startElement( "fileset" );
130
131 if ( id != null )
132 {
133 writer.addAttribute( "id", id );
134 }
135
136 File dir = fileSet.getDir( fileSet.getProject() );
137 writer.addAttribute( "dir", dir.getAbsolutePath() );
138
139 DirectoryScanner scanner = fileSet.getDirectoryScanner( fileSet.getProject() );
140 scanner.scan();
141 String[] files = scanner.getIncludedFiles();
142
143 for ( int i = 0; i < files.length; ++i )
144 {
145 writer.startElement( "include" );
146 writer.addAttribute( "name", files[i] );
147 writer.endElement();
148 }
149
150 writer.endElement();
151 }
152
153
154
155
156
157
158
159 public void writeProperty( String name, String value )
160 {
161 writer.startElement( "property" );
162
163 writer.addAttribute( "name", name );
164 writer.addAttribute( "value", value );
165
166 writer.endElement();
167 }
168
169
170
171
172
173
174 public void writeEcho( String message )
175 {
176 writer.startElement( "echo" );
177
178 writer.addAttribute( "level", "info" );
179 writer.addAttribute( "message", message );
180
181 writer.endElement();
182 }
183
184
185
186
187
188
189
190 public void writeFileList( FileList fileList, String id )
191 {
192 writer.startElement( "filelist" );
193 writer.addAttribute( "id", id );
194 File dir = fileList.getDir( fileList.getProject() );
195 writer.addAttribute( "dir", dir.getAbsolutePath() );
196
197 String[] files = fileList.getFiles( fileList.getProject() );
198 for ( int i = 0; i < files.length; ++i )
199 {
200 writer.startElement( "file" );
201 writer.addAttribute( "name", files[i] );
202 writer.endElement();
203 }
204 writer.endElement();
205 }
206
207
208
209
210
211
212
213 public void writePath( Path path, String pathId )
214 {
215 writer.startElement( "path" );
216 writer.addAttribute( "id", pathId );
217 String[] paths = path.list();
218 for ( int i = 0; i < paths.length; ++i )
219 {
220 writer.startElement( "pathelement" );
221 writer.addAttribute( "path", paths[i] );
222 writer.endElement();
223 }
224 writer.endElement();
225 }
226 }