1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc. All rights reserved.
3 // https://developers.google.com/protocol-buffers/
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 #include <sstream>
32
33 #include <google/protobuf/compiler/code_generator.h>
34 #include <google/protobuf/compiler/plugin.h>
35 #include <google/protobuf/descriptor.h>
36 #include <google/protobuf/descriptor.pb.h>
37 #include <google/protobuf/io/printer.h>
38 #include <google/protobuf/io/zero_copy_stream.h>
39 #include <google/protobuf/stubs/strutil.h>
40
41 #include <google/protobuf/compiler/csharp/csharp_generator.h>
42 #include <google/protobuf/compiler/csharp/csharp_helpers.h>
43 #include <google/protobuf/compiler/csharp/csharp_names.h>
44 #include <google/protobuf/compiler/csharp/csharp_options.h>
45 #include <google/protobuf/compiler/csharp/csharp_reflection_class.h>
46
47 using google::protobuf::internal::scoped_ptr;
48
49 namespace google {
50 namespace protobuf {
51 namespace compiler {
52 namespace csharp {
53
GenerateFile(const google::protobuf::FileDescriptor * file,io::Printer * printer,const Options * options)54 void GenerateFile(const google::protobuf::FileDescriptor* file,
55 io::Printer* printer,
56 const Options* options) {
57 ReflectionClassGenerator reflectionClassGenerator(file, options);
58 reflectionClassGenerator.Generate(printer);
59 }
60
Generate(const FileDescriptor * file,const string & parameter,GeneratorContext * generator_context,string * error) const61 bool Generator::Generate(
62 const FileDescriptor* file,
63 const string& parameter,
64 GeneratorContext* generator_context,
65 string* error) const {
66
67 vector<pair<string, string> > options;
68 ParseGeneratorParameter(parameter, &options);
69
70 // We only support proto3 - but we make an exception for descriptor.proto.
71 if (file->syntax() != FileDescriptor::SYNTAX_PROTO3 && !IsDescriptorProto(file)) {
72 *error = "C# code generation only supports proto3 syntax";
73 return false;
74 }
75
76 struct Options cli_options;
77
78 for (int i = 0; i < options.size(); i++) {
79 if (options[i].first == "file_extension") {
80 cli_options.file_extension = options[i].second;
81 } else if (options[i].first == "base_namespace") {
82 cli_options.base_namespace = options[i].second;
83 cli_options.base_namespace_specified = true;
84 } else if (options[i].first == "internal_access") {
85 cli_options.internal_access = true;
86 } else if (options[i].first == "legacy_enum_values") {
87 // TODO: Remove this before final release
88 cli_options.legacy_enum_values = true;
89 } else {
90 *error = "Unknown generator option: " + options[i].first;
91 return false;
92 }
93 }
94
95 string filename_error = "";
96 std::string filename = GetOutputFile(file,
97 cli_options.file_extension,
98 cli_options.base_namespace_specified,
99 cli_options.base_namespace,
100 &filename_error);
101
102 if (filename.empty()) {
103 *error = filename_error;
104 return false;
105 }
106 scoped_ptr<io::ZeroCopyOutputStream> output(
107 generator_context->Open(filename));
108 io::Printer printer(output.get(), '$');
109
110 GenerateFile(file, &printer, &cli_options);
111
112 return true;
113 }
114
115 } // namespace csharp
116 } // namespace compiler
117 } // namespace protobuf
118 } // namespace google
119