DataBinder.java revision b6887f1479c3ecec38a7989748ef33de1fbcd973
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.store.ResourceBundle;
20import android.databinding.tool.util.L;
21import android.databinding.tool.writer.JavaFileWriter;
22
23import java.util.ArrayList;
24import java.util.HashSet;
25import java.util.List;
26import java.util.Map;
27import java.util.Set;
28
29/**
30 * The main class that handles parsing files and generating classes.
31 */
32public class DataBinder {
33    List<LayoutBinder> mLayoutBinders = new ArrayList<LayoutBinder>();
34
35    private JavaFileWriter mFileWriter;
36
37    Set<String> writtenClasses = new HashSet<String>();
38
39    public DataBinder(ResourceBundle resourceBundle) {
40        L.d("reading resource bundle into data binder");
41        for (Map.Entry<String, List<ResourceBundle.LayoutFileBundle>> entry :
42                resourceBundle.getLayoutBundles().entrySet()) {
43            for (ResourceBundle.LayoutFileBundle bundle : entry.getValue()) {
44                mLayoutBinders.add(new LayoutBinder(bundle));
45            }
46        }
47    }
48    public List<LayoutBinder> getLayoutBinders() {
49        return mLayoutBinders;
50    }
51
52    public void writerBaseClasses(boolean isLibrary) {
53        for (LayoutBinder layoutBinder : mLayoutBinders) {
54            if (isLibrary || layoutBinder.hasVariations()) {
55                String className = layoutBinder.getClassName();
56                String canonicalName = layoutBinder.getPackage() + "." + className;
57                if (writtenClasses.contains(canonicalName)) {
58                    continue;
59                }
60                L.d("writing data binder base %s", canonicalName);
61                mFileWriter.writeToFile(canonicalName, layoutBinder.writeViewBinderBaseClass());
62                writtenClasses.add(canonicalName);
63            }
64        }
65    }
66
67    public void writeBinders(int minSdk) {
68        for (LayoutBinder layoutBinder : mLayoutBinders) {
69            String className = layoutBinder.getImplementationName();
70            String canonicalName = layoutBinder.getPackage() + "." + className;
71            L.d("writing data binder %s", canonicalName);
72            writtenClasses.add(canonicalName);
73            mFileWriter.writeToFile(canonicalName, layoutBinder.writeViewBinder(minSdk));
74        }
75    }
76
77    public Set<String> getWrittenClassNames() {
78        return writtenClasses;
79    }
80
81    public void setFileWriter(JavaFileWriter fileWriter) {
82        mFileWriter = fileWriter;
83    }
84
85    public JavaFileWriter getFileWriter() {
86        return mFileWriter;
87    }
88}
89