This source file includes following definitions.
- EscapeJavadoc
- WriteDocCommentBodyForLocation
- WriteDocCommentBody
- FirstLineOf
- WriteMessageDocComment
- WriteFieldDocComment
- WriteEnumDocComment
- WriteEnumValueDocComment
- WriteServiceDocComment
- WriteMethodDocComment
#include <google/protobuf/compiler/java/java_doc_comment.h>
#include <vector>
#include <google/protobuf/io/printer.h>
#include <google/protobuf/stubs/strutil.h>
namespace google {
namespace protobuf {
namespace compiler {
namespace java {
string EscapeJavadoc(const string& input) {
string result;
result.reserve(input.size() * 2);
char prev = '*';
for (string::size_type i = 0; i < input.size(); i++) {
char c = input[i];
switch (c) {
case '*':
if (prev == '/') {
result.append("*");
} else {
result.push_back(c);
}
break;
case '/':
if (prev == '*') {
result.append("/");
} else {
result.push_back(c);
}
break;
case '@':
if (prev == '{') {
result.append("@");
} else {
result.push_back(c);
}
break;
case '<':
result.append("<");
break;
case '>':
result.append(">");
break;
case '&':
result.append("&");
break;
case '\\':
result.append("\");
break;
default:
result.push_back(c);
break;
}
prev = c;
}
return result;
}
static void WriteDocCommentBodyForLocation(
io::Printer* printer, const SourceLocation& location) {
string comments = location.leading_comments.empty() ?
location.trailing_comments : location.leading_comments;
if (!comments.empty()) {
comments = EscapeJavadoc(comments);
vector<string> lines;
SplitStringAllowEmpty(comments, "\n", &lines);
while (!lines.empty() && lines.back().empty()) {
lines.pop_back();
}
printer->Print(
" *\n"
" * <pre>\n");
for (int i = 0; i < lines.size(); i++) {
if (!lines[i].empty() && lines[i][0] == '/') {
printer->Print(" * $line$\n", "line", lines[i]);
} else {
printer->Print(" *$line$\n", "line", lines[i]);
}
}
printer->Print(" * </pre>\n");
}
}
template <typename DescriptorType>
static void WriteDocCommentBody(
io::Printer* printer, const DescriptorType* descriptor) {
SourceLocation location;
if (descriptor->GetSourceLocation(&location)) {
WriteDocCommentBodyForLocation(printer, location);
}
}
static string FirstLineOf(const string& value) {
string result = value;
string::size_type pos = result.find_first_of('\n');
if (pos != string::npos) {
result.erase(pos);
}
if (!result.empty() && result[result.size() - 1] == '{') {
result.append(" ... }");
}
return result;
}
void WriteMessageDocComment(io::Printer* printer, const Descriptor* message) {
printer->Print(
"/**\n"
" * Protobuf type {@code $fullname$}\n",
"fullname", EscapeJavadoc(message->full_name()));
WriteDocCommentBody(printer, message);
printer->Print(" */\n");
}
void WriteFieldDocComment(io::Printer* printer, const FieldDescriptor* field) {
printer->Print(
"/**\n"
" * <code>$def$</code>\n",
"def", EscapeJavadoc(FirstLineOf(field->DebugString())));
WriteDocCommentBody(printer, field);
printer->Print(" */\n");
}
void WriteEnumDocComment(io::Printer* printer, const EnumDescriptor* enum_) {
printer->Print(
"/**\n"
" * Protobuf enum {@code $fullname$}\n",
"fullname", EscapeJavadoc(enum_->full_name()));
WriteDocCommentBody(printer, enum_);
printer->Print(" */\n");
}
void WriteEnumValueDocComment(io::Printer* printer,
const EnumValueDescriptor* value) {
printer->Print(
"/**\n"
" * <code>$def$</code>\n",
"def", EscapeJavadoc(FirstLineOf(value->DebugString())));
WriteDocCommentBody(printer, value);
printer->Print(" */\n");
}
void WriteServiceDocComment(io::Printer* printer,
const ServiceDescriptor* service) {
printer->Print(
"/**\n"
" * Protobuf service {@code $fullname$}\n",
"fullname", EscapeJavadoc(service->full_name()));
WriteDocCommentBody(printer, service);
printer->Print(" */\n");
}
void WriteMethodDocComment(io::Printer* printer,
const MethodDescriptor* method) {
printer->Print(
"/**\n"
" * <code>$def$</code>\n",
"def", EscapeJavadoc(FirstLineOf(method->DebugString())));
WriteDocCommentBody(printer, method);
printer->Print(" */\n");
}
}
}
}
}