DataBinder.java revision 7ff60c24c6de7ba0c674fe65a82ad4a88dab2e5d
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    public DataBinder(ResourceBundle resourceBundle) {
38        L.d("reading resource bundle into data binder");
39        for (Map.Entry<String, List<ResourceBundle.LayoutFileBundle>> entry :
40                resourceBundle.getLayoutBundles().entrySet()) {
41            for (ResourceBundle.LayoutFileBundle bundle : entry.getValue()) {
42                mLayoutBinders.add(new LayoutBinder(bundle));
43            }
44        }
45    }
46    public List<LayoutBinder> getLayoutBinders() {
47        return mLayoutBinders;
48    }
49
50    public void writerBaseClasses(boolean isLibrary) {
51        Set<String> writtenFiles = new HashSet<String>();
52        for (LayoutBinder layoutBinder : mLayoutBinders) {
53            if (isLibrary || layoutBinder.hasVariations()) {
54                String className = layoutBinder.getClassName();
55                if (writtenFiles.contains(className)) {
56                    continue;
57                }
58                mFileWriter.writeToFile(layoutBinder.getPackage() + "." + className,
59                        layoutBinder.writeViewBinderBaseClass());
60                writtenFiles.add(className);
61            }
62        }
63    }
64
65    public void writeBinders(int minSdk) {
66        for (LayoutBinder layoutBinder : mLayoutBinders) {
67            String className = layoutBinder.getImplementationName();
68            L.d("writing data binder %s", className);
69            mFileWriter.writeToFile(layoutBinder.getPackage() + "." + className,
70                    layoutBinder.writeViewBinder(minSdk));
71        }
72    }
73
74    public void setFileWriter(JavaFileWriter fileWriter) {
75        mFileWriter = fileWriter;
76    }
77
78    public JavaFileWriter getFileWriter() {
79        return mFileWriter;
80    }
81}
82