1 /* 2 * Copyright (C) 2015 The Android Open Source Project 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * http://www.apache.org/licenses/LICENSE-2.0 7 * Unless required by applicable law or agreed to in writing, software 8 * distributed under the License is distributed on an "AS IS" BASIS, 9 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 * See the License for the specific language governing permissions and 11 * limitations under the License. 12 */ 13 14 package android.databinding.tool; 15 16 import android.databinding.tool.store.ResourceBundle; 17 import android.databinding.tool.util.L; 18 import android.databinding.tool.writer.BRWriter; 19 import android.databinding.tool.writer.DataBinderWriter; 20 import android.databinding.tool.writer.JavaFileWriter; 21 22 import java.util.Set; 23 24 /** 25 * Chef class for compiler. 26 * 27 * Different build systems can initiate a version of this to handle their work 28 */ 29 public class CompilerChef { 30 private JavaFileWriter mFileWriter; 31 private ResourceBundle mResourceBundle; 32 private DataBinder mDataBinder; 33 34 private CompilerChef() { 35 } 36 37 public static CompilerChef createChef(ResourceBundle bundle, JavaFileWriter fileWriter) { 38 CompilerChef chef = new CompilerChef(); 39 40 chef.mResourceBundle = bundle; 41 chef.mFileWriter = fileWriter; 42 chef.mResourceBundle.validateMultiResLayouts(); 43 return chef; 44 } 45 46 public ResourceBundle getResourceBundle() { 47 return mResourceBundle; 48 } 49 50 public void ensureDataBinder() { 51 if (mDataBinder == null) { 52 mDataBinder = new DataBinder(mResourceBundle); 53 mDataBinder.setFileWriter(mFileWriter); 54 } 55 } 56 57 public boolean hasAnythingToGenerate() { 58 L.d("checking if we have anything to generate. bundle size: %s", 59 mResourceBundle == null ? -1 : mResourceBundle.getLayoutBundles().size()); 60 return mResourceBundle != null && mResourceBundle.getLayoutBundles().size() > 0; 61 } 62 63 public void writeDataBinderMapper(int minSdk, BRWriter brWriter) { 64 ensureDataBinder(); 65 final String pkg = "android.databinding"; 66 DataBinderWriter dbr = new DataBinderWriter(pkg, mResourceBundle.getAppPackage(), 67 "DataBinderMapper", mDataBinder.getLayoutBinders(), minSdk); 68 mFileWriter.writeToFile(pkg + "." + dbr.getClassName(), dbr.write(brWriter)); 69 } 70 71 /** 72 * Adds variables to list of Bindables. 73 */ 74 public void addBRVariables(BindableHolder bindables) { 75 ensureDataBinder(); 76 for (LayoutBinder layoutBinder : mDataBinder.mLayoutBinders) { 77 for (String variableName : layoutBinder.getUserDefinedVariables().keySet()) { 78 bindables.addVariable(variableName, layoutBinder.getClassName()); 79 } 80 } 81 } 82 83 public void sealModels() { 84 ensureDataBinder(); 85 mDataBinder.sealModels(); 86 } 87 88 public void writeViewBinderInterfaces(boolean isLibrary) { 89 ensureDataBinder(); 90 mDataBinder.writerBaseClasses(isLibrary); 91 } 92 93 public void writeViewBinders(int minSdk) { 94 ensureDataBinder(); 95 mDataBinder.writeBinders(minSdk); 96 } 97 98 public void writeComponent() { 99 ensureDataBinder(); 100 mDataBinder.writeComponent(); 101 } 102 103 public Set<String> getWrittenClassNames() { 104 ensureDataBinder(); 105 return mDataBinder.getWrittenClassNames(); 106 } 107 108 public interface BindableHolder { 109 void addVariable(String variableName, String containingClassName); 110 } 111 } 112