Move to Android N-MR1 SDK.
[android_tools.git] / sdk / sources / android-25 / android / databinding / tool / DataBinder.java
blob0280bd43fb6a3146a448be8950a314f672e84aa4
1 /*
2 * Copyright (C) 2015 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package android.databinding.tool;
19 import android.databinding.tool.processing.Scope;
20 import android.databinding.tool.processing.ScopedException;
21 import android.databinding.tool.store.ResourceBundle;
22 import android.databinding.tool.util.L;
23 import android.databinding.tool.writer.ComponentWriter;
24 import android.databinding.tool.writer.JavaFileWriter;
26 import java.util.ArrayList;
27 import java.util.HashSet;
28 import java.util.List;
29 import java.util.Map;
30 import java.util.Set;
32 /**
33 * The main class that handles parsing files and generating classes.
35 public class DataBinder {
36 List<LayoutBinder> mLayoutBinders = new ArrayList<LayoutBinder>();
37 private static final String COMPONENT_CLASS = "android.databinding.DataBindingComponent";
39 private JavaFileWriter mFileWriter;
41 Set<String> writtenClasses = new HashSet<String>();
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);
56 public List<LayoutBinder> getLayoutBinders() {
57 return mLayoutBinders;
60 public void sealModels() {
61 for (LayoutBinder layoutBinder : mLayoutBinders) {
62 layoutBinder.sealModel();
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;
76 L.d("writing data binder base %s", canonicalName);
77 mFileWriter.writeToFile(canonicalName,
78 layoutBinder.writeViewBinderBaseClass(isLibrary));
79 writtenClasses.add(canonicalName);
81 } catch (ScopedException ex){
82 Scope.defer(ex);
83 } finally {
84 Scope.exit();
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();
106 public void writeComponent() {
107 ComponentWriter componentWriter = new ComponentWriter();
109 writtenClasses.add(COMPONENT_CLASS);
110 mFileWriter.writeToFile(COMPONENT_CLASS, componentWriter.createComponent());
113 public Set<String> getWrittenClassNames() {
114 return writtenClasses;
117 public void setFileWriter(JavaFileWriter fileWriter) {
118 mFileWriter = fileWriter;
121 public JavaFileWriter getFileWriter() {
122 return mFileWriter;