javanano_generator.cc revision 35d9fd84ff1d9ecdb91156b757cc9fdcc3f25249
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 {
124      *error = "Ignore unknown javanano generator option: " + options[i].first;
125    }
126  }
127
128  // -----------------------------------------------------------------
129
130  FileGenerator file_generator(file, params);
131  if (!file_generator.Validate(error)) {
132    return false;
133  }
134
135  string package_dir =
136    StringReplace(file_generator.java_package(), ".", "/", true);
137  if (!package_dir.empty()) package_dir += "/";
138
139  vector<string> all_files;
140
141  string java_filename = package_dir;
142  java_filename += file_generator.classname();
143  java_filename += ".java";
144  all_files.push_back(java_filename);
145
146  // Generate main java file.
147  scoped_ptr<io::ZeroCopyOutputStream> output(
148    output_directory->Open(java_filename));
149  io::Printer printer(output.get(), '$');
150  file_generator.Generate(&printer);
151
152  // Generate sibling files.
153  file_generator.GenerateSiblings(package_dir, output_directory, &all_files);
154
155  // Generate output list if requested.
156  if (!output_list_file.empty()) {
157    // Generate output list.  This is just a simple text file placed in a
158    // deterministic location which lists the .java files being generated.
159    scoped_ptr<io::ZeroCopyOutputStream> srclist_raw_output(
160      output_directory->Open(output_list_file));
161    io::Printer srclist_printer(srclist_raw_output.get(), '$');
162    for (int i = 0; i < all_files.size(); i++) {
163      srclist_printer.Print("$filename$\n", "filename", all_files[i]);
164    }
165  }
166
167  return true;
168}
169
170}  // namespace java
171}  // namespace compiler
172}  // namespace protobuf
173}  // namespace google
174