summaryrefslogtreecommitdiff
path: root/compiler/src/main/java/com/android/databinding/LayoutBinder.java
blob: a285e165a224abccc590a492d2eec70c0158148e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.databinding;

import com.google.common.base.Preconditions;

import com.android.databinding.expr.Dependency;
import com.android.databinding.expr.Expr;
import com.android.databinding.expr.ExprModel;
import com.android.databinding.expr.IdentifierExpr;
import com.android.databinding.store.ResourceBundle;
import com.android.databinding.util.ParserHelper;
import com.android.databinding.writer.LayoutBinderWriter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Keeps all information about the bindings per layout file
 */
public class LayoutBinder {

    /*
    * val pkg: String, val projectPackage: String, val baseClassName: String,
        val layoutName:String, val lb: LayoutExprBinding*/
    private final ExprModel mExprModel;
    private final ExpressionParser mExpressionParser;
    private final List<BindingTarget> mBindingTargets;
    private String mPackage;
    private String mProjectPackage;
    private String mBaseClassName;
    private final HashMap<String, String> mUserDefinedVariables = new HashMap<String, String>();

    private LayoutBinderWriter mWriter;
    private ResourceBundle.LayoutFileBundle mBundle;

    public LayoutBinder(ResourceBundle resourceBundle,
            ResourceBundle.LayoutFileBundle layoutBundle) {
        mExprModel = new ExprModel();
        mExpressionParser = new ExpressionParser(mExprModel);
        mBindingTargets = new ArrayList<BindingTarget>();
        mBundle = layoutBundle;
        mProjectPackage = resourceBundle.getAppPackage();
        mPackage = mProjectPackage + ".generated";
        mBaseClassName = ParserHelper.INSTANCE$.toClassName(layoutBundle.getFileName()) + "Binding";
        // copy over data.
        for (Map.Entry<String, String> variable : mBundle.getVariables().entrySet()) {
            addVariable(variable.getKey(), variable.getValue());
        }

        for (Map.Entry<String, String> userImport : mBundle.getImports().entrySet()) {
            mExprModel.addImport(userImport.getKey(), userImport.getValue());
        }
        for (ResourceBundle.BindingTargetBundle targetBundle : mBundle.getBindingTargetBundles()) {
            final BindingTarget bindingTarget = createBindingTarget(targetBundle);
            for (ResourceBundle.BindingTargetBundle.BindingBundle bindingBundle : targetBundle
                    .getBindingBundleList()) {
                bindingTarget.addBinding(bindingBundle.getName(), parse(bindingBundle.getExpr()));
            }
        }
    }

    public void resolveWhichExpressionsAreUsed() {
        List<Expr> used = new ArrayList<Expr>();
        for (BindingTarget target : mBindingTargets) {
            for (Binding binding : target.getBindings()) {
                binding.getExpr().setIsUsed(true);
                used.add(binding.getExpr());
            }
        }
        while (!used.isEmpty()) {
            Expr e = used.remove(used.size() - 1);
            for (Dependency dep : e.getDependencies()) {
                if (!dep.getOther().isUsed()) {
                    used.add(dep.getOther());
                    dep.getOther().setIsUsed(true);
                }
            }
        }
    }

    public IdentifierExpr addVariable(String name, String type) {
        Preconditions.checkState(!mUserDefinedVariables.containsKey(name),
                "%s has already been defined as %s", name, type);
        final IdentifierExpr id = mExprModel.identifier(name);
        id.setUserDefinedType(type);
        id.enableDirectInvalidation();
        mUserDefinedVariables.put(name, type);
        return id;
    }

    public HashMap<String, String> getUserDefinedVariables() {
        return mUserDefinedVariables;
    }

    public BindingTarget createBindingTarget(ResourceBundle.BindingTargetBundle targetBundle) {
        final BindingTarget target = new BindingTarget(targetBundle);
        mBindingTargets.add(target);
        target.setModel(mExprModel);
        return target;
    }

    public Expr parse(String input) {
        final Expr parsed = mExpressionParser.parse(input);
        parsed.setBindingExpression(true);
        return parsed;
    }

    public List<BindingTarget> getBindingTargets() {
        return mBindingTargets;
    }

    public boolean isEmpty() {
        return mExprModel.size() == 0;
    }

    public ExprModel getModel() {
        return mExprModel;
    }

    private void ensureWriter() {
        if (mWriter == null) {
            mWriter = new LayoutBinderWriter(this);
        }
    }

    public String writeViewBinderInterface() {
        ensureWriter();
        return mWriter.writeBaseClass();
    }


    public String writeViewBinder() {
        mExprModel.seal();
        ensureWriter();
        Preconditions.checkNotNull(mPackage, "package cannot be null");
        Preconditions.checkNotNull(mProjectPackage, "project package cannot be null");
        Preconditions.checkNotNull(mBaseClassName, "base class name cannot be null");
        return mWriter.write();
    }

    public String getPackage() {
        return mPackage;
    }

    public void setPackage(String aPackage) {
        mPackage = aPackage;
    }

    public String getProjectPackage() {
        return mProjectPackage;
    }

    public String getLayoutname() {
        return mBundle.getFileName();
    }

    public String getClassName() {
        final String suffix;
        if (hasVariations()) {
            suffix = mBundle.getConfigName();
        } else {
            suffix = "";
        }
        return mBaseClassName + suffix + "Impl";

    }
    
    public String getInterfaceName() {
        return mBaseClassName;
    }

    public int getId() {
        return mBundle.getLayoutId();
    }

    public boolean hasVariations() {
        return mBundle.hasVariations();
    }

}