1 package org.apache.maven.jelly.tags.maven;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
70 checkAttribute( outputFile, "outputFile" );
71
72
73 invokeBody( output );
74
75
76
77 File outputFileDirectory = outputFile.getParentFile();
78
79 if ( !outputFileDirectory.exists() )
80 {
81 outputFileDirectory.mkdirs();
82 }
83
84 FileOutputStream fos = new FileOutputStream( outputFile );
85
86
87
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 )
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 }