Java Code Examples for org.stringtemplate.v4.STGroupFile#getInstanceOf()

The following examples show how to use org.stringtemplate.v4.STGroupFile#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: TestGenerator.java    From fix-orchestra with Apache License 2.0 6 votes vote down vote up
private void generateFeature(AutoIndentWriter writer, Repository repository, FlowType flow,
    ActorType actor) {
  final STGroupFile stFeatureGroup = new STGroupFile("templates/feature.stg");

  ST st = stFeatureGroup.getInstanceOf("feature");
  st.add("repository", repository);
  st.add("flow", flow);
  st.add("actor", actor);
  st.write(writer, errorListener);
  
  repository.getMessages().getMessage().stream()
  .filter(m -> flow.getName().equals(m.getFlow())).forEach(message -> {
    Responses responses = message.getResponses();
    if (responses != null)
      responses.getResponse().forEach(response -> generateFeatureScenario(writer, stFeatureGroup, repository, actor, message, response));
  });
}
 
Example 2
Source File: TestGenerator.java    From fix-orchestra with Apache License 2.0 6 votes vote down vote up
private void generateFeatureScenario(AutoIndentWriter writer, STGroupFile stFeatureGroup, Repository repository, ActorType actor,
    MessageType message, ResponseType response) {
  ST st = stFeatureGroup.getInstanceOf("scenario");
  st.add("actor", actor);
  st.add("message", message);
  st.add("response", response);
  st.write(writer, errorListener);
  List<Object> messageElements = message.getStructure().getComponentRefOrGroupRefOrFieldRef();
  generateFeatureMessageElements(writer, stFeatureGroup, messageElements);
  List<Object> actions = response.getMessageRefOrAssignOrTrigger();
  for (Object action : actions) {
    if (action instanceof MessageRefType) {
      MessageRefType messageRef = (MessageRefType) action;
      ST stMessage = stFeatureGroup.getInstanceOf("messageRef");
      stMessage.add("actor", actor);
      stMessage.add("messageRef", messageRef);
      stMessage.write(writer, errorListener);

      MessageType responseMessage = findMessage(messageRef.getName(), messageRef.getScenario());
      List<Object> responseMessageElements =
          responseMessage.getStructure().getComponentRefOrGroupRefOrFieldRef();
      generateFeatureMessageElements(writer, stFeatureGroup, responseMessageElements);
    }
  }
}
 
Example 3
Source File: Generators.java    From SugarOnRest with MIT License 4 votes vote down vote up
public static void generateNameOfClass(String destFolder) throws Exception {

        ClassLoader loader = Generators.class.getClassLoader();
        URL templateUrl = loader.getResource("NameOfTemplate.stg");
        StringBuilder stringBuilder = new StringBuilder();

        STGroupFile group = getTemplateGroupFile(templateUrl.getPath());

        // Class comment start
        ST template = group.getInstanceOf("templateComment");
        stringBuilder.append(template.render());
        stringBuilder.append(Utils.NewLine);

        // Package
        template = group.getInstanceOf("packageLine");
        template.add("packageLine", "package com.sugaronrest;");
        stringBuilder.append(template.render());
        stringBuilder.append(Utils.NewLine);
        stringBuilder.append(Utils.NewLine);

        // Class start
        template = group.getInstanceOf("classStart");
        stringBuilder.append(template.render());
        stringBuilder.append(Utils.NewLine);

        List<Table> tableList = getTables();
        for (Table table : tableList) {

            // Inner class start
            template = group.getInstanceOf("innerClassStart");
            template.add("name", table.getClassName());
            stringBuilder.append(template.render());
            stringBuilder.append(Utils.NewLine);

            // Set Modulename
            template = group.getInstanceOf("property");
            template.add("name", "SugarModuleName");
            template.add("columnname", table.getName());
            stringBuilder.append(template.render());
            stringBuilder.append(Utils.NewLine);

            // Set properties
            for (Column column : table.getColumns()) {
                template = group.getInstanceOf("property");
                template.add("name", column.getPropertyName());
                template.add("columnname", column.getName());
                stringBuilder.append(template.render());
                stringBuilder.append(Utils.NewLine);
            }

            // Inner class end
            template = group.getInstanceOf("innerClassEnd");
            stringBuilder.append(template.render());
            stringBuilder.append(Utils.NewLine);

            stringBuilder.append(Utils.NewLine);
        }

        // Class end
        template = group.getInstanceOf("classEnd");
        stringBuilder.append(template.render());

        String joinedPath = new File(destFolder,  "NameOf.java").toString();

        File file = new File(joinedPath);
        FileWriter fw = new FileWriter(file.getAbsoluteFile());
        BufferedWriter bw = new BufferedWriter(fw);
        bw.write(stringBuilder.toString());
        bw.close();
    }
 
Example 4
Source File: Generators.java    From SugarOnRest with MIT License 4 votes vote down vote up
public static void generateModuleMapper(String destFolder) throws Exception {

        ClassLoader loader = Generators.class.getClassLoader();
        URL templateUrl = loader.getResource("ModuleMapperTemplate.stg");
        StringBuilder stringBuilder = new StringBuilder();

        STGroupFile group = getTemplateGroupFile(templateUrl.getPath());

        // Class comment start
        ST template = group.getInstanceOf("packageAndImport");
        stringBuilder.append(template.render());
        stringBuilder.append(Utils.NewLine);

        // Class start
        template = group.getInstanceOf("classStart");
        stringBuilder.append(template.render());
        stringBuilder.append(Utils.NewLine);

        Map<String, String> allModules = readAllModules();
        List<Table> tableList = getTables();
        for (Table table : tableList) {
            String tablename = table.getName();
            String modulename = Utils.toPascalCase(table.getName());

            if (allModules.containsKey(tablename)) {
                modulename = allModules.get(tablename);

                // Add to map
                template = group.getInstanceOf("addToMap");
                template.add("tablename", tablename);
                template.add("modulename", modulename);
                stringBuilder.append(template.render());

                stringBuilder.append(Utils.NewLine);
            }
        }

        // Class end
        template = group.getInstanceOf("classEnd");
        stringBuilder.append(template.render());

        String joinedPath = new File(destFolder,  "ModuleMapper.java").toString();

        File file = new File(joinedPath);
        FileWriter fw = new FileWriter(file.getAbsoluteFile());
        BufferedWriter bw = new BufferedWriter(fw);
        bw.write(stringBuilder.toString());
        bw.close();
    }