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 
17 #ifndef AAPT_JAVA_CLASS_GENERATOR_H
18 #define AAPT_JAVA_CLASS_GENERATOR_H
19 
20 #include "ResourceTable.h"
21 #include "ResourceValues.h"
22 
23 #include <ostream>
24 #include <string>
25 
26 namespace aapt {
27 
28 /*
29  * Generates the R.java file for a resource table.
30  */
31 class JavaClassGenerator : ConstValueVisitor {
32 public:
33     /*
34      * A set of options for this JavaClassGenerator.
35      */
36     struct Options {
37         /*
38          * Specifies whether to use the 'final' modifier
39          * on resource entries. Default is true.
40          */
41         bool useFinal = true;
42     };
43 
44     JavaClassGenerator(const std::shared_ptr<const ResourceTable>& table, Options options);
45 
46     /*
47      * Writes the R.java file to `out`. Only symbols belonging to `package` are written.
48      * All symbols technically belong to a single package, but linked libraries will
49      * have their names mangled, denoting that they came from a different package.
50      * We need to generate these symbols in a separate file.
51      * Returns true on success.
52      */
53     bool generate(const std::u16string& package, std::ostream& out);
54 
55     /*
56      * ConstValueVisitor implementation.
57      */
58     void visit(const Styleable& styleable, ValueVisitorArgs& args);
59 
60     const std::string& getError() const;
61 
62 private:
63     bool generateType(const std::u16string& package, size_t packageId,
64                       const ResourceTableType& type, std::ostream& out);
65 
66     std::shared_ptr<const ResourceTable> mTable;
67     Options mOptions;
68     std::string mError;
69 };
70 
getError()71 inline const std::string& JavaClassGenerator::getError() const {
72     return mError;
73 }
74 
75 } // namespace aapt
76 
77 #endif // AAPT_JAVA_CLASS_GENERATOR_H
78