Java Code Examples for com.google.protobuf.TextFormat#print()

The following examples show how to use com.google.protobuf.TextFormat#print() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ActionGraphProtoOutputFormatterCallback.java    From bazel with Apache License 2.0 6 votes vote down vote up
@Override
public void close(boolean failFast) throws IOException {
  if (!failFast && printStream != null) {
    ActionGraphContainer actionGraphContainer = actionGraphDump.build();

    // Write the data.
    switch (outputType) {
      case BINARY:
        actionGraphContainer.writeTo(printStream);
        break;
      case TEXT:
        TextFormat.print(actionGraphContainer, printStream);
        break;
      case JSON:
        jsonPrinter.appendTo(actionGraphContainer, printStream);
        printStream.println();
        break;
      default:
        throw new IllegalStateException("Unknown outputType " + outputType.formatName());
    }
  }
}
 
Example 2
Source File: ProtoOutputFormatterCallback.java    From bazel with Apache License 2.0 6 votes vote down vote up
private void writeData(Message message) throws IOException {
  switch (outputType) {
    case BINARY:
      message.writeTo(outputStream);
      break;
    case TEXT:
      TextFormat.print(message, printStream);
      break;
    case JSON:
      jsonPrinter.appendTo(message, printStream);
      printStream.append('\n');
      break;
    default:
      throw new IllegalStateException("Unknown outputType " + outputType.formatName());
  }
}
 
Example 3
Source File: ProtobufHttpMessageConverter.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
protected void writeInternal(Message message, HttpOutputMessage outputMessage)
		throws IOException, HttpMessageNotWritableException {

	MediaType contentType = outputMessage.getHeaders().getContentType();
	if (contentType == null) {
		contentType = getDefaultContentType(message);
		Assert.state(contentType != null, "No content type");
	}
	Charset charset = contentType.getCharset();
	if (charset == null) {
		charset = DEFAULT_CHARSET;
	}

	if (PROTOBUF.isCompatibleWith(contentType)) {
		setProtoHeader(outputMessage, message);
		CodedOutputStream codedOutputStream = CodedOutputStream.newInstance(outputMessage.getBody());
		message.writeTo(codedOutputStream);
		codedOutputStream.flush();
	}
	else if (TEXT_PLAIN.isCompatibleWith(contentType)) {
		OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputMessage.getBody(), charset);
		TextFormat.print(message, outputStreamWriter);
		outputStreamWriter.flush();
		outputMessage.getBody().flush();
	}
	else if (this.protobufFormatSupport != null) {
		this.protobufFormatSupport.print(message, outputMessage.getBody(), contentType, charset);
		outputMessage.getBody().flush();
	}
}
 
Example 4
Source File: ProtobufHttpMessageConverter.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
protected void writeInternal(Message message, HttpOutputMessage outputMessage)
		throws IOException, HttpMessageNotWritableException {

	MediaType contentType = outputMessage.getHeaders().getContentType();
	if (contentType == null) {
		contentType = getDefaultContentType(message);
		Assert.state(contentType != null, "No content type");
	}
	Charset charset = contentType.getCharset();
	if (charset == null) {
		charset = DEFAULT_CHARSET;
	}

	if (PROTOBUF.isCompatibleWith(contentType)) {
		setProtoHeader(outputMessage, message);
		CodedOutputStream codedOutputStream = CodedOutputStream.newInstance(outputMessage.getBody());
		message.writeTo(codedOutputStream);
		codedOutputStream.flush();
	}
	else if (TEXT_PLAIN.isCompatibleWith(contentType)) {
		OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputMessage.getBody(), charset);
		TextFormat.print(message, outputStreamWriter);
		outputStreamWriter.flush();
		outputMessage.getBody().flush();
	}
	else if (this.protobufFormatSupport != null) {
		this.protobufFormatSupport.print(message, outputMessage.getBody(), contentType, charset);
		outputMessage.getBody().flush();
	}
}
 
Example 5
Source File: ProtobufHttpMessageConverter.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
protected void writeInternal(Message message, HttpOutputMessage outputMessage)
		throws IOException, HttpMessageNotWritableException {

	MediaType contentType = outputMessage.getHeaders().getContentType();
	if (contentType == null) {
		contentType = getDefaultContentType(message);
	}
	Charset charset = contentType.getCharset();
	if (charset == null) {
		charset = DEFAULT_CHARSET;
	}

	if (MediaType.TEXT_PLAIN.isCompatibleWith(contentType)) {
		OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputMessage.getBody(), charset);
		TextFormat.print(message, outputStreamWriter);
		outputStreamWriter.flush();
	}
	else if (MediaType.APPLICATION_JSON.isCompatibleWith(contentType)) {
		JSON_FORMAT.print(message, outputMessage.getBody(), charset);
	}
	else if (MediaType.APPLICATION_XML.isCompatibleWith(contentType)) {
		XML_FORMAT.print(message, outputMessage.getBody(), charset);
	}
	else if (MediaType.TEXT_HTML.isCompatibleWith(contentType)) {
		HTML_FORMAT.print(message, outputMessage.getBody(), charset);
	}
	else if (PROTOBUF.isCompatibleWith(contentType)) {
		setProtoHeader(outputMessage, message);
		FileCopyUtils.copy(message.toByteArray(), outputMessage.getBody());
	}
}
 
Example 6
Source File: OutputUtil.java    From api-compiler with Apache License 2.0 5 votes vote down vote up
/**
 * Writes a proto to a file in text format.
 */
public static void writeProtoTextToFile(File outputFile, Message proto)
    throws IOException {
  try (BufferedWriter outWriter = Files.newWriter(outputFile, StandardCharsets.UTF_8)) {
    TextFormat.print(proto, outWriter);
  }
}
 
Example 7
Source File: AntXmlParser.java    From rich-test-results with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws IOException, XmlParseException {
  if (args.length != 1) {
    System.err.println("Usage: java AntXmlParser path/to/results.xml");
    System.exit(1);
  }
  String path = args[0];
  ImmutableList<TestSuite> testSuites = new AntXmlParser()
      .parse(new FileInputStream(path), UTF_8);
  for (TestSuite testSuite : testSuites) {
    TextFormat.print(testSuite, System.out);
  }
}
 
Example 8
Source File: ConfigGeneratorDriver.java    From api-compiler with Apache License 2.0 4 votes vote down vote up
private void generateOutputFiles(Service serviceConfig)
    throws FileNotFoundException, IOException {
  // Create normalized service proto, in binary form.
  if (!Strings.isNullOrEmpty(options.get(BIN_OUT))) {
    File outFileBinaryServiceConfig = new File(options.get(BIN_OUT));
    OutputStream normalizedOut = new FileOutputStream(outFileBinaryServiceConfig);
    serviceConfig.writeTo(normalizedOut);
    normalizedOut.close();
  }

  // Create normalized service proto, in text form.
  if (!Strings.isNullOrEmpty(options.get(TXT_OUT))) {
    File outFileTxtServiceConfig = new File(options.get(TXT_OUT));
    try (PrintWriter textPrintWriter =
        new PrintWriter(outFileTxtServiceConfig, StandardCharsets.UTF_8.name())) {
      TextFormat.print(serviceConfig, textPrintWriter);
    }
  }

  // Create normalized service proto, in json form.
  if (!Strings.isNullOrEmpty(options.get(JSON_OUT))) {
    File outFileJsonServiceConfig = new File(options.get(JSON_OUT));
    TypeRegistry registry =
        addPlatformExtensions(TypeRegistry.newBuilder())
            .add(Service.getDescriptor())
            .add(com.google.protobuf.BoolValue.getDescriptor())
            .add(com.google.protobuf.BytesValue.getDescriptor())
            .add(com.google.protobuf.DoubleValue.getDescriptor())
            .add(com.google.protobuf.FloatValue.getDescriptor())
            .add(com.google.protobuf.Int32Value.getDescriptor())
            .add(com.google.protobuf.Int64Value.getDescriptor())
            .add(com.google.protobuf.StringValue.getDescriptor())
            .add(com.google.protobuf.UInt32Value.getDescriptor())
            .add(com.google.protobuf.UInt64Value.getDescriptor())
            .build();
    JsonFormat.Printer jsonPrinter = JsonFormat.printer().usingTypeRegistry(registry);
    try (PrintWriter jsonPrintWriter =
        new PrintWriter(outFileJsonServiceConfig, StandardCharsets.UTF_8.name())) {
      jsonPrinter.appendTo(serviceConfig, jsonPrintWriter);
    }
  }
}
 
Example 9
Source File: ToolUtil.java    From api-compiler with Apache License 2.0 4 votes vote down vote up
/** Writes a proto out to a file. */
public static void writeTextProto(Message content, String outputName) throws IOException {
  try (BufferedWriter output = new BufferedWriter(new FileWriter(outputName))) {
    TextFormat.print(content, output);
  }
}
 
Example 10
Source File: ProtobufRowDataConverter.java    From sql-layer with GNU Affero General Public License v3.0 4 votes vote down vote up
public void format(DynamicMessage msg, Appendable output) throws IOException {
    TextFormat.print(msg, output);
}
 
Example 11
Source File: ProtobufRowConverter.java    From sql-layer with GNU Affero General Public License v3.0 4 votes vote down vote up
public void format(DynamicMessage msg, Appendable output) throws IOException {
    TextFormat.print(msg, output);
}
 
Example 12
Source File: ProtobufResultProcessor.java    From ic3 with Apache License 2.0 4 votes vote down vote up
public void processResult(String appName, Ic3Data.Application.Builder ic3Builder,
    String protobufDestination, boolean binary,
    Map<String, Ic3Data.Application.Component.Builder> componentNameToBuilderMap,
    int analysisClassesCount, Writer writer) throws IOException {
  for (Result result : Results.getResults()) {
    ((Ic3Result) result).dump();
    analyzeResult(result);
    writeResultToProtobuf(result, ic3Builder, componentNameToBuilderMap);
  }

  ic3Builder.setAnalysisEnd(System.currentTimeMillis() / 1000);

  String extension = binary ? "dat" : "txt";
  String path =
      String.format("%s/%s_%s.%s", protobufDestination, ic3Builder.getName(),
          ic3Builder.getVersion(), extension);
  System.out.println("PATH: " + path);
  if (binary) {
    FileOutputStream fileOutputStream = new FileOutputStream(path);
    ic3Builder.build().writeTo(fileOutputStream);
    fileOutputStream.close();
  } else {
    FileWriter fileWriter = new FileWriter(path);
    TextFormat.print(ic3Builder, fileWriter);
    fileWriter.close();
  }

  Timers.v().totalTimer.end();

  String statistics =
      appName + " " + analysisClassesCount + " " + PropagationTimers.v().reachableMethods + " "
          + preciseNonLinking[0] + " " + preciseNonLinking[3] + " " + preciseNonLinking[1] + " "
          + preciseNonLinking[2] + " " + preciseLinking[0] + " " + preciseLinking[3] + " "
          + preciseLinking[1] + " " + preciseLinking[2] + " " + imprecise[0] + " " + imprecise[3]
          + " " + imprecise[1] + " " + imprecise[2] + " " + bottom[0] + " " + bottom[1] + " "
          + bottom[2] + " " + top[0] + " " + top[1] + " " + top[2] + " " + nonexistent[0] + " "
          + nonexistent[1] + " " + nonexistent[2] + " " + providerArgument + " " + imprecise[4]
          + " " + preciseFieldValueCount[0] + " " + preciseFieldValueCount[1] + " "
          + preciseFieldValueCount[2] + " " + partiallyPreciseFieldValueCount[0] + " "
          + partiallyPreciseFieldValueCount[1] + " " + partiallyPreciseFieldValueCount[2] + " "
          + impreciseFieldValueCount[0] + " " + impreciseFieldValueCount[1] + " "
          + impreciseFieldValueCount[2] + " " + PropagationTimers.v().modelParsing.getTime()
          + " " + Timers.v().mainGeneration.getTime() + " "
          + Timers.v().entryPointMapping.getTime() + " " + Timers.v().classLoading.getTime()
          + " " + PropagationTimers.v().problemGeneration.getTime() + " "
          + PropagationTimers.v().ideSolution.getTime() + " "
          + PropagationTimers.v().valueComposition.getTime() + " "
          + PropagationTimers.v().resultGeneration.getTime() + " "
          + (PropagationTimers.v().soot.getTime() - PropagationTimers.v().totalTimer.getTime())
          + " " + (Timers.v().misc.getTime() + PropagationTimers.v().misc.getTime()) + " "
          + Timers.v().totalTimer.getTime() + "\n";

  if (logger.isInfoEnabled()) {
    logger.info(statistics);
  }
  if (writer != null) {
    writer.write(statistics);
    writer.close();
  }
}