Java 类com.google.protobuf.ExtensionRegistry 实例源码

项目:jigsaw-payment    文件:JsonJacksonFormat.java   
/**
 * Parse a text-format message from {@code input} and merge the contents into {@code builder}.
 * Extensions will be recognized if they are registered in {@code extensionRegistry}.
 * @throws IOException
 */
public void merge(JsonParser parser,
                         ExtensionRegistry extensionRegistry,
                         Message.Builder builder) throws IOException {

    JsonToken token = parser.nextToken();
    if (token.equals(JsonToken.START_OBJECT)) {
        token = parser.nextToken();
    }
    while (token != null && !token.equals(JsonToken.END_OBJECT)) {
        mergeField(parser, extensionRegistry, builder);
        token = parser.nextToken();
    }

    // Test to make sure the tokenizer has reached the end of the stream.
    if (parser.nextToken() != null) {
        throw new RuntimeException("Expecting the end of the stream, but there seems to be more data!  Check the input for a valid JSON format.");
    }
}
项目:jigsaw-payment    文件:JsonJacksonFormat.java   
private void handleMissingField(String fieldName, JsonParser parser,
                                       ExtensionRegistry extensionRegistry,
                                       UnknownFieldSet.Builder builder) throws IOException {

    JsonToken token = parser.nextToken();
    if (token.equals(JsonToken.START_OBJECT)) {
        // Message structure
        token = parser.nextToken(); // skip name
        while (token != null && !token.equals(JsonToken.END_OBJECT)) {
            handleMissingField(fieldName, parser, extensionRegistry, builder);
            token = parser.nextToken(); // get } or field name
        }
    } else if (token.equals(JsonToken.START_ARRAY)) {
        // Collection
        do {
            handleMissingField(fieldName, parser, extensionRegistry, builder);
            token = parser.getCurrentToken(); // got value or ]
        } while (token != null && !token.equals(JsonToken.END_ARRAY));
    } else {
        // Primitive value
        // NULL, INT, BOOL, STRING
        // nothing to do..
    }
}
项目:jigsaw-payment    文件:JsonJacksonFormat.java   
private void handleValue(JsonParser parser,
                                ExtensionRegistry extensionRegistry,
                                Message.Builder builder,
                                FieldDescriptor field,
                                ExtensionRegistry.ExtensionInfo extension,
                                boolean unknown) throws IOException {

    Object value = null;
    if (field.getJavaType() == FieldDescriptor.JavaType.MESSAGE) {
        value = handleObject(parser, extensionRegistry, builder, field, extension, unknown);
    } else {
        value = handlePrimitive(parser, field);
    }
    if (value != null) {
        if (field.isRepeated()) {
            builder.addRepeatedField(field, value);
        } else {
            builder.setField(field, value);
        }
    }
}
项目:jigsaw-payment    文件:JsonFormat.java   
/**
 * Parse a text-format message from {@code input} and merge the contents into {@code builder}.
 * Extensions will be recognized if they are registered in {@code extensionRegistry}.
 */
public void merge(CharSequence input,
                         ExtensionRegistry extensionRegistry,
                         Message.Builder builder) throws ParseException {
    Tokenizer tokenizer = new Tokenizer(input);

    // Based on the state machine @ http://json.org/

    tokenizer.consume("{"); // Needs to happen when the object starts.
    while (!tokenizer.tryConsume("}")) { // Continue till the object is done
        mergeField(tokenizer, extensionRegistry, builder);
    }
    // Test to make sure the tokenizer has reached the end of the stream.
    if (!tokenizer.atEnd()) {
        throw tokenizer.parseException("Expecting the end of the stream, but there seems to be more data!  Check the input for a valid JSON format.");
    }
}
项目:jigsaw-payment    文件:JsonFormat.java   
private void handleValue(Tokenizer tokenizer,
                                ExtensionRegistry extensionRegistry,
                                Message.Builder builder,
                                FieldDescriptor field,
                                ExtensionRegistry.ExtensionInfo extension,
                                boolean unknown) throws ParseException {

    Object value = null;
    if (field.getJavaType() == FieldDescriptor.JavaType.MESSAGE) {
        value = handleObject(tokenizer, extensionRegistry, builder, field, extension, unknown);
    } else {
        value = handlePrimitive(tokenizer, field);
    }
    if (value != null) {
        if (field.isRepeated()) {
            builder.addRepeatedField(field, value);
        } else {
            builder.setField(field, value);
        }
    }
}
项目:beam    文件:ProtoCoder.java   
/**
 * Returns a {@link ProtoCoder} like this one, but with the extensions from the given classes
 * registered.
 *
 * <p>Each of the extension host classes must be an class automatically generated by the
 * Protocol Buffers compiler, {@code protoc}, that contains messages.
 *
 * <p>Does not modify this object.
 */
public ProtoCoder<T> withExtensionsFrom(Iterable<Class<?>> moreExtensionHosts) {
  for (Class<?> extensionHost : moreExtensionHosts) {
    // Attempt to access the required method, to make sure it's present.
    try {
      Method registerAllExtensions =
          extensionHost.getDeclaredMethod("registerAllExtensions", ExtensionRegistry.class);
      checkArgument(
          Modifier.isStatic(registerAllExtensions.getModifiers()),
          "Method registerAllExtensions() must be static");
    } catch (NoSuchMethodException | SecurityException e) {
      throw new IllegalArgumentException(
          String.format("Unable to register extensions for %s", extensionHost.getCanonicalName()),
          e);
    }
  }

  return new ProtoCoder<>(
      protoMessageClass,
      new ImmutableSet.Builder<Class<?>>()
          .addAll(extensionHostClasses)
          .addAll(moreExtensionHosts)
          .build());
}
项目:beam    文件:ProtoCoder.java   
/**
 * Returns the {@link ExtensionRegistry} listing all known Protocol Buffers extension messages
 * to {@code T} registered with this {@link ProtoCoder}.
 */
public ExtensionRegistry getExtensionRegistry() {
  if (memoizedExtensionRegistry == null) {
    ExtensionRegistry registry = ExtensionRegistry.newInstance();
    for (Class<?> extensionHost : extensionHostClasses) {
      try {
        extensionHost
            .getDeclaredMethod("registerAllExtensions", ExtensionRegistry.class)
            .invoke(null, registry);
      } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
        throw new IllegalStateException(e);
      }
    }
    memoizedExtensionRegistry = registry.getUnmodifiable();
  }
  return memoizedExtensionRegistry;
}
项目:beam    文件:ProtobufUtil.java   
/**
 * Recursively checks whether the specified class uses any Protocol Buffers fields that cannot
 * be deterministically encoded.
 *
 * @throws NonDeterministicException if the object cannot be encoded deterministically.
 */
static void verifyDeterministic(ProtoCoder<?> coder) throws NonDeterministicException {
  Class<? extends Message> message = coder.getMessageType();
  ExtensionRegistry registry = coder.getExtensionRegistry();
  Set<Descriptor> descriptors = getRecursiveDescriptorsForClass(message, registry);
  for (Descriptor d : descriptors) {
    for (FieldDescriptor fd : d.getFields()) {
      // If there is a transitively reachable Protocol Buffers map field, then this object cannot
      // be encoded deterministically.
      if (fd.isMapField()) {
        String reason =
            String.format(
                "Protocol Buffers message %s transitively includes Map field %s (from file %s)."
                    + " Maps cannot be deterministically encoded.",
                message.getName(),
                fd.getFullName(),
                fd.getFile().getFullName());
        throw new NonDeterministicException(coder, reason);
      }
    }
  }
}
项目:protoc-gen-as3    文件:Main.java   
public static void main(String[] args) throws IOException {
    ExtensionRegistry registry = ExtensionRegistry.newInstance();
    Options.registerAllExtensions(registry);
    PluginProtos.CodeGeneratorRequest request = PluginProtos.CodeGeneratorRequest.
            parseFrom(System.in, registry);
    PluginProtos.CodeGeneratorResponse response;
    try {
        Scope<Object> root = buildScopeTree(request);
        PluginProtos.CodeGeneratorResponse.Builder responseBuilder =
                PluginProtos.CodeGeneratorResponse.newBuilder();
        writeFiles(root, responseBuilder);
        response = responseBuilder.build();
    } catch (Exception e) {
        // 出错,报告给 protoc ,然后退出
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);
        e.printStackTrace(pw);
        pw.flush();
        PluginProtos.CodeGeneratorResponse.newBuilder().setError(sw.toString()).
                build().writeTo(System.out);
        System.out.flush();
        return;
    }
    response.writeTo(System.out);
    System.out.flush();
}
项目:jme3_xbuf    文件:Xbuf.java   
/**
 * A full constructor that allow to define every service (to injection).
 * @param assetManager the AssetManager used to load assets (texture, sound,...)
 * @param registry the protobuf registry for extensions
 * @param loader4Materials the xbuf way to load materials (null == use default implementation)
 * @param loader4Relations the xbuf way to load relations (null == use default implementation)
 */
public Xbuf(AssetManager assetManager, ExtensionRegistry registry, MaterialsMerger loader4Materials, RelationsMerger loader4Relations){
    loader4Materials = (loader4Materials != null) ?loader4Materials : new MaterialsMerger(assetManager);
    loader4Relations = (loader4Relations != null) ?loader4Relations : new RelationsMerger(loader4Materials);
    mergers=new LinkedList<Merger>();
    mergers.add(new NodesMerger());
    mergers.add(new MeshesMerger(loader4Materials));
    mergers.add(loader4Materials);
    mergers.add(new LightsMerger());
    mergers.add(new SkeletonsMerger());
    mergers.add(new AnimationsMerger());
    mergers.add(new CustomParamsMerger());
    mergers.add(new PhysicsMerger());

    // relations should be the last because it reuse data provide by other (put in components)
    mergers.add(loader4Relations);

    this.registry=registry!=null?registry:ExtensionRegistry.newInstance();
    setupExtensionRegistry(this.registry);
}
项目:HIndex    文件:SpatialPartition.java   
public static ValuePartition parseFrom(final byte[] bytes) throws DeserializationException {
  ValuePartitionProtos.ValuePartition valuePartition = null;
  try {
    ExtensionRegistry registry = ExtensionRegistry.newInstance();
    registry
        .add(org.apache.hadoop.hbase.index.protobuf.generated.ValuePartitionProtos.SpatialPartition.offset);
    registry
        .add(org.apache.hadoop.hbase.index.protobuf.generated.ValuePartitionProtos.SpatialPartition.length);
    valuePartition =
        ValuePartitionProtos.ValuePartition.newBuilder().mergeFrom(bytes, registry).build();
  } catch (InvalidProtocolBufferException e) {
    throw new DeserializationException(e);
  }
  return new SpatialPartition(
      valuePartition
          .getExtension(org.apache.hadoop.hbase.index.protobuf.generated.ValuePartitionProtos.SpatialPartition.offset),
      valuePartition
          .getExtension(org.apache.hadoop.hbase.index.protobuf.generated.ValuePartitionProtos.SpatialPartition.length));
}
项目:HIndex    文件:SeparatorPartition.java   
public static ValuePartition parseFrom(final byte[] bytes) throws DeserializationException {
  ValuePartitionProtos.ValuePartition valuePartition;
  try {
    ExtensionRegistry registry = ExtensionRegistry.newInstance();
    registry
        .add(org.apache.hadoop.hbase.index.protobuf.generated.ValuePartitionProtos.SeparatorPartition.separator);
    registry
        .add(org.apache.hadoop.hbase.index.protobuf.generated.ValuePartitionProtos.SeparatorPartition.position);
    valuePartition = ValuePartitionProtos.ValuePartition.parseFrom(bytes, registry);
  } catch (InvalidProtocolBufferException e) {
    throw new DeserializationException(e);
  }
  return new SeparatorPartition(
      valuePartition
          .getExtension(
            org.apache.hadoop.hbase.index.protobuf.generated.ValuePartitionProtos.SeparatorPartition.separator)
          .toByteArray(),
      valuePartition
          .getExtension(org.apache.hadoop.hbase.index.protobuf.generated.ValuePartitionProtos.SeparatorPartition.position));
}
项目:datacollector    文件:ProtobufDataParser.java   
public ProtobufDataParser(
    ProtoConfigurableEntity.Context context,
    String messageId,
    Descriptors.Descriptor descriptor,
    Map<String, Set<Descriptors.FieldDescriptor>> messageTypeToExtensionMap,
    ExtensionRegistry extensionRegistry,
    InputStream inputStream,
    String readerOffset,
    int maxObjectLength,
    boolean isDelimited
) throws IOException, Descriptors.DescriptorValidationException, DataParserException {
  this.context = context;
  this.inputStream = new OverrunInputStream(inputStream, maxObjectLength, true);
  this.messageId = messageId;
  this.messageTypeToExtensionMap = messageTypeToExtensionMap;
  this.extensionRegistry = extensionRegistry;
  this.descriptor = descriptor;
  this.builder = DynamicMessage.newBuilder(descriptor);
  this.isDelimited = isDelimited;

  // skip to the required location
  if (readerOffset != null && !readerOffset.isEmpty() && !readerOffset.equals("0")) {
    int offset = Integer.parseInt(readerOffset);
    this.inputStream.skip(offset);
  }
}
项目:datacollector    文件:ProtobufDataParserFactory.java   
public ProtobufDataParserFactory(Settings settings) throws StageException {
  super(settings);
  this.protoDescriptorFile = settings.getConfig(ProtobufConstants.PROTO_DESCRIPTOR_FILE_KEY);
  this.messageType = settings.getConfig(ProtobufConstants.MESSAGE_TYPE_KEY);
  this.isDelimited = settings.getConfig(ProtobufConstants.DELIMITED_KEY);
  messageTypeToExtensionMap = new HashMap<>();
  defaultValueMap = new HashMap<>();
  // Get the descriptor for the expected message type
  descriptor = ProtobufTypeUtil.getDescriptor(
    settings.getContext(),
    protoDescriptorFile,
    messageType,
    messageTypeToExtensionMap,
    defaultValueMap
  );

  // Build the extension registry based on the cached extension map
  extensionRegistry = ExtensionRegistry.newInstance();
  for(Map.Entry<String, Set<Descriptors.FieldDescriptor>> e : messageTypeToExtensionMap.entrySet()) {
    Set<Descriptors.FieldDescriptor> value = e.getValue();
    for (Descriptors.FieldDescriptor f : value) {
      extensionRegistry.add(f);
    }
  }
}
项目:protobuf-utils    文件:MessageInputStream.java   
public static <T extends GeneratedMessage> MessageInputStream<T> createBinaryStream(final InputStream is, String msgType) throws IOException {
    final Registry reg = Registry.getInstance();
    final ExtensionRegistry extReg = reg.getExtensionRegistry();
    final GeneratedMessage msg = reg.getInstanceForType(msgType);

    if (msg == null) {
        throw new RuntimeException("Type " + msgType + " not found.");
    }

    final Parser parser = msg.getParserForType();
    return new MessageInputStream<T>() {

        {
            next = readNext();
        }

        @Override
        protected T readNext()  throws IOException {
            if (is.available() > 0) {
                return (T) parser.parseDelimitedFrom(is, extReg);
            }
            return null;
        }
    };
}
项目:protobuf-el    文件:CompareWithOriginalsTest.java   
private void assertReserializationInvariant(final FileDescriptor fileWithUnknownFieldsProto,
    final ExtensionRegistry registry) throws InvalidProtocolBufferException {
  final FileDescriptorProto expectedProtoWithUnknownFields = fileWithUnknownFieldsProto.toProto();

  final FileDescriptorProto actualProtoWithExtensions =
      FileDescriptorProto.parseFrom(expectedProtoWithUnknownFields.toByteString(), registry);
  final FileDescriptorProto actualProtoWithUnknownFields =
      FileDescriptorProto.parseFrom(actualProtoWithExtensions.toByteString());

  softly
      .assertThat(actualProtoWithUnknownFields.toString())
      .as("check reserialization invariant textual equality for %s",
          fileWithUnknownFieldsProto.getName())
      .isEqualTo(expectedProtoWithUnknownFields.toString());

  softly.assertThat(actualProtoWithUnknownFields)
      .as("check reserialization invariant for %s", fileWithUnknownFieldsProto.getName())
      .isEqualTo(expectedProtoWithUnknownFields);
}
项目:onos    文件:PipeconfHelper.java   
/**
 * Extracts and returns a P4Info protobuf message from the given pipeconf. If the pipeconf does not define any
 * extension of type {@link PiPipeconf.ExtensionType#P4_INFO_TEXT}, returns null;
 *
 * @param pipeconf pipeconf
 * @return P4Info or null
 */
static P4Info getP4Info(PiPipeconf pipeconf) {
    return P4INFOS.computeIfAbsent(pipeconf.id(), piPipeconfId -> {
        if (!pipeconf.extension(P4_INFO_TEXT).isPresent()) {
            log.warn("Missing P4Info extension in pipeconf {}", pipeconf.id());
            return null;
        }

        InputStream p4InfoStream = pipeconf.extension(P4_INFO_TEXT).get();
        P4Info.Builder p4iInfoBuilder = P4Info.newBuilder();
        try {
            TextFormat.getParser().merge(new InputStreamReader(p4InfoStream), ExtensionRegistry.getEmptyRegistry(),
                                         p4iInfoBuilder);
        } catch (IOException ex) {
            log.warn("Unable to parse P4Info of pipeconf {}: {}", pipeconf.id(), ex.getMessage());
            return null;
        }

        return p4iInfoBuilder.build();
    });
}
项目:GitHub    文件:ProtoConverterFactoryTest.java   
@Before public void setUp() {
  Retrofit retrofit = new Retrofit.Builder()
      .baseUrl(server.url("/"))
      .addConverterFactory(ProtoConverterFactory.create())
      .build();
  service = retrofit.create(Service.class);

  ExtensionRegistry registry = ExtensionRegistry.newInstance();
  PhoneProtos.registerAllExtensions(registry);
  Retrofit retrofitWithRegistry = new Retrofit.Builder()
      .baseUrl(server.url("/"))
      .addConverterFactory(ProtoConverterFactory.createWithRegistry(registry))
      .build();
  serviceWithRegistry = retrofitWithRegistry.create(ServiceWithRegistry.class);
}
项目:GitHub    文件:ProtoConverterFactoryTest.java   
@Before public void setUp() {
  Retrofit retrofit = new Retrofit.Builder()
      .baseUrl(server.url("/"))
      .addConverterFactory(ProtoConverterFactory.create())
      .build();
  service = retrofit.create(Service.class);

  ExtensionRegistry registry = ExtensionRegistry.newInstance();
  PhoneProtos.registerAllExtensions(registry);
  Retrofit retrofitWithRegistry = new Retrofit.Builder()
      .baseUrl(server.url("/"))
      .addConverterFactory(ProtoConverterFactory.createWithRegistry(registry))
      .build();
  serviceWithRegistry = retrofitWithRegistry.create(ServiceWithRegistry.class);
}
项目:jigsaw-payment    文件:AbstractCharBasedFormatter.java   
/**
   * Parse a text-format message from {@code input} and merge the contents into {@code builder}.
   * Extensions will be recognized if they are registered in {@code extensionRegistry}.
   */
  public void merge(Readable input,
        ExtensionRegistry extensionRegistry,
        Message.Builder builder) throws IOException {
      // Read the entire input to a String then parse that.

      // If StreamTokenizer were not quite so crippled, or if there were a kind
      // of Reader that could read in chunks that match some particular regex,
      // or if we wanted to write a custom Reader to tokenize our stream, then
      // we would not have to read to one big String. Alas, none of these is
      // the case. Oh well.

merge(TextUtils.toStringBuilder(input), extensionRegistry, builder);
  }
项目:jigsaw-payment    文件:XmlJavaxFormat.java   
/**
 * Parse a text-format message from {@code input} and merge the contents into {@code builder}.
 * Extensions will be recognized if they are registered in {@code extensionRegistry}.
 * @throws IOException 
 */
public void merge(InputStream input, Charset cs,
        ExtensionRegistry extensionRegistry, Message.Builder builder) throws IOException {

    XMLEventReader parser;
    try {
        parser = xmlInputFactory.createXMLEventReader(input);
        merge(parser, extensionRegistry, builder);
    } catch (XMLStreamException e) {
        throw new IOException(e);
    }
}
项目:jigsaw-payment    文件:XmlJavaxFormat.java   
/**
 * Parse a text-format message from {@code input} and merge the contents into {@code builder}.
 * Extensions will be recognized if they are registered in {@code extensionRegistry}.
 * @throws IOException 
 */
public void merge(XMLEventReader parser, 
        ExtensionRegistry extensionRegistry,
        Message.Builder builder) throws IOException {

    try {
        XMLEvent messageElement = parser.nextTag();

        if (messageElement.isStartElement()) {
            final String messageName = messageElement.asStartElement().getName().getLocalPart();
            assert builder.getDescriptorForType().getName().equals(messageName);

            while (parser.hasNext() && !parser.peek().isEndDocument()) {
                XMLEvent event = parser.nextTag();

                if (event.isStartElement()) {
                    mergeField(parser, 
                            event, 
                            extensionRegistry, builder);
                    XMLEvent endElement = parser.nextTag();
                    assert endElement.isEndElement();
                } else if (event.isEndElement()) {
                    //parser.nextTag().isEndDocument()
                    break;
                } else {
                    throw new RuntimeException("Expecting the end of the stream, but there seems to be more data!  Check the input for a valid XML format.");
                }
            }
        }
    } catch (XMLStreamException e) {
        throw new IOException(e);
    }
}
项目:jigsaw-payment    文件:XmlJavaxFormat.java   
private void handleMissingField(String fieldName, XMLEventReader parser,
        XMLEvent event, ExtensionRegistry extensionRegistry, 
        UnknownFieldSet.Builder builder) throws XMLStreamException {

    // skip over the unknown fields, since we can't map them by id, then this message must not know about them.
    // We 'could' map them into the UnknownFieldSet, however none of the other formatters support this..
    // but in the future it would probably be useful for the case: Message A (v2) -> Message B (v1) -> Xml -> Message A (v2) 
    // this would require extra meta data in the xml to know the type of the unknown-field.


    if (event.isStartElement()) {
        /**
         * This loop will eat up everything inside "6"
         * So when this method is called, fieldName = 6, and event is set at index="11"
         * <unknown-field index="6">
         *      <unknown-field index="11">566667</unknown-field>
         *      <unknown-field index="15">
         *          <unknown-field index="16">566667</unknown-field>
         *      </unknown-field>
         * </unknown-field>
         */
        int depth = 1; // we start 1 level down, the value of "6"
        while (parser.hasNext()) {
            XMLEvent nextEvent = parser.nextEvent();
            if (nextEvent.isEndElement()) {
                depth--;
                if (depth <= 0 && parser.peek().isEndElement()) {
                    break;
                }
            } else if (nextEvent.isStartElement()) {
                depth++;
            }
        }
    } else if (event.isCharacters()) {
        // done, let it slide.
    }
}
项目:jigsaw-payment    文件:XmlJavaxFormat.java   
private Message.Builder createSubBuilder(Message.Builder builder,
        FieldDescriptor field, 
        ExtensionRegistry.ExtensionInfo extension) {

    Message.Builder subBuilder;
    if (extension == null) {
        subBuilder = builder.newBuilderForField(field);
    } else {
        subBuilder = extension.defaultInstance.newBuilderForType();
    }
    return subBuilder;
}
项目:jigsaw-payment    文件:XmlJavaxFormat.java   
private Object handleObject(XMLEventReader parser,
        XMLEvent startEvent,
        ExtensionRegistry extensionRegistry, 
        Message.Builder builder,
        FieldDescriptor field, 
        ExtensionRegistry.ExtensionInfo extension) throws XMLStreamException {

    Message.Builder subBuilder = createSubBuilder(builder, field, extension);

    XMLEvent event = startEvent;
    int depth = 0; // initialize to 0
    do {
        if (event.isStartElement()) {
            depth++; // we're inside the element
            mergeField(parser, event, 
                    extensionRegistry, subBuilder);
            XMLEvent nextEvent = parser.nextTag();

            if (nextEvent.isEndElement()) {
                depth--;
                // if we're back under the top level obj, and there is another close, we're done.
                if (depth <= 0 && parser.peek().isEndElement()) {
                    break;
                }
            } else if (nextEvent.isStartElement()) {
                depth++;
            }
        } else {
            // something is potentially wrong..
            break;
        }
    } while (parser.hasNext() && (event = parser.nextTag()) != null);

    return subBuilder.build();
}
项目:jigsaw-payment    文件:CouchDBFormat.java   
/**
 * Parse a text-format message from {@code input} and merge the contents into {@code builder}.
 * Extensions will be recognized if they are registered in {@code extensionRegistry}.
 */
public void merge(CharSequence input,
                         ExtensionRegistry extensionRegistry,
                         Message.Builder builder) throws ParseException {
    Tokenizer tokenizer = new Tokenizer(input);

    // Based on the state machine @ http://json.org/

    tokenizer.consume("{"); // Needs to happen when the object starts.
    while (!tokenizer.tryConsume("}")) { // Continue till the object is done
        mergeField(tokenizer, extensionRegistry, builder);
    }
}
项目:jigsaw-payment    文件:XmlFormat.java   
/**
 * Parse a text-format message from {@code input} and merge the contents into {@code builder}.
 * Extensions will be recognized if they are registered in {@code extensionRegistry}.
 */
public void merge(CharSequence input,
                         ExtensionRegistry extensionRegistry,
                         Message.Builder builder) throws ParseException {
    Tokenizer tokenizer = new Tokenizer(input);

    // Need to first consume the outer object name element
    consumeOpeningElement(tokenizer);

    while (!tokenizer.tryConsume("</")) { // Continue till the object is done
        mergeField(tokenizer, extensionRegistry, builder);
    }

    consumeClosingElement(tokenizer);
}
项目:jigsaw-payment    文件:XmlFormat.java   
private Object handleValue(Tokenizer tokenizer,
                                ExtensionRegistry extensionRegistry,
                                Message.Builder builder,
                                FieldDescriptor field,
                                ExtensionRegistry.ExtensionInfo extension) throws ParseException {

    Object value = null;
    if (field.getJavaType() == FieldDescriptor.JavaType.MESSAGE) {
        value = handleObject(tokenizer, extensionRegistry, builder, field, extension);
    } else {
        value = handlePrimitive(tokenizer, field);
    }

    return value;
}
项目:jigsaw-payment    文件:XmlFormat.java   
private Object handleObject(Tokenizer tokenizer,
                                   ExtensionRegistry extensionRegistry,
                                   Message.Builder builder,
                                   FieldDescriptor field,
                                   ExtensionRegistry.ExtensionInfo extension) throws ParseException {

    Object value;
    Message.Builder subBuilder;
    if (extension == null) {
        subBuilder = builder.newBuilderForField(field);
    } else {
        subBuilder = extension.defaultInstance.newBuilderForType();
    }

    //tokenizer.consume("<");
    String endToken = "</";

    while (!tokenizer.tryConsume(endToken)) {
        if (tokenizer.atEnd()) {
            throw tokenizer.parseException("Expected \"" + endToken + "\".");
        }
        mergeField(tokenizer, extensionRegistry, subBuilder);
    }

    value = subBuilder.build();
    return value;
}
项目:jigsaw-payment    文件:JavaPropsFormat.java   
/**
 * Parse a text-format message from {@code input} and merge the contents
 * into {@code builder}.  Extensions will be recognized if they are
 * registered in {@code extensionRegistry}.
 */
public void merge(final CharSequence input,
                         final ExtensionRegistry extensionRegistry,
                         final Message.Builder builder)
                         throws ParseException {
  final Tokenizer tokenizer = new Tokenizer(input);
  final Map<String, Message> subMessages = new HashMap<String, Message>();

  while (!tokenizer.atEnd()) {
    mergeField(tokenizer, extensionRegistry, subMessages, builder);
  }
}
项目:jigsaw-payment    文件:JsonJacksonFormat.java   
/**
 * Parse a text-format message from {@code input} and merge the contents into {@code builder}.
 * Extensions will be recognized if they are registered in {@code extensionRegistry}.
 * @throws IOException
 */
public void merge(InputStream input, Charset cs,
        ExtensionRegistry extensionRegistry, Message.Builder builder) throws IOException {

    JsonParser parser = jsonFactory.createJsonParser(input);
    merge(parser, extensionRegistry, builder);
}
项目:jigsaw-payment    文件:JsonJacksonFormat.java   
private Object handleObject(JsonParser parser,
                                   ExtensionRegistry extensionRegistry,
                                   Message.Builder builder,
                                   FieldDescriptor field,
                                   ExtensionRegistry.ExtensionInfo extension,
                                   boolean unknown) throws IOException {

    Message.Builder subBuilder;
    if (extension == null) {
        subBuilder = builder.newBuilderForField(field);
    } else {
        subBuilder = extension.defaultInstance.newBuilderForType();
    }

    JsonToken token = parser.getCurrentToken();
    if (JsonToken.VALUE_NULL == token) {
        return null;
    }

    if (unknown) {
        ByteString data = ByteString.copyFrom(parser.getBinaryValue());
        try {
            subBuilder.mergeFrom(data);
            return subBuilder.build();
        } catch (InvalidProtocolBufferException e) {
            throw new RuntimeException("Failed to build " + field.getFullName() + " from " + data);
        }
    }

    //token = parser.nextToken();
    if (token.equals(JsonToken.START_OBJECT)) {
     token = parser.nextToken();
     while (token != null && !token.equals(JsonToken.END_OBJECT)) {
         mergeField(parser, extensionRegistry, subBuilder);
         token = parser.nextToken();
     }
    }
    return subBuilder.build();
}
项目:jigsaw-payment    文件:JsonFormat.java   
private void handleMissingField(Tokenizer tokenizer,
                                       ExtensionRegistry extensionRegistry,
                                       Message.Builder builder) throws ParseException {
    tokenizer.tryConsume(":");
    if ("{".equals(tokenizer.currentToken())) {
        // Message structure
        tokenizer.consume("{");
        do {
            tokenizer.consumeIdentifier();
            handleMissingField(tokenizer, extensionRegistry, builder);
        } while (tokenizer.tryConsume(","));
        tokenizer.consume("}");
    } else if ("[".equals(tokenizer.currentToken())) {
        // Collection
        tokenizer.consume("[");
        do {
            handleMissingField(tokenizer, extensionRegistry, builder);
        } while (tokenizer.tryConsume(","));
        tokenizer.consume("]");
    } else { //if (!",".equals(tokenizer.currentToken)){
        // Primitive value
        if ("null".equals(tokenizer.currentToken())) {
            tokenizer.consume("null");
        } else if (tokenizer.lookingAtFloat()) {
            tokenizer.consumeFloat();
        } else if (tokenizer.lookingAtInteger()) {
            tokenizer.consumeInt64();
        } else if (tokenizer.lookingAtBoolean()) {
            tokenizer.consumeBoolean();
        } else if (tokenizer.currentToken().startsWith("'") || tokenizer.currentToken().startsWith("\"")) {
            tokenizer.consumeString();
        }
    }
}
项目:jigsaw-payment    文件:JsonFormat.java   
private Object handleObject(Tokenizer tokenizer,
                                   ExtensionRegistry extensionRegistry,
                                   Message.Builder builder,
                                   FieldDescriptor field,
                                   ExtensionRegistry.ExtensionInfo extension,
                                   boolean unknown) throws ParseException {

    Message.Builder subBuilder;
    if (extension == null) {
        subBuilder = builder.newBuilderForField(field);
    } else {
        subBuilder = extension.defaultInstance.newBuilderForType();
    }

    if (unknown) {
        ByteString data = tokenizer.consumeByteString();
        try {
            subBuilder.mergeFrom(data);
            return subBuilder.build();
        } catch (InvalidProtocolBufferException e) {
            throw tokenizer.parseException("Failed to build " + field.getFullName() + " from " + data);
        }
    }

    tokenizer.consume("{");
    String endToken = "}";

    while (!tokenizer.tryConsume(endToken)) {
        if (tokenizer.atEnd()) {
            throw tokenizer.parseException("Expected \"" + endToken + "\".");
        }
        mergeField(tokenizer, extensionRegistry, subBuilder);
        if (tokenizer.tryConsume(",")) {
            // there are more fields in the object, so continue
            continue;
        }
    }

    return subBuilder.build();
}
项目:twister2    文件:ExtraActionUtils.java   
public static ExtraActionInfo getExtraActionInfo(String extraActionFile) {
  ExtensionRegistry registry = ExtensionRegistry.newInstance();
  ExtraActionsBase.registerAllExtensions(registry);

  try (InputStream stream = Files.newInputStream(Paths.get(extraActionFile))) {
    CodedInputStream coded = CodedInputStream.newInstance(stream);
    return ExtraActionInfo.parseFrom(coded, registry);
  } catch (IOException e) {
    throw new RuntimeException("ERROR: failed to deserialize extra action file "
      + extraActionFile + ": " + e.getMessage(), e);
  }
}
项目:api-compiler    文件:ToolProtoUtil.java   
/**
 * Get the standard extension registry to use for processing service config. By default,
 * registers extensions from {@code google/api/annotations.proto} (and related proto files).
 */
public static ExtensionRegistry getStandardPlatformExtensions() {
  ExtensionRegistry registry = ExtensionRegistry.newInstance();
  AnnotationsProto.registerAllExtensions(registry);

  return registry;
}
项目:api-compiler    文件:ConfigGeneratorDriver.java   
private TypeRegistry.Builder addPlatformExtensions(TypeRegistry.Builder registryBuilder) {
  ExtensionRegistry extensions = ToolProtoUtil.getStandardPlatformExtensions();
  for (String extendedType : EXTENDED_ELEMENTS) {
    for (ExtensionRegistry.ExtensionInfo info :
        extensions.getAllImmutableExtensionsByExtendedType(extendedType)) {

      if (null != info.defaultInstance) {
        registryBuilder.add(info.defaultInstance.getDescriptorForType());
      }
    }
  }
  return registryBuilder;
}
项目:beam    文件:ProtobufUtil.java   
/**
 * Returns the {@link Descriptor} for the given Protocol Buffers {@link Message} as well as
 * every class it can include transitively.
 *
 * @throws IllegalArgumentException if there is an error in Java reflection.
 */
static Set<Descriptor> getRecursiveDescriptorsForClass(
    Class<? extends Message> clazz, ExtensionRegistry registry) {
  Descriptor root = getDescriptorForClass(clazz);
  Set<Descriptor> descriptors = new HashSet<>();
  recursivelyAddDescriptors(root, descriptors, registry);
  return descriptors;
}
项目:beam    文件:ProtobufUtil.java   
/**
 * Recursively walks the given {@link Message} class and verifies that every field or message
 * linked in uses the Protocol Buffers proto2 syntax.
 */
static void checkProto2Syntax(Class<? extends Message> clazz, ExtensionRegistry registry) {
  for (GenericDescriptor d : getRecursiveDescriptorsForClass(clazz, registry)) {
    Syntax s = d.getFile().getSyntax();
    checkArgument(
        s == Syntax.PROTO2,
        "Message %s or one of its dependencies does not use proto2 syntax: %s in file %s",
        clazz.getName(),
        d.getFullName(),
        d.getFile().getName());
  }
}
项目:beam    文件:ProtobufUtil.java   
private static void recursivelyAddDescriptors(
    FieldDescriptor field, Set<Descriptor> descriptors, ExtensionRegistry registry) {
  switch (field.getType()) {
    case BOOL:
    case BYTES:
    case DOUBLE:
    case ENUM:
    case FIXED32:
    case FIXED64:
    case FLOAT:
    case INT32:
    case INT64:
    case SFIXED32:
    case SFIXED64:
    case SINT32:
    case SINT64:
    case STRING:
    case UINT32:
    case UINT64:
      // Primitive types do not transitively access anything else.
      break;

    case GROUP:
    case MESSAGE:
      // Recursively adds all the fields from this nested Message.
      recursivelyAddDescriptors(field.getMessageType(), descriptors, registry);
      break;

    default:
      throw new UnsupportedOperationException(
          "Unexpected Protocol Buffers field type: " + field.getType());
  }
}