Coverage Report - org.apache.maven.artifact.ant.util.AntBuildWriter
 
Classes in this File Line Coverage Branch Coverage Complexity
AntBuildWriter
0%
0/64
0%
0/10
1.556
 
 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  0
 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  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  
      * Close the ant build writer
 84  
      *
 85  
      * @throws IOException
 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  
      * Open a target tag
 99  
      *
 100  
      * @param targetName
 101  
      * @throws IOException
 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  
      * Close a tag.
 112  
      *
 113  
      * @throws IOException
 114  
      */
 115  
     public void closeTarget()
 116  
         throws IOException
 117  
     {
 118  0
         writer.endElement();
 119  0
     }
 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  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  
      * Write an ant property
 155  
      *
 156  
      * @param name
 157  
      * @param value
 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  
      * Write an Ant echo task
 171  
      *
 172  
      * @param message
 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  
      * Write an Ant file list
 186  
      *
 187  
      * @param fileList
 188  
      * @param id
 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  
      * Write a path.
 209  
      *
 210  
      * @param path
 211  
      * @param pathId
 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  
 }