org.antlr.stringtemplate.AutoIndentWriter Java Examples

The following examples show how to use org.antlr.stringtemplate.AutoIndentWriter. 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: ProtoToJavaBeanCompiler.java    From protostuff with Apache License 2.0 7 votes vote down vote up
protected void writeMessages(ProtoModule module, Proto proto, String javaPackageName, StringTemplateGroup group)
        throws IOException
{
    for (Message m : proto.getMessages())
    {
        Writer writer = CompilerUtil.newWriter(module,
                javaPackageName, m.getName() + ".java");
        AutoIndentWriter out = new AutoIndentWriter(writer);

        StringTemplate messageBlock = group.getInstanceOf("message_block");
        messageBlock.setAttribute("message", m);
        messageBlock.setAttribute("module", module);
        messageBlock.setAttribute("options", module.getOptions());

        messageBlock.write(out);
        writer.close();
    }
}
 
Example #2
Source File: ProtoToJavaV2ProtocSchemaCompiler.java    From protostuff with Apache License 2.0 6 votes vote down vote up
@Override
protected void compile(ProtoModule module, Proto proto) throws IOException
{
    String javaPackageName = proto.getJavaPackageName();
    StringTemplateGroup group = getSTG(getOutputId());

    String fileName = resolveFileName(proto);
    Writer writer = CompilerUtil.newWriter(module, javaPackageName,
            "Schema" + fileName + ".java");

    AutoIndentWriter out = new AutoIndentWriter(writer);
    StringTemplate protoOuterBlock = group.getInstanceOf("proto_block");

    protoOuterBlock.setAttribute("proto", proto);
    protoOuterBlock.setAttribute("module", module);
    protoOuterBlock.setAttribute("options", module.getOptions());

    protoOuterBlock.write(out);
    writer.close();
}
 
Example #3
Source File: ProtoToJavaBeanCompiler.java    From protostuff with Apache License 2.0 6 votes vote down vote up
protected void writeEnums(ProtoModule module, Proto proto, String javaPackageName, StringTemplateGroup group)
        throws IOException
{
    for (EnumGroup eg : proto.getEnumGroups())
    {
        Writer writer = CompilerUtil.newWriter(module,
                javaPackageName, eg.getName() + ".java");
        AutoIndentWriter out = new AutoIndentWriter(writer);

        StringTemplate enumBlock = group.getInstanceOf("enum_block");
        enumBlock.setAttribute("eg", eg);
        enumBlock.setAttribute("module", module);
        enumBlock.setAttribute("options", module.getOptions());

        enumBlock.write(out);
        writer.close();
    }
}
 
Example #4
Source File: StringTemplateHelper.java    From attic-aurora with Apache License 2.0 6 votes vote down vote up
/**
 * Writes the populated template to an output writer by providing a closure with access to
 * the unpopulated template object.
 *
 * @param out Template output writer.
 * @param parameterSetter Consumer to populate the template.
 * @throws TemplateException If an exception was encountered while populating the template.
 */
public void writeTemplate(
    Writer out,
    Consumer<StringTemplate> parameterSetter) throws TemplateException {

  Preconditions.checkNotNull(out);
  Preconditions.checkNotNull(parameterSetter);

  StringTemplate stringTemplate = group.getInstanceOf(templatePath);
  try {
    parameterSetter.accept(stringTemplate);
    stringTemplate.write(new AutoIndentWriter(out));
  } catch (IOException e) {
    throw new TemplateException("Failed to write template: " + e, e);
  }
}
 
Example #5
Source File: PluginProtoCompiler.java    From protostuff with Apache License 2.0 5 votes vote down vote up
public static void compileServiceBlockTo(Writer writer,
        ProtoModule module, Service service,
        StringTemplate serviceBlockTemplate) throws IOException
{
    AutoIndentWriter out = new AutoIndentWriter(writer);

    StringTemplate messageBlock = serviceBlockTemplate.getInstanceOf();
    messageBlock.setAttribute("service", service);
    messageBlock.setAttribute("module", module);
    messageBlock.setAttribute("options", module.getOptions());

    messageBlock.write(out);
}
 
Example #6
Source File: PluginProtoCompiler.java    From protostuff with Apache License 2.0 5 votes vote down vote up
public static void compileEnumBlockTo(Writer writer,
        ProtoModule module, EnumGroup eg,
        StringTemplate enumBlockTemplate) throws IOException
{
    AutoIndentWriter out = new AutoIndentWriter(writer);

    StringTemplate enumBlock = enumBlockTemplate.getInstanceOf();
    enumBlock.setAttribute("eg", eg);
    enumBlock.setAttribute("module", module);
    enumBlock.setAttribute("options", module.getOptions());

    enumBlock.write(out);
}
 
Example #7
Source File: PluginProtoCompiler.java    From protostuff with Apache License 2.0 5 votes vote down vote up
public static void compileMessageBlockTo(Writer writer,
        ProtoModule module, Message message,
        StringTemplate messageBlockTemplate) throws IOException
{
    AutoIndentWriter out = new AutoIndentWriter(writer);

    StringTemplate messageBlock = messageBlockTemplate.getInstanceOf();
    messageBlock.setAttribute("message", message);
    messageBlock.setAttribute("module", module);
    messageBlock.setAttribute("options", module.getOptions());

    messageBlock.write(out);
}
 
Example #8
Source File: PluginProtoCompiler.java    From protostuff with Apache License 2.0 4 votes vote down vote up
public void compileProtoBlock(ProtoModule module, Proto proto,
        String packageName, StringTemplate protoBlockTemplate) throws IOException
{
    String name = ProtoUtil.toPascalCase(proto.getFile().getName().replace(
            ".proto", "")).toString();

    if (javaOutput)
    {
        String outerClassname = proto.getExtraOption("java_outer_classname");
        if (outerClassname != null)
            name = outerClassname;
    }

    final String fileName;
    if (outputPrefix.isEmpty() && outputSuffix.isEmpty())
    {
        // resolve the prefix/suffix from module option
        String outerFilePrefix = module.getOption("outer_file_prefix");
        if (outerFilePrefix != null)
            name = outerFilePrefix + name;

        String outerFileSuffix = module.getOption("outer_file_suffix");
        if (outerFileSuffix != null)
            name += outerFileSuffix;

        fileName = name + fileExtension;
    }
    else
    {
        // use the placeholder in the output name
        fileName = resolveFileName(name);
    }

    Writer writer = CompilerUtil.newWriter(module, packageName, fileName);

    AutoIndentWriter out = new AutoIndentWriter(writer);

    StringTemplate protoBlock = protoBlockTemplate.getInstanceOf();
    protoBlock.setAttribute("proto", proto);
    protoBlock.setAttribute("module", module);
    protoBlock.setAttribute("options", module.getOptions());

    protoBlock.write(out);
    writer.close();
}