Ulas Kirazci | 2337023 | 2013-03-14 16:44:33 -0700 | [diff] [blame] | 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/javanano/javanano_params.h> |
| 36 | #include <google/protobuf/compiler/javanano/javanano_generator.h> |
| 37 | #include <google/protobuf/compiler/javanano/javanano_file.h> |
| 38 | #include <google/protobuf/compiler/javanano/javanano_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 javanano { |
| 48 | |
Max Cai | bc8eec3 | 2014-01-14 14:54:48 +0000 | [diff] [blame] | 49 | namespace { |
| 50 | |
| 51 | string TrimString(const string& s) { |
| 52 | string::size_type start = s.find_first_not_of(" \n\r\t"); |
| 53 | if (start == string::npos) { |
| 54 | return ""; |
| 55 | } |
| 56 | string::size_type end = s.find_last_not_of(" \n\r\t") + 1; |
| 57 | return s.substr(start, end - start); |
| 58 | } |
| 59 | |
| 60 | } // namespace |
| 61 | |
Ulas Kirazci | 2337023 | 2013-03-14 16:44:33 -0700 | [diff] [blame] | 62 | void UpdateParamsRecursively(Params& params, |
| 63 | const FileDescriptor* file) { |
| 64 | // Add any parameters for this file |
| 65 | if (file->options().has_java_outer_classname()) { |
| 66 | params.set_java_outer_classname( |
| 67 | file->name(), file->options().java_outer_classname()); |
| 68 | } |
| 69 | if (file->options().has_java_package()) { |
Xiao Hang | 801e920 | 2015-07-27 17:46:22 -0700 | [diff] [blame] | 70 | string result = file->options().java_package(); |
Jisi Liu | 3b3c8ab | 2016-03-30 11:39:59 -0700 | [diff] [blame] | 71 | result += "nano"; |
Ulas Kirazci | 2337023 | 2013-03-14 16:44:33 -0700 | [diff] [blame] | 72 | params.set_java_package( |
Xiao Hang | 801e920 | 2015-07-27 17:46:22 -0700 | [diff] [blame] | 73 | file->name(), result); |
Ulas Kirazci | 2337023 | 2013-03-14 16:44:33 -0700 | [diff] [blame] | 74 | } |
Max Cai | 4c4d639 | 2013-07-24 14:16:38 +0100 | [diff] [blame] | 75 | if (file->options().has_java_multiple_files()) { |
| 76 | params.set_java_multiple_files( |
| 77 | file->name(), file->options().java_multiple_files()); |
| 78 | } |
Ulas Kirazci | 2337023 | 2013-03-14 16:44:33 -0700 | [diff] [blame] | 79 | |
| 80 | // Loop through all dependent files recursively |
| 81 | // adding dep |
| 82 | for (int i = 0; i < file->dependency_count(); i++) { |
| 83 | UpdateParamsRecursively(params, file->dependency(i)); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | JavaNanoGenerator::JavaNanoGenerator() {} |
| 88 | JavaNanoGenerator::~JavaNanoGenerator() {} |
| 89 | |
| 90 | bool JavaNanoGenerator::Generate(const FileDescriptor* file, |
| 91 | const string& parameter, |
Feng Xiao | cd980d1 | 2014-11-19 15:58:54 -0800 | [diff] [blame] | 92 | GeneratorContext* output_directory, |
Ulas Kirazci | 2337023 | 2013-03-14 16:44:33 -0700 | [diff] [blame] | 93 | string* error) const { |
| 94 | vector<pair<string, string> > options; |
| 95 | |
| 96 | ParseGeneratorParameter(parameter, &options); |
| 97 | |
| 98 | // ----------------------------------------------------------------- |
| 99 | // parse generator options |
| 100 | |
| 101 | // Name a file where we will write a list of generated file names, one |
| 102 | // per line. |
| 103 | string output_list_file; |
| 104 | Params params(file->name()); |
| 105 | |
Ulas Kirazci | 2337023 | 2013-03-14 16:44:33 -0700 | [diff] [blame] | 106 | // Update per file params |
| 107 | UpdateParamsRecursively(params, file); |
| 108 | |
| 109 | // Replace any existing options with ones from command line |
| 110 | for (int i = 0; i < options.size(); i++) { |
Max Cai | bc8eec3 | 2014-01-14 14:54:48 +0000 | [diff] [blame] | 111 | string option_name = TrimString(options[i].first); |
| 112 | string option_value = TrimString(options[i].second); |
| 113 | if (option_name == "output_list_file") { |
| 114 | output_list_file = option_value; |
| 115 | } else if (option_name == "java_package") { |
Ulas Kirazci | 2337023 | 2013-03-14 16:44:33 -0700 | [diff] [blame] | 116 | vector<string> parts; |
Max Cai | bc8eec3 | 2014-01-14 14:54:48 +0000 | [diff] [blame] | 117 | SplitStringUsing(option_value, "|", &parts); |
Ulas Kirazci | 2337023 | 2013-03-14 16:44:33 -0700 | [diff] [blame] | 118 | if (parts.size() != 2) { |
| 119 | *error = "Bad java_package, expecting filename|PackageName found '" |
Max Cai | bc8eec3 | 2014-01-14 14:54:48 +0000 | [diff] [blame] | 120 | + option_value + "'"; |
Ulas Kirazci | 2337023 | 2013-03-14 16:44:33 -0700 | [diff] [blame] | 121 | return false; |
| 122 | } |
| 123 | params.set_java_package(parts[0], parts[1]); |
Max Cai | bc8eec3 | 2014-01-14 14:54:48 +0000 | [diff] [blame] | 124 | } else if (option_name == "java_outer_classname") { |
Ulas Kirazci | 2337023 | 2013-03-14 16:44:33 -0700 | [diff] [blame] | 125 | vector<string> parts; |
Max Cai | bc8eec3 | 2014-01-14 14:54:48 +0000 | [diff] [blame] | 126 | SplitStringUsing(option_value, "|", &parts); |
Ulas Kirazci | 2337023 | 2013-03-14 16:44:33 -0700 | [diff] [blame] | 127 | if (parts.size() != 2) { |
| 128 | *error = "Bad java_outer_classname, " |
| 129 | "expecting filename|ClassName found '" |
Max Cai | bc8eec3 | 2014-01-14 14:54:48 +0000 | [diff] [blame] | 130 | + option_value + "'"; |
Ulas Kirazci | 2337023 | 2013-03-14 16:44:33 -0700 | [diff] [blame] | 131 | return false; |
| 132 | } |
| 133 | params.set_java_outer_classname(parts[0], parts[1]); |
Max Cai | bc8eec3 | 2014-01-14 14:54:48 +0000 | [diff] [blame] | 134 | } else if (option_name == "store_unknown_fields") { |
| 135 | params.set_store_unknown_fields(option_value == "true"); |
| 136 | } else if (option_name == "java_multiple_files") { |
| 137 | params.set_override_java_multiple_files(option_value == "true"); |
| 138 | } else if (option_name == "java_nano_generate_has") { |
| 139 | params.set_generate_has(option_value == "true"); |
| 140 | } else if (option_name == "enum_style") { |
| 141 | params.set_java_enum_style(option_value == "java"); |
| 142 | } else if (option_name == "optional_field_style") { |
| 143 | params.set_optional_field_accessors(option_value == "accessors"); |
Brian Duff | a892068 | 2014-09-30 10:57:50 -0700 | [diff] [blame] | 144 | params.set_use_reference_types_for_primitives(option_value == "reftypes" |
Brian Duff | 7fbf654 | 2014-10-01 12:55:00 -0700 | [diff] [blame] | 145 | || option_value == "reftypes_compat_mode"); |
Brian Duff | a892068 | 2014-09-30 10:57:50 -0700 | [diff] [blame] | 146 | params.set_reftypes_primitive_enums( |
Brian Duff | 7fbf654 | 2014-10-01 12:55:00 -0700 | [diff] [blame] | 147 | option_value == "reftypes_compat_mode"); |
| 148 | if (option_value == "reftypes_compat_mode") { |
| 149 | params.set_generate_clear(false); |
| 150 | } |
Max Cai | bc8eec3 | 2014-01-14 14:54:48 +0000 | [diff] [blame] | 151 | } else if (option_name == "generate_equals") { |
| 152 | params.set_generate_equals(option_value == "true"); |
Jie Dai | d9425a6 | 2014-04-21 14:29:03 -0700 | [diff] [blame] | 153 | } else if (option_name == "ignore_services") { |
| 154 | params.set_ignore_services(option_value == "true"); |
Jeff Davidson | ec4b1ce | 2014-04-22 23:25:53 -0700 | [diff] [blame] | 155 | } else if (option_name == "parcelable_messages") { |
| 156 | params.set_parcelable_messages(option_value == "true"); |
Brian Duff | d099a88 | 2014-10-01 13:33:40 -0700 | [diff] [blame] | 157 | } else if (option_name == "generate_clone") { |
| 158 | params.set_generate_clone(option_value == "true"); |
Jeff Davidson | ec19be2 | 2015-02-11 13:12:14 -0800 | [diff] [blame] | 159 | } else if (option_name == "generate_intdefs") { |
| 160 | params.set_generate_intdefs(option_value == "true"); |
Brian Duff | dac7e02 | 2015-02-21 15:22:43 -0800 | [diff] [blame] | 161 | } else if (option_name == "generate_clear") { |
| 162 | params.set_generate_clear(option_value == "true"); |
Ulas Kirazci | 2337023 | 2013-03-14 16:44:33 -0700 | [diff] [blame] | 163 | } else { |
Max Cai | bc8eec3 | 2014-01-14 14:54:48 +0000 | [diff] [blame] | 164 | *error = "Ignore unknown javanano generator option: " + option_name; |
Ulas Kirazci | 2337023 | 2013-03-14 16:44:33 -0700 | [diff] [blame] | 165 | } |
| 166 | } |
| 167 | |
Max Cai | 7176612 | 2013-09-18 17:10:31 +0100 | [diff] [blame] | 168 | // Check illegal parameter combinations |
Brian Duff | 10107cb | 2013-09-30 20:49:13 -0700 | [diff] [blame] | 169 | // Note: the enum-like optional_field_style generator param ensures |
| 170 | // that we can never have illegal combinations of field styles |
| 171 | // (e.g. reftypes and accessors can't be on at the same time). |
| 172 | if (params.generate_has() |
| 173 | && (params.optional_field_accessors() |
| 174 | || params.use_reference_types_for_primitives())) { |
Max Cai | 7176612 | 2013-09-18 17:10:31 +0100 | [diff] [blame] | 175 | error->assign("java_nano_generate_has=true cannot be used in conjunction" |
Brian Duff | 10107cb | 2013-09-30 20:49:13 -0700 | [diff] [blame] | 176 | " with optional_field_style=accessors or optional_field_style=reftypes"); |
Max Cai | 7176612 | 2013-09-18 17:10:31 +0100 | [diff] [blame] | 177 | return false; |
| 178 | } |
| 179 | |
Ulas Kirazci | 2337023 | 2013-03-14 16:44:33 -0700 | [diff] [blame] | 180 | // ----------------------------------------------------------------- |
| 181 | |
| 182 | FileGenerator file_generator(file, params); |
| 183 | if (!file_generator.Validate(error)) { |
| 184 | return false; |
| 185 | } |
| 186 | |
| 187 | string package_dir = |
| 188 | StringReplace(file_generator.java_package(), ".", "/", true); |
| 189 | if (!package_dir.empty()) package_dir += "/"; |
| 190 | |
| 191 | vector<string> all_files; |
| 192 | |
Max Cai | 06eed37 | 2013-07-29 17:20:50 +0100 | [diff] [blame] | 193 | if (IsOuterClassNeeded(params, file)) { |
| 194 | string java_filename = package_dir; |
| 195 | java_filename += file_generator.classname(); |
| 196 | java_filename += ".java"; |
| 197 | all_files.push_back(java_filename); |
Ulas Kirazci | 2337023 | 2013-03-14 16:44:33 -0700 | [diff] [blame] | 198 | |
Max Cai | 06eed37 | 2013-07-29 17:20:50 +0100 | [diff] [blame] | 199 | // Generate main java file. |
| 200 | scoped_ptr<io::ZeroCopyOutputStream> output( |
| 201 | output_directory->Open(java_filename)); |
| 202 | io::Printer printer(output.get(), '$'); |
| 203 | file_generator.Generate(&printer); |
| 204 | } |
Ulas Kirazci | 2337023 | 2013-03-14 16:44:33 -0700 | [diff] [blame] | 205 | |
| 206 | // Generate sibling files. |
| 207 | file_generator.GenerateSiblings(package_dir, output_directory, &all_files); |
| 208 | |
| 209 | // Generate output list if requested. |
| 210 | if (!output_list_file.empty()) { |
| 211 | // Generate output list. This is just a simple text file placed in a |
| 212 | // deterministic location which lists the .java files being generated. |
| 213 | scoped_ptr<io::ZeroCopyOutputStream> srclist_raw_output( |
| 214 | output_directory->Open(output_list_file)); |
| 215 | io::Printer srclist_printer(srclist_raw_output.get(), '$'); |
| 216 | for (int i = 0; i < all_files.size(); i++) { |
| 217 | srclist_printer.Print("$filename$\n", "filename", all_files[i]); |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | return true; |
| 222 | } |
| 223 | |
| 224 | } // namespace java |
| 225 | } // namespace compiler |
| 226 | } // namespace protobuf |
| 227 | } // namespace google |