javanano_generator.cc revision 0e055f079f53b07de3705838a7b4742ce56839f8
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 44namespace google { 45namespace protobuf { 46namespace compiler { 47namespace javanano { 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 68JavaNanoGenerator::JavaNanoGenerator() {} 69JavaNanoGenerator::~JavaNanoGenerator() {} 70 71bool JavaNanoGenerator::Generate(const FileDescriptor* file, 72 const string& parameter, 73 OutputDirectory* output_directory, 74 string* error) const { 75 vector<pair<string, string> > options; 76 77 ParseGeneratorParameter(parameter, &options); 78 79 // ----------------------------------------------------------------- 80 // parse generator options 81 82 // Name a file where we will write a list of generated file names, one 83 // per line. 84 string output_list_file; 85 Params params(file->name()); 86 87 // Get options from the proto file 88 if (file->options().has_java_multiple_files()) { 89 params.set_java_multiple_files(file->options().java_multiple_files()); 90 } 91 92 // Update per file params 93 UpdateParamsRecursively(params, file); 94 95 // Replace any existing options with ones from command line 96 for (int i = 0; i < options.size(); i++) { 97 if (options[i].first == "output_list_file") { 98 output_list_file = options[i].second; 99 } else if (options[i].first == "java_package") { 100 vector<string> parts; 101 SplitStringUsing(options[i].second, "|", &parts); 102 if (parts.size() != 2) { 103 *error = "Bad java_package, expecting filename|PackageName found '" 104 + options[i].second + "'"; 105 return false; 106 } 107 params.set_java_package(parts[0], parts[1]); 108 } else if (options[i].first == "java_outer_classname") { 109 vector<string> parts; 110 SplitStringUsing(options[i].second, "|", &parts); 111 if (parts.size() != 2) { 112 *error = "Bad java_outer_classname, " 113 "expecting filename|ClassName found '" 114 + options[i].second + "'"; 115 return false; 116 } 117 params.set_java_outer_classname(parts[0], parts[1]); 118 } else if (options[i].first == "store_unknown_fields") { 119 params.set_store_unknown_fields(options[i].second == "true"); 120 } else if (options[i].first == "java_multiple_files") { 121 params.set_java_multiple_files(options[i].second == "true"); 122 } else { 123 *error = "Ignore unknown javanano generator option: " + options[i].first; 124 } 125 } 126 127 // ----------------------------------------------------------------- 128 129 FileGenerator file_generator(file, params); 130 if (!file_generator.Validate(error)) { 131 return false; 132 } 133 134 string package_dir = 135 StringReplace(file_generator.java_package(), ".", "/", true); 136 if (!package_dir.empty()) package_dir += "/"; 137 138 vector<string> all_files; 139 140 string java_filename = package_dir; 141 java_filename += file_generator.classname(); 142 java_filename += ".java"; 143 all_files.push_back(java_filename); 144 145 // Generate main java file. 146 scoped_ptr<io::ZeroCopyOutputStream> output( 147 output_directory->Open(java_filename)); 148 io::Printer printer(output.get(), '$'); 149 file_generator.Generate(&printer); 150 151 // Generate sibling files. 152 file_generator.GenerateSiblings(package_dir, output_directory, &all_files); 153 154 // Generate output list if requested. 155 if (!output_list_file.empty()) { 156 // Generate output list. This is just a simple text file placed in a 157 // deterministic location which lists the .java files being generated. 158 scoped_ptr<io::ZeroCopyOutputStream> srclist_raw_output( 159 output_directory->Open(output_list_file)); 160 io::Printer srclist_printer(srclist_raw_output.get(), '$'); 161 for (int i = 0; i < all_files.size(); i++) { 162 srclist_printer.Print("$filename$\n", "filename", all_files[i]); 163 } 164 } 165 166 return true; 167} 168 169} // namespace java 170} // namespace compiler 171} // namespace protobuf 172} // namespace google 173