~statik/ubuntu/maverick/protobuf/A

« back to all changes in this revision

Viewing changes to src/google/protobuf/compiler/java/java_helpers.cc

  • Committer: Bazaar Package Importer
  • Author(s): Steve Kowalik
  • Date: 2010-02-11 11:13:19 UTC
  • mfrom: (2.2.2 squeeze)
  • Revision ID: james.westby@ubuntu.com-20100211111319-zdn8hmw0gh8s4cf8
Tags: 2.2.0a-0.1ubuntu1
* Merge from Debian testing.
* Remaining Ubuntu changes:
  - Don't use python2.4.
* Ubuntu changes dropped:
  - Disable death tests on Itanium, fixed upstream.

Show diffs side-by-side

added added

removed removed

Lines of Context:
37
37
#include <google/protobuf/compiler/java/java_helpers.h>
38
38
#include <google/protobuf/descriptor.pb.h>
39
39
#include <google/protobuf/stubs/strutil.h>
 
40
#include <google/protobuf/stubs/substitute.h>
40
41
 
41
42
namespace google {
42
43
namespace protobuf {
243
244
  return NULL;
244
245
}
245
246
 
 
247
bool AllAscii(const string& text) {
 
248
  for (int i = 0; i < text.size(); i++) {
 
249
    if ((text[i] & 0x80) != 0) {
 
250
      return false;
 
251
    }
 
252
  }
 
253
  return true;
 
254
}
 
255
 
 
256
string DefaultValue(const FieldDescriptor* field) {
 
257
  // Switch on cpp_type since we need to know which default_value_* method
 
258
  // of FieldDescriptor to call.
 
259
  switch (field->cpp_type()) {
 
260
    case FieldDescriptor::CPPTYPE_INT32:
 
261
      return SimpleItoa(field->default_value_int32());
 
262
    case FieldDescriptor::CPPTYPE_UINT32:
 
263
      // Need to print as a signed int since Java has no unsigned.
 
264
      return SimpleItoa(static_cast<int32>(field->default_value_uint32()));
 
265
    case FieldDescriptor::CPPTYPE_INT64:
 
266
      return SimpleItoa(field->default_value_int64()) + "L";
 
267
    case FieldDescriptor::CPPTYPE_UINT64:
 
268
      return SimpleItoa(static_cast<int64>(field->default_value_uint64())) +
 
269
             "L";
 
270
    case FieldDescriptor::CPPTYPE_DOUBLE:
 
271
      return SimpleDtoa(field->default_value_double()) + "D";
 
272
    case FieldDescriptor::CPPTYPE_FLOAT:
 
273
      return SimpleFtoa(field->default_value_float()) + "F";
 
274
    case FieldDescriptor::CPPTYPE_BOOL:
 
275
      return field->default_value_bool() ? "true" : "false";
 
276
    case FieldDescriptor::CPPTYPE_STRING:
 
277
      if (field->type() == FieldDescriptor::TYPE_BYTES) {
 
278
        if (field->has_default_value()) {
 
279
          // See comments in Internal.java for gory details.
 
280
          return strings::Substitute(
 
281
            "com.google.protobuf.Internal.bytesDefaultValue(\"$0\")",
 
282
            CEscape(field->default_value_string()));
 
283
        } else {
 
284
          return "com.google.protobuf.ByteString.EMPTY";
 
285
        }
 
286
      } else {
 
287
        if (AllAscii(field->default_value_string())) {
 
288
          // All chars are ASCII.  In this case CEscape() works fine.
 
289
          return "\"" + CEscape(field->default_value_string()) + "\"";
 
290
        } else {
 
291
          // See comments in Internal.java for gory details.
 
292
          return strings::Substitute(
 
293
            "com.google.protobuf.Internal.stringDefaultValue(\"$0\")",
 
294
            CEscape(field->default_value_string()));
 
295
        }
 
296
      }
 
297
 
 
298
    case FieldDescriptor::CPPTYPE_ENUM:
 
299
      return ClassName(field->enum_type()) + "." +
 
300
             field->default_value_enum()->name();
 
301
 
 
302
    case FieldDescriptor::CPPTYPE_MESSAGE:
 
303
      return ClassName(field->message_type()) + ".getDefaultInstance()";
 
304
 
 
305
    // No default because we want the compiler to complain if any new
 
306
    // types are added.
 
307
  }
 
308
 
 
309
  GOOGLE_LOG(FATAL) << "Can't get here.";
 
310
  return "";
 
311
}
 
312
 
246
313
}  // namespace java
247
314
}  // namespace compiler
248
315
}  // namespace protobuf