Java Code Examples for org.antlr.stringtemplate.StringTemplateGroup#getInstanceOf()

The following examples show how to use org.antlr.stringtemplate.StringTemplateGroup#getInstanceOf() . 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: MutationHtmlReportListener.java    From pitest with Apache License 2.0 6 votes vote down vote up
private void createPackageIndexPage(final PackageSummaryData psData) {
  final StringTemplateGroup group = new StringTemplateGroup("mutation_test");
  final StringTemplate st = group
      .getInstanceOf("templates/mutation/package_index");

  final Writer writer = this.outputStrategy.createWriterForFile(psData
      .getPackageDirectory() + File.separator + "index.html");
  st.setAttribute("packageData", psData);
  try {
    writer.write(st.toString());
    writer.close();
  } catch (final IOException e) {
    e.printStackTrace();
  }

}
 
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: 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 5
Source File: ContainerConfig.java    From gsn with GNU General Public License v3.0 5 votes vote down vote up
public void writeConfigurations ( ) throws FileNotFoundException , IOException {
	StringTemplateGroup templateGroup = new StringTemplateGroup( "ch.epfl.gsn" );
	StringTemplate st = templateGroup.getInstanceOf( "ch.epfl.gsn/gui/templates/templateConf" );
	st.setAttribute( "db_user" , storage.getJdbcUsername( ) );
	st.setAttribute( "db_password" , storage.getJdbcPassword( ) );
	st.setAttribute( "db_driver" , storage.getJdbcDriver( ) );
	st.setAttribute( "db_url" , storage.getJdbcURL( ) );

	FileWriter writer = new FileWriter( gsnConfigurationFileName );
	writer.write( st.toString( ) );
	writer.close( );

}
 
Example 6
Source File: Pst.java    From incubator-retired-wave with Apache License 2.0 5 votes vote down vote up
/**
 * Runs the code generation for all templates.
 */
public void run() throws PstException {
  List<PstException.TemplateException> exceptions = Lists.newArrayList();
  for (File template : templates) {
    try {
      MessageProperties properties = createProperties(template);
      String groupName = stripSuffix(".st", template.getName());
      String templateName = properties.hasTemplateName() ?
          properties.getTemplateName() : "";
      StringTemplateGroup group = new StringTemplateGroup(groupName + "Group", dir(template));
      StringTemplate st = group.getInstanceOf(groupName);
      for (Descriptor messageDescriptor : fd.getMessageTypes()) {
        Message message = new Message(messageDescriptor, templateName, properties);
        st.reset();
        st.setAttribute("m", message);
        write(st, new File(
            outputDir.getPath() + File.separator +
            message.getFullJavaType().replace('.', File.separatorChar) + "." +
            (properties.hasFileExtension() ? properties.getFileExtension() : "java")));
      }
    } catch (Exception e) {
      exceptions.add(new PstException.TemplateException(template.getPath(), e));
    }
  }
  if (!exceptions.isEmpty()) {
    throw new PstException(exceptions);
  }
}
 
Example 7
Source File: ProtoToJavaBeanModelCompiler.java    From protostuff with Apache License 2.0 5 votes vote down vote up
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 8
Source File: ProtoToJavaBeanModelCompiler.java    From protostuff with Apache License 2.0 5 votes vote down vote up
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 9
Source File: ProtoToProtoCompiler.java    From protostuff with 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 10
Source File: TedsToVirtualSensor.java    From gsn with GNU General Public License v3.0 5 votes vote down vote up
public TedsToVirtualSensor ( String templateDir , String templateFile ) {
   this.templateDir = templateDir;
   this.templatefile = templateFile;
   StringTemplateGroup grp = new StringTemplateGroup( "myGroup" , templateDir );
   vstmp = grp.getInstanceOf( templateFile );
   
}
 
Example 11
Source File: Pst.java    From swellrt with Apache License 2.0 5 votes vote down vote up
/**
 * Runs the code generation for all templates.
 */
public void run() throws PstException {
  List<PstException.TemplateException> exceptions = Lists.newArrayList();
  for (File template : templates) {
    try {
      MessageProperties properties = createProperties(template);
      String groupName = stripSuffix(".st", template.getName());
      String templateName = properties.hasTemplateName() ?
          properties.getTemplateName() : "";
      StringTemplateGroup group = new StringTemplateGroup(groupName + "Group", dir(template));
      StringTemplate st = group.getInstanceOf(groupName);
      for (Descriptor messageDescriptor : fd.getMessageTypes()) {
        Message message = new Message(messageDescriptor, templateName, properties);
        st.reset();
        st.setAttribute("m", message);
        write(st, new File(
            outputDir.getPath() + File.separator +
            message.getFullJavaType().replace('.', File.separatorChar) + "." +
            (properties.hasFileExtension() ? properties.getFileExtension() : "java")));
      }
    } catch (Exception e) {
      exceptions.add(new PstException.TemplateException(template.getPath(), e));
    }
  }
  if (!exceptions.isEmpty()) {
    throw new PstException(exceptions);
  }
}
 
Example 12
Source File: SemanticContext.java    From jFuzzyLogic with GNU Lesser General Public License v3.0 5 votes vote down vote up
public StringTemplate genExpr(CodeGenerator generator,
							  StringTemplateGroup templates,
							  DFA dfa)
{
	if ( templates!=null ) {
		return templates.getInstanceOf("true");
	}
	return new StringTemplate("true");
}
 
Example 13
Source File: JUnitCodeGen.java    From jFuzzyLogic with GNU Lesser General Public License v3.0 5 votes vote down vote up
protected String genSupportingMethods(StringTemplateGroup group, String lexerName, String parserName) {
	StringTemplate supportingMethodST;
	String parserPath;
	String treeParserPath;
	/** Set up appropriate class path for parser/tree parser if using package */
	if ( grammarInfo.getHeader()!=null ) {
		parserPath = grammarInfo.getHeader()+"."+parserName;
		treeParserPath = grammarInfo.getHeader()+"."+grammarInfo.getTreeGrammarName();
	}
	else {
		parserPath = parserName;
		treeParserPath = grammarInfo.getTreeGrammarName();
	}
	/** Set up different parser executer for parser grammar and tree grammar */
	if ( grammarInfo.getTreeGrammarName()!=null ) {	// Load template for testing tree grammar
		supportingMethodST = group.getInstanceOf("execTreeParser");
		supportingMethodST.setAttribute("treeParserName", grammarInfo.getTreeGrammarName());
		supportingMethodST.setAttribute("treeParserPath", treeParserPath);
	}
	else {	// Load template for testing grammar
		supportingMethodST = group.getInstanceOf("execParser");
	}
	supportingMethodST.setAttribute("lexerName", lexerName);
	supportingMethodST.setAttribute("parserName", parserName);
	supportingMethodST.setAttribute("parserPath", parserPath);
	return supportingMethodST.toString();
}
 
Example 14
Source File: JUnitCodeGen.java    From jFuzzyLogic with GNU Lesser General Public License v3.0 5 votes vote down vote up
protected String genClassHeader(StringTemplateGroup group, String junitFileName) {
	StringTemplate classHeaderST = group.getInstanceOf("classHeader");
	if ( grammarInfo.getHeader()!=null ) {	// Set up class package if there is
		classHeaderST.setAttribute("header", "package "+grammarInfo.getHeader()+";");
	}
	classHeaderST.setAttribute("junitFileName", junitFileName);
	return classHeaderST.toString();
}
 
Example 15
Source File: ValuesSQLTemplate.java    From quetzal with Eclipse Public License 2.0 5 votes vote down vote up
public static void main(String[] args) {
	 String st = "group sql;\n"
	 		+ "values(values, values_project,  store_name) ::= << \n"
			+ "( select <values_project:{ v |  struct_col.col<i> AS <v>} ; separator=\",\"> from (select storename from <store_name> limit 1) dummy lateral view explode(array(<values:{struct(<it; separator=\",\">)};separator=\",\">)) LATTEMP_<store_name> AS struct_col  ) AS TEMP"
		 	//+" (select VALUES <values:{(<it; separator=\",\">)};separator=\",\">) AS TEMP(<values_project; separator=\",\">) \n"
			+">>";
	 System.out.println("template text: "+st);
	 BufferedReader configReader = new BufferedReader(new StringReader(st));
        StringTemplateGroup group = new StringTemplateGroup(configReader);
	 StringTemplate template =  group.getInstanceOf("values");//new StringTemplate(st);
	 System.out.println("template: "+template.toString());
	 template.setAttribute("store_name", "uobm1a");
	 List<String> projects = new LinkedList<String>();
	 projects.add("column1");
	 projects.add("column2");
	 template.setAttribute("values_project",projects);
	 List< List<String>> values = new LinkedList<List<String>>();
	 List<String> value = new LinkedList<String>();
	 value.add("'test1'");
	 value.add("'test2'");
	 values.add(value);
	 value = new LinkedList<String>();
	 value.add("'test3'");
	 value.add("'test4'");
	 values.add(value);
	 
	 template.setAttribute("values", values);
	 System.out.println("string: "+template.toString());
}
 
Example 16
Source File: MutationHtmlReportListener.java    From pitest with Apache License 2.0 5 votes vote down vote up
private void createIndexPages() {

    final StringTemplateGroup group = new StringTemplateGroup("mutation_test");
    final StringTemplate st = group
        .getInstanceOf("templates/mutation/mutation_package_index");

    final Writer writer = this.outputStrategy.createWriterForFile("index.html");
    final MutationTotals totals = new MutationTotals();

    final List<PackageSummaryData> psd = new ArrayList<>(
        this.packageSummaryData.values());
    Collections.sort(psd);
    for (final PackageSummaryData psData : psd) {
      totals.add(psData.getTotals());
      createPackageIndexPage(psData);
    }

    st.setAttribute("totals", totals);
    st.setAttribute("packageSummaries", psd);
    try {
      writer.write(st.toString());
      writer.close();
    } catch (final IOException e) {
      e.printStackTrace();
    }

  }
 
Example 17
Source File: SQLTemplateTest.java    From quetzal with Eclipse Public License 2.0 4 votes vote down vote up
/**
 * @param args
 * @throws FileNotFoundException 
 */
public static void main(String[] args) throws FileNotFoundException {

	// Use the constructor that accepts a Reader
	StringTemplateGroup sql = new StringTemplateGroup(new FileReader("src/com/ibm/rdf/store/sparql11/sqltemplate/SQLTemplates"));
	StringTemplate t = sql.getInstanceOf("simple_ph_exp1");
	t.setAttribute("project", "entry as x");
	t.setAttribute("project", "val5 as y");
	t.setAttribute("target", "DPH");
	t.setAttribute("target", "Q1");
	t.setAttribute("predicate_constraint","pred1 = 'p1'");
	t.setAttribute("predicate_constraint","pred2 = 'p2'");
	t.setAttribute("predicate_constraint","pred3 = 'p3'");
	t.setAttribute("val_constraint","val1 = 'v1'");
	t.setAttribute("val_constraint","val2 = 'v2'");
	t.setAttribute("val_constraint","val3 = 'v3'");
	t.setAttribute("sep", " OR ");
	System.out.println(t);

	HashSet<String> values = new HashSet<String>();
	values.add("p1");
	values.add("p2");
	
	StringTemplate st = new StringTemplate("$items:{SELECT $it.(\"last\")$ FROM $it.(\"first\")$ \n}; separator=\" UNION \" $ ");
	st.setAttribute("items.{first,last}", "John", "Smith");
	st.setAttribute("items.{first,last}", "Baron", "Von Munchhausen");
	String expecting =
		"Smith, John\n" +
		"Von Munchhausen, Baron\n";
	System.out.println("Template \n"+st);
	System.out.println("Expected \n"+expecting);



	StringTemplateGroup group =
		new StringTemplateGroup(new FileReader("src/com/ibm/rdf/store/sparql11/sqltemplate/SQLTemplates"),
				AngleBracketTemplateLexer.class);
	StringTemplate f = group.getInstanceOf("file");
	f.setAttribute("variables.{decl,format}", new Decl("i","int"), "intdecl");
	f.setAttribute("variables.{decl,format}", new Decl("a","int-array"), "intarray");
	System.out.println("f="+f);

	
	StringTemplate f2 = group.getInstanceOf("file2");
	f2.setAttribute("variables.{decl,format}", values, "intdecl2");
	f2.setAttribute("variables.{decl,format}", values, "intarray2");
	System.out.println("f="+f2);
	
}
 
Example 18
Source File: ProtoToJavaBeanCompilerTest.java    From protostuff with Apache License 2.0 3 votes vote down vote up
public void testSimpleLoad()
{

    StringTemplateGroup group = STCodeGenerator.getSTG("java_bean_primitives");

    StringTemplate messageBlock = group.getInstanceOf("message_block");

    assertEquals(0, STCodeGenerator.errorCount);
}
 
Example 19
Source File: MutationHtmlReportListener.java    From pitest with Apache License 2.0 3 votes vote down vote up
private void generateAnnotatedSourceFile(
    final MutationTestSummaryData mutationMetaData) {


  final String fileName = mutationMetaData.getPackageName()
      + File.separator + mutationMetaData.getFileName() + ".html";

  try (Writer writer = this.outputStrategy.createWriterForFile(fileName)) {

    final StringTemplateGroup group = new StringTemplateGroup("mutation_test");
    final StringTemplate st = group
        .getInstanceOf("templates/mutation/mutation_report");
    st.setAttribute("css", this.css);

    st.setAttribute("tests", mutationMetaData.getTests());

    st.setAttribute("mutators", mutationMetaData.getMutators());

    final SourceFile sourceFile = createAnnotatedSourceFile(mutationMetaData);

    st.setAttribute("sourceFile", sourceFile);
    st.setAttribute("mutatedClasses", mutationMetaData.getMutatedClasses());

    writer.write(st.toString());


  } catch (final IOException ex) {
    Log.getLogger().log(Level.WARNING, "Error while writing report", ex);
  }
}