• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2012 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.ant;
18 
19 import org.apache.tools.ant.BuildException;
20 import org.apache.tools.ant.Task;
21 import org.apache.tools.ant.types.Path;
22 
23 import java.io.File;
24 
25 /**
26  * Single input single output class. Execution is controlled
27  * by modification timestamp of the input and output files.
28  *
29  * Implementation classes must implement {@link #createOutput()}
30  *
31  */
32 public abstract class SingleInputOutputTask extends Task {
33 
34     private String mInput;
35     private String mOutput;
36 
setInput(Path inputPath)37     public void setInput(Path inputPath) {
38         mInput = TaskHelper.checkSinglePath("input", inputPath);
39     }
40 
setOutput(Path outputPath)41     public void setOutput(Path outputPath) {
42         mOutput = TaskHelper.checkSinglePath("output", outputPath);
43     }
44 
45     @Override
execute()46     public final void execute() throws BuildException {
47         if (mInput == null) {
48             throw new BuildException("Missing attribute input");
49         }
50         if (mOutput == null) {
51             throw new BuildException("Missing attribute output");
52         }
53 
54         // check if there's a need for the task to run.
55         File outputFile = new File(mOutput);
56         if (outputFile.isFile()) {
57             File inputFile = new File(mInput);
58             if (outputFile.lastModified() >= inputFile.lastModified()) {
59                 System.out.println(String.format(
60                         "Run cancelled: no changes to input file %1$s",
61                         inputFile.getAbsolutePath()));
62                 return;
63             }
64         }
65 
66         createOutput();
67     }
68 
createOutput()69     protected abstract void createOutput() throws BuildException;
70 
getInput()71     protected String getInput() {
72         return mInput;
73     }
74 
getOutput()75     protected String getOutput() {
76         return mOutput;
77     }
78 }
79