Save This Page
Home » maven-ant-tasks-2.1.0-src » org.apache.maven.artifact.ant.util » [javadoc | source]
    1   package org.apache.maven.artifact.ant.util;
    2   
    3   import java.io.File;
    4   import java.io.FileOutputStream;
    5   import java.io.IOException;
    6   import java.io.OutputStreamWriter;
    7   
    8   import org.apache.tools.ant.DirectoryScanner;
    9   import org.apache.tools.ant.types.FileList;
   10   import org.apache.tools.ant.types.FileSet;
   11   import org.apache.tools.ant.types.Path;
   12   import org.codehaus.plexus.util.IOUtil;
   13   import org.codehaus.plexus.util.StringUtils;
   14   import org.codehaus.plexus.util.xml.PrettyPrintXMLWriter;
   15   import org.codehaus.plexus.util.xml.XMLWriter;
   16   import org.codehaus.plexus.util.xml.XmlWriterUtil;
   17   
   18   /*
   19    * Licensed to the Apache Software Foundation (ASF) under one
   20    * or more contributor license agreements.  See the NOTICE file
   21    * distributed with this work for additional information
   22    * regarding copyright ownership.  The ASF licenses this file
   23    * to you under the Apache License, Version 2.0 (the
   24    * "License"); you may not use this file except in compliance
   25    * with the License.  You may obtain a copy of the License at
   26    *
   27    *  http://www.apache.org/licenses/LICENSE-2.0
   28    *
   29    * Unless required by applicable law or agreed to in writing,
   30    * software distributed under the License is distributed on an
   31    * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
   32    * KIND, either express or implied.  See the License for the
   33    * specific language governing permissions and limitations
   34    * under the License.
   35    */
   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       /**
   99        * Open a target tag
  100        * 
  101        * @param targetName
  102        * @throws IOException
  103        */
  104       public void openTarget( String targetName )
  105           throws IOException
  106       {
  107           writer.startElement( "target" );
  108           writer.addAttribute( "name", targetName );
  109       }
  110   
  111       /**
  112        * Close a tag.
  113        * 
  114        * @throws IOException
  115        */
  116       public void closeTarget()
  117           throws IOException
  118       {
  119           writer.endElement();
  120       }
  121   
  122       /**
  123        * Write an Ant fileset
  124        * 
  125        * @param fileSet
  126        * @param id
  127        */
  128       public void writeFileSet( FileSet fileSet, String id )
  129       {
  130           writer.startElement( "fileset" );
  131   
  132           if ( id != null )
  133           {
  134               writer.addAttribute( "id", id );
  135           }
  136   
  137           File dir = fileSet.getDir( fileSet.getProject() );
  138           writer.addAttribute( "dir", dir.getAbsolutePath() );
  139   
  140           DirectoryScanner scanner = fileSet.getDirectoryScanner( fileSet.getProject() );
  141           scanner.scan();
  142           String[] files = scanner.getIncludedFiles();
  143   
  144           for ( int i = 0; i < files.length; ++i )
  145           {
  146               writer.startElement( "include" );
  147               writer.addAttribute( "name", files[i] );
  148               writer.endElement();
  149           }
  150   
  151           writer.endElement();
  152       }
  153   
  154       /**
  155        * Write an ant property
  156        * 
  157        * @param name
  158        * @param value
  159        */
  160       public void writeProperty( String name, String value )
  161       {
  162           writer.startElement( "property" );
  163   
  164           writer.addAttribute( "name", name );
  165           writer.addAttribute( "value", value );
  166   
  167           writer.endElement();
  168       }
  169   
  170       /**
  171        * Write an Ant echo task 
  172        * 
  173        * @param message
  174        */
  175       public void writeEcho( String message )
  176       {
  177           writer.startElement( "echo" );
  178   
  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   }

Save This Page
Home » maven-ant-tasks-2.1.0-src » org.apache.maven.artifact.ant.util » [javadoc | source]