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 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 public boolean handleGeneratedFile(IFile file, int kind) { 49 if (kind == IResourceDelta.REMOVED || kind == IResourceDelta.CHANGED) { 50 IFile sourceFile = mProcessor.isOutput(file); 51 if (sourceFile != null) { 52 mToCompile.add(sourceFile); 53 return true; 54 } 55 } 56 57 return false; 58 } 59 60 public void handleSourceFile(IFile file, int kind) { 61 // first the file itself if this is a match for the processor's extension 62 if (mProcessor.getExtensions().contains(file.getFileExtension())) { 63 if (kind == IResourceDelta.REMOVED) { 64 mRemoved.add(file); 65 } else { 66 mToCompile.add(file); 67 } 68 } 69 70 // now the dependencies. In all case we compile the files that depend on the 71 // added/changed/removed file. 72 mToCompile.addAll(mProcessor.isDependency(file)); 73 } 74 75 public void handleResourceFile(IFile file, int kind) { 76 if (filterResourceFolder(file.getParent())) { 77 handleGeneratedFile(file, kind); 78 } 79 } 80 81 /** 82 * Called to restrict {@link #handleResourceFile(IFile, int)} on selected resource folders. 83 * @param folder 84 * @return 85 */ 86 protected boolean filterResourceFolder(IContainer folder) { 87 return false; 88 } 89 90 protected void addFileToCompile(IFile file) { 91 mToCompile.add(file); 92 } 93 94 Set<IFile> getFilesToCompile() { 95 return mToCompile; 96 } 97 98 protected void addRemovedFile(IFile file) { 99 mRemoved.add(file); 100 } 101 102 Set<IFile> getRemovedFiles() { 103 return mRemoved; 104 } 105 106 public void reset() { 107 mToCompile.clear(); 108 mRemoved.clear(); 109 } 110 111 protected SourceProcessor getProcessor() { 112 return mProcessor; 113 } 114 115 void init(SourceProcessor processor) { 116 mProcessor = processor; 117 } 118 } 119