1/* 2 * Copyright (C) 2015 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 17package android.databinding.tool; 18 19import android.databinding.tool.processing.Scope; 20import android.databinding.tool.processing.ScopedException; 21import android.databinding.tool.store.ResourceBundle; 22import android.databinding.tool.util.L; 23import android.databinding.tool.writer.ComponentWriter; 24import android.databinding.tool.writer.JavaFileWriter; 25 26import java.util.ArrayList; 27import java.util.HashSet; 28import java.util.List; 29import java.util.Map; 30import java.util.Set; 31 32/** 33 * The main class that handles parsing files and generating classes. 34 */ 35public class DataBinder { 36 List<LayoutBinder> mLayoutBinders = new ArrayList<LayoutBinder>(); 37 private static final String COMPONENT_CLASS = "android.databinding.DataBindingComponent"; 38 39 private JavaFileWriter mFileWriter; 40 41 Set<String> writtenClasses = new HashSet<String>(); 42 43 public DataBinder(ResourceBundle resourceBundle) { 44 L.d("reading resource bundle into data binder"); 45 for (Map.Entry<String, List<ResourceBundle.LayoutFileBundle>> entry : 46 resourceBundle.getLayoutBundles().entrySet()) { 47 for (ResourceBundle.LayoutFileBundle bundle : entry.getValue()) { 48 try { 49 mLayoutBinders.add(new LayoutBinder(bundle)); 50 } catch (ScopedException ex) { 51 Scope.defer(ex); 52 } 53 } 54 } 55 } 56 public List<LayoutBinder> getLayoutBinders() { 57 return mLayoutBinders; 58 } 59 60 public void sealModels() { 61 for (LayoutBinder layoutBinder : mLayoutBinders) { 62 layoutBinder.sealModel(); 63 } 64 } 65 66 public void writerBaseClasses(boolean isLibrary) { 67 for (LayoutBinder layoutBinder : mLayoutBinders) { 68 try { 69 Scope.enter(layoutBinder); 70 if (isLibrary || layoutBinder.hasVariations()) { 71 String className = layoutBinder.getClassName(); 72 String canonicalName = layoutBinder.getPackage() + "." + className; 73 if (writtenClasses.contains(canonicalName)) { 74 continue; 75 } 76 L.d("writing data binder base %s", canonicalName); 77 mFileWriter.writeToFile(canonicalName, 78 layoutBinder.writeViewBinderBaseClass(isLibrary)); 79 writtenClasses.add(canonicalName); 80 } 81 } catch (ScopedException ex){ 82 Scope.defer(ex); 83 } finally { 84 Scope.exit(); 85 } 86 } 87 } 88 89 public void writeBinders(int minSdk) { 90 for (LayoutBinder layoutBinder : mLayoutBinders) { 91 try { 92 Scope.enter(layoutBinder); 93 String className = layoutBinder.getImplementationName(); 94 String canonicalName = layoutBinder.getPackage() + "." + className; 95 L.d("writing data binder %s", canonicalName); 96 writtenClasses.add(canonicalName); 97 mFileWriter.writeToFile(canonicalName, layoutBinder.writeViewBinder(minSdk)); 98 } catch (ScopedException ex) { 99 Scope.defer(ex); 100 } finally { 101 Scope.exit(); 102 } 103 } 104 } 105 106 public void writeComponent() { 107 ComponentWriter componentWriter = new ComponentWriter(mLayoutBinders); 108 109 writtenClasses.add(COMPONENT_CLASS); 110 mFileWriter.writeToFile(COMPONENT_CLASS, componentWriter.createComponent()); 111 } 112 113 public Set<String> getWrittenClassNames() { 114 return writtenClasses; 115 } 116 117 public void setFileWriter(JavaFileWriter fileWriter) { 118 mFileWriter = fileWriter; 119 } 120 121 public JavaFileWriter getFileWriter() { 122 return mFileWriter; 123 } 124} 125