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