Java Code Examples for org.stringtemplate.v4.AutoIndentWriter

The following examples show how to use org.stringtemplate.v4.AutoIndentWriter. 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
Source Project: codebuff   Source File: CodeGenerator.java    License: BSD 2-Clause "Simplified" License 6 votes vote down vote up
public void write(ST code, String fileName) {
		try {
//			long start = System.currentTimeMillis();
			Writer w = tool.getOutputFileWriter(g, fileName);
			STWriter wr = new AutoIndentWriter(w);
			wr.setLineWidth(lineWidth);
			code.write(wr);
			w.close();
//			long stop = System.currentTimeMillis();
		}
		catch (IOException ioe) {
			tool.errMgr.toolError(ErrorType.CANNOT_WRITE_FILE,
								  ioe,
								  fileName);
		}
	}
 
Example 2
Source Project: fix-orchestra   Source File: TestGenerator.java    License: 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 3
Source Project: fix-orchestra   Source File: TestGenerator.java    License: 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 4
Source Project: buck   Source File: BuckPyFunction.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Returns a python string containing all of the default rule parameters that should be made
 * available to user defined rules in the python build file parser
 */
public String addDefaultAttributes() {
  STGroup group = buckPyFunctionTemplate.get();

  ST st;
  // STGroup#getInstanceOf may not be thread safe.
  // See discussion in: https://github.com/antlr/stringtemplate4/issues/61
  synchronized (group) {
    st = group.getInstanceOf("buck_py_attrs");
  }

  st.add("implicit_required_attrs", UDR_IMPLICIT_REQUIRED_ATTRIBUTES);
  st.add("implicit_optional_attrs", UDR_IMPLICIT_OPTIONAL_ATTRIBUTES);
  st.add("implicit_required_test_attrs", UDR_IMPLICIT_REQUIRED_TEST_ATTRIBUTES);
  st.add("implicit_optional_test_attrs", UDR_IMPLICIT_OPTIONAL_TEST_ATTRIBUTES);

  try {
    StringWriter stringWriter = new StringWriter();
    st.write(new AutoIndentWriter(stringWriter, "\n"));
    return stringWriter.toString();
  } catch (IOException e) {
    throw new IllegalStateException("ST writer should not throw with StringWriter", e);
  }
}
 
Example 5
Source Project: buck   Source File: BuckModuleAdapterPluginGenerator.java    License: Apache License 2.0 5 votes vote down vote up
private void writeStringTemplateToWriter(Writer writer, ST stringTemplate) throws IOException {
  AutoIndentWriter noIndentWriter = new AutoIndentWriter(writer);
  try {
    stringTemplate.write(noIndentWriter);
  } finally {
    try {
      writer.close();
    } catch (IOException e) {
      processingEnv
          .getMessager()
          .printMessage(Kind.WARNING, "Exception during close: " + ThrowablesUtils.toString(e));
    }
  }
}
 
Example 6
Source Project: buck   Source File: BuckPyFunction.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * Create a Python function definition for given rule type and parameters described by DTO type.
 */
public String toPythonFunction(RuleType type, Class<? extends DataTransferObject> dtoClass) {
  ImmutableList.Builder<StParamInfo> mandatory = ImmutableList.builder();
  ImmutableList.Builder<StParamInfo> optional = ImmutableList.builder();
  for (ParamInfo<?> param :
      typeCoercerFactory.getConstructorArgDescriptor(dtoClass).getParamInfos().values().stream()
          .sorted(Comparator.comparing(ParamInfo::getName))
          .collect(ImmutableList.toImmutableList())) {
    if (isSkippable(param)) {
      continue;
    }
    if (param.isOptional()) {
      optional.add(new StParamInfo(param));
    } else {
      mandatory.add(new StParamInfo(param));
    }
  }
  optional.add(
      StParamInfo.ofOptionalValue(
          VisibilityAttributes.VISIBILITY, VisibilityAttributes.VISIBILITY));
  optional.add(
      StParamInfo.ofOptionalValue(
          VisibilityAttributes.WITHIN_VIEW, VisibilityAttributes.WITHIN_VIEW));

  STGroup group = buckPyFunctionTemplate.get();
  ST st;
  // STGroup#getInstanceOf may not be thread safe.
  // See discussion in: https://github.com/antlr/stringtemplate4/issues/61
  synchronized (group) {
    st = group.getInstanceOf("buck_py_function");
  }
  st.add("name", type.getName());
  // Mandatory params must come before optional ones.
  st.add(
      "params",
      ImmutableList.builder().addAll(mandatory.build()).addAll(optional.build()).build());
  st.add("typePropName", TYPE_PROPERTY_NAME);
  StringWriter stringWriter = new StringWriter();
  try {
    st.write(new AutoIndentWriter(stringWriter, "\n"));
  } catch (IOException e) {
    throw new IllegalStateException("ST writer should not throw with StringWriter", e);
  }
  return stringWriter.toString();
}