1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc. All rights reserved.
3 // http://code.google.com/p/protobuf/
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 // * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 // * Redistributions in binary form must reproduce the above
12 // copyright notice, this list of conditions and the following disclaimer
13 // in the documentation and/or other materials provided with the
14 // distribution.
15 // * Neither the name of Google Inc. nor the names of its
16 // contributors may be used to endorse or promote products derived from
17 // this software without specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
31 // Author: kenton@google.com (Kenton Varda)
32 // Based on original Protocol Buffers design by
33 // Sanjay Ghemawat, Jeff Dean, and others.
34
35 #include <google/protobuf/compiler/javamicro/javamicro_params.h>
36 #include <google/protobuf/compiler/javamicro/javamicro_generator.h>
37 #include <google/protobuf/compiler/javamicro/javamicro_file.h>
38 #include <google/protobuf/compiler/javamicro/javamicro_helpers.h>
39 #include <google/protobuf/io/printer.h>
40 #include <google/protobuf/io/zero_copy_stream.h>
41 #include <google/protobuf/descriptor.pb.h>
42 #include <google/protobuf/stubs/strutil.h>
43
44 namespace google {
45 namespace protobuf {
46 namespace compiler {
47 namespace javamicro {
48
UpdateParamsRecursively(Params & params,const FileDescriptor * file)49 void UpdateParamsRecursively(Params& params,
50 const FileDescriptor* file) {
51 // Add any parameters for this file
52 if (file->options().has_java_outer_classname()) {
53 params.set_java_outer_classname(
54 file->name(), file->options().java_outer_classname());
55 }
56 if (file->options().has_java_package()) {
57 params.set_java_package(
58 file->name(), file->options().java_package());
59 }
60 if (file->options().has_java_multiple_files()) {
61 params.set_java_multiple_files(
62 file->name(), file->options().java_multiple_files());
63 }
64
65 // Loop through all dependent files recursively
66 // adding dep
67 for (int i = 0; i < file->dependency_count(); i++) {
68 UpdateParamsRecursively(params, file->dependency(i));
69 }
70 }
71
JavaMicroGenerator()72 JavaMicroGenerator::JavaMicroGenerator() {}
~JavaMicroGenerator()73 JavaMicroGenerator::~JavaMicroGenerator() {}
74
Generate(const FileDescriptor * file,const string & parameter,OutputDirectory * output_directory,string * error) const75 bool JavaMicroGenerator::Generate(const FileDescriptor* file,
76 const string& parameter,
77 OutputDirectory* output_directory,
78 string* error) const {
79 vector<pair<string, string> > options;
80
81 // GOOGLE_LOG(INFO) << "wink: JavaMicroGenerator::Generate INFO";
82 // GOOGLE_LOG(WARNING) << "wink: JavaMicroGenerator::Generate WARNING";
83 // GOOGLE_LOG(ERROR) << "wink: JavaMicroGenerator::Generate ERROR";
84 // GOOGLE_LOG(FATAL) << "wink: JavaMicroGenerator::Generate";
85
86 ParseGeneratorParameter(parameter, &options);
87
88 // -----------------------------------------------------------------
89 // parse generator options
90
91 // Name a file where we will write a list of generated file names, one
92 // per line.
93 string output_list_file;
94 Params params(file->name());
95
96 // Update per file params
97 UpdateParamsRecursively(params, file);
98
99 // Replace any existing options with ones from command line
100 for (int i = 0; i < options.size(); i++) {
101 // GOOGLE_LOG(WARNING) << "first=" << options[i].first
102 // << " second=" << options[i].second;
103 if (options[i].first == "output_list_file") {
104 output_list_file = options[i].second;
105 } else if (options[i].first == "opt") {
106 if (options[i].second == "speed") {
107 params.set_optimization(JAVAMICRO_OPT_SPEED);
108 } else if (options[i].second == "space") {
109 params.set_optimization(JAVAMICRO_OPT_SPACE);
110 } else {
111 *error = "Unknown javamicro generator option: opt="
112 + options[i].second + " expecting opt=space or opt=speed";
113 return false;
114 }
115 } else if (options[i].first == "java_package") {
116 vector<string> parts;
117 SplitStringUsing(options[i].second, "|", &parts);
118 if (parts.size() != 2) {
119 *error = "Bad java_package, expecting filename|PackageName found '"
120 + options[i].second + "'";
121 return false;
122 }
123 params.set_java_package(parts[0], parts[1]);
124 } else if (options[i].first == "java_outer_classname") {
125 vector<string> parts;
126 SplitStringUsing(options[i].second, "|", &parts);
127 if (parts.size() != 2) {
128 *error = "Bad java_outer_classname, "
129 "expecting filename|ClassName found '"
130 + options[i].second + "'";
131 return false;
132 }
133 params.set_java_outer_classname(parts[0], parts[1]);
134 } else if (options[i].first == "java_multiple_files") {
135 params.set_override_java_multiple_files(options[i].second == "true");
136 } else if (options[i].first == "java_use_vector") {
137 params.set_java_use_vector(options[i].second == "true");
138 } else {
139 *error = "Ignore unknown javamicro generator option: " + options[i].first;
140 }
141 }
142
143 #if 0
144 GOOGLE_LOG(WARNING) << "optimization()=" << params.optimization();
145 GOOGLE_LOG(WARNING) << "java_multiple_files()=" << params.java_multiple_files();
146 GOOGLE_LOG(WARNING) << "java_use_vector()=" << params.java_use_vector();
147
148 GOOGLE_LOG(WARNING) << "----------";
149 for (Params::NameMap::const_iterator it = params.java_packages().begin();
150 it != params.java_packages().end();
151 ++it) {
152 GOOGLE_LOG(WARNING) << "cn.filename=" << it->first << " package=" << it->second;
153 }
154 for (Params::NameMap::const_iterator it = params.java_outer_classnames().begin();
155 it != params.java_outer_classnames().end();
156 ++it) {
157 GOOGLE_LOG(WARNING) << "cn.filename=" << it->first << " classname=" << it->second;
158 }
159 GOOGLE_LOG(WARNING) << "==========";
160
161 #endif
162
163 // -----------------------------------------------------------------
164
165 FileGenerator file_generator(file, params);
166 if (!file_generator.Validate(error)) {
167 return false;
168 }
169
170 string package_dir =
171 StringReplace(file_generator.java_package(), ".", "/", true);
172 if (!package_dir.empty()) package_dir += "/";
173
174 vector<string> all_files;
175
176 if (IsOuterClassNeeded(params, file)) {
177 string java_filename = package_dir;
178 java_filename += file_generator.classname();
179 java_filename += ".java";
180 all_files.push_back(java_filename);
181
182 // Generate main java file.
183 scoped_ptr<io::ZeroCopyOutputStream> output(
184 output_directory->Open(java_filename));
185 io::Printer printer(output.get(), '$');
186 file_generator.Generate(&printer);
187 }
188
189 // Generate sibling files.
190 file_generator.GenerateSiblings(package_dir, output_directory, &all_files);
191
192 // Generate output list if requested.
193 if (!output_list_file.empty()) {
194 // Generate output list. This is just a simple text file placed in a
195 // deterministic location which lists the .java files being generated.
196 scoped_ptr<io::ZeroCopyOutputStream> srclist_raw_output(
197 output_directory->Open(output_list_file));
198 io::Printer srclist_printer(srclist_raw_output.get(), '$');
199 for (int i = 0; i < all_files.size(); i++) {
200 srclist_printer.Print("$filename$\n", "filename", all_files[i]);
201 }
202 }
203
204 return true;
205 }
206
207 } // namespace java
208 } // namespace compiler
209 } // namespace protobuf
210 } // namespace google
211