javamicro_generator.cc revision e2d542951c059563a3b7f74c257dac4f222d9dc5
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 44namespace google { 45namespace protobuf { 46namespace compiler { 47namespace javamicro { 48 49void 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 61 // Loop through all dependent files recursively 62 // adding dep 63 for (int i = 0; i < file->dependency_count(); i++) { 64 UpdateParamsRecursively(params, file->dependency(i)); 65 } 66} 67 68JavaMicroGenerator::JavaMicroGenerator() {} 69JavaMicroGenerator::~JavaMicroGenerator() {} 70 71bool JavaMicroGenerator::Generate(const FileDescriptor* file, 72 const string& parameter, 73 OutputDirectory* output_directory, 74 string* error) const { 75 vector<pair<string, string> > options; 76 77// GOOGLE_LOG(INFO) << "wink: JavaMicroGenerator::Generate INFO"; 78// GOOGLE_LOG(WARNING) << "wink: JavaMicroGenerator::Generate WARNING"; 79// GOOGLE_LOG(ERROR) << "wink: JavaMicroGenerator::Generate ERROR"; 80// GOOGLE_LOG(FATAL) << "wink: JavaMicroGenerator::Generate"; 81 82 ParseGeneratorParameter(parameter, &options); 83 84 // ----------------------------------------------------------------- 85 // parse generator options 86 87 // Name a file where we will write a list of generated file names, one 88 // per line. 89 string output_list_file; 90 Params params(file->name()); 91 92 // Get options from the proto file 93 if (file->options().has_java_multiple_files()) { 94 params.set_java_multiple_files(file->options().java_multiple_files()); 95 } 96 97 // Update per file params 98 UpdateParamsRecursively(params, file); 99 100 // Replace any existing options with ones from command line 101 for (int i = 0; i < options.size(); i++) { 102 // GOOGLE_LOG(WARNING) << "first=" << options[i].first 103 // << " second=" << options[i].second; 104 if (options[i].first == "output_list_file") { 105 output_list_file = options[i].second; 106 } else if (options[i].first == "opt") { 107 if (options[i].second == "speed") { 108 params.set_optimization(JAVAMICRO_OPT_SPEED); 109 } else if (options[i].second == "space") { 110 params.set_optimization(JAVAMICRO_OPT_SPACE); 111 } else { 112 *error = "Unknown javamicro generator option: opt=" 113 + options[i].second + " expecting opt=space or opt=speed"; 114 return false; 115 } 116 } else if (options[i].first == "java_package") { 117 vector<string> parts; 118 SplitStringUsing(options[i].second, "|", &parts); 119 if (parts.size() != 2) { 120 *error = "Bad java_package, expecting filename|PackageName found '" 121 + options[i].second + "'"; 122 return false; 123 } 124 params.set_java_package(parts[0], parts[1]); 125 } else if (options[i].first == "java_outer_classname") { 126 vector<string> parts; 127 SplitStringUsing(options[i].second, "|", &parts); 128 if (parts.size() != 2) { 129 *error = "Bad java_outer_classname, " 130 "expecting filename|ClassName found '" 131 + options[i].second + "'"; 132 return false; 133 } 134 params.set_java_outer_classname(parts[0], parts[1]); 135 } else if (options[i].first == "java_multiple_files") { 136 params.set_java_multiple_files(options[i].second == "true"); 137 } else if (options[i].first == "java_use_vector") { 138 params.set_java_use_vector(options[i].second == "true"); 139 } else { 140 *error = "Ignore unknown javamicro generator option: " + options[i].first; 141 } 142 } 143 144#if 0 145 GOOGLE_LOG(WARNING) << "optimization()=" << params.optimization(); 146 GOOGLE_LOG(WARNING) << "java_multiple_files()=" << params.java_multiple_files(); 147 GOOGLE_LOG(WARNING) << "java_use_vector()=" << params.java_use_vector(); 148 149 GOOGLE_LOG(WARNING) << "----------"; 150 for (Params::NameMap::const_iterator it = params.java_packages().begin(); 151 it != params.java_packages().end(); 152 ++it) { 153 GOOGLE_LOG(WARNING) << "cn.filename=" << it->first << " package=" << it->second; 154 } 155 for (Params::NameMap::const_iterator it = params.java_outer_classnames().begin(); 156 it != params.java_outer_classnames().end(); 157 ++it) { 158 GOOGLE_LOG(WARNING) << "cn.filename=" << it->first << " classname=" << it->second; 159 } 160 GOOGLE_LOG(WARNING) << "=========="; 161 162#endif 163 164 // ----------------------------------------------------------------- 165 166 FileGenerator file_generator(file, params); 167 if (!file_generator.Validate(error)) { 168 return false; 169 } 170 171 string package_dir = 172 StringReplace(file_generator.java_package(), ".", "/", true); 173 if (!package_dir.empty()) package_dir += "/"; 174 175 vector<string> all_files; 176 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 // Generate sibling files. 189 file_generator.GenerateSiblings(package_dir, output_directory, &all_files); 190 191 // Generate output list if requested. 192 if (!output_list_file.empty()) { 193 // Generate output list. This is just a simple text file placed in a 194 // deterministic location which lists the .java files being generated. 195 scoped_ptr<io::ZeroCopyOutputStream> srclist_raw_output( 196 output_directory->Open(output_list_file)); 197 io::Printer srclist_printer(srclist_raw_output.get(), '$'); 198 for (int i = 0; i < all_files.size(); i++) { 199 srclist_printer.Print("$filename$\n", "filename", all_files[i]); 200 } 201 } 202 203 return true; 204} 205 206} // namespace java 207} // namespace compiler 208} // namespace protobuf 209} // namespace google 210