use of net.morimekta.util.io.IndentedPrintWriter in project providence by morimekta.
the class PrettySerializer method serialize.
@Override
public <Message extends PMessage<Message, Field>, Field extends PField> int serialize(@Nonnull OutputStream out, @Nonnull PServiceCall<Message, Field> call) throws IOException {
CountingOutputStream cout = new CountingOutputStream(out);
IndentedPrintWriter builder = new IndentedPrintWriter(cout, indent, newline);
if (call.getSequence() != 0) {
builder.format("%d: ", call.getSequence());
}
builder.format("%s %s", call.getType().asString().toLowerCase(Locale.US), call.getMethod()).begin(indent + indent);
appendMessage(builder, call.getMessage(), true);
builder.end().newline().flush();
return cout.getByteCount();
}
use of net.morimekta.util.io.IndentedPrintWriter in project providence by morimekta.
the class LogFormatter method formatTo.
/**
* Format message and write to the output stream.
*
* @param out The output stream to write to.
* @param message The message to be written.
* @param <Message> The message type.
* @param <Field> The field type.
*/
public <Message extends PMessage<Message, Field>, Field extends PField> void formatTo(OutputStream out, Message message) {
IndentedPrintWriter builder = new IndentedPrintWriter(out, indent, newline);
if (message == null) {
builder.append(null);
} else {
builder.append(message.descriptor().getQualifiedName()).append(space);
appendMessage(builder, message);
}
builder.flush();
}
use of net.morimekta.util.io.IndentedPrintWriter in project providence by morimekta.
the class JsonSerializer method serialize.
public <T extends PMessage<T, F>, F extends PField> void serialize(@Nonnull PrintWriter output, @Nonnull T message) throws IOException {
JsonWriter jsonWriter = prettyPrint ? new PrettyJsonWriter(new IndentedPrintWriter(output)) : new JsonWriter(output);
appendMessage(jsonWriter, message);
jsonWriter.flush();
}
use of net.morimekta.util.io.IndentedPrintWriter in project providence by morimekta.
the class PrettySerializer method serialize.
public <Message extends PMessage<Message, Field>, Field extends PField> int serialize(@Nonnull OutputStream out, @Nonnull Message message) {
CountingOutputStream cout = new CountingOutputStream(out);
IndentedPrintWriter builder = new IndentedPrintWriter(cout, indent, newline);
if (prefixWithQualifiedName) {
builder.append(message.descriptor().getQualifiedName()).append(space);
}
appendMessage(builder, message, false);
builder.flush();
return cout.getByteCount();
}
use of net.morimekta.util.io.IndentedPrintWriter in project providence by morimekta.
the class JavaGenerator method generate.
@Override
@SuppressWarnings("resource")
public void generate(@Nonnull ProgramTypeRegistry registry) throws IOException, GeneratorException {
CProgram program = registry.getProgram();
String javaPackage = JUtils.getJavaPackage(program);
JHelper helper = new JHelper(registry);
String path = JUtils.getPackageClassPath(javaPackage);
if (program.getConstants().size() > 0) {
String file = helper.getConstantsClassName(program) + ".java";
OutputStream out = new BufferedOutputStream(getFileManager().create(path, file));
try {
IndentedPrintWriter writer = new IndentedPrintWriter(out);
appendFileHeader(writer, helper, program);
constFomatter(writer, helper).appendProgramClass(program);
writer.flush();
} finally {
try {
getFileManager().finalize(out);
} catch (IOException e) {
e.printStackTrace();
}
}
}
if (javaOptions.hazelcast_portable && program.getConstants().stream().anyMatch(t -> t.getName().equals("FACTORY_ID"))) {
String file = helper.getHazelcastFactoryClassName(program) + ".java";
OutputStream out = new BufferedOutputStream(getFileManager().create(path, file));
try {
IndentedPrintWriter writer = new IndentedPrintWriter(out);
appendFileHeader(writer, helper, program);
hazelcastFomatter(writer, helper).appendProgramClass(program);
writer.flush();
} finally {
try {
getFileManager().finalize(out);
} catch (IOException e) {
e.printStackTrace();
}
}
}
for (PDeclaredDescriptor<?> type : program.getDeclaredTypes()) {
String file = JUtils.getClassName(type) + ".java";
OutputStream out = new BufferedOutputStream(getFileManager().create(path, file));
try {
IndentedPrintWriter writer = new IndentedPrintWriter(out);
appendFileHeader(writer, helper, program);
switch(type.getType()) {
case MESSAGE:
messageFormatter(writer, helper).appendMessageClass((PMessageDescriptor<?, ?>) type);
break;
case ENUM:
enumFormatter(writer).appendEnumClass((CEnumDescriptor) type);
break;
default:
throw new GeneratorException("Unhandled declaration type.");
}
writer.flush();
} finally {
try {
getFileManager().finalize(out);
} catch (IOException e) {
e.printStackTrace();
}
}
}
for (CService service : program.getServices()) {
String file = JUtils.getClassName(service) + ".java";
OutputStream out = new BufferedOutputStream(getFileManager().create(path, file));
try {
IndentedPrintWriter writer = new IndentedPrintWriter(out);
appendFileHeader(writer, helper, program);
serviceFormatter(writer, helper).appendServiceClass(service);
writer.flush();
} finally {
try {
getFileManager().finalize(out);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
Aggregations