1/* Copyright (C) 2003 Vladimir Roubtsov. All rights reserved.
2 *
3 * This program and the accompanying materials are made available under
4 * the terms of the Common Public License v1.0 which accompanies this distribution,
5 * and is available at http://www.eclipse.org/legal/cpl-v10.html
6 *
7 * $Id: instrTask.java,v 1.1.1.1.2.1 2004/07/08 10:52:12 vlad_r Exp $
8 */
9package com.vladium.emma.instr;
10
11import java.io.File;
12
13import org.apache.tools.ant.BuildException;
14import org.apache.tools.ant.types.EnumeratedAttribute;
15import org.apache.tools.ant.types.Path;
16import org.apache.tools.ant.types.Reference;
17
18import com.vladium.util.asserts.$assert;
19import com.vladium.emma.ant.FilterTask;
20import com.vladium.emma.ant.SuppressableTask;
21
22// ----------------------------------------------------------------------------
23/**
24 * @author Vlad Roubtsov, (C) 2003
25 */
26public
27final class instrTask extends FilterTask
28{
29    // public: ................................................................
30
31
32    public static final class ModeAttribute extends EnumeratedAttribute
33    {
34        public String [] getValues ()
35        {
36            return VALUES;
37        }
38
39        private static final String [] VALUES = new String [] {"copy", "overwrite", "fullcopy"};
40
41    } // end of nested class
42
43
44    public instrTask (final SuppressableTask parent)
45    {
46        super (parent);
47
48        m_outMode = InstrProcessor.OutMode.OUT_MODE_COPY; // default
49    }
50
51
52    public void execute () throws BuildException
53    {
54        if (isEnabled ())
55        {
56            if (m_instrpath == null)
57                throw (BuildException) newBuildException (getTaskName ()
58                    + ": instrumentation path must be specified", location).fillInStackTrace ();
59
60            if ((m_outMode != InstrProcessor.OutMode.OUT_MODE_OVERWRITE) && (m_outDir == null))
61                throw (BuildException) newBuildException (getTaskName ()
62                    + ": output directory must be specified for '" + m_outMode + "' output mode", location).fillInStackTrace ();
63
64            InstrProcessor processor = InstrProcessor.create ();
65
66            $assert.ASSERT (m_instrpath != null, "m_instrpath not set");
67            processor.setInstrPath (m_instrpath.list (), true); // TODO: an option to set 'canonical'?
68            // processor.setDependsMode ()
69            processor.setInclExclFilter (getFilterSpecs ());
70            $assert.ASSERT (m_outMode != null, "m_outMode not set");
71            processor.setOutMode (m_outMode);
72            processor.setInstrOutDir (m_outDir != null ? m_outDir.getAbsolutePath () : null);
73            processor.setMetaOutFile (m_outFile != null ? m_outFile.getAbsolutePath () : null);
74            processor.setMetaOutMerge (m_outFileMerge);
75            processor.setPropertyOverrides (getTaskSettings ());
76
77            processor.run ();
78        }
79    }
80
81
82    // instrpath attribute/element:
83
84    public void setInstrpath (final Path path)
85    {
86        if (m_instrpath == null)
87            m_instrpath = path;
88        else
89            m_instrpath.append (path);
90    }
91
92    public void setInstrpathRef (final Reference ref)
93    {
94        createInstrpath ().setRefid (ref);
95    }
96
97    public Path createInstrpath ()
98    {
99        if (m_instrpath == null)
100            m_instrpath = new Path (project);
101
102        return m_instrpath.createPath ();
103    }
104
105
106    // outdir|destdir attribute:
107
108    public void setOutdir (final File dir)
109    {
110        if (m_outDir != null)
111            throw (BuildException) newBuildException (getTaskName ()
112                + ": outdir|destdir attribute already set", location).fillInStackTrace ();
113
114        m_outDir = dir;
115    }
116
117    public void setDestdir (final File dir)
118    {
119        if (m_outDir != null)
120            throw (BuildException) newBuildException (getTaskName ()
121                + ": outdir|destdir attribute already set", location).fillInStackTrace ();
122
123        m_outDir = dir;
124    }
125
126
127    // metadatafile|outfile attribute:
128
129    public void setMetadatafile (final File file)
130    {
131        if (m_outFile != null)
132            throw (BuildException) newBuildException (getTaskName ()
133                + ": metadata file attribute already set", location).fillInStackTrace ();
134
135        m_outFile = file;
136    }
137
138    public void setOutfile (final File file)
139    {
140        if (m_outFile != null)
141            throw (BuildException) newBuildException (getTaskName ()
142                + ": metadata file attribute already set", location).fillInStackTrace ();
143
144        m_outFile = file;
145    }
146
147    // merge attribute:
148
149    public void setMerge (final boolean merge)
150    {
151        m_outFileMerge = merge ? Boolean.TRUE : Boolean.FALSE;
152    }
153
154
155    // mode attribute:
156
157    public void setMode (final ModeAttribute mode)
158    {
159        final InstrProcessor.OutMode outMode = InstrProcessor.OutMode.nameToMode (mode.getValue ());
160        if (outMode == null)
161            throw (BuildException) newBuildException (getTaskName ()
162                + ": invalid output mode: " + mode.getValue (), location).fillInStackTrace ();
163
164        m_outMode = outMode;
165    }
166
167    // protected: .............................................................
168
169    // package: ...............................................................
170
171    // private: ...............................................................
172
173
174    private Path m_instrpath;
175    private InstrProcessor.OutMode m_outMode;
176    private File m_outDir;
177    private File m_outFile;
178    private Boolean m_outFileMerge;
179
180} // end of class
181// ----------------------------------------------------------------------------