1 /*
2  * Copyright (C) 2011 The Android Open Source Project
3  *
4  * Licensed under the Eclipse Public License, Version 1.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.eclipse.org/org/documents/epl-v10.php
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.ide.eclipse.adt.internal.build;
18 
19 import org.eclipse.core.resources.IContainer;
20 import org.eclipse.core.resources.IFile;
21 import org.eclipse.core.resources.IResourceDelta;
22 
23 import java.util.HashSet;
24 import java.util.Set;
25 
26 /**
27  * Base source change handler for the {@link SourceProcessor} classes.
28  *
29  * It can be used as is, as long as the matching {@link SourceProcessor} properly implements
30  * its abstract methods, and the processor does not output resource files,
31  * or can be extended to provide custom implementation for:
32  * {@link #handleSourceFile(IFile, int)}
33  * {@link #handleGeneratedFile(IFile, int)}
34  * {@link #handleResourceFile(IFile, int)}
35  * {@link #filterResourceFolder(IContainer)}
36  *
37  */
38 public class DefaultSourceChangeHandler implements SourceChangeHandler {
39 
40     private SourceProcessor mProcessor;
41 
42     /** List of source files found that are modified or new. */
43     private final Set<IFile> mToCompile = new HashSet<IFile>();
44 
45     /** List of source files that have been removed. */
46     private final Set<IFile> mRemoved = new HashSet<IFile>();
47 
48     @Override
handleGeneratedFile(IFile file, int kind)49     public boolean handleGeneratedFile(IFile file, int kind) {
50         if (kind == IResourceDelta.REMOVED || kind == IResourceDelta.CHANGED) {
51             IFile sourceFile = mProcessor.isOutput(file);
52             if (sourceFile != null) {
53                 mToCompile.add(sourceFile);
54                 return true;
55             }
56         }
57 
58         return false;
59     }
60 
61     @Override
handleSourceFile(IFile file, int kind)62     public void handleSourceFile(IFile file, int kind) {
63         // first the file itself if this is a match for the processor's extension
64         if (mProcessor.getExtensions().contains(file.getFileExtension())) {
65             if (kind == IResourceDelta.REMOVED) {
66                 mRemoved.add(file);
67             } else {
68                 mToCompile.add(file);
69             }
70         }
71 
72         // now the dependencies. In all case we compile the files that depend on the
73         // added/changed/removed file.
74         mToCompile.addAll(mProcessor.isDependency(file));
75     }
76 
addFileToCompile(IFile file)77     protected void addFileToCompile(IFile file) {
78         mToCompile.add(file);
79     }
80 
getFilesToCompile()81     Set<IFile> getFilesToCompile() {
82         return mToCompile;
83     }
84 
addRemovedFile(IFile file)85     protected void addRemovedFile(IFile file) {
86         mRemoved.add(file);
87     }
88 
getRemovedFiles()89     Set<IFile> getRemovedFiles() {
90         return mRemoved;
91     }
92 
reset()93     public void reset() {
94         mToCompile.clear();
95         mRemoved.clear();
96     }
97 
getProcessor()98     protected SourceProcessor getProcessor() {
99         return mProcessor;
100     }
101 
init(SourceProcessor processor)102     void init(SourceProcessor processor) {
103         mProcessor = processor;
104     }
105 }
106