| 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 // http://code.google.com/p/protobuf/ |
| 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. |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 83 string* output); | 83 string* output); |
| 84 | 84 |
| 85 // Class for those users which require more fine-grained control over how | 85 // Class for those users which require more fine-grained control over how |
| 86 // a protobuffer message is printed out. | 86 // a protobuffer message is printed out. |
| 87 class LIBPROTOBUF_EXPORT Printer { | 87 class LIBPROTOBUF_EXPORT Printer { |
| 88 public: | 88 public: |
| 89 Printer(); | 89 Printer(); |
| 90 ~Printer(); | 90 ~Printer(); |
| 91 | 91 |
| 92 // Like TextFormat::Print | 92 // Like TextFormat::Print |
| 93 bool Print(const Message& message, io::ZeroCopyOutputStream* output); | 93 bool Print(const Message& message, io::ZeroCopyOutputStream* output) const; |
| 94 // Like TextFormat::PrintUnknownFields | 94 // Like TextFormat::PrintUnknownFields |
| 95 bool PrintUnknownFields(const UnknownFieldSet& unknown_fields, | 95 bool PrintUnknownFields(const UnknownFieldSet& unknown_fields, |
| 96 io::ZeroCopyOutputStream* output); | 96 io::ZeroCopyOutputStream* output) const; |
| 97 // Like TextFormat::PrintToString | 97 // Like TextFormat::PrintToString |
| 98 bool PrintToString(const Message& message, string* output); | 98 bool PrintToString(const Message& message, string* output) const; |
| 99 // Like TextFormat::PrintUnknownFieldsToString | 99 // Like TextFormat::PrintUnknownFieldsToString |
| 100 bool PrintUnknownFieldsToString(const UnknownFieldSet& unknown_fields, | 100 bool PrintUnknownFieldsToString(const UnknownFieldSet& unknown_fields, |
| 101 string* output); | 101 string* output) const; |
| 102 // Like TextFormat::PrintFieldValueToString | 102 // Like TextFormat::PrintFieldValueToString |
| 103 void PrintFieldValueToString(const Message& message, | 103 void PrintFieldValueToString(const Message& message, |
| 104 const FieldDescriptor* field, | 104 const FieldDescriptor* field, |
| 105 int index, | 105 int index, |
| 106 string* output); | 106 string* output) const; |
| 107 | 107 |
| 108 // Adjust the initial indent level of all output. Each indent level is | 108 // Adjust the initial indent level of all output. Each indent level is |
| 109 // equal to two spaces. | 109 // equal to two spaces. |
| 110 void SetInitialIndentLevel(int indent_level) { | 110 void SetInitialIndentLevel(int indent_level) { |
| 111 initial_indent_level_ = indent_level; | 111 initial_indent_level_ = indent_level; |
| 112 } | 112 } |
| 113 | 113 |
| 114 // If printing in single line mode, then the entire message will be output | 114 // If printing in single line mode, then the entire message will be output |
| 115 // on a single line with no line breaks. | 115 // on a single line with no line breaks. |
| 116 void SetSingleLineMode(bool single_line_mode) { | 116 void SetSingleLineMode(bool single_line_mode) { |
| 117 single_line_mode_ = single_line_mode; | 117 single_line_mode_ = single_line_mode; |
| 118 } | 118 } |
| 119 | 119 |
| 120 // Set true to print repeated primitives in a format like: | 120 // Set true to print repeated primitives in a format like: |
| 121 // field_name: [1, 2, 3, 4] | 121 // field_name: [1, 2, 3, 4] |
| 122 // instead of printing each value on its own line. Short format applies | 122 // instead of printing each value on its own line. Short format applies |
| 123 // only to primitive values -- i.e. everything except strings and | 123 // only to primitive values -- i.e. everything except strings and |
| 124 // sub-messages/groups. Note that at present this format is not recognized | 124 // sub-messages/groups. |
| 125 // by the parser. | |
| 126 void SetUseShortRepeatedPrimitives(bool use_short_repeated_primitives) { | 125 void SetUseShortRepeatedPrimitives(bool use_short_repeated_primitives) { |
| 127 use_short_repeated_primitives_ = use_short_repeated_primitives; | 126 use_short_repeated_primitives_ = use_short_repeated_primitives; |
| 128 } | 127 } |
| 129 | 128 |
| 130 // Set true to output UTF-8 instead of ASCII. The only difference | 129 // Set true to output UTF-8 instead of ASCII. The only difference |
| 131 // is that bytes >= 0x80 in string fields will not be escaped, | 130 // is that bytes >= 0x80 in string fields will not be escaped, |
| 132 // because they are assumed to be part of UTF-8 multi-byte | 131 // because they are assumed to be part of UTF-8 multi-byte |
| 133 // sequences. | 132 // sequences. |
| 134 void SetUseUtf8StringEscaping(bool as_utf8) { | 133 void SetUseUtf8StringEscaping(bool as_utf8) { |
| 135 utf8_string_escaping_ = as_utf8; | 134 utf8_string_escaping_ = as_utf8; |
| 136 } | 135 } |
| 137 | 136 |
| 138 private: | 137 private: |
| 139 // Forward declaration of an internal class used to print the text | 138 // Forward declaration of an internal class used to print the text |
| 140 // output to the OutputStream (see text_format.cc for implementation). | 139 // output to the OutputStream (see text_format.cc for implementation). |
| 141 class TextGenerator; | 140 class TextGenerator; |
| 142 | 141 |
| 143 // Internal Print method, used for writing to the OutputStream via | 142 // Internal Print method, used for writing to the OutputStream via |
| 144 // the TextGenerator class. | 143 // the TextGenerator class. |
| 145 void Print(const Message& message, | 144 void Print(const Message& message, |
| 146 TextGenerator& generator); | 145 TextGenerator& generator) const; |
| 147 | 146 |
| 148 // Print a single field. | 147 // Print a single field. |
| 149 void PrintField(const Message& message, | 148 void PrintField(const Message& message, |
| 150 const Reflection* reflection, | 149 const Reflection* reflection, |
| 151 const FieldDescriptor* field, | 150 const FieldDescriptor* field, |
| 152 TextGenerator& generator); | 151 TextGenerator& generator) const; |
| 153 | 152 |
| 154 // Print a repeated primitive field in short form. | 153 // Print a repeated primitive field in short form. |
| 155 void PrintShortRepeatedField(const Message& message, | 154 void PrintShortRepeatedField(const Message& message, |
| 156 const Reflection* reflection, | 155 const Reflection* reflection, |
| 157 const FieldDescriptor* field, | 156 const FieldDescriptor* field, |
| 158 TextGenerator& generator); | 157 TextGenerator& generator) const; |
| 159 | 158 |
| 160 // Print the name of a field -- i.e. everything that comes before the | 159 // Print the name of a field -- i.e. everything that comes before the |
| 161 // ':' for a single name/value pair. | 160 // ':' for a single name/value pair. |
| 162 void PrintFieldName(const Message& message, | 161 void PrintFieldName(const Message& message, |
| 163 const Reflection* reflection, | 162 const Reflection* reflection, |
| 164 const FieldDescriptor* field, | 163 const FieldDescriptor* field, |
| 165 TextGenerator& generator); | 164 TextGenerator& generator) const; |
| 166 | 165 |
| 167 // Outputs a textual representation of the value of the field supplied on | 166 // Outputs a textual representation of the value of the field supplied on |
| 168 // the message supplied or the default value if not set. | 167 // the message supplied or the default value if not set. |
| 169 void PrintFieldValue(const Message& message, | 168 void PrintFieldValue(const Message& message, |
| 170 const Reflection* reflection, | 169 const Reflection* reflection, |
| 171 const FieldDescriptor* field, | 170 const FieldDescriptor* field, |
| 172 int index, | 171 int index, |
| 173 TextGenerator& generator); | 172 TextGenerator& generator) const; |
| 174 | 173 |
| 175 // Print the fields in an UnknownFieldSet. They are printed by tag number | 174 // Print the fields in an UnknownFieldSet. They are printed by tag number |
| 176 // only. Embedded messages are heuristically identified by attempting to | 175 // only. Embedded messages are heuristically identified by attempting to |
| 177 // parse them. | 176 // parse them. |
| 178 void PrintUnknownFields(const UnknownFieldSet& unknown_fields, | 177 void PrintUnknownFields(const UnknownFieldSet& unknown_fields, |
| 179 TextGenerator& generator); | 178 TextGenerator& generator) const; |
| 180 | 179 |
| 181 int initial_indent_level_; | 180 int initial_indent_level_; |
| 182 | 181 |
| 183 bool single_line_mode_; | 182 bool single_line_mode_; |
| 184 | 183 |
| 185 bool use_short_repeated_primitives_; | 184 bool use_short_repeated_primitives_; |
| 186 | 185 |
| 187 bool utf8_string_escaping_; | 186 bool utf8_string_escaping_; |
| 188 }; | 187 }; |
| 189 | 188 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 200 // Like Merge(), but reads directly from a string. | 199 // Like Merge(), but reads directly from a string. |
| 201 static bool MergeFromString(const string& input, Message* output); | 200 static bool MergeFromString(const string& input, Message* output); |
| 202 | 201 |
| 203 // Parse the given text as a single field value and store it into the | 202 // Parse the given text as a single field value and store it into the |
| 204 // given field of the given message. If the field is a repeated field, | 203 // given field of the given message. If the field is a repeated field, |
| 205 // the new value will be added to the end | 204 // the new value will be added to the end |
| 206 static bool ParseFieldValueFromString(const string& input, | 205 static bool ParseFieldValueFromString(const string& input, |
| 207 const FieldDescriptor* field, | 206 const FieldDescriptor* field, |
| 208 Message* message); | 207 Message* message); |
| 209 | 208 |
| 209 // Interface that TextFormat::Parser can use to find extensions. |
| 210 // This class may be extended in the future to find more information |
| 211 // like fields, etc. |
| 212 class LIBPROTOBUF_EXPORT Finder { |
| 213 public: |
| 214 virtual ~Finder(); |
| 215 |
| 216 // Try to find an extension of *message by fully-qualified field |
| 217 // name. Returns NULL if no extension is known for this name or number. |
| 218 virtual const FieldDescriptor* FindExtension( |
| 219 Message* message, |
| 220 const string& name) const = 0; |
| 221 }; |
| 222 |
| 210 // For more control over parsing, use this class. | 223 // For more control over parsing, use this class. |
| 211 class LIBPROTOBUF_EXPORT Parser { | 224 class LIBPROTOBUF_EXPORT Parser { |
| 212 public: | 225 public: |
| 213 Parser(); | 226 Parser(); |
| 214 ~Parser(); | 227 ~Parser(); |
| 215 | 228 |
| 216 // Like TextFormat::Parse(). | 229 // Like TextFormat::Parse(). |
| 217 bool Parse(io::ZeroCopyInputStream* input, Message* output); | 230 bool Parse(io::ZeroCopyInputStream* input, Message* output); |
| 218 // Like TextFormat::ParseFromString(). | 231 // Like TextFormat::ParseFromString(). |
| 219 bool ParseFromString(const string& input, Message* output); | 232 bool ParseFromString(const string& input, Message* output); |
| 220 // Like TextFormat::Merge(). | 233 // Like TextFormat::Merge(). |
| 221 bool Merge(io::ZeroCopyInputStream* input, Message* output); | 234 bool Merge(io::ZeroCopyInputStream* input, Message* output); |
| 222 // Like TextFormat::MergeFromString(). | 235 // Like TextFormat::MergeFromString(). |
| 223 bool MergeFromString(const string& input, Message* output); | 236 bool MergeFromString(const string& input, Message* output); |
| 224 | 237 |
| 225 // Set where to report parse errors. If NULL (the default), errors will | 238 // Set where to report parse errors. If NULL (the default), errors will |
| 226 // be printed to stderr. | 239 // be printed to stderr. |
| 227 void RecordErrorsTo(io::ErrorCollector* error_collector) { | 240 void RecordErrorsTo(io::ErrorCollector* error_collector) { |
| 228 error_collector_ = error_collector; | 241 error_collector_ = error_collector; |
| 229 } | 242 } |
| 230 | 243 |
| 244 // Set how parser finds extensions. If NULL (the default), the |
| 245 // parser will use the standard Reflection object associated with |
| 246 // the message being parsed. |
| 247 void SetFinder(Finder* finder) { |
| 248 finder_ = finder; |
| 249 } |
| 250 |
| 231 // Normally parsing fails if, after parsing, output->IsInitialized() | 251 // Normally parsing fails if, after parsing, output->IsInitialized() |
| 232 // returns false. Call AllowPartialMessage(true) to skip this check. | 252 // returns false. Call AllowPartialMessage(true) to skip this check. |
| 233 void AllowPartialMessage(bool allow) { | 253 void AllowPartialMessage(bool allow) { |
| 234 allow_partial_ = allow; | 254 allow_partial_ = allow; |
| 235 } | 255 } |
| 236 | 256 |
| 237 // Like TextFormat::ParseFieldValueFromString | 257 // Like TextFormat::ParseFieldValueFromString |
| 238 bool ParseFieldValueFromString(const string& input, | 258 bool ParseFieldValueFromString(const string& input, |
| 239 const FieldDescriptor* field, | 259 const FieldDescriptor* field, |
| 240 Message* output); | 260 Message* output); |
| 241 | 261 |
| 242 private: | 262 private: |
| 243 // Forward declaration of an internal class used to parse text | 263 // Forward declaration of an internal class used to parse text |
| 244 // representations (see text_format.cc for implementation). | 264 // representations (see text_format.cc for implementation). |
| 245 class ParserImpl; | 265 class ParserImpl; |
| 246 | 266 |
| 247 // Like TextFormat::Merge(). The provided implementation is used | 267 // Like TextFormat::Merge(). The provided implementation is used |
| 248 // to do the parsing. | 268 // to do the parsing. |
| 249 bool MergeUsingImpl(io::ZeroCopyInputStream* input, | 269 bool MergeUsingImpl(io::ZeroCopyInputStream* input, |
| 250 Message* output, | 270 Message* output, |
| 251 ParserImpl* parser_impl); | 271 ParserImpl* parser_impl); |
| 252 | 272 |
| 253 io::ErrorCollector* error_collector_; | 273 io::ErrorCollector* error_collector_; |
| 274 Finder* finder_; |
| 254 bool allow_partial_; | 275 bool allow_partial_; |
| 255 }; | 276 }; |
| 256 | 277 |
| 257 private: | 278 private: |
| 258 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(TextFormat); | 279 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(TextFormat); |
| 259 }; | 280 }; |
| 260 | 281 |
| 261 } // namespace protobuf | 282 } // namespace protobuf |
| 262 | 283 |
| 263 } // namespace google | 284 } // namespace google |
| 264 #endif // GOOGLE_PROTOBUF_TEXT_FORMAT_H__ | 285 #endif // GOOGLE_PROTOBUF_TEXT_FORMAT_H__ |
| OLD | NEW |