View Javadoc

1   package org.apache.maven.artifact.ant.util;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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   * Utility class for writing an Ant build file.
39   */
40  public class AntBuildWriter
41  {
42      public static final String DEFAULT_FILE_ENCODING = "UTF-8";
43  
44      /**
45       * The default line indenter
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       * Open an Ant build file for writing. Opens the file and prints the opening project tag.
55       *
56       * @param buildFile
57       * @param name
58       * @param defaultTarget
59       * @throws IOException
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       * Close the ant build writer
84       *
85       * @throws IOException
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       * Open a target tag
99       *
100      * @param targetName
101      * @throws IOException
102      */
103     public void openTarget( String targetName )
104         throws IOException
105     {
106         writer.startElement( "target" );
107         writer.addAttribute( "name", targetName );
108     }
109 
110     /**
111      * Close a tag.
112      *
113      * @throws IOException
114      */
115     public void closeTarget()
116         throws IOException
117     {
118         writer.endElement();
119     }
120 
121     /**
122      * Write an Ant fileset
123      *
124      * @param fileSet
125      * @param id
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      * Write an ant property
155      *
156      * @param name
157      * @param value
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      * Write an Ant echo task
171      *
172      * @param message
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      * Write an Ant file list
186      *
187      * @param fileList
188      * @param id
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      * Write a path.
209      *
210      * @param path
211      * @param pathId
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 }