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 | 0 | 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 | 0 | String encoding = DEFAULT_FILE_ENCODING; |
65 | |
|
66 | 0 | if ( ! dependenciesBuildFile.getParentFile().exists() ) |
67 | |
{ |
68 | 0 | dependenciesBuildFile.getParentFile().mkdirs(); |
69 | |
} |
70 | 0 | outputStreamWriter = new OutputStreamWriter( new FileOutputStream( dependenciesBuildFile ), encoding ); |
71 | |
|
72 | 0 | writer = |
73 | |
new PrettyPrintXMLWriter( outputStreamWriter, StringUtils.repeat( " ", DEFAULT_INDENTATION_SIZE ), |
74 | |
encoding, null ); |
75 | 0 | writer.startElement( "project" ); |
76 | 0 | writer.addAttribute( "name", name ); |
77 | 0 | writer.addAttribute( "default", defaultTarget ); |
78 | |
|
79 | 0 | XmlWriterUtil.writeLineBreak( writer ); |
80 | 0 | } |
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
public void closeAntBuild() |
88 | |
throws IOException |
89 | |
{ |
90 | 0 | writer.endElement(); |
91 | |
|
92 | 0 | XmlWriterUtil.writeLineBreak( writer ); |
93 | |
|
94 | 0 | IOUtil.close( outputStreamWriter ); |
95 | 0 | } |
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | |
public void openTarget( String targetName ) |
104 | |
throws IOException |
105 | |
{ |
106 | 0 | writer.startElement( "target" ); |
107 | 0 | writer.addAttribute( "name", targetName ); |
108 | 0 | } |
109 | |
|
110 | |
|
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
public void closeTarget() |
116 | |
throws IOException |
117 | |
{ |
118 | 0 | writer.endElement(); |
119 | 0 | } |
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | |
|
127 | |
public void writeFileSet( FileSet fileSet, String id ) |
128 | |
{ |
129 | 0 | writer.startElement( "fileset" ); |
130 | |
|
131 | 0 | if ( id != null ) |
132 | |
{ |
133 | 0 | writer.addAttribute( "id", id ); |
134 | |
} |
135 | |
|
136 | 0 | File dir = fileSet.getDir( fileSet.getProject() ); |
137 | 0 | writer.addAttribute( "dir", dir.getAbsolutePath() ); |
138 | |
|
139 | 0 | DirectoryScanner scanner = fileSet.getDirectoryScanner( fileSet.getProject() ); |
140 | 0 | scanner.scan(); |
141 | 0 | String[] files = scanner.getIncludedFiles(); |
142 | |
|
143 | 0 | for ( int i = 0; i < files.length; ++i ) |
144 | |
{ |
145 | 0 | writer.startElement( "include" ); |
146 | 0 | writer.addAttribute( "name", files[i] ); |
147 | 0 | writer.endElement(); |
148 | |
} |
149 | |
|
150 | 0 | writer.endElement(); |
151 | 0 | } |
152 | |
|
153 | |
|
154 | |
|
155 | |
|
156 | |
|
157 | |
|
158 | |
|
159 | |
public void writeProperty( String name, String value ) |
160 | |
{ |
161 | 0 | writer.startElement( "property" ); |
162 | |
|
163 | 0 | writer.addAttribute( "name", name ); |
164 | 0 | writer.addAttribute( "value", value ); |
165 | |
|
166 | 0 | writer.endElement(); |
167 | 0 | } |
168 | |
|
169 | |
|
170 | |
|
171 | |
|
172 | |
|
173 | |
|
174 | |
public void writeEcho( String message ) |
175 | |
{ |
176 | 0 | writer.startElement( "echo" ); |
177 | |
|
178 | 0 | writer.addAttribute( "level", "info" ); |
179 | 0 | writer.addAttribute( "message", message ); |
180 | |
|
181 | 0 | writer.endElement(); |
182 | 0 | } |
183 | |
|
184 | |
|
185 | |
|
186 | |
|
187 | |
|
188 | |
|
189 | |
|
190 | |
public void writeFileList( FileList fileList, String id ) |
191 | |
{ |
192 | 0 | writer.startElement( "filelist" ); |
193 | 0 | writer.addAttribute( "id", id ); |
194 | 0 | File dir = fileList.getDir( fileList.getProject() ); |
195 | 0 | writer.addAttribute( "dir", dir.getAbsolutePath() ); |
196 | |
|
197 | 0 | String[] files = fileList.getFiles( fileList.getProject() ); |
198 | 0 | for ( int i = 0; i < files.length; ++i ) |
199 | |
{ |
200 | 0 | writer.startElement( "file" ); |
201 | 0 | writer.addAttribute( "name", files[i] ); |
202 | 0 | writer.endElement(); |
203 | |
} |
204 | 0 | writer.endElement(); |
205 | 0 | } |
206 | |
|
207 | |
|
208 | |
|
209 | |
|
210 | |
|
211 | |
|
212 | |
|
213 | |
public void writePath( Path path, String pathId ) |
214 | |
{ |
215 | 0 | writer.startElement( "path" ); |
216 | 0 | writer.addAttribute( "id", pathId ); |
217 | 0 | String[] paths = path.list(); |
218 | 0 | for ( int i = 0; i < paths.length; ++i ) |
219 | |
{ |
220 | 0 | writer.startElement( "pathelement" ); |
221 | 0 | writer.addAttribute( "path", paths[i] ); |
222 | 0 | writer.endElement(); |
223 | |
} |
224 | 0 | writer.endElement(); |
225 | 0 | } |
226 | |
} |