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