Debian Codegraph @

animal-sniffer-ant-tasks/src/main/java/org/codehaus/mojo/animal_sniffer/ant/BuildSignaturesTask.java

raw
1 package org.codehaus.mojo.animal_sniffer.ant;
2
3 /*
4 * The MIT License
5 *
6 * Copyright (c) 2008 Kohsuke Kawaguchi and codehaus.org.
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 * THE SOFTWARE.
25 *
26 */
27
28 import java.io.File;
29 import java.io.FileInputStream;
30 import java.io.FileOutputStream;
31 import java.io.IOException;
32 import java.io.InputStream;
33 import java.util.Vector;
34
35 import org.apache.tools.ant.BuildException;
36 import org.apache.tools.ant.Project;
37 import org.apache.tools.ant.Task;
38 import org.apache.tools.ant.types.Path;
39 import org.codehaus.mojo.animal_sniffer.SignatureBuilder;
40
41 /**
42 * @author Kohsuke Kawaguchi
43 */
44 public class BuildSignaturesTask
45 extends Task
46 {
47
48 private File destfile;
49
50 private Vector<Path> paths = new Vector<Path>();
51
52 private Vector<Signature> signatures = new Vector<Signature>();
53
54 private Vector<Ignore> includeClasses = new Vector<Ignore>();
55
56 private Vector<Ignore> excludeClasses = new Vector<Ignore>();
57
58 public void setDestfile( File dest )
59 {
60 this.destfile = dest;
61 }
62
63 public void addPath( Path path )
64 {
65 paths.add( path );
66 }
67
68 public Signature createSignature()
69 {
70 Signature signature = new Signature();
71 signatures.add( signature );
72 return signature;
73 }
74
75 public Ignore createIncludeClasses()
76 {
77 final Ignore result = new Ignore();
78 includeClasses.add( result );
79 return result;
80 }
81
82 public Ignore createExcludeClasses()
83 {
84 final Ignore result = new Ignore();
85 excludeClasses.add( result );
86 return result;
87 }
88
89 protected void validate()
90 {
91 if ( destfile == null )
92 {
93 throw new BuildException( "destfile not set" );
94 }
95 if ( paths.size() < 1 )
96 {
97 throw new BuildException( "path not set" );
98 }
99 for ( Signature signature : signatures )
100 {
101 if ( signature.getSrc() == null )
102 {
103 throw new BuildException( "signature src not set" );
104 }
105 if ( !signature.getSrc().isFile() )
106 {
107 throw new BuildException( "signature " + signature.getSrc() + " does not exist" );
108 }
109 }
110 for ( Ignore tmp : includeClasses )
111 {
112 if ( tmp.getClassName() == null )
113 {
114 throw new BuildException( "includeClasses className not set" );
115 }
116 }
117 for ( Ignore tmp : excludeClasses )
118 {
119 if ( tmp.getClassName() == null )
120 {
121 throw new BuildException( "excludeClasses className not set" );
122 }
123 }
124
125 }
126
127 public void execute()
128 throws BuildException
129 {
130 validate();
131 try
132 {
133 Vector<InputStream> inStreams = new Vector<InputStream>();
134 for ( Signature signature : signatures )
135 {
136 log( "Importing signatures from " + signature.getSrc() );
137 inStreams.add( new FileInputStream( signature.getSrc() ) );
138 }
139
140 SignatureBuilder builder =
141 new SignatureBuilder( inStreams.toArray( new InputStream[inStreams.size()] ),
142 new FileOutputStream( destfile ), new AntLogger( this ) );
143 for ( Ignore tmp: includeClasses )
144 {
145 builder.addInclude( tmp.getClassName() );
146 }
147 for ( Ignore tmp : excludeClasses )
148 {
149 builder.addExclude( tmp.getClassName() );
150 }
151 for ( Path path : paths )
152 {
153 final String[] files = path.list();
154 for ( int j = 0; j < files.length; j++ )
155 {
156 log( "Capturing signatures from " + files[j], Project.MSG_INFO );
157 process( builder, new File( files[j] ) );
158 }
159 }
160 builder.close();
161 }
162 catch ( IOException e )
163 {
164 throw new BuildException( e );
165 }
166 }
167
168 private void process( SignatureBuilder builder, File f )
169 throws IOException
170 {
171 if ( f.exists() )
172 {
173 builder.process( f );
174 }
175 }
176 }