1 /*
2  * Copyright (C) 2018 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 #ifndef JAVA_LANG_BUILDER_H_
17 #define JAVA_LANG_BUILDER_H_
18 
19 #include <iostream>
20 #include <sstream>
21 #include <vector>
22 
23 // Build Java language code to instantiate views.
24 //
25 // This has a very small interface to make it easier to generate additional
26 // backends, such as a direct-to-DEX version.
27 class JavaLangViewBuilder {
28  public:
29   JavaLangViewBuilder(std::string package, std::string layout_name, std::ostream& out = std::cout)
package_(package)30       : package_(package), layout_name_(layout_name), out_(out) {}
31 
32   // Begin generating a class. Adds the package boilerplate, etc.
33   void Start() const;
34   // Finish generating a class, closing off any open curly braces, etc.
35   void Finish() const;
36 
37   // Begin creating a view (i.e. process the opening tag)
38   void StartView(const std::string& class_name, bool is_viewgroup);
39   // Finish a view, after all of its child nodes have been processed.
40   void FinishView();
41 
42  private:
43   const std::string MakeVar(std::string prefix);
44 
45   std::string const package_;
46   std::string const layout_name_;
47 
48   std::ostream& out_;
49 
50   size_t view_id_ = 0;
51 
52   struct StackEntry {
53       // The class name for this view object
54       const std::string class_name;
55 
56       // The variable name that is holding the view object
57       const std::string view_var;
58 
59       // The variable name that holds the object's layout parameters
60       const std::string layout_params_var;
61   };
62   std::vector<StackEntry> view_stack_;
63 };
64 
65 #endif  // JAVA_LANG_BUILDER_H_
66