View Javadoc

1   package org.apache.maven.jelly.tags.maven;
2   
3   /* ====================================================================
4    *   Licensed to the Apache Software Foundation (ASF) under one or more
5    *   contributor license agreements.  See the NOTICE file distributed with
6    *   this work for additional information regarding copyright ownership.
7    *   The ASF licenses this file to You under the Apache License, Version 2.0
8    *   (the "License"); you may not use this file except in compliance with
9    *   the License.  You may obtain a copy of the License at
10   *
11   *       http://www.apache.org/licenses/LICENSE-2.0
12   *
13   *   Unless required by applicable law or agreed to in writing, software
14   *   distributed under the License is distributed on an "AS IS" BASIS,
15   *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   *   See the License for the specific language governing permissions and
17   *   limitations under the License.
18   * ====================================================================
19   */
20  
21  import java.io.File;
22  import java.io.FileInputStream;
23  import java.io.FileOutputStream;
24  import java.io.IOException;
25  import java.util.ArrayList;
26  import java.util.Iterator;
27  
28  import org.apache.commons.jelly.JellyTagException;
29  import org.apache.commons.jelly.MissingAttributeException;
30  import org.apache.commons.jelly.XMLOutput;
31  
32  /**
33   * Concat a set of files together into a single file.
34   *
35   * @author <a href="mailto:jason@zenplex.com">Jason van Zyl</a>
36   * @version $Id: ConcatTag.java 517014 2007-03-11 21:15:50Z ltheussl $
37   */
38  public class ConcatTag
39      extends MavenTag
40  {
41      /** Output file. */
42      private File outputFile;
43  
44      /** List of file to concatenate together. */
45      private ArrayList files = new ArrayList();
46  
47      /** Set output file. */
48      public void setOutputFile( File outputFile )
49      {
50          this.outputFile = outputFile;
51      }
52  
53      /** Add a file to the list of files of be concatenated. */
54      public void addFile( File file )
55      {
56          files.add( file );
57      }
58  
59      /**
60       * Perform functionality provided by the tag.
61       *
62       * @throws JellyTagException If there is an error while concatenating the file.
63       */
64      public void doTag( XMLOutput output )
65          throws MissingAttributeException, JellyTagException
66      {
67          try
68          {
69              // Make sure we have an output file.
70              checkAttribute( outputFile, "outputFile" );
71  
72              // Collect all the input files by invoking the body.
73              invokeBody( output );
74  
75              // If the directory where the output file is going to land
76              // doesn't exists then create it.
77              File outputFileDirectory = outputFile.getParentFile();
78  
79              if ( !outputFileDirectory.exists() )
80              {
81                  outputFileDirectory.mkdirs();
82              }
83  
84              FileOutputStream fos = new FileOutputStream( outputFile );
85  
86              // We should now have all the files we want to concat so
87              // lets do it.
88              for ( Iterator i = files.iterator(); i.hasNext(); )
89              {
90                  File f = (File) i.next();
91                  FileInputStream in = new FileInputStream( f );
92  
93                  int count;
94                  byte[] b = new byte[512];
95                  while ( ( count = in.read( b ) ) > 0 ) // blocking read
96                  {
97                      fos.write( b, 0, count );
98                  }
99  
100                 in.close();
101             }
102 
103             fos.flush();
104             fos.close();
105         }
106         catch ( IOException e )
107         {
108             throw new JellyTagException( e );
109         }
110     }
111 }