| OLD | NEW |
| 1 // Protocol Buffers - Google's data interchange format | 1 // Protocol Buffers - Google's data interchange format |
| 2 // Copyright 2008 Google Inc. All rights reserved. | 2 // Copyright 2008 Google Inc. All rights reserved. |
| 3 // http://code.google.com/p/protobuf/ | 3 // https://developers.google.com/protocol-buffers/ |
| 4 // | 4 // |
| 5 // Redistribution and use in source and binary forms, with or without | 5 // Redistribution and use in source and binary forms, with or without |
| 6 // modification, are permitted provided that the following conditions are | 6 // modification, are permitted provided that the following conditions are |
| 7 // met: | 7 // met: |
| 8 // | 8 // |
| 9 // * Redistributions of source code must retain the above copyright | 9 // * Redistributions of source code must retain the above copyright |
| 10 // notice, this list of conditions and the following disclaimer. | 10 // notice, this list of conditions and the following disclaimer. |
| 11 // * Redistributions in binary form must reproduce the above | 11 // * Redistributions in binary form must reproduce the above |
| 12 // copyright notice, this list of conditions and the following disclaimer | 12 // copyright notice, this list of conditions and the following disclaimer |
| 13 // in the documentation and/or other materials provided with the | 13 // in the documentation and/or other materials provided with the |
| (...skipping 18 matching lines...) Expand all Loading... |
| 32 // Based on original Protocol Buffers design by | 32 // Based on original Protocol Buffers design by |
| 33 // Sanjay Ghemawat, Jeff Dean, and others. | 33 // Sanjay Ghemawat, Jeff Dean, and others. |
| 34 // | 34 // |
| 35 // Utilities for printing and parsing protocol messages in a human-readable, | 35 // Utilities for printing and parsing protocol messages in a human-readable, |
| 36 // text-based format. | 36 // text-based format. |
| 37 | 37 |
| 38 #ifndef GOOGLE_PROTOBUF_TEXT_FORMAT_H__ | 38 #ifndef GOOGLE_PROTOBUF_TEXT_FORMAT_H__ |
| 39 #define GOOGLE_PROTOBUF_TEXT_FORMAT_H__ | 39 #define GOOGLE_PROTOBUF_TEXT_FORMAT_H__ |
| 40 | 40 |
| 41 #include <map> | 41 #include <map> |
| 42 #include <memory> |
| 43 #ifndef _SHARED_PTR_H |
| 44 #include <google/protobuf/stubs/shared_ptr.h> |
| 45 #endif |
| 42 #include <string> | 46 #include <string> |
| 43 #include <vector> | 47 #include <vector> |
| 48 |
| 44 #include <google/protobuf/stubs/common.h> | 49 #include <google/protobuf/stubs/common.h> |
| 50 #include <google/protobuf/descriptor.h> |
| 45 #include <google/protobuf/message.h> | 51 #include <google/protobuf/message.h> |
| 46 #include <google/protobuf/descriptor.h> | |
| 47 | 52 |
| 48 namespace google { | 53 namespace google { |
| 49 namespace protobuf { | 54 namespace protobuf { |
| 50 | 55 |
| 51 namespace io { | 56 namespace io { |
| 52 class ErrorCollector; // tokenizer.h | 57 class ErrorCollector; // tokenizer.h |
| 53 } | 58 } |
| 54 | 59 |
| 55 // This class implements protocol buffer text format. Printing and parsing | 60 // This class implements protocol buffer text format. Printing and parsing |
| 56 // protocol messages in text format is useful for debugging and human editing | 61 // protocol messages in text format is useful for debugging and human editing |
| (...skipping 21 matching lines...) Expand all Loading... |
| 78 | 83 |
| 79 // Outputs a textual representation of the value of the field supplied on | 84 // Outputs a textual representation of the value of the field supplied on |
| 80 // the message supplied. For non-repeated fields, an index of -1 must | 85 // the message supplied. For non-repeated fields, an index of -1 must |
| 81 // be supplied. Note that this method will print the default value for a | 86 // be supplied. Note that this method will print the default value for a |
| 82 // field if it is not set. | 87 // field if it is not set. |
| 83 static void PrintFieldValueToString(const Message& message, | 88 static void PrintFieldValueToString(const Message& message, |
| 84 const FieldDescriptor* field, | 89 const FieldDescriptor* field, |
| 85 int index, | 90 int index, |
| 86 string* output); | 91 string* output); |
| 87 | 92 |
| 93 // The default printer that converts scalar values from fields into |
| 94 // their string representation. |
| 95 // You can derive from this FieldValuePrinter if you want to have |
| 96 // fields to be printed in a different way and register it at the |
| 97 // Printer. |
| 98 class LIBPROTOBUF_EXPORT FieldValuePrinter { |
| 99 public: |
| 100 FieldValuePrinter(); |
| 101 virtual ~FieldValuePrinter(); |
| 102 virtual string PrintBool(bool val) const; |
| 103 virtual string PrintInt32(int32 val) const; |
| 104 virtual string PrintUInt32(uint32 val) const; |
| 105 virtual string PrintInt64(int64 val) const; |
| 106 virtual string PrintUInt64(uint64 val) const; |
| 107 virtual string PrintFloat(float val) const; |
| 108 virtual string PrintDouble(double val) const; |
| 109 virtual string PrintString(const string& val) const; |
| 110 virtual string PrintBytes(const string& val) const; |
| 111 virtual string PrintEnum(int32 val, const string& name) const; |
| 112 virtual string PrintFieldName(const Message& message, |
| 113 const Reflection* reflection, |
| 114 const FieldDescriptor* field) const; |
| 115 virtual string PrintMessageStart(const Message& message, |
| 116 int field_index, |
| 117 int field_count, |
| 118 bool single_line_mode) const; |
| 119 virtual string PrintMessageEnd(const Message& message, |
| 120 int field_index, |
| 121 int field_count, |
| 122 bool single_line_mode) const; |
| 123 |
| 124 private: |
| 125 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(FieldValuePrinter); |
| 126 }; |
| 127 |
| 88 // Class for those users which require more fine-grained control over how | 128 // Class for those users which require more fine-grained control over how |
| 89 // a protobuffer message is printed out. | 129 // a protobuffer message is printed out. |
| 90 class LIBPROTOBUF_EXPORT Printer { | 130 class LIBPROTOBUF_EXPORT Printer { |
| 91 public: | 131 public: |
| 92 Printer(); | 132 Printer(); |
| 93 ~Printer(); | 133 ~Printer(); |
| 94 | 134 |
| 95 // Like TextFormat::Print | 135 // Like TextFormat::Print |
| 96 bool Print(const Message& message, io::ZeroCopyOutputStream* output) const; | 136 bool Print(const Message& message, io::ZeroCopyOutputStream* output) const; |
| 97 // Like TextFormat::PrintUnknownFields | 137 // Like TextFormat::PrintUnknownFields |
| (...skipping 15 matching lines...) Expand all Loading... |
| 113 void SetInitialIndentLevel(int indent_level) { | 153 void SetInitialIndentLevel(int indent_level) { |
| 114 initial_indent_level_ = indent_level; | 154 initial_indent_level_ = indent_level; |
| 115 } | 155 } |
| 116 | 156 |
| 117 // If printing in single line mode, then the entire message will be output | 157 // If printing in single line mode, then the entire message will be output |
| 118 // on a single line with no line breaks. | 158 // on a single line with no line breaks. |
| 119 void SetSingleLineMode(bool single_line_mode) { | 159 void SetSingleLineMode(bool single_line_mode) { |
| 120 single_line_mode_ = single_line_mode; | 160 single_line_mode_ = single_line_mode; |
| 121 } | 161 } |
| 122 | 162 |
| 163 bool IsInSingleLineMode() { |
| 164 return single_line_mode_; |
| 165 } |
| 166 |
| 167 // If use_field_number is true, uses field number instead of field name. |
| 168 void SetUseFieldNumber(bool use_field_number) { |
| 169 use_field_number_ = use_field_number; |
| 170 } |
| 171 |
| 123 // Set true to print repeated primitives in a format like: | 172 // Set true to print repeated primitives in a format like: |
| 124 // field_name: [1, 2, 3, 4] | 173 // field_name: [1, 2, 3, 4] |
| 125 // instead of printing each value on its own line. Short format applies | 174 // instead of printing each value on its own line. Short format applies |
| 126 // only to primitive values -- i.e. everything except strings and | 175 // only to primitive values -- i.e. everything except strings and |
| 127 // sub-messages/groups. | 176 // sub-messages/groups. |
| 128 void SetUseShortRepeatedPrimitives(bool use_short_repeated_primitives) { | 177 void SetUseShortRepeatedPrimitives(bool use_short_repeated_primitives) { |
| 129 use_short_repeated_primitives_ = use_short_repeated_primitives; | 178 use_short_repeated_primitives_ = use_short_repeated_primitives; |
| 130 } | 179 } |
| 131 | 180 |
| 132 // Set true to output UTF-8 instead of ASCII. The only difference | 181 // Set true to output UTF-8 instead of ASCII. The only difference |
| 133 // is that bytes >= 0x80 in string fields will not be escaped, | 182 // is that bytes >= 0x80 in string fields will not be escaped, |
| 134 // because they are assumed to be part of UTF-8 multi-byte | 183 // because they are assumed to be part of UTF-8 multi-byte |
| 135 // sequences. | 184 // sequences. This will change the default FieldValuePrinter. |
| 136 void SetUseUtf8StringEscaping(bool as_utf8) { | 185 void SetUseUtf8StringEscaping(bool as_utf8); |
| 137 utf8_string_escaping_ = as_utf8; | 186 |
| 187 // Set the default FieldValuePrinter that is used for all fields that |
| 188 // don't have a field-specific printer registered. |
| 189 // Takes ownership of the printer. |
| 190 void SetDefaultFieldValuePrinter(const FieldValuePrinter* printer); |
| 191 |
| 192 // Sets whether we want to hide unknown fields or not. |
| 193 // Usually unknown fields are printed in a generic way that includes the |
| 194 // tag number of the field instead of field name. However, sometimes it |
| 195 // is useful to be able to print the message without unknown fields (e.g. |
| 196 // for the python protobuf version to maintain consistency between its pure |
| 197 // python and c++ implementations). |
| 198 void SetHideUnknownFields(bool hide) { |
| 199 hide_unknown_fields_ = hide; |
| 138 } | 200 } |
| 139 | 201 |
| 202 // If print_message_fields_in_index_order is true, print fields of a proto |
| 203 // message using the order defined in source code instead of the field |
| 204 // number. By default, use the field number order. |
| 205 void SetPrintMessageFieldsInIndexOrder( |
| 206 bool print_message_fields_in_index_order) { |
| 207 print_message_fields_in_index_order_ = |
| 208 print_message_fields_in_index_order; |
| 209 } |
| 210 |
| 211 // If expand==true, expand google.protobuf.Any payloads. The output |
| 212 // will be of form |
| 213 // [type_url] { <value_printed_in_text> } |
| 214 // |
| 215 // If expand==false, print Any using the default printer. The output will |
| 216 // look like |
| 217 // type_url: "<type_url>" value: "serialized_content" |
| 218 void SetExpandAny(bool expand) { |
| 219 expand_any_ = expand; |
| 220 } |
| 221 |
| 222 // If non-zero, we truncate all string fields that are longer than this |
| 223 // threshold. This is useful when the proto message has very long strings, |
| 224 // e.g., dump of encoded image file. |
| 225 // |
| 226 // NOTE(hfgong): Setting a non-zero value breaks round-trip safe |
| 227 // property of TextFormat::Printer. That is, from the printed message, we |
| 228 // cannot fully recover the original string field any more. |
| 229 void SetTruncateStringFieldLongerThan( |
| 230 const int64 truncate_string_field_longer_than) { |
| 231 truncate_string_field_longer_than_ = truncate_string_field_longer_than; |
| 232 } |
| 233 |
| 234 // Register a custom field-specific FieldValuePrinter for fields |
| 235 // with a particular FieldDescriptor. |
| 236 // Returns "true" if the registration succeeded, or "false", if there is |
| 237 // already a printer for that FieldDescriptor. |
| 238 // Takes ownership of the printer on successful registration. |
| 239 bool RegisterFieldValuePrinter(const FieldDescriptor* field, |
| 240 const FieldValuePrinter* printer); |
| 241 |
| 140 private: | 242 private: |
| 141 // Forward declaration of an internal class used to print the text | 243 // Forward declaration of an internal class used to print the text |
| 142 // output to the OutputStream (see text_format.cc for implementation). | 244 // output to the OutputStream (see text_format.cc for implementation). |
| 143 class TextGenerator; | 245 class TextGenerator; |
| 144 | 246 |
| 145 // Internal Print method, used for writing to the OutputStream via | 247 // Internal Print method, used for writing to the OutputStream via |
| 146 // the TextGenerator class. | 248 // the TextGenerator class. |
| 147 void Print(const Message& message, | 249 void Print(const Message& message, |
| 148 TextGenerator& generator) const; | 250 TextGenerator& generator) const; |
| 149 | 251 |
| (...skipping 23 matching lines...) Expand all Loading... |
| 173 const FieldDescriptor* field, | 275 const FieldDescriptor* field, |
| 174 int index, | 276 int index, |
| 175 TextGenerator& generator) const; | 277 TextGenerator& generator) const; |
| 176 | 278 |
| 177 // Print the fields in an UnknownFieldSet. They are printed by tag number | 279 // Print the fields in an UnknownFieldSet. They are printed by tag number |
| 178 // only. Embedded messages are heuristically identified by attempting to | 280 // only. Embedded messages are heuristically identified by attempting to |
| 179 // parse them. | 281 // parse them. |
| 180 void PrintUnknownFields(const UnknownFieldSet& unknown_fields, | 282 void PrintUnknownFields(const UnknownFieldSet& unknown_fields, |
| 181 TextGenerator& generator) const; | 283 TextGenerator& generator) const; |
| 182 | 284 |
| 285 bool PrintAny(const Message& message, TextGenerator& generator) const; |
| 286 |
| 183 int initial_indent_level_; | 287 int initial_indent_level_; |
| 184 | 288 |
| 185 bool single_line_mode_; | 289 bool single_line_mode_; |
| 186 | 290 |
| 291 bool use_field_number_; |
| 292 |
| 187 bool use_short_repeated_primitives_; | 293 bool use_short_repeated_primitives_; |
| 188 | 294 |
| 189 bool utf8_string_escaping_; | 295 bool hide_unknown_fields_; |
| 296 |
| 297 bool print_message_fields_in_index_order_; |
| 298 |
| 299 bool expand_any_; |
| 300 |
| 301 int64 truncate_string_field_longer_than_; |
| 302 |
| 303 google::protobuf::scoped_ptr<const FieldValuePrinter> default_field_value_pr
inter_; |
| 304 typedef map<const FieldDescriptor*, |
| 305 const FieldValuePrinter*> CustomPrinterMap; |
| 306 CustomPrinterMap custom_printers_; |
| 190 }; | 307 }; |
| 191 | 308 |
| 192 // Parses a text-format protocol message from the given input stream to | 309 // Parses a text-format protocol message from the given input stream to |
| 193 // the given message object. This function parses the format written | 310 // the given message object. This function parses the human-readable format |
| 194 // by Print(). | 311 // written by Print(). Returns true on success. The message is cleared first, |
| 312 // even if the function fails -- See Merge() to avoid this behavior. |
| 313 // |
| 314 // Example input: "user {\n id: 123 extra { gender: MALE language: 'en' }\n}" |
| 315 // |
| 316 // One use for this function is parsing handwritten strings in test code. |
| 317 // Another use is to parse the output from google::protobuf::Message::DebugStr
ing() |
| 318 // (or ShortDebugString()), because these functions output using |
| 319 // google::protobuf::TextFormat::Print(). |
| 320 // |
| 321 // If you would like to read a protocol buffer serialized in the |
| 322 // (non-human-readable) binary wire format, see |
| 323 // google::protobuf::MessageLite::ParseFromString(). |
| 195 static bool Parse(io::ZeroCopyInputStream* input, Message* output); | 324 static bool Parse(io::ZeroCopyInputStream* input, Message* output); |
| 196 // Like Parse(), but reads directly from a string. | 325 // Like Parse(), but reads directly from a string. |
| 197 static bool ParseFromString(const string& input, Message* output); | 326 static bool ParseFromString(const string& input, Message* output); |
| 198 | 327 |
| 199 // Like Parse(), but the data is merged into the given message, as if | 328 // Like Parse(), but the data is merged into the given message, as if |
| 200 // using Message::MergeFrom(). | 329 // using Message::MergeFrom(). |
| 201 static bool Merge(io::ZeroCopyInputStream* input, Message* output); | 330 static bool Merge(io::ZeroCopyInputStream* input, Message* output); |
| 202 // Like Merge(), but reads directly from a string. | 331 // Like Merge(), but reads directly from a string. |
| 203 static bool MergeFromString(const string& input, Message* output); | 332 static bool MergeFromString(const string& input, Message* output); |
| 204 | 333 |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 307 void WriteLocationsTo(ParseInfoTree* tree) { | 436 void WriteLocationsTo(ParseInfoTree* tree) { |
| 308 parse_info_tree_ = tree; | 437 parse_info_tree_ = tree; |
| 309 } | 438 } |
| 310 | 439 |
| 311 // Normally parsing fails if, after parsing, output->IsInitialized() | 440 // Normally parsing fails if, after parsing, output->IsInitialized() |
| 312 // returns false. Call AllowPartialMessage(true) to skip this check. | 441 // returns false. Call AllowPartialMessage(true) to skip this check. |
| 313 void AllowPartialMessage(bool allow) { | 442 void AllowPartialMessage(bool allow) { |
| 314 allow_partial_ = allow; | 443 allow_partial_ = allow; |
| 315 } | 444 } |
| 316 | 445 |
| 446 // Allow field names to be matched case-insensitively. |
| 447 // This is not advisable if there are fields that only differ in case, or |
| 448 // if you want to enforce writing in the canonical form. |
| 449 // This is 'false' by default. |
| 450 void AllowCaseInsensitiveField(bool allow) { |
| 451 allow_case_insensitive_field_ = allow; |
| 452 } |
| 453 |
| 317 // Like TextFormat::ParseFieldValueFromString | 454 // Like TextFormat::ParseFieldValueFromString |
| 318 bool ParseFieldValueFromString(const string& input, | 455 bool ParseFieldValueFromString(const string& input, |
| 319 const FieldDescriptor* field, | 456 const FieldDescriptor* field, |
| 320 Message* output); | 457 Message* output); |
| 321 | 458 |
| 322 | 459 |
| 460 void AllowFieldNumber(bool allow) { |
| 461 allow_field_number_ = allow; |
| 462 } |
| 463 |
| 323 private: | 464 private: |
| 324 // Forward declaration of an internal class used to parse text | 465 // Forward declaration of an internal class used to parse text |
| 325 // representations (see text_format.cc for implementation). | 466 // representations (see text_format.cc for implementation). |
| 326 class ParserImpl; | 467 class ParserImpl; |
| 327 | 468 |
| 328 // Like TextFormat::Merge(). The provided implementation is used | 469 // Like TextFormat::Merge(). The provided implementation is used |
| 329 // to do the parsing. | 470 // to do the parsing. |
| 330 bool MergeUsingImpl(io::ZeroCopyInputStream* input, | 471 bool MergeUsingImpl(io::ZeroCopyInputStream* input, |
| 331 Message* output, | 472 Message* output, |
| 332 ParserImpl* parser_impl); | 473 ParserImpl* parser_impl); |
| 333 | 474 |
| 334 io::ErrorCollector* error_collector_; | 475 io::ErrorCollector* error_collector_; |
| 335 Finder* finder_; | 476 Finder* finder_; |
| 336 ParseInfoTree* parse_info_tree_; | 477 ParseInfoTree* parse_info_tree_; |
| 337 bool allow_partial_; | 478 bool allow_partial_; |
| 479 bool allow_case_insensitive_field_; |
| 338 bool allow_unknown_field_; | 480 bool allow_unknown_field_; |
| 481 bool allow_unknown_enum_; |
| 482 bool allow_field_number_; |
| 483 bool allow_relaxed_whitespace_; |
| 484 bool allow_singular_overwrites_; |
| 339 }; | 485 }; |
| 340 | 486 |
| 487 |
| 341 private: | 488 private: |
| 342 // Hack: ParseInfoTree declares TextFormat as a friend which should extend | 489 // Hack: ParseInfoTree declares TextFormat as a friend which should extend |
| 343 // the friendship to TextFormat::Parser::ParserImpl, but unfortunately some | 490 // the friendship to TextFormat::Parser::ParserImpl, but unfortunately some |
| 344 // old compilers (e.g. GCC 3.4.6) don't implement this correctly. We provide | 491 // old compilers (e.g. GCC 3.4.6) don't implement this correctly. We provide |
| 345 // helpers for ParserImpl to call methods of ParseInfoTree. | 492 // helpers for ParserImpl to call methods of ParseInfoTree. |
| 346 static inline void RecordLocation(ParseInfoTree* info_tree, | 493 static inline void RecordLocation(ParseInfoTree* info_tree, |
| 347 const FieldDescriptor* field, | 494 const FieldDescriptor* field, |
| 348 ParseLocation location); | 495 ParseLocation location); |
| 349 static inline ParseInfoTree* CreateNested(ParseInfoTree* info_tree, | 496 static inline ParseInfoTree* CreateNested(ParseInfoTree* info_tree, |
| 350 const FieldDescriptor* field); | 497 const FieldDescriptor* field); |
| 351 | 498 |
| 352 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(TextFormat); | 499 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(TextFormat); |
| 353 }; | 500 }; |
| 354 | 501 |
| 355 inline void TextFormat::RecordLocation(ParseInfoTree* info_tree, | 502 inline void TextFormat::RecordLocation(ParseInfoTree* info_tree, |
| 356 const FieldDescriptor* field, | 503 const FieldDescriptor* field, |
| 357 ParseLocation location) { | 504 ParseLocation location) { |
| 358 info_tree->RecordLocation(field, location); | 505 info_tree->RecordLocation(field, location); |
| 359 } | 506 } |
| 360 | 507 |
| 508 |
| 361 inline TextFormat::ParseInfoTree* TextFormat::CreateNested( | 509 inline TextFormat::ParseInfoTree* TextFormat::CreateNested( |
| 362 ParseInfoTree* info_tree, const FieldDescriptor* field) { | 510 ParseInfoTree* info_tree, const FieldDescriptor* field) { |
| 363 return info_tree->CreateNested(field); | 511 return info_tree->CreateNested(field); |
| 364 } | 512 } |
| 365 | 513 |
| 366 } // namespace protobuf | 514 } // namespace protobuf |
| 367 | 515 |
| 368 } // namespace google | 516 } // namespace google |
| 369 #endif // GOOGLE_PROTOBUF_TEXT_FORMAT_H__ | 517 #endif // GOOGLE_PROTOBUF_TEXT_FORMAT_H__ |
| OLD | NEW |