Java Code Examples for org.antlr.stringtemplate.StringTemplate#write()

The following examples show how to use org.antlr.stringtemplate.StringTemplate#write() . These examples are extracted from open source projects. 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
@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 2
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 3
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
/**
 * 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 Project: yatspec   File: WikiResultRenderer.java    License: Apache License 2.0 5 votes vote down vote up
public String render(Result result) throws Exception {
    final EnhancedStringTemplateGroup group = new EnhancedStringTemplateGroup(getClass());
    sequence(customRenderers).fold(group, registerRenderer());
    group.registerRenderer(instanceOf(JavaSource.class), callable(new JavaSourceRenderer()));
    group.registerRenderer(instanceOf(Notes.class), callable(new NotesRenderer()));
    final StringTemplate template = group.getInstanceOf("wiki");
    template.setAttribute("testResult", result);
    StringWriter writer = new StringWriter();
    template.write(new NoIndentWriter(writer));
    return writer.toString();
}
 
Example 6
Source Project: yatspec   File: HtmlResultRenderer.java    License: Apache License 2.0 5 votes vote down vote up
public String render(Result result) throws Exception {
    final EnhancedStringTemplateGroup group = new EnhancedStringTemplateGroup(getClass());
    group.registerRenderer(always().and(not(instanceOf(Number.class))), Xml.escape());
    group.registerRenderer(instanceOf(ScenarioTableHeader.class), callable(new ScenarioTableHeaderRenderer()));
    group.registerRenderer(instanceOf(JavaSource.class), callable(new JavaSourceRenderer()));
    group.registerRenderer(instanceOf(Notes.class), callable(new NotesRenderer()));
    group.registerRenderer(instanceOf(LinkingNote.class), callable(new LinkingNoteRenderer(result.getTestClass())));
    group.registerRenderer(instanceOf(ContentAtUrl.class), asString());
    sequence(customRenderers).fold(group, registerRenderer());
    for (Class document : Creator.optionalClass("org.jdom.Document")) {
        group.registerRenderer(instanceOf(document), callable(Creator.<Renderer>create(Class.forName("com.googlecode.yatspec.plugin.jdom.DocumentRenderer"))));
    }

    final StringTemplate template = group.getInstanceOf("yatspec");
    template.setAttribute("script", loadContent("xregexp.js"));
    template.setAttribute("script", loadContent("yatspec.js"));
    for (Content customScript : customScripts) {
        template.setAttribute("script", customScript);
    }
    for (Content customHeaderContent : customHeaderContents) {
        template.setAttribute("customHeaderContent", customHeaderContent);
    }
    template.setAttribute("stylesheet", loadContent("yatspec.css"));
    template.setAttribute("cssClass", getCssMap());
    template.setAttribute("testResult", result);
    StringWriter writer = new StringWriter();
    template.write(new NoIndentWriter(writer));
    return writer.toString();
}
 
Example 7
Source Project: protostuff   File: ProtoToProtoCompiler.java    License: Apache License 2.0 5 votes vote down vote up
public static String extendBy(StringTemplateGroup group, Message extend, Message by) throws IOException
{
    StringWriter stringer = new StringWriter(16);
    NoIndentWriter out = new NoIndentWriter(stringer);

    StringTemplate messageBlock = group.getInstanceOf("extend_by");
    messageBlock.setAttribute("message", extend);
    messageBlock.setAttribute("by", by);
    messageBlock.write(out);

    return stringer.toString();
}
 
Example 8
Source Project: protostuff   File: PluginProtoCompiler.java    License: 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 9
Source Project: protostuff   File: PluginProtoCompiler.java    License: 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 10
Source Project: protostuff   File: PluginProtoCompiler.java    License: 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 11
public String getRemoteModelName(StringTemplateGroup group, Message message) throws IOException
{
    StringWriter writer = new StringWriter(16);
    NoIndentWriter out = new NoIndentWriter(writer);

    StringTemplate messageBlock = group.getInstanceOf("remote_model_name");
    messageBlock.setAttribute("message", message);
    messageBlock.setAttribute("name", message.getName());
    messageBlock.write(out);

    String result = writer.toString();
    writer.close();

    return result;
}
 
Example 12
public String getRemoteModelSchemaName(StringTemplateGroup group, Message message) throws IOException
{
    StringWriter writer = new StringWriter(16);
    NoIndentWriter out = new NoIndentWriter(writer);

    StringTemplate messageBlock = group.getInstanceOf("remote_model_schema_name");
    messageBlock.setAttribute("message", message);
    messageBlock.setAttribute("name", message.getName());
    messageBlock.write(out);

    String result = writer.toString();
    writer.close();

    return result;
}
 
Example 13
Source Project: protostuff   File: PluginProtoCompiler.java    License: 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();
}