Search in sources :

Example 1 with IndentedPrintWriter

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();
}
Also used : CountingOutputStream(net.morimekta.util.io.CountingOutputStream) IndentedPrintWriter(net.morimekta.util.io.IndentedPrintWriter)

Example 2 with IndentedPrintWriter

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();
}
Also used : IndentedPrintWriter(net.morimekta.util.io.IndentedPrintWriter)

Example 3 with IndentedPrintWriter

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();
}
Also used : PrettyJsonWriter(net.morimekta.util.json.PrettyJsonWriter) PrettyJsonWriter(net.morimekta.util.json.PrettyJsonWriter) JsonWriter(net.morimekta.util.json.JsonWriter) IndentedPrintWriter(net.morimekta.util.io.IndentedPrintWriter)

Example 4 with IndentedPrintWriter

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();
}
Also used : CountingOutputStream(net.morimekta.util.io.CountingOutputStream) IndentedPrintWriter(net.morimekta.util.io.IndentedPrintWriter)

Example 5 with IndentedPrintWriter

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();
            }
        }
    }
}
Also used : ProgramTypeRegistry(net.morimekta.providence.reflect.util.ProgramTypeRegistry) OutputStream(java.io.OutputStream) CService(net.morimekta.providence.reflect.contained.CService) HazelcastPortableProgramFormatter(net.morimekta.providence.generator.format.java.program.extras.HazelcastPortableProgramFormatter) BaseMessageFormatter(net.morimekta.providence.generator.format.java.shared.BaseMessageFormatter) CProgram(net.morimekta.providence.reflect.contained.CProgram) Generator(net.morimekta.providence.generator.Generator) GeneratorException(net.morimekta.providence.generator.GeneratorException) JUtils(net.morimekta.providence.generator.format.java.utils.JUtils) IndentedPrintWriter(net.morimekta.util.io.IndentedPrintWriter) BaseServiceFormatter(net.morimekta.providence.generator.format.java.shared.BaseServiceFormatter) IOException(java.io.IOException) PDeclaredDescriptor(net.morimekta.providence.descriptor.PDeclaredDescriptor) BufferedOutputStream(java.io.BufferedOutputStream) BaseProgramFormatter(net.morimekta.providence.generator.format.java.shared.BaseProgramFormatter) JHelper(net.morimekta.providence.generator.format.java.utils.JHelper) CEnumDescriptor(net.morimekta.providence.reflect.contained.CEnumDescriptor) FileManager(net.morimekta.providence.generator.util.FileManager) BaseEnumFormatter(net.morimekta.providence.generator.format.java.shared.BaseEnumFormatter) PMessageDescriptor(net.morimekta.providence.descriptor.PMessageDescriptor) GeneratorOptions(net.morimekta.providence.generator.GeneratorOptions) Nonnull(javax.annotation.Nonnull) JHelper(net.morimekta.providence.generator.format.java.utils.JHelper) CProgram(net.morimekta.providence.reflect.contained.CProgram) OutputStream(java.io.OutputStream) BufferedOutputStream(java.io.BufferedOutputStream) CService(net.morimekta.providence.reflect.contained.CService) IOException(java.io.IOException) GeneratorException(net.morimekta.providence.generator.GeneratorException) BufferedOutputStream(java.io.BufferedOutputStream) IndentedPrintWriter(net.morimekta.util.io.IndentedPrintWriter)

Aggregations

IndentedPrintWriter (net.morimekta.util.io.IndentedPrintWriter)5 CountingOutputStream (net.morimekta.util.io.CountingOutputStream)2 BufferedOutputStream (java.io.BufferedOutputStream)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 Nonnull (javax.annotation.Nonnull)1 PDeclaredDescriptor (net.morimekta.providence.descriptor.PDeclaredDescriptor)1 PMessageDescriptor (net.morimekta.providence.descriptor.PMessageDescriptor)1 Generator (net.morimekta.providence.generator.Generator)1 GeneratorException (net.morimekta.providence.generator.GeneratorException)1 GeneratorOptions (net.morimekta.providence.generator.GeneratorOptions)1 HazelcastPortableProgramFormatter (net.morimekta.providence.generator.format.java.program.extras.HazelcastPortableProgramFormatter)1 BaseEnumFormatter (net.morimekta.providence.generator.format.java.shared.BaseEnumFormatter)1 BaseMessageFormatter (net.morimekta.providence.generator.format.java.shared.BaseMessageFormatter)1 BaseProgramFormatter (net.morimekta.providence.generator.format.java.shared.BaseProgramFormatter)1 BaseServiceFormatter (net.morimekta.providence.generator.format.java.shared.BaseServiceFormatter)1 JHelper (net.morimekta.providence.generator.format.java.utils.JHelper)1 JUtils (net.morimekta.providence.generator.format.java.utils.JUtils)1 FileManager (net.morimekta.providence.generator.util.FileManager)1 CEnumDescriptor (net.morimekta.providence.reflect.contained.CEnumDescriptor)1